dib8000.c 107 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558
  1. /*
  2. * Linux-DVB Driver for DiBcom's DiB8000 chip (ISDB-T).
  3. *
  4. * Copyright (C) 2009 DiBcom (http://www.dibcom.fr/)
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License as
  8. * published by the Free Software Foundation, version 2.
  9. */
  10. #include <linux/kernel.h>
  11. #include <linux/slab.h>
  12. #include <linux/i2c.h>
  13. #include <linux/mutex.h>
  14. #include "dvb_math.h"
  15. #include "dvb_frontend.h"
  16. #include "dib8000.h"
  17. #define LAYER_ALL -1
  18. #define LAYER_A 1
  19. #define LAYER_B 2
  20. #define LAYER_C 3
  21. #define FE_CALLBACK_TIME_NEVER 0xffffffff
  22. #define MAX_NUMBER_OF_FRONTENDS 6
  23. static int debug;
  24. module_param(debug, int, 0644);
  25. MODULE_PARM_DESC(debug, "turn on debugging (default: 0)");
  26. #define dprintk(args...) do { if (debug) { printk(KERN_DEBUG "DiB8000: "); printk(args); printk("\n"); } } while (0)
  27. #define FE_STATUS_TUNE_FAILED 0
  28. struct i2c_device {
  29. struct i2c_adapter *adap;
  30. u8 addr;
  31. u8 *i2c_write_buffer;
  32. u8 *i2c_read_buffer;
  33. struct mutex *i2c_buffer_lock;
  34. };
  35. struct dib8000_state {
  36. struct dib8000_config cfg;
  37. struct i2c_device i2c;
  38. struct dibx000_i2c_master i2c_master;
  39. u16 wbd_ref;
  40. u8 current_band;
  41. u32 current_bandwidth;
  42. struct dibx000_agc_config *current_agc;
  43. u32 timf;
  44. u32 timf_default;
  45. u8 div_force_off:1;
  46. u8 div_state:1;
  47. u16 div_sync_wait;
  48. u8 agc_state;
  49. u8 differential_constellation;
  50. u8 diversity_onoff;
  51. s16 ber_monitored_layer;
  52. u16 gpio_dir;
  53. u16 gpio_val;
  54. u16 revision;
  55. u8 isdbt_cfg_loaded;
  56. enum frontend_tune_state tune_state;
  57. u32 status;
  58. struct dvb_frontend *fe[MAX_NUMBER_OF_FRONTENDS];
  59. /* for the I2C transfer */
  60. struct i2c_msg msg[2];
  61. u8 i2c_write_buffer[4];
  62. u8 i2c_read_buffer[2];
  63. struct mutex i2c_buffer_lock;
  64. u8 input_mode_mpeg;
  65. u16 tuner_enable;
  66. struct i2c_adapter dib8096p_tuner_adap;
  67. };
  68. enum dib8000_power_mode {
  69. DIB8000_POWER_ALL = 0,
  70. DIB8000_POWER_INTERFACE_ONLY,
  71. };
  72. static u16 dib8000_i2c_read16(struct i2c_device *i2c, u16 reg)
  73. {
  74. u16 ret;
  75. struct i2c_msg msg[2] = {
  76. {.addr = i2c->addr >> 1, .flags = 0, .len = 2},
  77. {.addr = i2c->addr >> 1, .flags = I2C_M_RD, .len = 2},
  78. };
  79. if (mutex_lock_interruptible(i2c->i2c_buffer_lock) < 0) {
  80. dprintk("could not acquire lock");
  81. return 0;
  82. }
  83. msg[0].buf = i2c->i2c_write_buffer;
  84. msg[0].buf[0] = reg >> 8;
  85. msg[0].buf[1] = reg & 0xff;
  86. msg[1].buf = i2c->i2c_read_buffer;
  87. if (i2c_transfer(i2c->adap, msg, 2) != 2)
  88. dprintk("i2c read error on %d", reg);
  89. ret = (msg[1].buf[0] << 8) | msg[1].buf[1];
  90. mutex_unlock(i2c->i2c_buffer_lock);
  91. return ret;
  92. }
  93. static u16 dib8000_read_word(struct dib8000_state *state, u16 reg)
  94. {
  95. u16 ret;
  96. if (mutex_lock_interruptible(&state->i2c_buffer_lock) < 0) {
  97. dprintk("could not acquire lock");
  98. return 0;
  99. }
  100. state->i2c_write_buffer[0] = reg >> 8;
  101. state->i2c_write_buffer[1] = reg & 0xff;
  102. memset(state->msg, 0, 2 * sizeof(struct i2c_msg));
  103. state->msg[0].addr = state->i2c.addr >> 1;
  104. state->msg[0].flags = 0;
  105. state->msg[0].buf = state->i2c_write_buffer;
  106. state->msg[0].len = 2;
  107. state->msg[1].addr = state->i2c.addr >> 1;
  108. state->msg[1].flags = I2C_M_RD;
  109. state->msg[1].buf = state->i2c_read_buffer;
  110. state->msg[1].len = 2;
  111. if (i2c_transfer(state->i2c.adap, state->msg, 2) != 2)
  112. dprintk("i2c read error on %d", reg);
  113. ret = (state->i2c_read_buffer[0] << 8) | state->i2c_read_buffer[1];
  114. mutex_unlock(&state->i2c_buffer_lock);
  115. return ret;
  116. }
  117. static u32 dib8000_read32(struct dib8000_state *state, u16 reg)
  118. {
  119. u16 rw[2];
  120. rw[0] = dib8000_read_word(state, reg + 0);
  121. rw[1] = dib8000_read_word(state, reg + 1);
  122. return ((rw[0] << 16) | (rw[1]));
  123. }
  124. static int dib8000_i2c_write16(struct i2c_device *i2c, u16 reg, u16 val)
  125. {
  126. struct i2c_msg msg = {.addr = i2c->addr >> 1, .flags = 0, .len = 4};
  127. int ret = 0;
  128. if (mutex_lock_interruptible(i2c->i2c_buffer_lock) < 0) {
  129. dprintk("could not acquire lock");
  130. return -EINVAL;
  131. }
  132. msg.buf = i2c->i2c_write_buffer;
  133. msg.buf[0] = (reg >> 8) & 0xff;
  134. msg.buf[1] = reg & 0xff;
  135. msg.buf[2] = (val >> 8) & 0xff;
  136. msg.buf[3] = val & 0xff;
  137. ret = i2c_transfer(i2c->adap, &msg, 1) != 1 ? -EREMOTEIO : 0;
  138. mutex_unlock(i2c->i2c_buffer_lock);
  139. return ret;
  140. }
  141. static int dib8000_write_word(struct dib8000_state *state, u16 reg, u16 val)
  142. {
  143. int ret;
  144. if (mutex_lock_interruptible(&state->i2c_buffer_lock) < 0) {
  145. dprintk("could not acquire lock");
  146. return -EINVAL;
  147. }
  148. state->i2c_write_buffer[0] = (reg >> 8) & 0xff;
  149. state->i2c_write_buffer[1] = reg & 0xff;
  150. state->i2c_write_buffer[2] = (val >> 8) & 0xff;
  151. state->i2c_write_buffer[3] = val & 0xff;
  152. memset(&state->msg[0], 0, sizeof(struct i2c_msg));
  153. state->msg[0].addr = state->i2c.addr >> 1;
  154. state->msg[0].flags = 0;
  155. state->msg[0].buf = state->i2c_write_buffer;
  156. state->msg[0].len = 4;
  157. ret = (i2c_transfer(state->i2c.adap, state->msg, 1) != 1 ?
  158. -EREMOTEIO : 0);
  159. mutex_unlock(&state->i2c_buffer_lock);
  160. return ret;
  161. }
  162. static const s16 coeff_2k_sb_1seg_dqpsk[8] = {
  163. (769 << 5) | 0x0a, (745 << 5) | 0x03, (595 << 5) | 0x0d, (769 << 5) | 0x0a, (920 << 5) | 0x09, (784 << 5) | 0x02, (519 << 5) | 0x0c,
  164. (920 << 5) | 0x09
  165. };
  166. static const s16 coeff_2k_sb_1seg[8] = {
  167. (692 << 5) | 0x0b, (683 << 5) | 0x01, (519 << 5) | 0x09, (692 << 5) | 0x0b, 0 | 0x1f, 0 | 0x1f, 0 | 0x1f, 0 | 0x1f
  168. };
  169. static const s16 coeff_2k_sb_3seg_0dqpsk_1dqpsk[8] = {
  170. (832 << 5) | 0x10, (912 << 5) | 0x05, (900 << 5) | 0x12, (832 << 5) | 0x10, (-931 << 5) | 0x0f, (912 << 5) | 0x04, (807 << 5) | 0x11,
  171. (-931 << 5) | 0x0f
  172. };
  173. static const s16 coeff_2k_sb_3seg_0dqpsk[8] = {
  174. (622 << 5) | 0x0c, (941 << 5) | 0x04, (796 << 5) | 0x10, (622 << 5) | 0x0c, (982 << 5) | 0x0c, (519 << 5) | 0x02, (572 << 5) | 0x0e,
  175. (982 << 5) | 0x0c
  176. };
  177. static const s16 coeff_2k_sb_3seg_1dqpsk[8] = {
  178. (699 << 5) | 0x14, (607 << 5) | 0x04, (944 << 5) | 0x13, (699 << 5) | 0x14, (-720 << 5) | 0x0d, (640 << 5) | 0x03, (866 << 5) | 0x12,
  179. (-720 << 5) | 0x0d
  180. };
  181. static const s16 coeff_2k_sb_3seg[8] = {
  182. (664 << 5) | 0x0c, (925 << 5) | 0x03, (937 << 5) | 0x10, (664 << 5) | 0x0c, (-610 << 5) | 0x0a, (697 << 5) | 0x01, (836 << 5) | 0x0e,
  183. (-610 << 5) | 0x0a
  184. };
  185. static const s16 coeff_4k_sb_1seg_dqpsk[8] = {
  186. (-955 << 5) | 0x0e, (687 << 5) | 0x04, (818 << 5) | 0x10, (-955 << 5) | 0x0e, (-922 << 5) | 0x0d, (750 << 5) | 0x03, (665 << 5) | 0x0f,
  187. (-922 << 5) | 0x0d
  188. };
  189. static const s16 coeff_4k_sb_1seg[8] = {
  190. (638 << 5) | 0x0d, (683 << 5) | 0x02, (638 << 5) | 0x0d, (638 << 5) | 0x0d, (-655 << 5) | 0x0a, (517 << 5) | 0x00, (698 << 5) | 0x0d,
  191. (-655 << 5) | 0x0a
  192. };
  193. static const s16 coeff_4k_sb_3seg_0dqpsk_1dqpsk[8] = {
  194. (-707 << 5) | 0x14, (910 << 5) | 0x06, (889 << 5) | 0x16, (-707 << 5) | 0x14, (-958 << 5) | 0x13, (993 << 5) | 0x05, (523 << 5) | 0x14,
  195. (-958 << 5) | 0x13
  196. };
  197. static const s16 coeff_4k_sb_3seg_0dqpsk[8] = {
  198. (-723 << 5) | 0x13, (910 << 5) | 0x05, (777 << 5) | 0x14, (-723 << 5) | 0x13, (-568 << 5) | 0x0f, (547 << 5) | 0x03, (696 << 5) | 0x12,
  199. (-568 << 5) | 0x0f
  200. };
  201. static const s16 coeff_4k_sb_3seg_1dqpsk[8] = {
  202. (-940 << 5) | 0x15, (607 << 5) | 0x05, (915 << 5) | 0x16, (-940 << 5) | 0x15, (-848 << 5) | 0x13, (683 << 5) | 0x04, (543 << 5) | 0x14,
  203. (-848 << 5) | 0x13
  204. };
  205. static const s16 coeff_4k_sb_3seg[8] = {
  206. (612 << 5) | 0x12, (910 << 5) | 0x04, (864 << 5) | 0x14, (612 << 5) | 0x12, (-869 << 5) | 0x13, (683 << 5) | 0x02, (869 << 5) | 0x12,
  207. (-869 << 5) | 0x13
  208. };
  209. static const s16 coeff_8k_sb_1seg_dqpsk[8] = {
  210. (-835 << 5) | 0x12, (684 << 5) | 0x05, (735 << 5) | 0x14, (-835 << 5) | 0x12, (-598 << 5) | 0x10, (781 << 5) | 0x04, (739 << 5) | 0x13,
  211. (-598 << 5) | 0x10
  212. };
  213. static const s16 coeff_8k_sb_1seg[8] = {
  214. (673 << 5) | 0x0f, (683 << 5) | 0x03, (808 << 5) | 0x12, (673 << 5) | 0x0f, (585 << 5) | 0x0f, (512 << 5) | 0x01, (780 << 5) | 0x0f,
  215. (585 << 5) | 0x0f
  216. };
  217. static const s16 coeff_8k_sb_3seg_0dqpsk_1dqpsk[8] = {
  218. (863 << 5) | 0x17, (930 << 5) | 0x07, (878 << 5) | 0x19, (863 << 5) | 0x17, (0 << 5) | 0x14, (521 << 5) | 0x05, (980 << 5) | 0x18,
  219. (0 << 5) | 0x14
  220. };
  221. static const s16 coeff_8k_sb_3seg_0dqpsk[8] = {
  222. (-924 << 5) | 0x17, (910 << 5) | 0x06, (774 << 5) | 0x17, (-924 << 5) | 0x17, (-877 << 5) | 0x15, (565 << 5) | 0x04, (553 << 5) | 0x15,
  223. (-877 << 5) | 0x15
  224. };
  225. static const s16 coeff_8k_sb_3seg_1dqpsk[8] = {
  226. (-921 << 5) | 0x19, (607 << 5) | 0x06, (881 << 5) | 0x19, (-921 << 5) | 0x19, (-921 << 5) | 0x14, (713 << 5) | 0x05, (1018 << 5) | 0x18,
  227. (-921 << 5) | 0x14
  228. };
  229. static const s16 coeff_8k_sb_3seg[8] = {
  230. (514 << 5) | 0x14, (910 << 5) | 0x05, (861 << 5) | 0x17, (514 << 5) | 0x14, (690 << 5) | 0x14, (683 << 5) | 0x03, (662 << 5) | 0x15,
  231. (690 << 5) | 0x14
  232. };
  233. static const s16 ana_fe_coeff_3seg[24] = {
  234. 81, 80, 78, 74, 68, 61, 54, 45, 37, 28, 19, 11, 4, 1022, 1017, 1013, 1010, 1008, 1008, 1008, 1008, 1010, 1014, 1017
  235. };
  236. static const s16 ana_fe_coeff_1seg[24] = {
  237. 249, 226, 164, 82, 5, 981, 970, 988, 1018, 20, 31, 26, 8, 1012, 1000, 1018, 1012, 8, 15, 14, 9, 3, 1017, 1003
  238. };
  239. static const s16 ana_fe_coeff_13seg[24] = {
  240. 396, 305, 105, -51, -77, -12, 41, 31, -11, -30, -11, 14, 15, -2, -13, -7, 5, 8, 1, -6, -7, -3, 0, 1
  241. };
  242. static u16 fft_to_mode(struct dib8000_state *state)
  243. {
  244. u16 mode;
  245. switch (state->fe[0]->dtv_property_cache.transmission_mode) {
  246. case TRANSMISSION_MODE_2K:
  247. mode = 1;
  248. break;
  249. case TRANSMISSION_MODE_4K:
  250. mode = 2;
  251. break;
  252. default:
  253. case TRANSMISSION_MODE_AUTO:
  254. case TRANSMISSION_MODE_8K:
  255. mode = 3;
  256. break;
  257. }
  258. return mode;
  259. }
  260. static void dib8000_set_acquisition_mode(struct dib8000_state *state)
  261. {
  262. u16 nud = dib8000_read_word(state, 298);
  263. nud |= (1 << 3) | (1 << 0);
  264. dprintk("acquisition mode activated");
  265. dib8000_write_word(state, 298, nud);
  266. }
  267. static int dib8000_set_output_mode(struct dvb_frontend *fe, int mode)
  268. {
  269. struct dib8000_state *state = fe->demodulator_priv;
  270. u16 outreg, fifo_threshold, smo_mode, sram = 0x0205; /* by default SDRAM deintlv is enabled */
  271. outreg = 0;
  272. fifo_threshold = 1792;
  273. smo_mode = (dib8000_read_word(state, 299) & 0x0050) | (1 << 1);
  274. dprintk("-I- Setting output mode for demod %p to %d",
  275. &state->fe[0], mode);
  276. switch (mode) {
  277. case OUTMODE_MPEG2_PAR_GATED_CLK: // STBs with parallel gated clock
  278. outreg = (1 << 10); /* 0x0400 */
  279. break;
  280. case OUTMODE_MPEG2_PAR_CONT_CLK: // STBs with parallel continues clock
  281. outreg = (1 << 10) | (1 << 6); /* 0x0440 */
  282. break;
  283. case OUTMODE_MPEG2_SERIAL: // STBs with serial input
  284. outreg = (1 << 10) | (2 << 6) | (0 << 1); /* 0x0482 */
  285. break;
  286. case OUTMODE_DIVERSITY:
  287. if (state->cfg.hostbus_diversity) {
  288. outreg = (1 << 10) | (4 << 6); /* 0x0500 */
  289. sram &= 0xfdff;
  290. } else
  291. sram |= 0x0c00;
  292. break;
  293. case OUTMODE_MPEG2_FIFO: // e.g. USB feeding
  294. smo_mode |= (3 << 1);
  295. fifo_threshold = 512;
  296. outreg = (1 << 10) | (5 << 6);
  297. break;
  298. case OUTMODE_HIGH_Z: // disable
  299. outreg = 0;
  300. break;
  301. case OUTMODE_ANALOG_ADC:
  302. outreg = (1 << 10) | (3 << 6);
  303. dib8000_set_acquisition_mode(state);
  304. break;
  305. default:
  306. dprintk("Unhandled output_mode passed to be set for demod %p",
  307. &state->fe[0]);
  308. return -EINVAL;
  309. }
  310. if (state->cfg.output_mpeg2_in_188_bytes)
  311. smo_mode |= (1 << 5);
  312. dib8000_write_word(state, 299, smo_mode);
  313. dib8000_write_word(state, 300, fifo_threshold); /* synchronous fread */
  314. dib8000_write_word(state, 1286, outreg);
  315. dib8000_write_word(state, 1291, sram);
  316. return 0;
  317. }
  318. static int dib8000_set_diversity_in(struct dvb_frontend *fe, int onoff)
  319. {
  320. struct dib8000_state *state = fe->demodulator_priv;
  321. u16 sync_wait = dib8000_read_word(state, 273) & 0xfff0;
  322. if (!state->differential_constellation) {
  323. dib8000_write_word(state, 272, 1 << 9); //dvsy_off_lmod4 = 1
  324. dib8000_write_word(state, 273, sync_wait | (1 << 2) | 2); // sync_enable = 1; comb_mode = 2
  325. } else {
  326. dib8000_write_word(state, 272, 0); //dvsy_off_lmod4 = 0
  327. dib8000_write_word(state, 273, sync_wait); // sync_enable = 0; comb_mode = 0
  328. }
  329. state->diversity_onoff = onoff;
  330. switch (onoff) {
  331. case 0: /* only use the internal way - not the diversity input */
  332. dib8000_write_word(state, 270, 1);
  333. dib8000_write_word(state, 271, 0);
  334. break;
  335. case 1: /* both ways */
  336. dib8000_write_word(state, 270, 6);
  337. dib8000_write_word(state, 271, 6);
  338. break;
  339. case 2: /* only the diversity input */
  340. dib8000_write_word(state, 270, 0);
  341. dib8000_write_word(state, 271, 1);
  342. break;
  343. }
  344. return 0;
  345. }
  346. static void dib8000_set_power_mode(struct dib8000_state *state, enum dib8000_power_mode mode)
  347. {
  348. /* by default everything is going to be powered off */
  349. u16 reg_774 = 0x3fff, reg_775 = 0xffff, reg_776 = 0xffff,
  350. reg_900 = (dib8000_read_word(state, 900) & 0xfffc) | 0x3,
  351. reg_1280;
  352. if (state->revision != 0x8090)
  353. reg_1280 = (dib8000_read_word(state, 1280) & 0x00ff) | 0xff00;
  354. else
  355. reg_1280 = (dib8000_read_word(state, 1280) & 0x707f) | 0x8f80;
  356. /* now, depending on the requested mode, we power on */
  357. switch (mode) {
  358. /* power up everything in the demod */
  359. case DIB8000_POWER_ALL:
  360. reg_774 = 0x0000;
  361. reg_775 = 0x0000;
  362. reg_776 = 0x0000;
  363. reg_900 &= 0xfffc;
  364. if (state->revision != 0x8090)
  365. reg_1280 &= 0x00ff;
  366. else
  367. reg_1280 &= 0x707f;
  368. break;
  369. case DIB8000_POWER_INTERFACE_ONLY:
  370. if (state->revision != 0x8090)
  371. reg_1280 &= 0x00ff;
  372. else
  373. reg_1280 &= 0xfa7b;
  374. break;
  375. }
  376. dprintk("powermode : 774 : %x ; 775 : %x; 776 : %x ; 900 : %x; 1280 : %x", reg_774, reg_775, reg_776, reg_900, reg_1280);
  377. dib8000_write_word(state, 774, reg_774);
  378. dib8000_write_word(state, 775, reg_775);
  379. dib8000_write_word(state, 776, reg_776);
  380. dib8000_write_word(state, 900, reg_900);
  381. dib8000_write_word(state, 1280, reg_1280);
  382. }
  383. static int dib8000_init_sdram(struct dib8000_state *state)
  384. {
  385. u16 reg = 0;
  386. dprintk("Init sdram");
  387. reg = dib8000_read_word(state, 274)&0xfff0;
  388. /* P_dintlv_delay_ram = 7 because of MobileSdram */
  389. dib8000_write_word(state, 274, reg | 0x7);
  390. dib8000_write_word(state, 1803, (7<<2));
  391. reg = dib8000_read_word(state, 1280);
  392. /* force restart P_restart_sdram */
  393. dib8000_write_word(state, 1280, reg | (1<<2));
  394. /* release restart P_restart_sdram */
  395. dib8000_write_word(state, 1280, reg);
  396. return 0;
  397. }
  398. static int dib8000_set_adc_state(struct dib8000_state *state, enum dibx000_adc_states no)
  399. {
  400. int ret = 0;
  401. u16 reg, reg_907 = dib8000_read_word(state, 907);
  402. u16 reg_908 = dib8000_read_word(state, 908);
  403. switch (no) {
  404. case DIBX000_SLOW_ADC_ON:
  405. if (state->revision != 0x8090) {
  406. reg_908 |= (1 << 1) | (1 << 0);
  407. ret |= dib8000_write_word(state, 908, reg_908);
  408. reg_908 &= ~(1 << 1);
  409. } else {
  410. reg = dib8000_read_word(state, 1925);
  411. /* en_slowAdc = 1 & reset_sladc = 1 */
  412. dib8000_write_word(state, 1925, reg |
  413. (1<<4) | (1<<2));
  414. /* read acces to make it works... strange ... */
  415. reg = dib8000_read_word(state, 1925);
  416. msleep(20);
  417. /* en_slowAdc = 1 & reset_sladc = 0 */
  418. dib8000_write_word(state, 1925, reg & ~(1<<4));
  419. reg = dib8000_read_word(state, 921) & ~((0x3 << 14)
  420. | (0x3 << 12));
  421. /* ref = Vin1 => Vbg ; sel = Vin0 or Vin3 ;
  422. (Vin2 = Vcm) */
  423. dib8000_write_word(state, 921, reg | (1 << 14)
  424. | (3 << 12));
  425. }
  426. break;
  427. case DIBX000_SLOW_ADC_OFF:
  428. if (state->revision == 0x8090) {
  429. reg = dib8000_read_word(state, 1925);
  430. /* reset_sladc = 1 en_slowAdc = 0 */
  431. dib8000_write_word(state, 1925,
  432. (reg & ~(1<<2)) | (1<<4));
  433. }
  434. reg_908 |= (1 << 1) | (1 << 0);
  435. break;
  436. case DIBX000_ADC_ON:
  437. reg_907 &= 0x0fff;
  438. reg_908 &= 0x0003;
  439. break;
  440. case DIBX000_ADC_OFF: // leave the VBG voltage on
  441. reg_907 |= (1 << 14) | (1 << 13) | (1 << 12);
  442. reg_908 |= (1 << 5) | (1 << 4) | (1 << 3) | (1 << 2);
  443. break;
  444. case DIBX000_VBG_ENABLE:
  445. reg_907 &= ~(1 << 15);
  446. break;
  447. case DIBX000_VBG_DISABLE:
  448. reg_907 |= (1 << 15);
  449. break;
  450. default:
  451. break;
  452. }
  453. ret |= dib8000_write_word(state, 907, reg_907);
  454. ret |= dib8000_write_word(state, 908, reg_908);
  455. return ret;
  456. }
  457. static int dib8000_set_bandwidth(struct dvb_frontend *fe, u32 bw)
  458. {
  459. struct dib8000_state *state = fe->demodulator_priv;
  460. u32 timf;
  461. if (bw == 0)
  462. bw = 6000;
  463. if (state->timf == 0) {
  464. dprintk("using default timf");
  465. timf = state->timf_default;
  466. } else {
  467. dprintk("using updated timf");
  468. timf = state->timf;
  469. }
  470. dib8000_write_word(state, 29, (u16) ((timf >> 16) & 0xffff));
  471. dib8000_write_word(state, 30, (u16) ((timf) & 0xffff));
  472. return 0;
  473. }
  474. static int dib8000_sad_calib(struct dib8000_state *state)
  475. {
  476. if (state->revision == 0x8090) {
  477. dprintk("%s: the sad calibration is not needed for the dib8096P",
  478. __func__);
  479. return 0;
  480. }
  481. /* internal */
  482. dib8000_write_word(state, 923, (0 << 1) | (0 << 0));
  483. dib8000_write_word(state, 924, 776); // 0.625*3.3 / 4096
  484. /* do the calibration */
  485. dib8000_write_word(state, 923, (1 << 0));
  486. dib8000_write_word(state, 923, (0 << 0));
  487. msleep(1);
  488. return 0;
  489. }
  490. int dib8000_set_wbd_ref(struct dvb_frontend *fe, u16 value)
  491. {
  492. struct dib8000_state *state = fe->demodulator_priv;
  493. if (value > 4095)
  494. value = 4095;
  495. state->wbd_ref = value;
  496. return dib8000_write_word(state, 106, value);
  497. }
  498. EXPORT_SYMBOL(dib8000_set_wbd_ref);
  499. static void dib8000_reset_pll_common(struct dib8000_state *state, const struct dibx000_bandwidth_config *bw)
  500. {
  501. dprintk("ifreq: %d %x, inversion: %d", bw->ifreq, bw->ifreq, bw->ifreq >> 25);
  502. if (state->revision != 0x8090) {
  503. dib8000_write_word(state, 23,
  504. (u16) (((bw->internal * 1000) >> 16) & 0xffff));
  505. dib8000_write_word(state, 24,
  506. (u16) ((bw->internal * 1000) & 0xffff));
  507. } else {
  508. dib8000_write_word(state, 23, (u16) (((bw->internal / 2 * 1000) >> 16) & 0xffff));
  509. dib8000_write_word(state, 24,
  510. (u16) ((bw->internal / 2 * 1000) & 0xffff));
  511. }
  512. dib8000_write_word(state, 27, (u16) ((bw->ifreq >> 16) & 0x01ff));
  513. dib8000_write_word(state, 28, (u16) (bw->ifreq & 0xffff));
  514. dib8000_write_word(state, 26, (u16) ((bw->ifreq >> 25) & 0x0003));
  515. if (state->revision != 0x8090)
  516. dib8000_write_word(state, 922, bw->sad_cfg);
  517. }
  518. static void dib8000_reset_pll(struct dib8000_state *state)
  519. {
  520. const struct dibx000_bandwidth_config *pll = state->cfg.pll;
  521. u16 clk_cfg1, reg;
  522. if (state->revision != 0x8090) {
  523. dib8000_write_word(state, 901,
  524. (pll->pll_prediv << 8) | (pll->pll_ratio << 0));
  525. clk_cfg1 = (1 << 10) | (0 << 9) | (pll->IO_CLK_en_core << 8) |
  526. (pll->bypclk_div << 5) | (pll->enable_refdiv << 4) |
  527. (1 << 3) | (pll->pll_range << 1) |
  528. (pll->pll_reset << 0);
  529. dib8000_write_word(state, 902, clk_cfg1);
  530. clk_cfg1 = (clk_cfg1 & 0xfff7) | (pll->pll_bypass << 3);
  531. dib8000_write_word(state, 902, clk_cfg1);
  532. dprintk("clk_cfg1: 0x%04x", clk_cfg1);
  533. /* smpl_cfg: P_refclksel=2, P_ensmplsel=1 nodivsmpl=1 */
  534. if (state->cfg.pll->ADClkSrc == 0)
  535. dib8000_write_word(state, 904,
  536. (0 << 15) | (0 << 12) | (0 << 10) |
  537. (pll->modulo << 8) |
  538. (pll->ADClkSrc << 7) | (0 << 1));
  539. else if (state->cfg.refclksel != 0)
  540. dib8000_write_word(state, 904, (0 << 15) | (1 << 12) |
  541. ((state->cfg.refclksel & 0x3) << 10) |
  542. (pll->modulo << 8) |
  543. (pll->ADClkSrc << 7) | (0 << 1));
  544. else
  545. dib8000_write_word(state, 904, (0 << 15) | (1 << 12) |
  546. (3 << 10) | (pll->modulo << 8) |
  547. (pll->ADClkSrc << 7) | (0 << 1));
  548. } else {
  549. dib8000_write_word(state, 1856, (!pll->pll_reset<<13) |
  550. (pll->pll_range<<12) | (pll->pll_ratio<<6) |
  551. (pll->pll_prediv));
  552. reg = dib8000_read_word(state, 1857);
  553. dib8000_write_word(state, 1857, reg|(!pll->pll_bypass<<15));
  554. reg = dib8000_read_word(state, 1858); /* Force clk out pll /2 */
  555. dib8000_write_word(state, 1858, reg | 1);
  556. dib8000_write_word(state, 904, (pll->modulo << 8));
  557. }
  558. dib8000_reset_pll_common(state, pll);
  559. }
  560. int dib8000_update_pll(struct dvb_frontend *fe,
  561. struct dibx000_bandwidth_config *pll)
  562. {
  563. struct dib8000_state *state = fe->demodulator_priv;
  564. u16 reg_1857, reg_1856 = dib8000_read_word(state, 1856);
  565. u8 loopdiv, prediv;
  566. u32 internal, xtal;
  567. /* get back old values */
  568. prediv = reg_1856 & 0x3f;
  569. loopdiv = (reg_1856 >> 6) & 0x3f;
  570. if ((pll != NULL) && (pll->pll_prediv != prediv ||
  571. pll->pll_ratio != loopdiv)) {
  572. dprintk("Updating pll (prediv: old = %d new = %d ; loopdiv : old = %d new = %d)", prediv, pll->pll_prediv, loopdiv, pll->pll_ratio);
  573. reg_1856 &= 0xf000;
  574. reg_1857 = dib8000_read_word(state, 1857);
  575. /* disable PLL */
  576. dib8000_write_word(state, 1857, reg_1857 & ~(1 << 15));
  577. dib8000_write_word(state, 1856, reg_1856 |
  578. ((pll->pll_ratio & 0x3f) << 6) |
  579. (pll->pll_prediv & 0x3f));
  580. /* write new system clk into P_sec_len */
  581. internal = dib8000_read32(state, 23) / 1000;
  582. dprintk("Old Internal = %d", internal);
  583. xtal = 2 * (internal / loopdiv) * prediv;
  584. internal = 1000 * (xtal/pll->pll_prediv) * pll->pll_ratio;
  585. dprintk("Xtal = %d , New Fmem = %d New Fdemod = %d, New Fsampling = %d", xtal, internal/1000, internal/2000, internal/8000);
  586. dprintk("New Internal = %d", internal);
  587. dib8000_write_word(state, 23,
  588. (u16) (((internal / 2) >> 16) & 0xffff));
  589. dib8000_write_word(state, 24, (u16) ((internal / 2) & 0xffff));
  590. /* enable PLL */
  591. dib8000_write_word(state, 1857, reg_1857 | (1 << 15));
  592. while (((dib8000_read_word(state, 1856)>>15)&0x1) != 1)
  593. dprintk("Waiting for PLL to lock");
  594. /* verify */
  595. reg_1856 = dib8000_read_word(state, 1856);
  596. dprintk("PLL Updated with prediv = %d and loopdiv = %d",
  597. reg_1856&0x3f, (reg_1856>>6)&0x3f);
  598. return 0;
  599. }
  600. return -EINVAL;
  601. }
  602. EXPORT_SYMBOL(dib8000_update_pll);
  603. static int dib8000_reset_gpio(struct dib8000_state *st)
  604. {
  605. /* reset the GPIOs */
  606. dib8000_write_word(st, 1029, st->cfg.gpio_dir);
  607. dib8000_write_word(st, 1030, st->cfg.gpio_val);
  608. /* TODO 782 is P_gpio_od */
  609. dib8000_write_word(st, 1032, st->cfg.gpio_pwm_pos);
  610. dib8000_write_word(st, 1037, st->cfg.pwm_freq_div);
  611. return 0;
  612. }
  613. static int dib8000_cfg_gpio(struct dib8000_state *st, u8 num, u8 dir, u8 val)
  614. {
  615. st->cfg.gpio_dir = dib8000_read_word(st, 1029);
  616. st->cfg.gpio_dir &= ~(1 << num); /* reset the direction bit */
  617. st->cfg.gpio_dir |= (dir & 0x1) << num; /* set the new direction */
  618. dib8000_write_word(st, 1029, st->cfg.gpio_dir);
  619. st->cfg.gpio_val = dib8000_read_word(st, 1030);
  620. st->cfg.gpio_val &= ~(1 << num); /* reset the direction bit */
  621. st->cfg.gpio_val |= (val & 0x01) << num; /* set the new value */
  622. dib8000_write_word(st, 1030, st->cfg.gpio_val);
  623. dprintk("gpio dir: %x: gpio val: %x", st->cfg.gpio_dir, st->cfg.gpio_val);
  624. return 0;
  625. }
  626. int dib8000_set_gpio(struct dvb_frontend *fe, u8 num, u8 dir, u8 val)
  627. {
  628. struct dib8000_state *state = fe->demodulator_priv;
  629. return dib8000_cfg_gpio(state, num, dir, val);
  630. }
  631. EXPORT_SYMBOL(dib8000_set_gpio);
  632. static const u16 dib8000_defaults[] = {
  633. /* auto search configuration - lock0 by default waiting
  634. * for cpil_lock; lock1 cpil_lock; lock2 tmcc_sync_lock */
  635. 3, 7,
  636. 0x0004,
  637. 0x0400,
  638. 0x0814,
  639. 12, 11,
  640. 0x001b,
  641. 0x7740,
  642. 0x005b,
  643. 0x8d80,
  644. 0x01c9,
  645. 0xc380,
  646. 0x0000,
  647. 0x0080,
  648. 0x0000,
  649. 0x0090,
  650. 0x0001,
  651. 0xd4c0,
  652. /*1, 32,
  653. 0x6680 // P_corm_thres Lock algorithms configuration */
  654. 11, 80, /* set ADC level to -16 */
  655. (1 << 13) - 825 - 117,
  656. (1 << 13) - 837 - 117,
  657. (1 << 13) - 811 - 117,
  658. (1 << 13) - 766 - 117,
  659. (1 << 13) - 737 - 117,
  660. (1 << 13) - 693 - 117,
  661. (1 << 13) - 648 - 117,
  662. (1 << 13) - 619 - 117,
  663. (1 << 13) - 575 - 117,
  664. (1 << 13) - 531 - 117,
  665. (1 << 13) - 501 - 117,
  666. 4, 108,
  667. 0,
  668. 0,
  669. 0,
  670. 0,
  671. 1, 175,
  672. 0x0410,
  673. 1, 179,
  674. 8192, // P_fft_nb_to_cut
  675. 6, 181,
  676. 0x2800, // P_coff_corthres_ ( 2k 4k 8k ) 0x2800
  677. 0x2800,
  678. 0x2800,
  679. 0x2800, // P_coff_cpilthres_ ( 2k 4k 8k ) 0x2800
  680. 0x2800,
  681. 0x2800,
  682. 2, 193,
  683. 0x0666, // P_pha3_thres
  684. 0x0000, // P_cti_use_cpe, P_cti_use_prog
  685. 2, 205,
  686. 0x200f, // P_cspu_regul, P_cspu_win_cut
  687. 0x000f, // P_des_shift_work
  688. 5, 215,
  689. 0x023d, // P_adp_regul_cnt
  690. 0x00a4, // P_adp_noise_cnt
  691. 0x00a4, // P_adp_regul_ext
  692. 0x7ff0, // P_adp_noise_ext
  693. 0x3ccc, // P_adp_fil
  694. 1, 230,
  695. 0x0000, // P_2d_byp_ti_num
  696. 1, 263,
  697. 0x800, //P_equal_thres_wgn
  698. 1, 268,
  699. (2 << 9) | 39, // P_equal_ctrl_synchro, P_equal_speedmode
  700. 1, 270,
  701. 0x0001, // P_div_lock0_wait
  702. 1, 285,
  703. 0x0020, //p_fec_
  704. 1, 299,
  705. 0x0062, /* P_smo_mode, P_smo_rs_discard, P_smo_fifo_flush, P_smo_pid_parse, P_smo_error_discard */
  706. 1, 338,
  707. (1 << 12) | // P_ctrl_corm_thres4pre_freq_inh=1
  708. (1 << 10) |
  709. (0 << 9) | /* P_ctrl_pre_freq_inh=0 */
  710. (3 << 5) | /* P_ctrl_pre_freq_step=3 */
  711. (1 << 0), /* P_pre_freq_win_len=1 */
  712. 0,
  713. };
  714. static u16 dib8000_identify(struct i2c_device *client)
  715. {
  716. u16 value;
  717. //because of glitches sometimes
  718. value = dib8000_i2c_read16(client, 896);
  719. if ((value = dib8000_i2c_read16(client, 896)) != 0x01b3) {
  720. dprintk("wrong Vendor ID (read=0x%x)", value);
  721. return 0;
  722. }
  723. value = dib8000_i2c_read16(client, 897);
  724. if (value != 0x8000 && value != 0x8001 &&
  725. value != 0x8002 && value != 0x8090) {
  726. dprintk("wrong Device ID (%x)", value);
  727. return 0;
  728. }
  729. switch (value) {
  730. case 0x8000:
  731. dprintk("found DiB8000A");
  732. break;
  733. case 0x8001:
  734. dprintk("found DiB8000B");
  735. break;
  736. case 0x8002:
  737. dprintk("found DiB8000C");
  738. break;
  739. case 0x8090:
  740. dprintk("found DiB8096P");
  741. break;
  742. }
  743. return value;
  744. }
  745. static int dib8000_reset(struct dvb_frontend *fe)
  746. {
  747. struct dib8000_state *state = fe->demodulator_priv;
  748. if ((state->revision = dib8000_identify(&state->i2c)) == 0)
  749. return -EINVAL;
  750. /* sram lead in, rdy */
  751. if (state->revision != 0x8090)
  752. dib8000_write_word(state, 1287, 0x0003);
  753. if (state->revision == 0x8000)
  754. dprintk("error : dib8000 MA not supported");
  755. dibx000_reset_i2c_master(&state->i2c_master);
  756. dib8000_set_power_mode(state, DIB8000_POWER_ALL);
  757. /* always leave the VBG voltage on - it consumes almost nothing but takes a long time to start */
  758. dib8000_set_adc_state(state, DIBX000_VBG_ENABLE);
  759. /* restart all parts */
  760. dib8000_write_word(state, 770, 0xffff);
  761. dib8000_write_word(state, 771, 0xffff);
  762. dib8000_write_word(state, 772, 0xfffc);
  763. if (state->revision == 0x8090)
  764. dib8000_write_word(state, 1280, 0x0045);
  765. else
  766. dib8000_write_word(state, 1280, 0x004d);
  767. dib8000_write_word(state, 1281, 0x000c);
  768. dib8000_write_word(state, 770, 0x0000);
  769. dib8000_write_word(state, 771, 0x0000);
  770. dib8000_write_word(state, 772, 0x0000);
  771. dib8000_write_word(state, 898, 0x0004); // sad
  772. dib8000_write_word(state, 1280, 0x0000);
  773. dib8000_write_word(state, 1281, 0x0000);
  774. /* drives */
  775. if (state->revision != 0x8090) {
  776. if (state->cfg.drives)
  777. dib8000_write_word(state, 906, state->cfg.drives);
  778. else {
  779. dprintk("using standard PAD-drive-settings, please adjust settings in config-struct to be optimal.");
  780. /* min drive SDRAM - not optimal - adjust */
  781. dib8000_write_word(state, 906, 0x2d98);
  782. }
  783. }
  784. dib8000_reset_pll(state);
  785. if (state->revision != 0x8090)
  786. dib8000_write_word(state, 898, 0x0004);
  787. if (dib8000_reset_gpio(state) != 0)
  788. dprintk("GPIO reset was not successful.");
  789. if ((state->revision != 0x8090) &&
  790. (dib8000_set_output_mode(fe, OUTMODE_HIGH_Z) != 0))
  791. dprintk("OUTPUT_MODE could not be resetted.");
  792. state->current_agc = NULL;
  793. // P_iqc_alpha_pha, P_iqc_alpha_amp, P_iqc_dcc_alpha, ...
  794. /* P_iqc_ca2 = 0; P_iqc_impnc_on = 0; P_iqc_mode = 0; */
  795. if (state->cfg.pll->ifreq == 0)
  796. dib8000_write_word(state, 40, 0x0755); /* P_iqc_corr_inh = 0 enable IQcorr block */
  797. else
  798. dib8000_write_word(state, 40, 0x1f55); /* P_iqc_corr_inh = 1 disable IQcorr block */
  799. {
  800. u16 l = 0, r;
  801. const u16 *n;
  802. n = dib8000_defaults;
  803. l = *n++;
  804. while (l) {
  805. r = *n++;
  806. do {
  807. dib8000_write_word(state, r, *n++);
  808. r++;
  809. } while (--l);
  810. l = *n++;
  811. }
  812. }
  813. if (state->revision != 0x8090)
  814. dib8000_write_word(state, 903, (0 << 4) | 2);
  815. state->isdbt_cfg_loaded = 0;
  816. //div_cfg override for special configs
  817. if (state->cfg.div_cfg != 0)
  818. dib8000_write_word(state, 903, state->cfg.div_cfg);
  819. /* unforce divstr regardless whether i2c enumeration was done or not */
  820. dib8000_write_word(state, 1285, dib8000_read_word(state, 1285) & ~(1 << 1));
  821. dib8000_set_bandwidth(fe, 6000);
  822. dib8000_set_adc_state(state, DIBX000_SLOW_ADC_ON);
  823. if (state->revision != 0x8090) {
  824. dib8000_sad_calib(state);
  825. dib8000_set_adc_state(state, DIBX000_SLOW_ADC_OFF);
  826. }
  827. dib8000_set_power_mode(state, DIB8000_POWER_INTERFACE_ONLY);
  828. return 0;
  829. }
  830. static void dib8000_restart_agc(struct dib8000_state *state)
  831. {
  832. // P_restart_iqc & P_restart_agc
  833. dib8000_write_word(state, 770, 0x0a00);
  834. dib8000_write_word(state, 770, 0x0000);
  835. }
  836. static int dib8000_update_lna(struct dib8000_state *state)
  837. {
  838. u16 dyn_gain;
  839. if (state->cfg.update_lna) {
  840. // read dyn_gain here (because it is demod-dependent and not tuner)
  841. dyn_gain = dib8000_read_word(state, 390);
  842. if (state->cfg.update_lna(state->fe[0], dyn_gain)) {
  843. dib8000_restart_agc(state);
  844. return 1;
  845. }
  846. }
  847. return 0;
  848. }
  849. static int dib8000_set_agc_config(struct dib8000_state *state, u8 band)
  850. {
  851. struct dibx000_agc_config *agc = NULL;
  852. int i;
  853. u16 reg;
  854. if (state->current_band == band && state->current_agc != NULL)
  855. return 0;
  856. state->current_band = band;
  857. for (i = 0; i < state->cfg.agc_config_count; i++)
  858. if (state->cfg.agc[i].band_caps & band) {
  859. agc = &state->cfg.agc[i];
  860. break;
  861. }
  862. if (agc == NULL) {
  863. dprintk("no valid AGC configuration found for band 0x%02x", band);
  864. return -EINVAL;
  865. }
  866. state->current_agc = agc;
  867. /* AGC */
  868. dib8000_write_word(state, 76, agc->setup);
  869. dib8000_write_word(state, 77, agc->inv_gain);
  870. dib8000_write_word(state, 78, agc->time_stabiliz);
  871. dib8000_write_word(state, 101, (agc->alpha_level << 12) | agc->thlock);
  872. // Demod AGC loop configuration
  873. dib8000_write_word(state, 102, (agc->alpha_mant << 5) | agc->alpha_exp);
  874. dib8000_write_word(state, 103, (agc->beta_mant << 6) | agc->beta_exp);
  875. dprintk("WBD: ref: %d, sel: %d, active: %d, alpha: %d",
  876. state->wbd_ref != 0 ? state->wbd_ref : agc->wbd_ref, agc->wbd_sel, !agc->perform_agc_softsplit, agc->wbd_sel);
  877. /* AGC continued */
  878. if (state->wbd_ref != 0)
  879. dib8000_write_word(state, 106, state->wbd_ref);
  880. else // use default
  881. dib8000_write_word(state, 106, agc->wbd_ref);
  882. if (state->revision == 0x8090) {
  883. reg = dib8000_read_word(state, 922) & (0x3 << 2);
  884. dib8000_write_word(state, 922, reg | (agc->wbd_sel << 2));
  885. }
  886. dib8000_write_word(state, 107, (agc->wbd_alpha << 9) | (agc->perform_agc_softsplit << 8));
  887. dib8000_write_word(state, 108, agc->agc1_max);
  888. dib8000_write_word(state, 109, agc->agc1_min);
  889. dib8000_write_word(state, 110, agc->agc2_max);
  890. dib8000_write_word(state, 111, agc->agc2_min);
  891. dib8000_write_word(state, 112, (agc->agc1_pt1 << 8) | agc->agc1_pt2);
  892. dib8000_write_word(state, 113, (agc->agc1_slope1 << 8) | agc->agc1_slope2);
  893. dib8000_write_word(state, 114, (agc->agc2_pt1 << 8) | agc->agc2_pt2);
  894. dib8000_write_word(state, 115, (agc->agc2_slope1 << 8) | agc->agc2_slope2);
  895. dib8000_write_word(state, 75, agc->agc1_pt3);
  896. if (state->revision != 0x8090)
  897. dib8000_write_word(state, 923,
  898. (dib8000_read_word(state, 923) & 0xffe3) |
  899. (agc->wbd_inv << 4) | (agc->wbd_sel << 2));
  900. return 0;
  901. }
  902. void dib8000_pwm_agc_reset(struct dvb_frontend *fe)
  903. {
  904. struct dib8000_state *state = fe->demodulator_priv;
  905. dib8000_set_adc_state(state, DIBX000_ADC_ON);
  906. dib8000_set_agc_config(state, (unsigned char)(BAND_OF_FREQUENCY(fe->dtv_property_cache.frequency / 1000)));
  907. }
  908. EXPORT_SYMBOL(dib8000_pwm_agc_reset);
  909. static int dib8000_agc_soft_split(struct dib8000_state *state)
  910. {
  911. u16 agc, split_offset;
  912. if (!state->current_agc || !state->current_agc->perform_agc_softsplit || state->current_agc->split.max == 0)
  913. return FE_CALLBACK_TIME_NEVER;
  914. // n_agc_global
  915. agc = dib8000_read_word(state, 390);
  916. if (agc > state->current_agc->split.min_thres)
  917. split_offset = state->current_agc->split.min;
  918. else if (agc < state->current_agc->split.max_thres)
  919. split_offset = state->current_agc->split.max;
  920. else
  921. split_offset = state->current_agc->split.max *
  922. (agc - state->current_agc->split.min_thres) /
  923. (state->current_agc->split.max_thres - state->current_agc->split.min_thres);
  924. dprintk("AGC split_offset: %d", split_offset);
  925. // P_agc_force_split and P_agc_split_offset
  926. dib8000_write_word(state, 107, (dib8000_read_word(state, 107) & 0xff00) | split_offset);
  927. return 5000;
  928. }
  929. static int dib8000_agc_startup(struct dvb_frontend *fe)
  930. {
  931. struct dib8000_state *state = fe->demodulator_priv;
  932. enum frontend_tune_state *tune_state = &state->tune_state;
  933. int ret = 0;
  934. u16 reg, upd_demod_gain_period = 0x8000;
  935. switch (*tune_state) {
  936. case CT_AGC_START:
  937. // set power-up level: interf+analog+AGC
  938. if (state->revision != 0x8090)
  939. dib8000_set_adc_state(state, DIBX000_ADC_ON);
  940. else {
  941. dib8000_set_power_mode(state, DIB8000_POWER_ALL);
  942. reg = dib8000_read_word(state, 1947)&0xff00;
  943. dib8000_write_word(state, 1946,
  944. upd_demod_gain_period & 0xFFFF);
  945. /* bit 14 = enDemodGain */
  946. dib8000_write_word(state, 1947, reg | (1<<14) |
  947. ((upd_demod_gain_period >> 16) & 0xFF));
  948. /* enable adc i & q */
  949. reg = dib8000_read_word(state, 1920);
  950. dib8000_write_word(state, 1920, (reg | 0x3) &
  951. (~(1 << 7)));
  952. }
  953. if (dib8000_set_agc_config(state, (unsigned char)(BAND_OF_FREQUENCY(fe->dtv_property_cache.frequency / 1000))) != 0) {
  954. *tune_state = CT_AGC_STOP;
  955. state->status = FE_STATUS_TUNE_FAILED;
  956. break;
  957. }
  958. ret = 70;
  959. *tune_state = CT_AGC_STEP_0;
  960. break;
  961. case CT_AGC_STEP_0:
  962. //AGC initialization
  963. if (state->cfg.agc_control)
  964. state->cfg.agc_control(fe, 1);
  965. dib8000_restart_agc(state);
  966. // wait AGC rough lock time
  967. ret = 50;
  968. *tune_state = CT_AGC_STEP_1;
  969. break;
  970. case CT_AGC_STEP_1:
  971. // wait AGC accurate lock time
  972. ret = 70;
  973. if (dib8000_update_lna(state))
  974. // wait only AGC rough lock time
  975. ret = 50;
  976. else
  977. *tune_state = CT_AGC_STEP_2;
  978. break;
  979. case CT_AGC_STEP_2:
  980. dib8000_agc_soft_split(state);
  981. if (state->cfg.agc_control)
  982. state->cfg.agc_control(fe, 0);
  983. *tune_state = CT_AGC_STOP;
  984. break;
  985. default:
  986. ret = dib8000_agc_soft_split(state);
  987. break;
  988. }
  989. return ret;
  990. }
  991. static void dib8096p_host_bus_drive(struct dib8000_state *state, u8 drive)
  992. {
  993. u16 reg;
  994. drive &= 0x7;
  995. /* drive host bus 2, 3, 4 */
  996. reg = dib8000_read_word(state, 1798) &
  997. ~(0x7 | (0x7 << 6) | (0x7 << 12));
  998. reg |= (drive<<12) | (drive<<6) | drive;
  999. dib8000_write_word(state, 1798, reg);
  1000. /* drive host bus 5,6 */
  1001. reg = dib8000_read_word(state, 1799) & ~((0x7 << 2) | (0x7 << 8));
  1002. reg |= (drive<<8) | (drive<<2);
  1003. dib8000_write_word(state, 1799, reg);
  1004. /* drive host bus 7, 8, 9 */
  1005. reg = dib8000_read_word(state, 1800) &
  1006. ~(0x7 | (0x7 << 6) | (0x7 << 12));
  1007. reg |= (drive<<12) | (drive<<6) | drive;
  1008. dib8000_write_word(state, 1800, reg);
  1009. /* drive host bus 10, 11 */
  1010. reg = dib8000_read_word(state, 1801) & ~((0x7 << 2) | (0x7 << 8));
  1011. reg |= (drive<<8) | (drive<<2);
  1012. dib8000_write_word(state, 1801, reg);
  1013. /* drive host bus 12, 13, 14 */
  1014. reg = dib8000_read_word(state, 1802) &
  1015. ~(0x7 | (0x7 << 6) | (0x7 << 12));
  1016. reg |= (drive<<12) | (drive<<6) | drive;
  1017. dib8000_write_word(state, 1802, reg);
  1018. }
  1019. static u32 dib8096p_calcSyncFreq(u32 P_Kin, u32 P_Kout,
  1020. u32 insertExtSynchro, u32 syncSize)
  1021. {
  1022. u32 quantif = 3;
  1023. u32 nom = (insertExtSynchro * P_Kin+syncSize);
  1024. u32 denom = P_Kout;
  1025. u32 syncFreq = ((nom << quantif) / denom);
  1026. if ((syncFreq & ((1 << quantif) - 1)) != 0)
  1027. syncFreq = (syncFreq >> quantif) + 1;
  1028. else
  1029. syncFreq = (syncFreq >> quantif);
  1030. if (syncFreq != 0)
  1031. syncFreq = syncFreq - 1;
  1032. return syncFreq;
  1033. }
  1034. static void dib8096p_cfg_DibTx(struct dib8000_state *state, u32 P_Kin,
  1035. u32 P_Kout, u32 insertExtSynchro, u32 synchroMode,
  1036. u32 syncWord, u32 syncSize)
  1037. {
  1038. dprintk("Configure DibStream Tx");
  1039. dib8000_write_word(state, 1615, 1);
  1040. dib8000_write_word(state, 1603, P_Kin);
  1041. dib8000_write_word(state, 1605, P_Kout);
  1042. dib8000_write_word(state, 1606, insertExtSynchro);
  1043. dib8000_write_word(state, 1608, synchroMode);
  1044. dib8000_write_word(state, 1609, (syncWord >> 16) & 0xffff);
  1045. dib8000_write_word(state, 1610, syncWord & 0xffff);
  1046. dib8000_write_word(state, 1612, syncSize);
  1047. dib8000_write_word(state, 1615, 0);
  1048. }
  1049. static void dib8096p_cfg_DibRx(struct dib8000_state *state, u32 P_Kin,
  1050. u32 P_Kout, u32 synchroMode, u32 insertExtSynchro,
  1051. u32 syncWord, u32 syncSize, u32 dataOutRate)
  1052. {
  1053. u32 syncFreq;
  1054. dprintk("Configure DibStream Rx synchroMode = %d", synchroMode);
  1055. if ((P_Kin != 0) && (P_Kout != 0)) {
  1056. syncFreq = dib8096p_calcSyncFreq(P_Kin, P_Kout,
  1057. insertExtSynchro, syncSize);
  1058. dib8000_write_word(state, 1542, syncFreq);
  1059. }
  1060. dib8000_write_word(state, 1554, 1);
  1061. dib8000_write_word(state, 1536, P_Kin);
  1062. dib8000_write_word(state, 1537, P_Kout);
  1063. dib8000_write_word(state, 1539, synchroMode);
  1064. dib8000_write_word(state, 1540, (syncWord >> 16) & 0xffff);
  1065. dib8000_write_word(state, 1541, syncWord & 0xffff);
  1066. dib8000_write_word(state, 1543, syncSize);
  1067. dib8000_write_word(state, 1544, dataOutRate);
  1068. dib8000_write_word(state, 1554, 0);
  1069. }
  1070. static void dib8096p_enMpegMux(struct dib8000_state *state, int onoff)
  1071. {
  1072. u16 reg_1287;
  1073. reg_1287 = dib8000_read_word(state, 1287);
  1074. switch (onoff) {
  1075. case 1:
  1076. reg_1287 &= ~(1 << 8);
  1077. break;
  1078. case 0:
  1079. reg_1287 |= (1 << 8);
  1080. break;
  1081. }
  1082. dib8000_write_word(state, 1287, reg_1287);
  1083. }
  1084. static void dib8096p_configMpegMux(struct dib8000_state *state,
  1085. u16 pulseWidth, u16 enSerialMode, u16 enSerialClkDiv2)
  1086. {
  1087. u16 reg_1287;
  1088. dprintk("Enable Mpeg mux");
  1089. dib8096p_enMpegMux(state, 0);
  1090. /* If the input mode is MPEG do not divide the serial clock */
  1091. if ((enSerialMode == 1) && (state->input_mode_mpeg == 1))
  1092. enSerialClkDiv2 = 0;
  1093. reg_1287 = ((pulseWidth & 0x1f) << 3) |
  1094. ((enSerialMode & 0x1) << 2) | (enSerialClkDiv2 & 0x1);
  1095. dib8000_write_word(state, 1287, reg_1287);
  1096. dib8096p_enMpegMux(state, 1);
  1097. }
  1098. static void dib8096p_setDibTxMux(struct dib8000_state *state, int mode)
  1099. {
  1100. u16 reg_1288 = dib8000_read_word(state, 1288) & ~(0x7 << 7);
  1101. switch (mode) {
  1102. case MPEG_ON_DIBTX:
  1103. dprintk("SET MPEG ON DIBSTREAM TX");
  1104. dib8096p_cfg_DibTx(state, 8, 5, 0, 0, 0, 0);
  1105. reg_1288 |= (1 << 9); break;
  1106. case DIV_ON_DIBTX:
  1107. dprintk("SET DIV_OUT ON DIBSTREAM TX");
  1108. dib8096p_cfg_DibTx(state, 5, 5, 0, 0, 0, 0);
  1109. reg_1288 |= (1 << 8); break;
  1110. case ADC_ON_DIBTX:
  1111. dprintk("SET ADC_OUT ON DIBSTREAM TX");
  1112. dib8096p_cfg_DibTx(state, 20, 5, 10, 0, 0, 0);
  1113. reg_1288 |= (1 << 7); break;
  1114. default:
  1115. break;
  1116. }
  1117. dib8000_write_word(state, 1288, reg_1288);
  1118. }
  1119. static void dib8096p_setHostBusMux(struct dib8000_state *state, int mode)
  1120. {
  1121. u16 reg_1288 = dib8000_read_word(state, 1288) & ~(0x7 << 4);
  1122. switch (mode) {
  1123. case DEMOUT_ON_HOSTBUS:
  1124. dprintk("SET DEM OUT OLD INTERF ON HOST BUS");
  1125. dib8096p_enMpegMux(state, 0);
  1126. reg_1288 |= (1 << 6);
  1127. break;
  1128. case DIBTX_ON_HOSTBUS:
  1129. dprintk("SET DIBSTREAM TX ON HOST BUS");
  1130. dib8096p_enMpegMux(state, 0);
  1131. reg_1288 |= (1 << 5);
  1132. break;
  1133. case MPEG_ON_HOSTBUS:
  1134. dprintk("SET MPEG MUX ON HOST BUS");
  1135. reg_1288 |= (1 << 4);
  1136. break;
  1137. default:
  1138. break;
  1139. }
  1140. dib8000_write_word(state, 1288, reg_1288);
  1141. }
  1142. static int dib8096p_set_diversity_in(struct dvb_frontend *fe, int onoff)
  1143. {
  1144. struct dib8000_state *state = fe->demodulator_priv;
  1145. u16 reg_1287;
  1146. switch (onoff) {
  1147. case 0: /* only use the internal way - not the diversity input */
  1148. dprintk("%s mode OFF : by default Enable Mpeg INPUT",
  1149. __func__);
  1150. /* outputRate = 8 */
  1151. dib8096p_cfg_DibRx(state, 8, 5, 0, 0, 0, 8, 0);
  1152. /* Do not divide the serial clock of MPEG MUX in
  1153. SERIAL MODE in case input mode MPEG is used */
  1154. reg_1287 = dib8000_read_word(state, 1287);
  1155. /* enSerialClkDiv2 == 1 ? */
  1156. if ((reg_1287 & 0x1) == 1) {
  1157. /* force enSerialClkDiv2 = 0 */
  1158. reg_1287 &= ~0x1;
  1159. dib8000_write_word(state, 1287, reg_1287);
  1160. }
  1161. state->input_mode_mpeg = 1;
  1162. break;
  1163. case 1: /* both ways */
  1164. case 2: /* only the diversity input */
  1165. dprintk("%s ON : Enable diversity INPUT", __func__);
  1166. dib8096p_cfg_DibRx(state, 5, 5, 0, 0, 0, 0, 0);
  1167. state->input_mode_mpeg = 0;
  1168. break;
  1169. }
  1170. dib8000_set_diversity_in(state->fe[0], onoff);
  1171. return 0;
  1172. }
  1173. static int dib8096p_set_output_mode(struct dvb_frontend *fe, int mode)
  1174. {
  1175. struct dib8000_state *state = fe->demodulator_priv;
  1176. u16 outreg, smo_mode, fifo_threshold;
  1177. u8 prefer_mpeg_mux_use = 1;
  1178. int ret = 0;
  1179. dib8096p_host_bus_drive(state, 1);
  1180. fifo_threshold = 1792;
  1181. smo_mode = (dib8000_read_word(state, 299) & 0x0050) | (1 << 1);
  1182. outreg = dib8000_read_word(state, 1286) &
  1183. ~((1 << 10) | (0x7 << 6) | (1 << 1));
  1184. switch (mode) {
  1185. case OUTMODE_HIGH_Z:
  1186. outreg = 0;
  1187. break;
  1188. case OUTMODE_MPEG2_SERIAL:
  1189. if (prefer_mpeg_mux_use) {
  1190. dprintk("dib8096P setting output mode TS_SERIAL using Mpeg Mux");
  1191. dib8096p_configMpegMux(state, 3, 1, 1);
  1192. dib8096p_setHostBusMux(state, MPEG_ON_HOSTBUS);
  1193. } else {/* Use Smooth block */
  1194. dprintk("dib8096P setting output mode TS_SERIAL using Smooth bloc");
  1195. dib8096p_setHostBusMux(state,
  1196. DEMOUT_ON_HOSTBUS);
  1197. outreg |= (2 << 6) | (0 << 1);
  1198. }
  1199. break;
  1200. case OUTMODE_MPEG2_PAR_GATED_CLK:
  1201. if (prefer_mpeg_mux_use) {
  1202. dprintk("dib8096P setting output mode TS_PARALLEL_GATED using Mpeg Mux");
  1203. dib8096p_configMpegMux(state, 2, 0, 0);
  1204. dib8096p_setHostBusMux(state, MPEG_ON_HOSTBUS);
  1205. } else { /* Use Smooth block */
  1206. dprintk("dib8096P setting output mode TS_PARALLEL_GATED using Smooth block");
  1207. dib8096p_setHostBusMux(state,
  1208. DEMOUT_ON_HOSTBUS);
  1209. outreg |= (0 << 6);
  1210. }
  1211. break;
  1212. case OUTMODE_MPEG2_PAR_CONT_CLK: /* Using Smooth block only */
  1213. dprintk("dib8096P setting output mode TS_PARALLEL_CONT using Smooth block");
  1214. dib8096p_setHostBusMux(state, DEMOUT_ON_HOSTBUS);
  1215. outreg |= (1 << 6);
  1216. break;
  1217. case OUTMODE_MPEG2_FIFO:
  1218. /* Using Smooth block because not supported
  1219. by new Mpeg Mux bloc */
  1220. dprintk("dib8096P setting output mode TS_FIFO using Smooth block");
  1221. dib8096p_setHostBusMux(state, DEMOUT_ON_HOSTBUS);
  1222. outreg |= (5 << 6);
  1223. smo_mode |= (3 << 1);
  1224. fifo_threshold = 512;
  1225. break;
  1226. case OUTMODE_DIVERSITY:
  1227. dprintk("dib8096P setting output mode MODE_DIVERSITY");
  1228. dib8096p_setDibTxMux(state, DIV_ON_DIBTX);
  1229. dib8096p_setHostBusMux(state, DIBTX_ON_HOSTBUS);
  1230. break;
  1231. case OUTMODE_ANALOG_ADC:
  1232. dprintk("dib8096P setting output mode MODE_ANALOG_ADC");
  1233. dib8096p_setDibTxMux(state, ADC_ON_DIBTX);
  1234. dib8096p_setHostBusMux(state, DIBTX_ON_HOSTBUS);
  1235. break;
  1236. }
  1237. if (mode != OUTMODE_HIGH_Z)
  1238. outreg |= (1<<10);
  1239. dprintk("output_mpeg2_in_188_bytes = %d",
  1240. state->cfg.output_mpeg2_in_188_bytes);
  1241. if (state->cfg.output_mpeg2_in_188_bytes)
  1242. smo_mode |= (1 << 5);
  1243. ret |= dib8000_write_word(state, 299, smo_mode);
  1244. /* synchronous fread */
  1245. ret |= dib8000_write_word(state, 299 + 1, fifo_threshold);
  1246. ret |= dib8000_write_word(state, 1286, outreg);
  1247. return ret;
  1248. }
  1249. static int map_addr_to_serpar_number(struct i2c_msg *msg)
  1250. {
  1251. if (msg->buf[0] <= 15)
  1252. msg->buf[0] -= 1;
  1253. else if (msg->buf[0] == 17)
  1254. msg->buf[0] = 15;
  1255. else if (msg->buf[0] == 16)
  1256. msg->buf[0] = 17;
  1257. else if (msg->buf[0] == 19)
  1258. msg->buf[0] = 16;
  1259. else if (msg->buf[0] >= 21 && msg->buf[0] <= 25)
  1260. msg->buf[0] -= 3;
  1261. else if (msg->buf[0] == 28)
  1262. msg->buf[0] = 23;
  1263. else if (msg->buf[0] == 99)
  1264. msg->buf[0] = 99;
  1265. else
  1266. return -EINVAL;
  1267. return 0;
  1268. }
  1269. static int dib8096p_tuner_write_serpar(struct i2c_adapter *i2c_adap,
  1270. struct i2c_msg msg[], int num)
  1271. {
  1272. struct dib8000_state *state = i2c_get_adapdata(i2c_adap);
  1273. u8 n_overflow = 1;
  1274. u16 i = 1000;
  1275. u16 serpar_num = msg[0].buf[0];
  1276. while (n_overflow == 1 && i) {
  1277. n_overflow = (dib8000_read_word(state, 1984) >> 1) & 0x1;
  1278. i--;
  1279. if (i == 0)
  1280. dprintk("Tuner ITF: write busy (overflow)");
  1281. }
  1282. dib8000_write_word(state, 1985, (1 << 6) | (serpar_num & 0x3f));
  1283. dib8000_write_word(state, 1986, (msg[0].buf[1] << 8) | msg[0].buf[2]);
  1284. return num;
  1285. }
  1286. static int dib8096p_tuner_read_serpar(struct i2c_adapter *i2c_adap,
  1287. struct i2c_msg msg[], int num)
  1288. {
  1289. struct dib8000_state *state = i2c_get_adapdata(i2c_adap);
  1290. u8 n_overflow = 1, n_empty = 1;
  1291. u16 i = 1000;
  1292. u16 serpar_num = msg[0].buf[0];
  1293. u16 read_word;
  1294. while (n_overflow == 1 && i) {
  1295. n_overflow = (dib8000_read_word(state, 1984) >> 1) & 0x1;
  1296. i--;
  1297. if (i == 0)
  1298. dprintk("TunerITF: read busy (overflow)");
  1299. }
  1300. dib8000_write_word(state, 1985, (0<<6) | (serpar_num&0x3f));
  1301. i = 1000;
  1302. while (n_empty == 1 && i) {
  1303. n_empty = dib8000_read_word(state, 1984)&0x1;
  1304. i--;
  1305. if (i == 0)
  1306. dprintk("TunerITF: read busy (empty)");
  1307. }
  1308. read_word = dib8000_read_word(state, 1987);
  1309. msg[1].buf[0] = (read_word >> 8) & 0xff;
  1310. msg[1].buf[1] = (read_word) & 0xff;
  1311. return num;
  1312. }
  1313. static int dib8096p_tuner_rw_serpar(struct i2c_adapter *i2c_adap,
  1314. struct i2c_msg msg[], int num)
  1315. {
  1316. if (map_addr_to_serpar_number(&msg[0]) == 0) {
  1317. if (num == 1) /* write */
  1318. return dib8096p_tuner_write_serpar(i2c_adap, msg, 1);
  1319. else /* read */
  1320. return dib8096p_tuner_read_serpar(i2c_adap, msg, 2);
  1321. }
  1322. return num;
  1323. }
  1324. static int dib8096p_rw_on_apb(struct i2c_adapter *i2c_adap,
  1325. struct i2c_msg msg[], int num, u16 apb_address)
  1326. {
  1327. struct dib8000_state *state = i2c_get_adapdata(i2c_adap);
  1328. u16 word;
  1329. if (num == 1) { /* write */
  1330. dib8000_write_word(state, apb_address,
  1331. ((msg[0].buf[1] << 8) | (msg[0].buf[2])));
  1332. } else {
  1333. word = dib8000_read_word(state, apb_address);
  1334. msg[1].buf[0] = (word >> 8) & 0xff;
  1335. msg[1].buf[1] = (word) & 0xff;
  1336. }
  1337. return num;
  1338. }
  1339. static int dib8096p_tuner_xfer(struct i2c_adapter *i2c_adap,
  1340. struct i2c_msg msg[], int num)
  1341. {
  1342. struct dib8000_state *state = i2c_get_adapdata(i2c_adap);
  1343. u16 apb_address = 0, word;
  1344. int i = 0;
  1345. switch (msg[0].buf[0]) {
  1346. case 0x12:
  1347. apb_address = 1920;
  1348. break;
  1349. case 0x14:
  1350. apb_address = 1921;
  1351. break;
  1352. case 0x24:
  1353. apb_address = 1922;
  1354. break;
  1355. case 0x1a:
  1356. apb_address = 1923;
  1357. break;
  1358. case 0x22:
  1359. apb_address = 1924;
  1360. break;
  1361. case 0x33:
  1362. apb_address = 1926;
  1363. break;
  1364. case 0x34:
  1365. apb_address = 1927;
  1366. break;
  1367. case 0x35:
  1368. apb_address = 1928;
  1369. break;
  1370. case 0x36:
  1371. apb_address = 1929;
  1372. break;
  1373. case 0x37:
  1374. apb_address = 1930;
  1375. break;
  1376. case 0x38:
  1377. apb_address = 1931;
  1378. break;
  1379. case 0x39:
  1380. apb_address = 1932;
  1381. break;
  1382. case 0x2a:
  1383. apb_address = 1935;
  1384. break;
  1385. case 0x2b:
  1386. apb_address = 1936;
  1387. break;
  1388. case 0x2c:
  1389. apb_address = 1937;
  1390. break;
  1391. case 0x2d:
  1392. apb_address = 1938;
  1393. break;
  1394. case 0x2e:
  1395. apb_address = 1939;
  1396. break;
  1397. case 0x2f:
  1398. apb_address = 1940;
  1399. break;
  1400. case 0x30:
  1401. apb_address = 1941;
  1402. break;
  1403. case 0x31:
  1404. apb_address = 1942;
  1405. break;
  1406. case 0x32:
  1407. apb_address = 1943;
  1408. break;
  1409. case 0x3e:
  1410. apb_address = 1944;
  1411. break;
  1412. case 0x3f:
  1413. apb_address = 1945;
  1414. break;
  1415. case 0x40:
  1416. apb_address = 1948;
  1417. break;
  1418. case 0x25:
  1419. apb_address = 936;
  1420. break;
  1421. case 0x26:
  1422. apb_address = 937;
  1423. break;
  1424. case 0x27:
  1425. apb_address = 938;
  1426. break;
  1427. case 0x28:
  1428. apb_address = 939;
  1429. break;
  1430. case 0x1d:
  1431. /* get sad sel request */
  1432. i = ((dib8000_read_word(state, 921) >> 12)&0x3);
  1433. word = dib8000_read_word(state, 924+i);
  1434. msg[1].buf[0] = (word >> 8) & 0xff;
  1435. msg[1].buf[1] = (word) & 0xff;
  1436. return num;
  1437. case 0x1f:
  1438. if (num == 1) { /* write */
  1439. word = (u16) ((msg[0].buf[1] << 8) |
  1440. msg[0].buf[2]);
  1441. /* in the VGAMODE Sel are located on bit 0/1 */
  1442. word &= 0x3;
  1443. word = (dib8000_read_word(state, 921) &
  1444. ~(3<<12)) | (word<<12);
  1445. /* Set the proper input */
  1446. dib8000_write_word(state, 921, word);
  1447. return num;
  1448. }
  1449. }
  1450. if (apb_address != 0) /* R/W acces via APB */
  1451. return dib8096p_rw_on_apb(i2c_adap, msg, num, apb_address);
  1452. else /* R/W access via SERPAR */
  1453. return dib8096p_tuner_rw_serpar(i2c_adap, msg, num);
  1454. return 0;
  1455. }
  1456. static u32 dib8096p_i2c_func(struct i2c_adapter *adapter)
  1457. {
  1458. return I2C_FUNC_I2C;
  1459. }
  1460. static struct i2c_algorithm dib8096p_tuner_xfer_algo = {
  1461. .master_xfer = dib8096p_tuner_xfer,
  1462. .functionality = dib8096p_i2c_func,
  1463. };
  1464. struct i2c_adapter *dib8096p_get_i2c_tuner(struct dvb_frontend *fe)
  1465. {
  1466. struct dib8000_state *st = fe->demodulator_priv;
  1467. return &st->dib8096p_tuner_adap;
  1468. }
  1469. EXPORT_SYMBOL(dib8096p_get_i2c_tuner);
  1470. int dib8096p_tuner_sleep(struct dvb_frontend *fe, int onoff)
  1471. {
  1472. struct dib8000_state *state = fe->demodulator_priv;
  1473. u16 en_cur_state;
  1474. dprintk("sleep dib8096p: %d", onoff);
  1475. en_cur_state = dib8000_read_word(state, 1922);
  1476. /* LNAs and MIX are ON and therefore it is a valid configuration */
  1477. if (en_cur_state > 0xff)
  1478. state->tuner_enable = en_cur_state ;
  1479. if (onoff)
  1480. en_cur_state &= 0x00ff;
  1481. else {
  1482. if (state->tuner_enable != 0)
  1483. en_cur_state = state->tuner_enable;
  1484. }
  1485. dib8000_write_word(state, 1922, en_cur_state);
  1486. return 0;
  1487. }
  1488. EXPORT_SYMBOL(dib8096p_tuner_sleep);
  1489. static const s32 lut_1000ln_mant[] =
  1490. {
  1491. 908, 7003, 7090, 7170, 7244, 7313, 7377, 7438, 7495, 7549, 7600
  1492. };
  1493. s32 dib8000_get_adc_power(struct dvb_frontend *fe, u8 mode)
  1494. {
  1495. struct dib8000_state *state = fe->demodulator_priv;
  1496. u32 ix = 0, tmp_val = 0, exp = 0, mant = 0;
  1497. s32 val;
  1498. val = dib8000_read32(state, 384);
  1499. if (mode) {
  1500. tmp_val = val;
  1501. while (tmp_val >>= 1)
  1502. exp++;
  1503. mant = (val * 1000 / (1<<exp));
  1504. ix = (u8)((mant-1000)/100); /* index of the LUT */
  1505. val = (lut_1000ln_mant[ix] + 693*(exp-20) - 6908);
  1506. val = (val*256)/1000;
  1507. }
  1508. return val;
  1509. }
  1510. EXPORT_SYMBOL(dib8000_get_adc_power);
  1511. int dib8090p_get_dc_power(struct dvb_frontend *fe, u8 IQ)
  1512. {
  1513. struct dib8000_state *state = fe->demodulator_priv;
  1514. int val = 0;
  1515. switch (IQ) {
  1516. case 1:
  1517. val = dib8000_read_word(state, 403);
  1518. break;
  1519. case 0:
  1520. val = dib8000_read_word(state, 404);
  1521. break;
  1522. }
  1523. if (val & 0x200)
  1524. val -= 1024;
  1525. return val;
  1526. }
  1527. EXPORT_SYMBOL(dib8090p_get_dc_power);
  1528. static void dib8000_update_timf(struct dib8000_state *state)
  1529. {
  1530. u32 timf = state->timf = dib8000_read32(state, 435);
  1531. dib8000_write_word(state, 29, (u16) (timf >> 16));
  1532. dib8000_write_word(state, 30, (u16) (timf & 0xffff));
  1533. dprintk("Updated timing frequency: %d (default: %d)", state->timf, state->timf_default);
  1534. }
  1535. u32 dib8000_ctrl_timf(struct dvb_frontend *fe, uint8_t op, uint32_t timf)
  1536. {
  1537. struct dib8000_state *state = fe->demodulator_priv;
  1538. switch (op) {
  1539. case DEMOD_TIMF_SET:
  1540. state->timf = timf;
  1541. break;
  1542. case DEMOD_TIMF_UPDATE:
  1543. dib8000_update_timf(state);
  1544. break;
  1545. case DEMOD_TIMF_GET:
  1546. break;
  1547. }
  1548. dib8000_set_bandwidth(state->fe[0], 6000);
  1549. return state->timf;
  1550. }
  1551. EXPORT_SYMBOL(dib8000_ctrl_timf);
  1552. static const u16 adc_target_16dB[11] = {
  1553. (1 << 13) - 825 - 117,
  1554. (1 << 13) - 837 - 117,
  1555. (1 << 13) - 811 - 117,
  1556. (1 << 13) - 766 - 117,
  1557. (1 << 13) - 737 - 117,
  1558. (1 << 13) - 693 - 117,
  1559. (1 << 13) - 648 - 117,
  1560. (1 << 13) - 619 - 117,
  1561. (1 << 13) - 575 - 117,
  1562. (1 << 13) - 531 - 117,
  1563. (1 << 13) - 501 - 117
  1564. };
  1565. static const u8 permu_seg[] = { 6, 5, 7, 4, 8, 3, 9, 2, 10, 1, 11, 0, 12 };
  1566. static void dib8000_set_channel(struct dib8000_state *state, u8 seq, u8 autosearching)
  1567. {
  1568. u16 mode, max_constellation, seg_diff_mask = 0, nbseg_diff = 0;
  1569. u8 guard, crate, constellation, timeI;
  1570. u16 i, coeff[4], P_cfr_left_edge = 0, P_cfr_right_edge = 0, seg_mask13 = 0x1fff; // All 13 segments enabled
  1571. const s16 *ncoeff = NULL, *ana_fe;
  1572. u16 tmcc_pow = 0;
  1573. u16 coff_pow = 0x2800;
  1574. u16 init_prbs = 0xfff;
  1575. u16 ana_gain = 0;
  1576. if (state->revision == 0x8090)
  1577. dib8000_init_sdram(state);
  1578. if (state->ber_monitored_layer != LAYER_ALL)
  1579. dib8000_write_word(state, 285, (dib8000_read_word(state, 285) & 0x60) | state->ber_monitored_layer);
  1580. else
  1581. dib8000_write_word(state, 285, dib8000_read_word(state, 285) & 0x60);
  1582. i = dib8000_read_word(state, 26) & 1; // P_dds_invspec
  1583. dib8000_write_word(state, 26, state->fe[0]->dtv_property_cache.inversion^i);
  1584. if (state->fe[0]->dtv_property_cache.isdbt_sb_mode) {
  1585. //compute new dds_freq for the seg and adjust prbs
  1586. int seg_offset =
  1587. state->fe[0]->dtv_property_cache.isdbt_sb_segment_idx -
  1588. (state->fe[0]->dtv_property_cache.isdbt_sb_segment_count / 2) -
  1589. (state->fe[0]->dtv_property_cache.isdbt_sb_segment_count % 2);
  1590. int clk = state->cfg.pll->internal;
  1591. u32 segtodds = ((u32) (430 << 23) / clk) << 3; // segtodds = SegBW / Fclk * pow(2,26)
  1592. int dds_offset = seg_offset * segtodds;
  1593. int new_dds, sub_channel;
  1594. if ((state->fe[0]->dtv_property_cache.isdbt_sb_segment_count % 2) == 0)
  1595. dds_offset -= (int)(segtodds / 2);
  1596. if (state->cfg.pll->ifreq == 0) {
  1597. if ((state->fe[0]->dtv_property_cache.inversion ^ i) == 0) {
  1598. dib8000_write_word(state, 26, dib8000_read_word(state, 26) | 1);
  1599. new_dds = dds_offset;
  1600. } else
  1601. new_dds = dds_offset;
  1602. // We shift tuning frequency if the wanted segment is :
  1603. // - the segment of center frequency with an odd total number of segments
  1604. // - the segment to the left of center frequency with an even total number of segments
  1605. // - the segment to the right of center frequency with an even total number of segments
  1606. if ((state->fe[0]->dtv_property_cache.delivery_system == SYS_ISDBT)
  1607. && (state->fe[0]->dtv_property_cache.isdbt_sb_mode == 1)
  1608. && (((state->fe[0]->dtv_property_cache.isdbt_sb_segment_count % 2)
  1609. && (state->fe[0]->dtv_property_cache.isdbt_sb_segment_idx ==
  1610. ((state->fe[0]->dtv_property_cache.isdbt_sb_segment_count / 2) + 1)))
  1611. || (((state->fe[0]->dtv_property_cache.isdbt_sb_segment_count % 2) == 0)
  1612. && (state->fe[0]->dtv_property_cache.isdbt_sb_segment_idx == (state->fe[0]->dtv_property_cache.isdbt_sb_segment_count / 2)))
  1613. || (((state->fe[0]->dtv_property_cache.isdbt_sb_segment_count % 2) == 0)
  1614. && (state->fe[0]->dtv_property_cache.isdbt_sb_segment_idx ==
  1615. ((state->fe[0]->dtv_property_cache.isdbt_sb_segment_count / 2) + 1)))
  1616. )) {
  1617. new_dds -= ((u32) (850 << 22) / clk) << 4; // new_dds = 850 (freq shift in KHz) / Fclk * pow(2,26)
  1618. }
  1619. } else {
  1620. if ((state->fe[0]->dtv_property_cache.inversion ^ i) == 0)
  1621. new_dds = state->cfg.pll->ifreq - dds_offset;
  1622. else
  1623. new_dds = state->cfg.pll->ifreq + dds_offset;
  1624. }
  1625. dib8000_write_word(state, 27, (u16) ((new_dds >> 16) & 0x01ff));
  1626. dib8000_write_word(state, 28, (u16) (new_dds & 0xffff));
  1627. if (state->fe[0]->dtv_property_cache.isdbt_sb_segment_count % 2)
  1628. sub_channel = ((state->fe[0]->dtv_property_cache.isdbt_sb_subchannel + (3 * seg_offset) + 1) % 41) / 3;
  1629. else
  1630. sub_channel = ((state->fe[0]->dtv_property_cache.isdbt_sb_subchannel + (3 * seg_offset)) % 41) / 3;
  1631. sub_channel -= 6;
  1632. if (state->fe[0]->dtv_property_cache.transmission_mode == TRANSMISSION_MODE_2K
  1633. || state->fe[0]->dtv_property_cache.transmission_mode == TRANSMISSION_MODE_4K) {
  1634. dib8000_write_word(state, 219, dib8000_read_word(state, 219) | 0x1); //adp_pass =1
  1635. dib8000_write_word(state, 190, dib8000_read_word(state, 190) | (0x1 << 14)); //pha3_force_pha_shift = 1
  1636. } else {
  1637. dib8000_write_word(state, 219, dib8000_read_word(state, 219) & 0xfffe); //adp_pass =0
  1638. dib8000_write_word(state, 190, dib8000_read_word(state, 190) & 0xbfff); //pha3_force_pha_shift = 0
  1639. }
  1640. switch (state->fe[0]->dtv_property_cache.transmission_mode) {
  1641. case TRANSMISSION_MODE_2K:
  1642. switch (sub_channel) {
  1643. case -6:
  1644. init_prbs = 0x0;
  1645. break; // 41, 0, 1
  1646. case -5:
  1647. init_prbs = 0x423;
  1648. break; // 02~04
  1649. case -4:
  1650. init_prbs = 0x9;
  1651. break; // 05~07
  1652. case -3:
  1653. init_prbs = 0x5C7;
  1654. break; // 08~10
  1655. case -2:
  1656. init_prbs = 0x7A6;
  1657. break; // 11~13
  1658. case -1:
  1659. init_prbs = 0x3D8;
  1660. break; // 14~16
  1661. case 0:
  1662. init_prbs = 0x527;
  1663. break; // 17~19
  1664. case 1:
  1665. init_prbs = 0x7FF;
  1666. break; // 20~22
  1667. case 2:
  1668. init_prbs = 0x79B;
  1669. break; // 23~25
  1670. case 3:
  1671. init_prbs = 0x3D6;
  1672. break; // 26~28
  1673. case 4:
  1674. init_prbs = 0x3A2;
  1675. break; // 29~31
  1676. case 5:
  1677. init_prbs = 0x53B;
  1678. break; // 32~34
  1679. case 6:
  1680. init_prbs = 0x2F4;
  1681. break; // 35~37
  1682. default:
  1683. case 7:
  1684. init_prbs = 0x213;
  1685. break; // 38~40
  1686. }
  1687. break;
  1688. case TRANSMISSION_MODE_4K:
  1689. switch (sub_channel) {
  1690. case -6:
  1691. init_prbs = 0x0;
  1692. break; // 41, 0, 1
  1693. case -5:
  1694. init_prbs = 0x208;
  1695. break; // 02~04
  1696. case -4:
  1697. init_prbs = 0xC3;
  1698. break; // 05~07
  1699. case -3:
  1700. init_prbs = 0x7B9;
  1701. break; // 08~10
  1702. case -2:
  1703. init_prbs = 0x423;
  1704. break; // 11~13
  1705. case -1:
  1706. init_prbs = 0x5C7;
  1707. break; // 14~16
  1708. case 0:
  1709. init_prbs = 0x3D8;
  1710. break; // 17~19
  1711. case 1:
  1712. init_prbs = 0x7FF;
  1713. break; // 20~22
  1714. case 2:
  1715. init_prbs = 0x3D6;
  1716. break; // 23~25
  1717. case 3:
  1718. init_prbs = 0x53B;
  1719. break; // 26~28
  1720. case 4:
  1721. init_prbs = 0x213;
  1722. break; // 29~31
  1723. case 5:
  1724. init_prbs = 0x29;
  1725. break; // 32~34
  1726. case 6:
  1727. init_prbs = 0xD0;
  1728. break; // 35~37
  1729. default:
  1730. case 7:
  1731. init_prbs = 0x48E;
  1732. break; // 38~40
  1733. }
  1734. break;
  1735. default:
  1736. case TRANSMISSION_MODE_8K:
  1737. switch (sub_channel) {
  1738. case -6:
  1739. init_prbs = 0x0;
  1740. break; // 41, 0, 1
  1741. case -5:
  1742. init_prbs = 0x740;
  1743. break; // 02~04
  1744. case -4:
  1745. init_prbs = 0x069;
  1746. break; // 05~07
  1747. case -3:
  1748. init_prbs = 0x7DD;
  1749. break; // 08~10
  1750. case -2:
  1751. init_prbs = 0x208;
  1752. break; // 11~13
  1753. case -1:
  1754. init_prbs = 0x7B9;
  1755. break; // 14~16
  1756. case 0:
  1757. init_prbs = 0x5C7;
  1758. break; // 17~19
  1759. case 1:
  1760. init_prbs = 0x7FF;
  1761. break; // 20~22
  1762. case 2:
  1763. init_prbs = 0x53B;
  1764. break; // 23~25
  1765. case 3:
  1766. init_prbs = 0x29;
  1767. break; // 26~28
  1768. case 4:
  1769. init_prbs = 0x48E;
  1770. break; // 29~31
  1771. case 5:
  1772. init_prbs = 0x4C4;
  1773. break; // 32~34
  1774. case 6:
  1775. init_prbs = 0x367;
  1776. break; // 33~37
  1777. default:
  1778. case 7:
  1779. init_prbs = 0x684;
  1780. break; // 38~40
  1781. }
  1782. break;
  1783. }
  1784. } else {
  1785. dib8000_write_word(state, 27, (u16) ((state->cfg.pll->ifreq >> 16) & 0x01ff));
  1786. dib8000_write_word(state, 28, (u16) (state->cfg.pll->ifreq & 0xffff));
  1787. dib8000_write_word(state, 26, (u16) ((state->cfg.pll->ifreq >> 25) & 0x0003));
  1788. }
  1789. /*P_mode == ?? */
  1790. dib8000_write_word(state, 10, (seq << 4));
  1791. // dib8000_write_word(state, 287, (dib8000_read_word(state, 287) & 0xe000) | 0x1000);
  1792. switch (state->fe[0]->dtv_property_cache.guard_interval) {
  1793. case GUARD_INTERVAL_1_32:
  1794. guard = 0;
  1795. break;
  1796. case GUARD_INTERVAL_1_16:
  1797. guard = 1;
  1798. break;
  1799. case GUARD_INTERVAL_1_8:
  1800. guard = 2;
  1801. break;
  1802. case GUARD_INTERVAL_1_4:
  1803. default:
  1804. guard = 3;
  1805. break;
  1806. }
  1807. dib8000_write_word(state, 1, (init_prbs << 2) | (guard & 0x3)); // ADDR 1
  1808. max_constellation = DQPSK;
  1809. for (i = 0; i < 3; i++) {
  1810. switch (state->fe[0]->dtv_property_cache.layer[i].modulation) {
  1811. case DQPSK:
  1812. constellation = 0;
  1813. break;
  1814. case QPSK:
  1815. constellation = 1;
  1816. break;
  1817. case QAM_16:
  1818. constellation = 2;
  1819. break;
  1820. case QAM_64:
  1821. default:
  1822. constellation = 3;
  1823. break;
  1824. }
  1825. switch (state->fe[0]->dtv_property_cache.layer[i].fec) {
  1826. case FEC_1_2:
  1827. crate = 1;
  1828. break;
  1829. case FEC_2_3:
  1830. crate = 2;
  1831. break;
  1832. case FEC_3_4:
  1833. crate = 3;
  1834. break;
  1835. case FEC_5_6:
  1836. crate = 5;
  1837. break;
  1838. case FEC_7_8:
  1839. default:
  1840. crate = 7;
  1841. break;
  1842. }
  1843. if ((state->fe[0]->dtv_property_cache.layer[i].interleaving > 0) &&
  1844. ((state->fe[0]->dtv_property_cache.layer[i].interleaving <= 3) ||
  1845. (state->fe[0]->dtv_property_cache.layer[i].interleaving == 4 && state->fe[0]->dtv_property_cache.isdbt_sb_mode == 1))
  1846. )
  1847. timeI = state->fe[0]->dtv_property_cache.layer[i].interleaving;
  1848. else
  1849. timeI = 0;
  1850. dib8000_write_word(state, 2 + i, (constellation << 10) | ((state->fe[0]->dtv_property_cache.layer[i].segment_count & 0xf) << 6) |
  1851. (crate << 3) | timeI);
  1852. if (state->fe[0]->dtv_property_cache.layer[i].segment_count > 0) {
  1853. switch (max_constellation) {
  1854. case DQPSK:
  1855. case QPSK:
  1856. if (state->fe[0]->dtv_property_cache.layer[i].modulation == QAM_16 ||
  1857. state->fe[0]->dtv_property_cache.layer[i].modulation == QAM_64)
  1858. max_constellation = state->fe[0]->dtv_property_cache.layer[i].modulation;
  1859. break;
  1860. case QAM_16:
  1861. if (state->fe[0]->dtv_property_cache.layer[i].modulation == QAM_64)
  1862. max_constellation = state->fe[0]->dtv_property_cache.layer[i].modulation;
  1863. break;
  1864. }
  1865. }
  1866. }
  1867. mode = fft_to_mode(state);
  1868. //dib8000_write_word(state, 5, 13); /*p_last_seg = 13*/
  1869. dib8000_write_word(state, 274, (dib8000_read_word(state, 274) & 0xffcf) |
  1870. ((state->fe[0]->dtv_property_cache.isdbt_partial_reception & 1) << 5) | ((state->fe[0]->dtv_property_cache.
  1871. isdbt_sb_mode & 1) << 4));
  1872. dprintk("mode = %d ; guard = %d", mode, state->fe[0]->dtv_property_cache.guard_interval);
  1873. /* signal optimization parameter */
  1874. if (state->fe[0]->dtv_property_cache.isdbt_partial_reception) {
  1875. seg_diff_mask = (state->fe[0]->dtv_property_cache.layer[0].modulation == DQPSK) << permu_seg[0];
  1876. for (i = 1; i < 3; i++)
  1877. nbseg_diff +=
  1878. (state->fe[0]->dtv_property_cache.layer[i].modulation == DQPSK) * state->fe[0]->dtv_property_cache.layer[i].segment_count;
  1879. for (i = 0; i < nbseg_diff; i++)
  1880. seg_diff_mask |= 1 << permu_seg[i + 1];
  1881. } else {
  1882. for (i = 0; i < 3; i++)
  1883. nbseg_diff +=
  1884. (state->fe[0]->dtv_property_cache.layer[i].modulation == DQPSK) * state->fe[0]->dtv_property_cache.layer[i].segment_count;
  1885. for (i = 0; i < nbseg_diff; i++)
  1886. seg_diff_mask |= 1 << permu_seg[i];
  1887. }
  1888. dprintk("nbseg_diff = %X (%d)", seg_diff_mask, seg_diff_mask);
  1889. state->differential_constellation = (seg_diff_mask != 0);
  1890. if (state->revision != 0x8090)
  1891. dib8000_set_diversity_in(state->fe[0], state->diversity_onoff);
  1892. else
  1893. dib8096p_set_diversity_in(state->fe[0], state->diversity_onoff);
  1894. if (state->fe[0]->dtv_property_cache.isdbt_sb_mode == 1) {
  1895. if (state->fe[0]->dtv_property_cache.isdbt_partial_reception == 1)
  1896. seg_mask13 = 0x00E0;
  1897. else // 1-segment
  1898. seg_mask13 = 0x0040;
  1899. } else
  1900. seg_mask13 = 0x1fff;
  1901. // WRITE: Mode & Diff mask
  1902. dib8000_write_word(state, 0, (mode << 13) | seg_diff_mask);
  1903. if ((seg_diff_mask) || (state->fe[0]->dtv_property_cache.isdbt_sb_mode))
  1904. dib8000_write_word(state, 268, (dib8000_read_word(state, 268) & 0xF9FF) | 0x0200);
  1905. else
  1906. dib8000_write_word(state, 268, (2 << 9) | 39); //init value
  1907. // ---- SMALL ----
  1908. // P_small_seg_diff
  1909. dib8000_write_word(state, 352, seg_diff_mask); // ADDR 352
  1910. dib8000_write_word(state, 353, seg_mask13); // ADDR 353
  1911. /* // P_small_narrow_band=0, P_small_last_seg=13, P_small_offset_num_car=5 */
  1912. // ---- SMALL ----
  1913. if (state->fe[0]->dtv_property_cache.isdbt_sb_mode == 1) {
  1914. switch (state->fe[0]->dtv_property_cache.transmission_mode) {
  1915. case TRANSMISSION_MODE_2K:
  1916. if (state->fe[0]->dtv_property_cache.isdbt_partial_reception == 0) {
  1917. if (state->fe[0]->dtv_property_cache.layer[0].modulation == DQPSK)
  1918. ncoeff = coeff_2k_sb_1seg_dqpsk;
  1919. else // QPSK or QAM
  1920. ncoeff = coeff_2k_sb_1seg;
  1921. } else { // 3-segments
  1922. if (state->fe[0]->dtv_property_cache.layer[0].modulation == DQPSK) {
  1923. if (state->fe[0]->dtv_property_cache.layer[1].modulation == DQPSK)
  1924. ncoeff = coeff_2k_sb_3seg_0dqpsk_1dqpsk;
  1925. else // QPSK or QAM on external segments
  1926. ncoeff = coeff_2k_sb_3seg_0dqpsk;
  1927. } else { // QPSK or QAM on central segment
  1928. if (state->fe[0]->dtv_property_cache.layer[1].modulation == DQPSK)
  1929. ncoeff = coeff_2k_sb_3seg_1dqpsk;
  1930. else // QPSK or QAM on external segments
  1931. ncoeff = coeff_2k_sb_3seg;
  1932. }
  1933. }
  1934. break;
  1935. case TRANSMISSION_MODE_4K:
  1936. if (state->fe[0]->dtv_property_cache.isdbt_partial_reception == 0) {
  1937. if (state->fe[0]->dtv_property_cache.layer[0].modulation == DQPSK)
  1938. ncoeff = coeff_4k_sb_1seg_dqpsk;
  1939. else // QPSK or QAM
  1940. ncoeff = coeff_4k_sb_1seg;
  1941. } else { // 3-segments
  1942. if (state->fe[0]->dtv_property_cache.layer[0].modulation == DQPSK) {
  1943. if (state->fe[0]->dtv_property_cache.layer[1].modulation == DQPSK) {
  1944. ncoeff = coeff_4k_sb_3seg_0dqpsk_1dqpsk;
  1945. } else { // QPSK or QAM on external segments
  1946. ncoeff = coeff_4k_sb_3seg_0dqpsk;
  1947. }
  1948. } else { // QPSK or QAM on central segment
  1949. if (state->fe[0]->dtv_property_cache.layer[1].modulation == DQPSK) {
  1950. ncoeff = coeff_4k_sb_3seg_1dqpsk;
  1951. } else // QPSK or QAM on external segments
  1952. ncoeff = coeff_4k_sb_3seg;
  1953. }
  1954. }
  1955. break;
  1956. case TRANSMISSION_MODE_AUTO:
  1957. case TRANSMISSION_MODE_8K:
  1958. default:
  1959. if (state->fe[0]->dtv_property_cache.isdbt_partial_reception == 0) {
  1960. if (state->fe[0]->dtv_property_cache.layer[0].modulation == DQPSK)
  1961. ncoeff = coeff_8k_sb_1seg_dqpsk;
  1962. else // QPSK or QAM
  1963. ncoeff = coeff_8k_sb_1seg;
  1964. } else { // 3-segments
  1965. if (state->fe[0]->dtv_property_cache.layer[0].modulation == DQPSK) {
  1966. if (state->fe[0]->dtv_property_cache.layer[1].modulation == DQPSK) {
  1967. ncoeff = coeff_8k_sb_3seg_0dqpsk_1dqpsk;
  1968. } else { // QPSK or QAM on external segments
  1969. ncoeff = coeff_8k_sb_3seg_0dqpsk;
  1970. }
  1971. } else { // QPSK or QAM on central segment
  1972. if (state->fe[0]->dtv_property_cache.layer[1].modulation == DQPSK) {
  1973. ncoeff = coeff_8k_sb_3seg_1dqpsk;
  1974. } else // QPSK or QAM on external segments
  1975. ncoeff = coeff_8k_sb_3seg;
  1976. }
  1977. }
  1978. break;
  1979. }
  1980. for (i = 0; i < 8; i++)
  1981. dib8000_write_word(state, 343 + i, ncoeff[i]);
  1982. }
  1983. // P_small_coef_ext_enable=ISDB-Tsb, P_small_narrow_band=ISDB-Tsb, P_small_last_seg=13, P_small_offset_num_car=5
  1984. dib8000_write_word(state, 351,
  1985. (state->fe[0]->dtv_property_cache.isdbt_sb_mode << 9) | (state->fe[0]->dtv_property_cache.isdbt_sb_mode << 8) | (13 << 4) | 5);
  1986. // ---- COFF ----
  1987. // Carloff, the most robust
  1988. if (state->fe[0]->dtv_property_cache.isdbt_sb_mode == 1) {
  1989. // P_coff_cpil_alpha=4, P_coff_inh=0, P_coff_cpil_winlen=64
  1990. // 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
  1991. dib8000_write_word(state, 187,
  1992. (4 << 12) | (0 << 11) | (63 << 5) | (0x3 << 3) | ((~state->fe[0]->dtv_property_cache.isdbt_partial_reception & 1) << 2)
  1993. | 0x3);
  1994. /* // P_small_coef_ext_enable = 1 */
  1995. /* dib8000_write_word(state, 351, dib8000_read_word(state, 351) | 0x200); */
  1996. if (state->fe[0]->dtv_property_cache.isdbt_partial_reception == 0) {
  1997. // 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)
  1998. if (mode == 3)
  1999. dib8000_write_word(state, 180, 0x1fcf | ((mode - 1) << 14));
  2000. else
  2001. dib8000_write_word(state, 180, 0x0fcf | ((mode - 1) << 14));
  2002. // P_ctrl_corm_thres4pre_freq_inh=1,P_ctrl_pre_freq_mode_sat=1,
  2003. // P_ctrl_pre_freq_inh=0, P_ctrl_pre_freq_step = 5, P_pre_freq_win_len=4
  2004. dib8000_write_word(state, 338, (1 << 12) | (1 << 10) | (0 << 9) | (5 << 5) | 4);
  2005. // P_ctrl_pre_freq_win_len=16, P_ctrl_pre_freq_thres_lockin=8
  2006. dib8000_write_word(state, 340, (16 << 6) | (8 << 0));
  2007. // P_ctrl_pre_freq_thres_lockout=6, P_small_use_tmcc/ac/cp=1
  2008. dib8000_write_word(state, 341, (6 << 3) | (1 << 2) | (1 << 1) | (1 << 0));
  2009. // P_coff_corthres_8k, 4k, 2k and P_coff_cpilthres_8k, 4k, 2k
  2010. dib8000_write_word(state, 181, 300);
  2011. dib8000_write_word(state, 182, 150);
  2012. dib8000_write_word(state, 183, 80);
  2013. dib8000_write_word(state, 184, 300);
  2014. dib8000_write_word(state, 185, 150);
  2015. dib8000_write_word(state, 186, 80);
  2016. } else { // Sound Broadcasting mode 3 seg
  2017. // P_coff_one_seg_sym= 1, P_coff_one_seg_width= 1, P_coff_winlen=63, P_coff_thres_lock=15
  2018. /* if (mode == 3) */
  2019. /* dib8000_write_word(state, 180, 0x2fca | ((0) << 14)); */
  2020. /* else */
  2021. /* dib8000_write_word(state, 180, 0x2fca | ((1) << 14)); */
  2022. dib8000_write_word(state, 180, 0x1fcf | (1 << 14));
  2023. // P_ctrl_corm_thres4pre_freq_inh = 1, P_ctrl_pre_freq_mode_sat=1,
  2024. // P_ctrl_pre_freq_inh=0, P_ctrl_pre_freq_step = 4, P_pre_freq_win_len=4
  2025. dib8000_write_word(state, 338, (1 << 12) | (1 << 10) | (0 << 9) | (4 << 5) | 4);
  2026. // P_ctrl_pre_freq_win_len=16, P_ctrl_pre_freq_thres_lockin=8
  2027. dib8000_write_word(state, 340, (16 << 6) | (8 << 0));
  2028. //P_ctrl_pre_freq_thres_lockout=6, P_small_use_tmcc/ac/cp=1
  2029. dib8000_write_word(state, 341, (6 << 3) | (1 << 2) | (1 << 1) | (1 << 0));
  2030. // P_coff_corthres_8k, 4k, 2k and P_coff_cpilthres_8k, 4k, 2k
  2031. dib8000_write_word(state, 181, 350);
  2032. dib8000_write_word(state, 182, 300);
  2033. dib8000_write_word(state, 183, 250);
  2034. dib8000_write_word(state, 184, 350);
  2035. dib8000_write_word(state, 185, 300);
  2036. dib8000_write_word(state, 186, 250);
  2037. }
  2038. } else if (state->isdbt_cfg_loaded == 0) { // if not Sound Broadcasting mode : put default values for 13 segments
  2039. dib8000_write_word(state, 180, (16 << 6) | 9);
  2040. dib8000_write_word(state, 187, (4 << 12) | (8 << 5) | 0x2);
  2041. coff_pow = 0x2800;
  2042. for (i = 0; i < 6; i++)
  2043. dib8000_write_word(state, 181 + i, coff_pow);
  2044. // P_ctrl_corm_thres4pre_freq_inh=1, P_ctrl_pre_freq_mode_sat=1,
  2045. // 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
  2046. dib8000_write_word(state, 338, (1 << 12) | (1 << 10) | (0 << 9) | (3 << 5) | 1);
  2047. // P_ctrl_pre_freq_win_len=8, P_ctrl_pre_freq_thres_lockin=6
  2048. dib8000_write_word(state, 340, (8 << 6) | (6 << 0));
  2049. // P_ctrl_pre_freq_thres_lockout=4, P_small_use_tmcc/ac/cp=1
  2050. dib8000_write_word(state, 341, (4 << 3) | (1 << 2) | (1 << 1) | (1 << 0));
  2051. }
  2052. // ---- FFT ----
  2053. if (state->fe[0]->dtv_property_cache.isdbt_sb_mode == 1 && state->fe[0]->dtv_property_cache.isdbt_partial_reception == 0)
  2054. dib8000_write_word(state, 178, 64); // P_fft_powrange=64
  2055. else
  2056. dib8000_write_word(state, 178, 32); // P_fft_powrange=32
  2057. /* make the cpil_coff_lock more robust but slower p_coff_winlen
  2058. * 6bits; p_coff_thres_lock 6bits (for coff lock if needed)
  2059. */
  2060. /* if ( ( nbseg_diff>0)&&(nbseg_diff<13))
  2061. dib8000_write_word(state, 187, (dib8000_read_word(state, 187) & 0xfffb) | (1 << 3)); */
  2062. dib8000_write_word(state, 189, ~seg_mask13 | seg_diff_mask); /* P_lmod4_seg_inh */
  2063. dib8000_write_word(state, 192, ~seg_mask13 | seg_diff_mask); /* P_pha3_seg_inh */
  2064. dib8000_write_word(state, 225, ~seg_mask13 | seg_diff_mask); /* P_tac_seg_inh */
  2065. if ((!state->fe[0]->dtv_property_cache.isdbt_sb_mode) && (state->cfg.pll->ifreq == 0))
  2066. dib8000_write_word(state, 266, ~seg_mask13 | seg_diff_mask | 0x40); /* P_equal_noise_seg_inh */
  2067. else
  2068. dib8000_write_word(state, 266, ~seg_mask13 | seg_diff_mask); /* P_equal_noise_seg_inh */
  2069. dib8000_write_word(state, 287, ~seg_mask13 | 0x1000); /* P_tmcc_seg_inh */
  2070. //dib8000_write_word(state, 288, ~seg_mask13 | seg_diff_mask); /* P_tmcc_seg_eq_inh */
  2071. if (!autosearching)
  2072. dib8000_write_word(state, 288, (~seg_mask13 | seg_diff_mask) & 0x1fff); /* P_tmcc_seg_eq_inh */
  2073. else
  2074. dib8000_write_word(state, 288, 0x1fff); //disable equalisation of the tmcc when autosearch to be able to find the DQPSK channels.
  2075. dprintk("287 = %X (%d)", ~seg_mask13 | 0x1000, ~seg_mask13 | 0x1000);
  2076. dib8000_write_word(state, 211, seg_mask13 & (~seg_diff_mask)); /* P_des_seg_enabled */
  2077. /* offset loop parameters */
  2078. if (state->fe[0]->dtv_property_cache.isdbt_sb_mode == 1) {
  2079. if (state->fe[0]->dtv_property_cache.isdbt_partial_reception == 0)
  2080. /* P_timf_alpha = (11-P_mode), P_corm_alpha=6, P_corm_thres=0x80 */
  2081. dib8000_write_word(state, 32, ((11 - mode) << 12) | (6 << 8) | 0x40);
  2082. else // Sound Broadcasting mode 3 seg
  2083. /* P_timf_alpha = (10-P_mode), P_corm_alpha=6, P_corm_thres=0x80 */
  2084. dib8000_write_word(state, 32, ((10 - mode) << 12) | (6 << 8) | 0x60);
  2085. } else
  2086. // TODO in 13 seg, timf_alpha can always be the same or not ?
  2087. /* P_timf_alpha = (9-P_mode, P_corm_alpha=6, P_corm_thres=0x80 */
  2088. dib8000_write_word(state, 32, ((9 - mode) << 12) | (6 << 8) | 0x80);
  2089. if (state->fe[0]->dtv_property_cache.isdbt_sb_mode == 1) {
  2090. if (state->fe[0]->dtv_property_cache.isdbt_partial_reception == 0)
  2091. /* P_ctrl_pha_off_max=3 P_ctrl_sfreq_inh =0 P_ctrl_sfreq_step = (11-P_mode) */
  2092. dib8000_write_word(state, 37, (3 << 5) | (0 << 4) | (10 - mode));
  2093. else // Sound Broadcasting mode 3 seg
  2094. /* P_ctrl_pha_off_max=3 P_ctrl_sfreq_inh =0 P_ctrl_sfreq_step = (10-P_mode) */
  2095. dib8000_write_word(state, 37, (3 << 5) | (0 << 4) | (9 - mode));
  2096. } else
  2097. /* P_ctrl_pha_off_max=3 P_ctrl_sfreq_inh =0 P_ctrl_sfreq_step = 9 */
  2098. dib8000_write_word(state, 37, (3 << 5) | (0 << 4) | (8 - mode));
  2099. /* P_dvsy_sync_wait - reuse mode */
  2100. switch (state->fe[0]->dtv_property_cache.transmission_mode) {
  2101. case TRANSMISSION_MODE_8K:
  2102. mode = 256;
  2103. break;
  2104. case TRANSMISSION_MODE_4K:
  2105. mode = 128;
  2106. break;
  2107. default:
  2108. case TRANSMISSION_MODE_2K:
  2109. mode = 64;
  2110. break;
  2111. }
  2112. if (state->cfg.diversity_delay == 0)
  2113. mode = (mode * (1 << (guard)) * 3) / 2 + 48; // add 50% SFN margin + compensate for one DVSY-fifo
  2114. else
  2115. mode = (mode * (1 << (guard)) * 3) / 2 + state->cfg.diversity_delay; // add 50% SFN margin + compensate for DVSY-fifo
  2116. mode <<= 4;
  2117. dib8000_write_word(state, 273, (dib8000_read_word(state, 273) & 0x000f) | mode);
  2118. /* channel estimation fine configuration */
  2119. switch (max_constellation) {
  2120. case QAM_64:
  2121. ana_gain = 0x7; // -1 : avoid def_est saturation when ADC target is -16dB
  2122. coeff[0] = 0x0148; /* P_adp_regul_cnt 0.04 */
  2123. coeff[1] = 0xfff0; /* P_adp_noise_cnt -0.002 */
  2124. coeff[2] = 0x00a4; /* P_adp_regul_ext 0.02 */
  2125. coeff[3] = 0xfff8; /* P_adp_noise_ext -0.001 */
  2126. //if (!state->cfg.hostbus_diversity) //if diversity, we should prehaps use the configuration of the max_constallation -1
  2127. break;
  2128. case QAM_16:
  2129. ana_gain = 0x7; // -1 : avoid def_est saturation when ADC target is -16dB
  2130. coeff[0] = 0x023d; /* P_adp_regul_cnt 0.07 */
  2131. coeff[1] = 0xffdf; /* P_adp_noise_cnt -0.004 */
  2132. coeff[2] = 0x00a4; /* P_adp_regul_ext 0.02 */
  2133. coeff[3] = 0xfff0; /* P_adp_noise_ext -0.002 */
  2134. //if (!((state->cfg.hostbus_diversity) && (max_constellation == QAM_16)))
  2135. break;
  2136. default:
  2137. ana_gain = 0; // 0 : goes along with ADC target at -22dB to keep good mobile performance and lock at sensitivity level
  2138. coeff[0] = 0x099a; /* P_adp_regul_cnt 0.3 */
  2139. coeff[1] = 0xffae; /* P_adp_noise_cnt -0.01 */
  2140. coeff[2] = 0x0333; /* P_adp_regul_ext 0.1 */
  2141. coeff[3] = 0xfff8; /* P_adp_noise_ext -0.002 */
  2142. break;
  2143. }
  2144. for (mode = 0; mode < 4; mode++)
  2145. dib8000_write_word(state, 215 + mode, coeff[mode]);
  2146. // update ana_gain depending on max constellation
  2147. dib8000_write_word(state, 116, ana_gain);
  2148. // update ADC target depending on ana_gain
  2149. if (ana_gain) { // set -16dB ADC target for ana_gain=-1
  2150. for (i = 0; i < 10; i++)
  2151. dib8000_write_word(state, 80 + i, adc_target_16dB[i]);
  2152. } else { // set -22dB ADC target for ana_gain=0
  2153. for (i = 0; i < 10; i++)
  2154. dib8000_write_word(state, 80 + i, adc_target_16dB[i] - 355);
  2155. }
  2156. // ---- ANA_FE ----
  2157. if (state->fe[0]->dtv_property_cache.isdbt_sb_mode) {
  2158. if (state->fe[0]->dtv_property_cache.isdbt_partial_reception == 1)
  2159. ana_fe = ana_fe_coeff_3seg;
  2160. else // 1-segment
  2161. ana_fe = ana_fe_coeff_1seg;
  2162. } else
  2163. ana_fe = ana_fe_coeff_13seg;
  2164. if (state->fe[0]->dtv_property_cache.isdbt_sb_mode == 1 || state->isdbt_cfg_loaded == 0)
  2165. for (mode = 0; mode < 24; mode++)
  2166. dib8000_write_word(state, 117 + mode, ana_fe[mode]);
  2167. // ---- CHAN_BLK ----
  2168. for (i = 0; i < 13; i++) {
  2169. if ((((~seg_diff_mask) >> i) & 1) == 1) {
  2170. P_cfr_left_edge += (1 << i) * ((i == 0) || ((((seg_mask13 & (~seg_diff_mask)) >> (i - 1)) & 1) == 0));
  2171. P_cfr_right_edge += (1 << i) * ((i == 12) || ((((seg_mask13 & (~seg_diff_mask)) >> (i + 1)) & 1) == 0));
  2172. }
  2173. }
  2174. dib8000_write_word(state, 222, P_cfr_left_edge); // P_cfr_left_edge
  2175. dib8000_write_word(state, 223, P_cfr_right_edge); // P_cfr_right_edge
  2176. // "P_cspu_left_edge" not used => do not care
  2177. // "P_cspu_right_edge" not used => do not care
  2178. if (state->fe[0]->dtv_property_cache.isdbt_sb_mode == 1) {
  2179. dib8000_write_word(state, 228, 1); // P_2d_mode_byp=1
  2180. dib8000_write_word(state, 205, dib8000_read_word(state, 205) & 0xfff0); // P_cspu_win_cut = 0
  2181. if (state->fe[0]->dtv_property_cache.isdbt_partial_reception == 0
  2182. && state->fe[0]->dtv_property_cache.transmission_mode == TRANSMISSION_MODE_2K) {
  2183. //dib8000_write_word(state, 219, dib8000_read_word(state, 219) & 0xfffe); // P_adp_pass = 0
  2184. dib8000_write_word(state, 265, 15); // P_equal_noise_sel = 15
  2185. }
  2186. } else if (state->isdbt_cfg_loaded == 0) {
  2187. dib8000_write_word(state, 228, 0); // default value
  2188. dib8000_write_word(state, 265, 31); // default value
  2189. dib8000_write_word(state, 205, 0x200f); // init value
  2190. }
  2191. // ---- TMCC ----
  2192. for (i = 0; i < 3; i++)
  2193. tmcc_pow +=
  2194. (((state->fe[0]->dtv_property_cache.layer[i].modulation == DQPSK) * 4 + 1) * state->fe[0]->dtv_property_cache.layer[i].segment_count);
  2195. // Quantif of "P_tmcc_dec_thres_?k" is (0, 5+mode, 9);
  2196. // Threshold is set at 1/4 of max power.
  2197. tmcc_pow *= (1 << (9 - 2));
  2198. dib8000_write_word(state, 290, tmcc_pow); // P_tmcc_dec_thres_2k
  2199. dib8000_write_word(state, 291, tmcc_pow); // P_tmcc_dec_thres_4k
  2200. dib8000_write_word(state, 292, tmcc_pow); // P_tmcc_dec_thres_8k
  2201. //dib8000_write_word(state, 287, (1 << 13) | 0x1000 );
  2202. // ---- PHA3 ----
  2203. if (state->isdbt_cfg_loaded == 0)
  2204. dib8000_write_word(state, 250, 3285); /*p_2d_hspeed_thr0 */
  2205. if (state->fe[0]->dtv_property_cache.isdbt_sb_mode == 1)
  2206. state->isdbt_cfg_loaded = 0;
  2207. else
  2208. state->isdbt_cfg_loaded = 1;
  2209. }
  2210. static int dib8000_autosearch_start(struct dvb_frontend *fe)
  2211. {
  2212. u8 factor;
  2213. u32 value;
  2214. struct dib8000_state *state = fe->demodulator_priv;
  2215. int slist = 0;
  2216. state->fe[0]->dtv_property_cache.inversion = 0;
  2217. if (!state->fe[0]->dtv_property_cache.isdbt_sb_mode)
  2218. state->fe[0]->dtv_property_cache.layer[0].segment_count = 13;
  2219. state->fe[0]->dtv_property_cache.layer[0].modulation = QAM_64;
  2220. state->fe[0]->dtv_property_cache.layer[0].fec = FEC_2_3;
  2221. state->fe[0]->dtv_property_cache.layer[0].interleaving = 0;
  2222. //choose the right list, in sb, always do everything
  2223. if (state->fe[0]->dtv_property_cache.isdbt_sb_mode) {
  2224. state->fe[0]->dtv_property_cache.transmission_mode = TRANSMISSION_MODE_8K;
  2225. state->fe[0]->dtv_property_cache.guard_interval = GUARD_INTERVAL_1_8;
  2226. slist = 7;
  2227. dib8000_write_word(state, 0, (dib8000_read_word(state, 0) & 0x9fff) | (1 << 13));
  2228. } else {
  2229. if (state->fe[0]->dtv_property_cache.guard_interval == GUARD_INTERVAL_AUTO) {
  2230. if (state->fe[0]->dtv_property_cache.transmission_mode == TRANSMISSION_MODE_AUTO) {
  2231. slist = 7;
  2232. dib8000_write_word(state, 0, (dib8000_read_word(state, 0) & 0x9fff) | (1 << 13)); // P_mode = 1 to have autosearch start ok with mode2
  2233. } else
  2234. slist = 3;
  2235. } else {
  2236. if (state->fe[0]->dtv_property_cache.transmission_mode == TRANSMISSION_MODE_AUTO) {
  2237. slist = 2;
  2238. dib8000_write_word(state, 0, (dib8000_read_word(state, 0) & 0x9fff) | (1 << 13)); // P_mode = 1
  2239. } else
  2240. slist = 0;
  2241. }
  2242. if (state->fe[0]->dtv_property_cache.transmission_mode == TRANSMISSION_MODE_AUTO)
  2243. state->fe[0]->dtv_property_cache.transmission_mode = TRANSMISSION_MODE_8K;
  2244. if (state->fe[0]->dtv_property_cache.guard_interval == GUARD_INTERVAL_AUTO)
  2245. state->fe[0]->dtv_property_cache.guard_interval = GUARD_INTERVAL_1_8;
  2246. dprintk("using list for autosearch : %d", slist);
  2247. dib8000_set_channel(state, (unsigned char)slist, 1);
  2248. //dib8000_write_word(state, 0, (dib8000_read_word(state, 0) & 0x9fff) | (1 << 13)); // P_mode = 1
  2249. factor = 1;
  2250. //set lock_mask values
  2251. dib8000_write_word(state, 6, 0x4);
  2252. dib8000_write_word(state, 7, 0x8);
  2253. dib8000_write_word(state, 8, 0x1000);
  2254. //set lock_mask wait time values
  2255. value = 50 * state->cfg.pll->internal * factor;
  2256. dib8000_write_word(state, 11, (u16) ((value >> 16) & 0xffff)); // lock0 wait time
  2257. dib8000_write_word(state, 12, (u16) (value & 0xffff)); // lock0 wait time
  2258. value = 100 * state->cfg.pll->internal * factor;
  2259. dib8000_write_word(state, 13, (u16) ((value >> 16) & 0xffff)); // lock1 wait time
  2260. dib8000_write_word(state, 14, (u16) (value & 0xffff)); // lock1 wait time
  2261. value = 1000 * state->cfg.pll->internal * factor;
  2262. dib8000_write_word(state, 15, (u16) ((value >> 16) & 0xffff)); // lock2 wait time
  2263. dib8000_write_word(state, 16, (u16) (value & 0xffff)); // lock2 wait time
  2264. value = dib8000_read_word(state, 0);
  2265. dib8000_write_word(state, 0, (u16) ((1 << 15) | value));
  2266. dib8000_read_word(state, 1284); // reset the INT. n_irq_pending
  2267. dib8000_write_word(state, 0, (u16) value);
  2268. }
  2269. return 0;
  2270. }
  2271. static int dib8000_autosearch_irq(struct dvb_frontend *fe)
  2272. {
  2273. struct dib8000_state *state = fe->demodulator_priv;
  2274. u16 irq_pending = dib8000_read_word(state, 1284);
  2275. if (irq_pending & 0x1) { // failed
  2276. dprintk("dib8000_autosearch_irq failed");
  2277. return 1;
  2278. }
  2279. if (irq_pending & 0x2) { // succeeded
  2280. dprintk("dib8000_autosearch_irq succeeded");
  2281. return 2;
  2282. }
  2283. return 0; // still pending
  2284. }
  2285. static int dib8000_tune(struct dvb_frontend *fe)
  2286. {
  2287. struct dib8000_state *state = fe->demodulator_priv;
  2288. int ret = 0;
  2289. u16 lock, value, mode = fft_to_mode(state);
  2290. // we are already tuned - just resuming from suspend
  2291. if (state == NULL)
  2292. return -EINVAL;
  2293. dib8000_set_bandwidth(fe, state->fe[0]->dtv_property_cache.bandwidth_hz / 1000);
  2294. dib8000_set_channel(state, 0, 0);
  2295. // restart demod
  2296. ret |= dib8000_write_word(state, 770, 0x4000);
  2297. ret |= dib8000_write_word(state, 770, 0x0000);
  2298. msleep(45);
  2299. /* P_ctrl_inh_cor=0, P_ctrl_alpha_cor=4, P_ctrl_inh_isi=0, P_ctrl_alpha_isi=3 */
  2300. /* ret |= dib8000_write_word(state, 29, (0 << 9) | (4 << 5) | (0 << 4) | (3 << 0) ); workaround inh_isi stays at 1 */
  2301. // never achieved a lock before - wait for timfreq to update
  2302. if (state->timf == 0) {
  2303. if (state->fe[0]->dtv_property_cache.isdbt_sb_mode == 1) {
  2304. if (state->fe[0]->dtv_property_cache.isdbt_partial_reception == 0)
  2305. msleep(300);
  2306. else // Sound Broadcasting mode 3 seg
  2307. msleep(500);
  2308. } else // 13 seg
  2309. msleep(200);
  2310. }
  2311. if (state->fe[0]->dtv_property_cache.isdbt_sb_mode == 1) {
  2312. if (state->fe[0]->dtv_property_cache.isdbt_partial_reception == 0) {
  2313. /* P_timf_alpha = (13-P_mode) , P_corm_alpha=6, P_corm_thres=0x40 alpha to check on board */
  2314. dib8000_write_word(state, 32, ((13 - mode) << 12) | (6 << 8) | 0x40);
  2315. //dib8000_write_word(state, 32, (8 << 12) | (6 << 8) | 0x80);
  2316. /* P_ctrl_sfreq_step= (12-P_mode) P_ctrl_sfreq_inh =0 P_ctrl_pha_off_max */
  2317. ret |= dib8000_write_word(state, 37, (12 - mode) | ((5 + mode) << 5));
  2318. } else { // Sound Broadcasting mode 3 seg
  2319. /* P_timf_alpha = (12-P_mode) , P_corm_alpha=6, P_corm_thres=0x60 alpha to check on board */
  2320. dib8000_write_word(state, 32, ((12 - mode) << 12) | (6 << 8) | 0x60);
  2321. ret |= dib8000_write_word(state, 37, (11 - mode) | ((5 + mode) << 5));
  2322. }
  2323. } else { // 13 seg
  2324. /* P_timf_alpha = 8 , P_corm_alpha=6, P_corm_thres=0x80 alpha to check on board */
  2325. dib8000_write_word(state, 32, ((11 - mode) << 12) | (6 << 8) | 0x80);
  2326. ret |= dib8000_write_word(state, 37, (10 - mode) | ((5 + mode) << 5));
  2327. }
  2328. // we achieved a coff_cpil_lock - it's time to update the timf
  2329. if (state->revision != 0x8090)
  2330. lock = dib8000_read_word(state, 568);
  2331. else
  2332. lock = dib8000_read_word(state, 570);
  2333. if ((lock >> 11) & 0x1)
  2334. dib8000_update_timf(state);
  2335. //now that tune is finished, lock0 should lock on fec_mpeg to output this lock on MP_LOCK. It's changed in autosearch start
  2336. dib8000_write_word(state, 6, 0x200);
  2337. if (state->revision == 0x8002) {
  2338. value = dib8000_read_word(state, 903);
  2339. dib8000_write_word(state, 903, value & ~(1 << 3));
  2340. msleep(1);
  2341. dib8000_write_word(state, 903, value | (1 << 3));
  2342. }
  2343. return ret;
  2344. }
  2345. static int dib8000_wakeup(struct dvb_frontend *fe)
  2346. {
  2347. struct dib8000_state *state = fe->demodulator_priv;
  2348. u8 index_frontend;
  2349. int ret;
  2350. dib8000_set_power_mode(state, DIB8000_POWER_ALL);
  2351. dib8000_set_adc_state(state, DIBX000_ADC_ON);
  2352. if (dib8000_set_adc_state(state, DIBX000_SLOW_ADC_ON) != 0)
  2353. dprintk("could not start Slow ADC");
  2354. if (state->revision != 0x8090)
  2355. dib8000_sad_calib(state);
  2356. for (index_frontend = 1; (index_frontend < MAX_NUMBER_OF_FRONTENDS) && (state->fe[index_frontend] != NULL); index_frontend++) {
  2357. ret = state->fe[index_frontend]->ops.init(state->fe[index_frontend]);
  2358. if (ret < 0)
  2359. return ret;
  2360. }
  2361. return 0;
  2362. }
  2363. static int dib8000_sleep(struct dvb_frontend *fe)
  2364. {
  2365. struct dib8000_state *state = fe->demodulator_priv;
  2366. u8 index_frontend;
  2367. int ret;
  2368. for (index_frontend = 1; (index_frontend < MAX_NUMBER_OF_FRONTENDS) && (state->fe[index_frontend] != NULL); index_frontend++) {
  2369. ret = state->fe[index_frontend]->ops.sleep(state->fe[index_frontend]);
  2370. if (ret < 0)
  2371. return ret;
  2372. }
  2373. if (state->revision != 0x8090)
  2374. dib8000_set_output_mode(fe, OUTMODE_HIGH_Z);
  2375. dib8000_set_power_mode(state, DIB8000_POWER_INTERFACE_ONLY);
  2376. return dib8000_set_adc_state(state, DIBX000_SLOW_ADC_OFF) | dib8000_set_adc_state(state, DIBX000_ADC_OFF);
  2377. }
  2378. enum frontend_tune_state dib8000_get_tune_state(struct dvb_frontend *fe)
  2379. {
  2380. struct dib8000_state *state = fe->demodulator_priv;
  2381. return state->tune_state;
  2382. }
  2383. EXPORT_SYMBOL(dib8000_get_tune_state);
  2384. int dib8000_set_tune_state(struct dvb_frontend *fe, enum frontend_tune_state tune_state)
  2385. {
  2386. struct dib8000_state *state = fe->demodulator_priv;
  2387. state->tune_state = tune_state;
  2388. return 0;
  2389. }
  2390. EXPORT_SYMBOL(dib8000_set_tune_state);
  2391. static int dib8000_get_frontend(struct dvb_frontend *fe)
  2392. {
  2393. struct dib8000_state *state = fe->demodulator_priv;
  2394. u16 i, val = 0;
  2395. fe_status_t stat;
  2396. u8 index_frontend, sub_index_frontend;
  2397. fe->dtv_property_cache.bandwidth_hz = 6000000;
  2398. for (index_frontend = 1; (index_frontend < MAX_NUMBER_OF_FRONTENDS) && (state->fe[index_frontend] != NULL); index_frontend++) {
  2399. state->fe[index_frontend]->ops.read_status(state->fe[index_frontend], &stat);
  2400. if (stat&FE_HAS_SYNC) {
  2401. dprintk("TMCC lock on the slave%i", index_frontend);
  2402. /* synchronize the cache with the other frontends */
  2403. state->fe[index_frontend]->ops.get_frontend(state->fe[index_frontend]);
  2404. for (sub_index_frontend = 0; (sub_index_frontend < MAX_NUMBER_OF_FRONTENDS) && (state->fe[sub_index_frontend] != NULL); sub_index_frontend++) {
  2405. if (sub_index_frontend != index_frontend) {
  2406. state->fe[sub_index_frontend]->dtv_property_cache.isdbt_sb_mode = state->fe[index_frontend]->dtv_property_cache.isdbt_sb_mode;
  2407. state->fe[sub_index_frontend]->dtv_property_cache.inversion = state->fe[index_frontend]->dtv_property_cache.inversion;
  2408. state->fe[sub_index_frontend]->dtv_property_cache.transmission_mode = state->fe[index_frontend]->dtv_property_cache.transmission_mode;
  2409. state->fe[sub_index_frontend]->dtv_property_cache.guard_interval = state->fe[index_frontend]->dtv_property_cache.guard_interval;
  2410. state->fe[sub_index_frontend]->dtv_property_cache.isdbt_partial_reception = state->fe[index_frontend]->dtv_property_cache.isdbt_partial_reception;
  2411. for (i = 0; i < 3; i++) {
  2412. state->fe[sub_index_frontend]->dtv_property_cache.layer[i].segment_count = state->fe[index_frontend]->dtv_property_cache.layer[i].segment_count;
  2413. state->fe[sub_index_frontend]->dtv_property_cache.layer[i].interleaving = state->fe[index_frontend]->dtv_property_cache.layer[i].interleaving;
  2414. state->fe[sub_index_frontend]->dtv_property_cache.layer[i].fec = state->fe[index_frontend]->dtv_property_cache.layer[i].fec;
  2415. state->fe[sub_index_frontend]->dtv_property_cache.layer[i].modulation = state->fe[index_frontend]->dtv_property_cache.layer[i].modulation;
  2416. }
  2417. }
  2418. }
  2419. return 0;
  2420. }
  2421. }
  2422. fe->dtv_property_cache.isdbt_sb_mode = dib8000_read_word(state, 508) & 0x1;
  2423. if (state->revision == 0x8090)
  2424. val = dib8000_read_word(state, 572);
  2425. else
  2426. val = dib8000_read_word(state, 570);
  2427. fe->dtv_property_cache.inversion = (val & 0x40) >> 6;
  2428. switch ((val & 0x30) >> 4) {
  2429. case 1:
  2430. fe->dtv_property_cache.transmission_mode = TRANSMISSION_MODE_2K;
  2431. break;
  2432. case 3:
  2433. default:
  2434. fe->dtv_property_cache.transmission_mode = TRANSMISSION_MODE_8K;
  2435. break;
  2436. }
  2437. switch (val & 0x3) {
  2438. case 0:
  2439. fe->dtv_property_cache.guard_interval = GUARD_INTERVAL_1_32;
  2440. dprintk("dib8000_get_frontend GI = 1/32 ");
  2441. break;
  2442. case 1:
  2443. fe->dtv_property_cache.guard_interval = GUARD_INTERVAL_1_16;
  2444. dprintk("dib8000_get_frontend GI = 1/16 ");
  2445. break;
  2446. case 2:
  2447. dprintk("dib8000_get_frontend GI = 1/8 ");
  2448. fe->dtv_property_cache.guard_interval = GUARD_INTERVAL_1_8;
  2449. break;
  2450. case 3:
  2451. dprintk("dib8000_get_frontend GI = 1/4 ");
  2452. fe->dtv_property_cache.guard_interval = GUARD_INTERVAL_1_4;
  2453. break;
  2454. }
  2455. val = dib8000_read_word(state, 505);
  2456. fe->dtv_property_cache.isdbt_partial_reception = val & 1;
  2457. dprintk("dib8000_get_frontend : partial_reception = %d ", fe->dtv_property_cache.isdbt_partial_reception);
  2458. for (i = 0; i < 3; i++) {
  2459. val = dib8000_read_word(state, 493 + i);
  2460. fe->dtv_property_cache.layer[i].segment_count = val & 0x0F;
  2461. dprintk("dib8000_get_frontend : Layer %d segments = %d ", i, fe->dtv_property_cache.layer[i].segment_count);
  2462. val = dib8000_read_word(state, 499 + i);
  2463. fe->dtv_property_cache.layer[i].interleaving = val & 0x3;
  2464. dprintk("dib8000_get_frontend : Layer %d time_intlv = %d ", i, fe->dtv_property_cache.layer[i].interleaving);
  2465. val = dib8000_read_word(state, 481 + i);
  2466. switch (val & 0x7) {
  2467. case 1:
  2468. fe->dtv_property_cache.layer[i].fec = FEC_1_2;
  2469. dprintk("dib8000_get_frontend : Layer %d Code Rate = 1/2 ", i);
  2470. break;
  2471. case 2:
  2472. fe->dtv_property_cache.layer[i].fec = FEC_2_3;
  2473. dprintk("dib8000_get_frontend : Layer %d Code Rate = 2/3 ", i);
  2474. break;
  2475. case 3:
  2476. fe->dtv_property_cache.layer[i].fec = FEC_3_4;
  2477. dprintk("dib8000_get_frontend : Layer %d Code Rate = 3/4 ", i);
  2478. break;
  2479. case 5:
  2480. fe->dtv_property_cache.layer[i].fec = FEC_5_6;
  2481. dprintk("dib8000_get_frontend : Layer %d Code Rate = 5/6 ", i);
  2482. break;
  2483. default:
  2484. fe->dtv_property_cache.layer[i].fec = FEC_7_8;
  2485. dprintk("dib8000_get_frontend : Layer %d Code Rate = 7/8 ", i);
  2486. break;
  2487. }
  2488. val = dib8000_read_word(state, 487 + i);
  2489. switch (val & 0x3) {
  2490. case 0:
  2491. dprintk("dib8000_get_frontend : Layer %d DQPSK ", i);
  2492. fe->dtv_property_cache.layer[i].modulation = DQPSK;
  2493. break;
  2494. case 1:
  2495. fe->dtv_property_cache.layer[i].modulation = QPSK;
  2496. dprintk("dib8000_get_frontend : Layer %d QPSK ", i);
  2497. break;
  2498. case 2:
  2499. fe->dtv_property_cache.layer[i].modulation = QAM_16;
  2500. dprintk("dib8000_get_frontend : Layer %d QAM16 ", i);
  2501. break;
  2502. case 3:
  2503. default:
  2504. dprintk("dib8000_get_frontend : Layer %d QAM64 ", i);
  2505. fe->dtv_property_cache.layer[i].modulation = QAM_64;
  2506. break;
  2507. }
  2508. }
  2509. /* synchronize the cache with the other frontends */
  2510. for (index_frontend = 1; (index_frontend < MAX_NUMBER_OF_FRONTENDS) && (state->fe[index_frontend] != NULL); index_frontend++) {
  2511. state->fe[index_frontend]->dtv_property_cache.isdbt_sb_mode = fe->dtv_property_cache.isdbt_sb_mode;
  2512. state->fe[index_frontend]->dtv_property_cache.inversion = fe->dtv_property_cache.inversion;
  2513. state->fe[index_frontend]->dtv_property_cache.transmission_mode = fe->dtv_property_cache.transmission_mode;
  2514. state->fe[index_frontend]->dtv_property_cache.guard_interval = fe->dtv_property_cache.guard_interval;
  2515. state->fe[index_frontend]->dtv_property_cache.isdbt_partial_reception = fe->dtv_property_cache.isdbt_partial_reception;
  2516. for (i = 0; i < 3; i++) {
  2517. state->fe[index_frontend]->dtv_property_cache.layer[i].segment_count = fe->dtv_property_cache.layer[i].segment_count;
  2518. state->fe[index_frontend]->dtv_property_cache.layer[i].interleaving = fe->dtv_property_cache.layer[i].interleaving;
  2519. state->fe[index_frontend]->dtv_property_cache.layer[i].fec = fe->dtv_property_cache.layer[i].fec;
  2520. state->fe[index_frontend]->dtv_property_cache.layer[i].modulation = fe->dtv_property_cache.layer[i].modulation;
  2521. }
  2522. }
  2523. return 0;
  2524. }
  2525. static int dib8000_set_frontend(struct dvb_frontend *fe)
  2526. {
  2527. struct dib8000_state *state = fe->demodulator_priv;
  2528. u8 nbr_pending, exit_condition, index_frontend;
  2529. s8 index_frontend_success = -1;
  2530. int time, ret;
  2531. int time_slave = FE_CALLBACK_TIME_NEVER;
  2532. if (state->fe[0]->dtv_property_cache.frequency == 0) {
  2533. dprintk("dib8000: must at least specify frequency ");
  2534. return 0;
  2535. }
  2536. if (state->fe[0]->dtv_property_cache.bandwidth_hz == 0) {
  2537. dprintk("dib8000: no bandwidth specified, set to default ");
  2538. state->fe[0]->dtv_property_cache.bandwidth_hz = 6000000;
  2539. }
  2540. for (index_frontend = 0; (index_frontend < MAX_NUMBER_OF_FRONTENDS) && (state->fe[index_frontend] != NULL); index_frontend++) {
  2541. /* synchronization of the cache */
  2542. state->fe[index_frontend]->dtv_property_cache.delivery_system = SYS_ISDBT;
  2543. memcpy(&state->fe[index_frontend]->dtv_property_cache, &fe->dtv_property_cache, sizeof(struct dtv_frontend_properties));
  2544. if (state->revision != 0x8090)
  2545. dib8000_set_output_mode(state->fe[index_frontend],
  2546. OUTMODE_HIGH_Z);
  2547. else
  2548. dib8096p_set_output_mode(state->fe[index_frontend],
  2549. OUTMODE_HIGH_Z);
  2550. if (state->fe[index_frontend]->ops.tuner_ops.set_params)
  2551. state->fe[index_frontend]->ops.tuner_ops.set_params(state->fe[index_frontend]);
  2552. dib8000_set_tune_state(state->fe[index_frontend], CT_AGC_START);
  2553. }
  2554. /* start up the AGC */
  2555. do {
  2556. time = dib8000_agc_startup(state->fe[0]);
  2557. for (index_frontend = 1; (index_frontend < MAX_NUMBER_OF_FRONTENDS) && (state->fe[index_frontend] != NULL); index_frontend++) {
  2558. time_slave = dib8000_agc_startup(state->fe[index_frontend]);
  2559. if (time == FE_CALLBACK_TIME_NEVER)
  2560. time = time_slave;
  2561. else if ((time_slave != FE_CALLBACK_TIME_NEVER) && (time_slave > time))
  2562. time = time_slave;
  2563. }
  2564. if (time != FE_CALLBACK_TIME_NEVER)
  2565. msleep(time / 10);
  2566. else
  2567. break;
  2568. exit_condition = 1;
  2569. for (index_frontend = 0; (index_frontend < MAX_NUMBER_OF_FRONTENDS) && (state->fe[index_frontend] != NULL); index_frontend++) {
  2570. if (dib8000_get_tune_state(state->fe[index_frontend]) != CT_AGC_STOP) {
  2571. exit_condition = 0;
  2572. break;
  2573. }
  2574. }
  2575. } while (exit_condition == 0);
  2576. for (index_frontend = 0; (index_frontend < MAX_NUMBER_OF_FRONTENDS) && (state->fe[index_frontend] != NULL); index_frontend++)
  2577. dib8000_set_tune_state(state->fe[index_frontend], CT_DEMOD_START);
  2578. if ((state->fe[0]->dtv_property_cache.delivery_system != SYS_ISDBT) ||
  2579. (state->fe[0]->dtv_property_cache.inversion == INVERSION_AUTO) ||
  2580. (state->fe[0]->dtv_property_cache.transmission_mode == TRANSMISSION_MODE_AUTO) ||
  2581. (state->fe[0]->dtv_property_cache.guard_interval == GUARD_INTERVAL_AUTO) ||
  2582. (((state->fe[0]->dtv_property_cache.isdbt_layer_enabled & (1 << 0)) != 0) &&
  2583. (state->fe[0]->dtv_property_cache.layer[0].segment_count != 0xff) &&
  2584. (state->fe[0]->dtv_property_cache.layer[0].segment_count != 0) &&
  2585. ((state->fe[0]->dtv_property_cache.layer[0].modulation == QAM_AUTO) ||
  2586. (state->fe[0]->dtv_property_cache.layer[0].fec == FEC_AUTO))) ||
  2587. (((state->fe[0]->dtv_property_cache.isdbt_layer_enabled & (1 << 1)) != 0) &&
  2588. (state->fe[0]->dtv_property_cache.layer[1].segment_count != 0xff) &&
  2589. (state->fe[0]->dtv_property_cache.layer[1].segment_count != 0) &&
  2590. ((state->fe[0]->dtv_property_cache.layer[1].modulation == QAM_AUTO) ||
  2591. (state->fe[0]->dtv_property_cache.layer[1].fec == FEC_AUTO))) ||
  2592. (((state->fe[0]->dtv_property_cache.isdbt_layer_enabled & (1 << 2)) != 0) &&
  2593. (state->fe[0]->dtv_property_cache.layer[2].segment_count != 0xff) &&
  2594. (state->fe[0]->dtv_property_cache.layer[2].segment_count != 0) &&
  2595. ((state->fe[0]->dtv_property_cache.layer[2].modulation == QAM_AUTO) ||
  2596. (state->fe[0]->dtv_property_cache.layer[2].fec == FEC_AUTO))) ||
  2597. (((state->fe[0]->dtv_property_cache.layer[0].segment_count == 0) ||
  2598. ((state->fe[0]->dtv_property_cache.isdbt_layer_enabled & (1 << 0)) == 0)) &&
  2599. ((state->fe[0]->dtv_property_cache.layer[1].segment_count == 0) ||
  2600. ((state->fe[0]->dtv_property_cache.isdbt_layer_enabled & (2 << 0)) == 0)) &&
  2601. ((state->fe[0]->dtv_property_cache.layer[2].segment_count == 0) || ((state->fe[0]->dtv_property_cache.isdbt_layer_enabled & (3 << 0)) == 0)))) {
  2602. int i = 100;
  2603. u8 found = 0;
  2604. u8 tune_failed = 0;
  2605. for (index_frontend = 0; (index_frontend < MAX_NUMBER_OF_FRONTENDS) && (state->fe[index_frontend] != NULL); index_frontend++) {
  2606. dib8000_set_bandwidth(state->fe[index_frontend], fe->dtv_property_cache.bandwidth_hz / 1000);
  2607. dib8000_autosearch_start(state->fe[index_frontend]);
  2608. }
  2609. do {
  2610. msleep(20);
  2611. nbr_pending = 0;
  2612. exit_condition = 0; /* 0: tune pending; 1: tune failed; 2:tune success */
  2613. for (index_frontend = 0; (index_frontend < MAX_NUMBER_OF_FRONTENDS) && (state->fe[index_frontend] != NULL); index_frontend++) {
  2614. if (((tune_failed >> index_frontend) & 0x1) == 0) {
  2615. found = dib8000_autosearch_irq(state->fe[index_frontend]);
  2616. switch (found) {
  2617. case 0: /* tune pending */
  2618. nbr_pending++;
  2619. break;
  2620. case 2:
  2621. dprintk("autosearch succeed on the frontend%i", index_frontend);
  2622. exit_condition = 2;
  2623. index_frontend_success = index_frontend;
  2624. break;
  2625. default:
  2626. dprintk("unhandled autosearch result");
  2627. case 1:
  2628. tune_failed |= (1 << index_frontend);
  2629. dprintk("autosearch failed for the frontend%i", index_frontend);
  2630. break;
  2631. }
  2632. }
  2633. }
  2634. /* if all tune are done and no success, exit: tune failed */
  2635. if ((nbr_pending == 0) && (exit_condition == 0))
  2636. exit_condition = 1;
  2637. } while ((exit_condition == 0) && i--);
  2638. if (exit_condition == 1) { /* tune failed */
  2639. dprintk("tune failed");
  2640. return 0;
  2641. }
  2642. dprintk("tune success on frontend%i", index_frontend_success);
  2643. dib8000_get_frontend(fe);
  2644. }
  2645. for (index_frontend = 0, ret = 0; (ret >= 0) && (index_frontend < MAX_NUMBER_OF_FRONTENDS) && (state->fe[index_frontend] != NULL); index_frontend++)
  2646. ret = dib8000_tune(state->fe[index_frontend]);
  2647. /* set output mode and diversity input */
  2648. if (state->revision != 0x8090) {
  2649. dib8000_set_output_mode(state->fe[0], state->cfg.output_mode);
  2650. for (index_frontend = 1;
  2651. (index_frontend < MAX_NUMBER_OF_FRONTENDS) &&
  2652. (state->fe[index_frontend] != NULL);
  2653. index_frontend++) {
  2654. dib8000_set_output_mode(state->fe[index_frontend],
  2655. OUTMODE_DIVERSITY);
  2656. dib8000_set_diversity_in(state->fe[index_frontend-1], 1);
  2657. }
  2658. /* turn off the diversity of the last chip */
  2659. dib8000_set_diversity_in(state->fe[index_frontend-1], 0);
  2660. } else {
  2661. dib8096p_set_output_mode(state->fe[0], state->cfg.output_mode);
  2662. if (state->cfg.enMpegOutput == 0) {
  2663. dib8096p_setDibTxMux(state, MPEG_ON_DIBTX);
  2664. dib8096p_setHostBusMux(state, DIBTX_ON_HOSTBUS);
  2665. }
  2666. for (index_frontend = 1;
  2667. (index_frontend < MAX_NUMBER_OF_FRONTENDS) &&
  2668. (state->fe[index_frontend] != NULL);
  2669. index_frontend++) {
  2670. dib8096p_set_output_mode(state->fe[index_frontend],
  2671. OUTMODE_DIVERSITY);
  2672. dib8096p_set_diversity_in(state->fe[index_frontend-1], 1);
  2673. }
  2674. /* turn off the diversity of the last chip */
  2675. dib8096p_set_diversity_in(state->fe[index_frontend-1], 0);
  2676. }
  2677. return ret;
  2678. }
  2679. static u16 dib8000_read_lock(struct dvb_frontend *fe)
  2680. {
  2681. struct dib8000_state *state = fe->demodulator_priv;
  2682. if (state->revision == 0x8090)
  2683. return dib8000_read_word(state, 570);
  2684. return dib8000_read_word(state, 568);
  2685. }
  2686. static int dib8000_read_status(struct dvb_frontend *fe, fe_status_t * stat)
  2687. {
  2688. struct dib8000_state *state = fe->demodulator_priv;
  2689. u16 lock_slave = 0, lock;
  2690. u8 index_frontend;
  2691. if (state->revision == 0x8090)
  2692. lock = dib8000_read_word(state, 570);
  2693. else
  2694. lock = dib8000_read_word(state, 568);
  2695. for (index_frontend = 1; (index_frontend < MAX_NUMBER_OF_FRONTENDS) && (state->fe[index_frontend] != NULL); index_frontend++)
  2696. lock_slave |= dib8000_read_lock(state->fe[index_frontend]);
  2697. *stat = 0;
  2698. if (((lock >> 13) & 1) || ((lock_slave >> 13) & 1))
  2699. *stat |= FE_HAS_SIGNAL;
  2700. if (((lock >> 8) & 1) || ((lock_slave >> 8) & 1)) /* Equal */
  2701. *stat |= FE_HAS_CARRIER;
  2702. if ((((lock >> 1) & 0xf) == 0xf) || (((lock_slave >> 1) & 0xf) == 0xf)) /* TMCC_SYNC */
  2703. *stat |= FE_HAS_SYNC;
  2704. if ((((lock >> 12) & 1) || ((lock_slave >> 12) & 1)) && ((lock >> 5) & 7)) /* FEC MPEG */
  2705. *stat |= FE_HAS_LOCK;
  2706. if (((lock >> 12) & 1) || ((lock_slave >> 12) & 1)) {
  2707. lock = dib8000_read_word(state, 554); /* Viterbi Layer A */
  2708. if (lock & 0x01)
  2709. *stat |= FE_HAS_VITERBI;
  2710. lock = dib8000_read_word(state, 555); /* Viterbi Layer B */
  2711. if (lock & 0x01)
  2712. *stat |= FE_HAS_VITERBI;
  2713. lock = dib8000_read_word(state, 556); /* Viterbi Layer C */
  2714. if (lock & 0x01)
  2715. *stat |= FE_HAS_VITERBI;
  2716. }
  2717. return 0;
  2718. }
  2719. static int dib8000_read_ber(struct dvb_frontend *fe, u32 * ber)
  2720. {
  2721. struct dib8000_state *state = fe->demodulator_priv;
  2722. /* 13 segments */
  2723. if (state->revision == 0x8090)
  2724. *ber = (dib8000_read_word(state, 562) << 16) |
  2725. dib8000_read_word(state, 563);
  2726. else
  2727. *ber = (dib8000_read_word(state, 560) << 16) |
  2728. dib8000_read_word(state, 561);
  2729. return 0;
  2730. }
  2731. static int dib8000_read_unc_blocks(struct dvb_frontend *fe, u32 * unc)
  2732. {
  2733. struct dib8000_state *state = fe->demodulator_priv;
  2734. /* packet error on 13 seg */
  2735. if (state->revision == 0x8090)
  2736. *unc = dib8000_read_word(state, 567);
  2737. else
  2738. *unc = dib8000_read_word(state, 565);
  2739. return 0;
  2740. }
  2741. static int dib8000_read_signal_strength(struct dvb_frontend *fe, u16 * strength)
  2742. {
  2743. struct dib8000_state *state = fe->demodulator_priv;
  2744. u8 index_frontend;
  2745. u16 val;
  2746. *strength = 0;
  2747. for (index_frontend = 1; (index_frontend < MAX_NUMBER_OF_FRONTENDS) && (state->fe[index_frontend] != NULL); index_frontend++) {
  2748. state->fe[index_frontend]->ops.read_signal_strength(state->fe[index_frontend], &val);
  2749. if (val > 65535 - *strength)
  2750. *strength = 65535;
  2751. else
  2752. *strength += val;
  2753. }
  2754. val = 65535 - dib8000_read_word(state, 390);
  2755. if (val > 65535 - *strength)
  2756. *strength = 65535;
  2757. else
  2758. *strength += val;
  2759. return 0;
  2760. }
  2761. static u32 dib8000_get_snr(struct dvb_frontend *fe)
  2762. {
  2763. struct dib8000_state *state = fe->demodulator_priv;
  2764. u32 n, s, exp;
  2765. u16 val;
  2766. if (state->revision != 0x8090)
  2767. val = dib8000_read_word(state, 542);
  2768. else
  2769. val = dib8000_read_word(state, 544);
  2770. n = (val >> 6) & 0xff;
  2771. exp = (val & 0x3f);
  2772. if ((exp & 0x20) != 0)
  2773. exp -= 0x40;
  2774. n <<= exp+16;
  2775. if (state->revision != 0x8090)
  2776. val = dib8000_read_word(state, 543);
  2777. else
  2778. val = dib8000_read_word(state, 545);
  2779. s = (val >> 6) & 0xff;
  2780. exp = (val & 0x3f);
  2781. if ((exp & 0x20) != 0)
  2782. exp -= 0x40;
  2783. s <<= exp+16;
  2784. if (n > 0) {
  2785. u32 t = (s/n) << 16;
  2786. return t + ((s << 16) - n*t) / n;
  2787. }
  2788. return 0xffffffff;
  2789. }
  2790. static int dib8000_read_snr(struct dvb_frontend *fe, u16 * snr)
  2791. {
  2792. struct dib8000_state *state = fe->demodulator_priv;
  2793. u8 index_frontend;
  2794. u32 snr_master;
  2795. snr_master = dib8000_get_snr(fe);
  2796. for (index_frontend = 1; (index_frontend < MAX_NUMBER_OF_FRONTENDS) && (state->fe[index_frontend] != NULL); index_frontend++)
  2797. snr_master += dib8000_get_snr(state->fe[index_frontend]);
  2798. if ((snr_master >> 16) != 0) {
  2799. snr_master = 10*intlog10(snr_master>>16);
  2800. *snr = snr_master / ((1 << 24) / 10);
  2801. }
  2802. else
  2803. *snr = 0;
  2804. return 0;
  2805. }
  2806. int dib8000_set_slave_frontend(struct dvb_frontend *fe, struct dvb_frontend *fe_slave)
  2807. {
  2808. struct dib8000_state *state = fe->demodulator_priv;
  2809. u8 index_frontend = 1;
  2810. while ((index_frontend < MAX_NUMBER_OF_FRONTENDS) && (state->fe[index_frontend] != NULL))
  2811. index_frontend++;
  2812. if (index_frontend < MAX_NUMBER_OF_FRONTENDS) {
  2813. dprintk("set slave fe %p to index %i", fe_slave, index_frontend);
  2814. state->fe[index_frontend] = fe_slave;
  2815. return 0;
  2816. }
  2817. dprintk("too many slave frontend");
  2818. return -ENOMEM;
  2819. }
  2820. EXPORT_SYMBOL(dib8000_set_slave_frontend);
  2821. int dib8000_remove_slave_frontend(struct dvb_frontend *fe)
  2822. {
  2823. struct dib8000_state *state = fe->demodulator_priv;
  2824. u8 index_frontend = 1;
  2825. while ((index_frontend < MAX_NUMBER_OF_FRONTENDS) && (state->fe[index_frontend] != NULL))
  2826. index_frontend++;
  2827. if (index_frontend != 1) {
  2828. dprintk("remove slave fe %p (index %i)", state->fe[index_frontend-1], index_frontend-1);
  2829. state->fe[index_frontend] = NULL;
  2830. return 0;
  2831. }
  2832. dprintk("no frontend to be removed");
  2833. return -ENODEV;
  2834. }
  2835. EXPORT_SYMBOL(dib8000_remove_slave_frontend);
  2836. struct dvb_frontend *dib8000_get_slave_frontend(struct dvb_frontend *fe, int slave_index)
  2837. {
  2838. struct dib8000_state *state = fe->demodulator_priv;
  2839. if (slave_index >= MAX_NUMBER_OF_FRONTENDS)
  2840. return NULL;
  2841. return state->fe[slave_index];
  2842. }
  2843. EXPORT_SYMBOL(dib8000_get_slave_frontend);
  2844. int dib8000_i2c_enumeration(struct i2c_adapter *host, int no_of_demods,
  2845. u8 default_addr, u8 first_addr, u8 is_dib8096p)
  2846. {
  2847. int k = 0, ret = 0;
  2848. u8 new_addr = 0;
  2849. struct i2c_device client = {.adap = host };
  2850. client.i2c_write_buffer = kzalloc(4 * sizeof(u8), GFP_KERNEL);
  2851. if (!client.i2c_write_buffer) {
  2852. dprintk("%s: not enough memory", __func__);
  2853. return -ENOMEM;
  2854. }
  2855. client.i2c_read_buffer = kzalloc(4 * sizeof(u8), GFP_KERNEL);
  2856. if (!client.i2c_read_buffer) {
  2857. dprintk("%s: not enough memory", __func__);
  2858. ret = -ENOMEM;
  2859. goto error_memory_read;
  2860. }
  2861. client.i2c_buffer_lock = kzalloc(sizeof(struct mutex), GFP_KERNEL);
  2862. if (!client.i2c_buffer_lock) {
  2863. dprintk("%s: not enough memory", __func__);
  2864. ret = -ENOMEM;
  2865. goto error_memory_lock;
  2866. }
  2867. mutex_init(client.i2c_buffer_lock);
  2868. for (k = no_of_demods - 1; k >= 0; k--) {
  2869. /* designated i2c address */
  2870. new_addr = first_addr + (k << 1);
  2871. client.addr = new_addr;
  2872. if (!is_dib8096p)
  2873. dib8000_i2c_write16(&client, 1287, 0x0003); /* sram lead in, rdy */
  2874. if (dib8000_identify(&client) == 0) {
  2875. /* sram lead in, rdy */
  2876. if (!is_dib8096p)
  2877. dib8000_i2c_write16(&client, 1287, 0x0003);
  2878. client.addr = default_addr;
  2879. if (dib8000_identify(&client) == 0) {
  2880. dprintk("#%d: not identified", k);
  2881. ret = -EINVAL;
  2882. goto error;
  2883. }
  2884. }
  2885. /* start diversity to pull_down div_str - just for i2c-enumeration */
  2886. dib8000_i2c_write16(&client, 1286, (1 << 10) | (4 << 6));
  2887. /* set new i2c address and force divstart */
  2888. dib8000_i2c_write16(&client, 1285, (new_addr << 2) | 0x2);
  2889. client.addr = new_addr;
  2890. dib8000_identify(&client);
  2891. dprintk("IC %d initialized (to i2c_address 0x%x)", k, new_addr);
  2892. }
  2893. for (k = 0; k < no_of_demods; k++) {
  2894. new_addr = first_addr | (k << 1);
  2895. client.addr = new_addr;
  2896. // unforce divstr
  2897. dib8000_i2c_write16(&client, 1285, new_addr << 2);
  2898. /* deactivate div - it was just for i2c-enumeration */
  2899. dib8000_i2c_write16(&client, 1286, 0);
  2900. }
  2901. error:
  2902. kfree(client.i2c_buffer_lock);
  2903. error_memory_lock:
  2904. kfree(client.i2c_read_buffer);
  2905. error_memory_read:
  2906. kfree(client.i2c_write_buffer);
  2907. return ret;
  2908. }
  2909. EXPORT_SYMBOL(dib8000_i2c_enumeration);
  2910. static int dib8000_fe_get_tune_settings(struct dvb_frontend *fe, struct dvb_frontend_tune_settings *tune)
  2911. {
  2912. tune->min_delay_ms = 1000;
  2913. tune->step_size = 0;
  2914. tune->max_drift = 0;
  2915. return 0;
  2916. }
  2917. static void dib8000_release(struct dvb_frontend *fe)
  2918. {
  2919. struct dib8000_state *st = fe->demodulator_priv;
  2920. u8 index_frontend;
  2921. for (index_frontend = 1; (index_frontend < MAX_NUMBER_OF_FRONTENDS) && (st->fe[index_frontend] != NULL); index_frontend++)
  2922. dvb_frontend_detach(st->fe[index_frontend]);
  2923. dibx000_exit_i2c_master(&st->i2c_master);
  2924. i2c_del_adapter(&st->dib8096p_tuner_adap);
  2925. kfree(st->fe[0]);
  2926. kfree(st);
  2927. }
  2928. struct i2c_adapter *dib8000_get_i2c_master(struct dvb_frontend *fe, enum dibx000_i2c_interface intf, int gating)
  2929. {
  2930. struct dib8000_state *st = fe->demodulator_priv;
  2931. return dibx000_get_i2c_adapter(&st->i2c_master, intf, gating);
  2932. }
  2933. EXPORT_SYMBOL(dib8000_get_i2c_master);
  2934. int dib8000_pid_filter_ctrl(struct dvb_frontend *fe, u8 onoff)
  2935. {
  2936. struct dib8000_state *st = fe->demodulator_priv;
  2937. u16 val = dib8000_read_word(st, 299) & 0xffef;
  2938. val |= (onoff & 0x1) << 4;
  2939. dprintk("pid filter enabled %d", onoff);
  2940. return dib8000_write_word(st, 299, val);
  2941. }
  2942. EXPORT_SYMBOL(dib8000_pid_filter_ctrl);
  2943. int dib8000_pid_filter(struct dvb_frontend *fe, u8 id, u16 pid, u8 onoff)
  2944. {
  2945. struct dib8000_state *st = fe->demodulator_priv;
  2946. dprintk("Index %x, PID %d, OnOff %d", id, pid, onoff);
  2947. return dib8000_write_word(st, 305 + id, onoff ? (1 << 13) | pid : 0);
  2948. }
  2949. EXPORT_SYMBOL(dib8000_pid_filter);
  2950. static const struct dvb_frontend_ops dib8000_ops = {
  2951. .delsys = { SYS_ISDBT },
  2952. .info = {
  2953. .name = "DiBcom 8000 ISDB-T",
  2954. .frequency_min = 44250000,
  2955. .frequency_max = 867250000,
  2956. .frequency_stepsize = 62500,
  2957. .caps = FE_CAN_INVERSION_AUTO |
  2958. FE_CAN_FEC_1_2 | FE_CAN_FEC_2_3 | FE_CAN_FEC_3_4 |
  2959. FE_CAN_FEC_5_6 | FE_CAN_FEC_7_8 | FE_CAN_FEC_AUTO |
  2960. FE_CAN_QPSK | FE_CAN_QAM_16 | FE_CAN_QAM_64 | FE_CAN_QAM_AUTO |
  2961. FE_CAN_TRANSMISSION_MODE_AUTO | FE_CAN_GUARD_INTERVAL_AUTO | FE_CAN_RECOVER | FE_CAN_HIERARCHY_AUTO,
  2962. },
  2963. .release = dib8000_release,
  2964. .init = dib8000_wakeup,
  2965. .sleep = dib8000_sleep,
  2966. .set_frontend = dib8000_set_frontend,
  2967. .get_tune_settings = dib8000_fe_get_tune_settings,
  2968. .get_frontend = dib8000_get_frontend,
  2969. .read_status = dib8000_read_status,
  2970. .read_ber = dib8000_read_ber,
  2971. .read_signal_strength = dib8000_read_signal_strength,
  2972. .read_snr = dib8000_read_snr,
  2973. .read_ucblocks = dib8000_read_unc_blocks,
  2974. };
  2975. struct dvb_frontend *dib8000_attach(struct i2c_adapter *i2c_adap, u8 i2c_addr, struct dib8000_config *cfg)
  2976. {
  2977. struct dvb_frontend *fe;
  2978. struct dib8000_state *state;
  2979. dprintk("dib8000_attach");
  2980. state = kzalloc(sizeof(struct dib8000_state), GFP_KERNEL);
  2981. if (state == NULL)
  2982. return NULL;
  2983. fe = kzalloc(sizeof(struct dvb_frontend), GFP_KERNEL);
  2984. if (fe == NULL)
  2985. goto error;
  2986. memcpy(&state->cfg, cfg, sizeof(struct dib8000_config));
  2987. state->i2c.adap = i2c_adap;
  2988. state->i2c.addr = i2c_addr;
  2989. state->i2c.i2c_write_buffer = state->i2c_write_buffer;
  2990. state->i2c.i2c_read_buffer = state->i2c_read_buffer;
  2991. mutex_init(&state->i2c_buffer_lock);
  2992. state->i2c.i2c_buffer_lock = &state->i2c_buffer_lock;
  2993. state->gpio_val = cfg->gpio_val;
  2994. state->gpio_dir = cfg->gpio_dir;
  2995. /* Ensure the output mode remains at the previous default if it's
  2996. * not specifically set by the caller.
  2997. */
  2998. if ((state->cfg.output_mode != OUTMODE_MPEG2_SERIAL) && (state->cfg.output_mode != OUTMODE_MPEG2_PAR_GATED_CLK))
  2999. state->cfg.output_mode = OUTMODE_MPEG2_FIFO;
  3000. state->fe[0] = fe;
  3001. fe->demodulator_priv = state;
  3002. memcpy(&state->fe[0]->ops, &dib8000_ops, sizeof(struct dvb_frontend_ops));
  3003. state->timf_default = cfg->pll->timf;
  3004. if (dib8000_identify(&state->i2c) == 0)
  3005. goto error;
  3006. dibx000_init_i2c_master(&state->i2c_master, DIB8000, state->i2c.adap, state->i2c.addr);
  3007. /* init 8096p tuner adapter */
  3008. strncpy(state->dib8096p_tuner_adap.name, "DiB8096P tuner interface",
  3009. sizeof(state->dib8096p_tuner_adap.name));
  3010. state->dib8096p_tuner_adap.algo = &dib8096p_tuner_xfer_algo;
  3011. state->dib8096p_tuner_adap.algo_data = NULL;
  3012. state->dib8096p_tuner_adap.dev.parent = state->i2c.adap->dev.parent;
  3013. i2c_set_adapdata(&state->dib8096p_tuner_adap, state);
  3014. i2c_add_adapter(&state->dib8096p_tuner_adap);
  3015. dib8000_reset(fe);
  3016. dib8000_write_word(state, 285, (dib8000_read_word(state, 285) & ~0x60) | (3 << 5)); /* ber_rs_len = 3 */
  3017. return fe;
  3018. error:
  3019. kfree(state);
  3020. return NULL;
  3021. }
  3022. EXPORT_SYMBOL(dib8000_attach);
  3023. MODULE_AUTHOR("Olivier Grenie <Olivier.Grenie@dibcom.fr, " "Patrick Boettcher <pboettcher@dibcom.fr>");
  3024. MODULE_DESCRIPTION("Driver for the DiBcom 8000 ISDB-T demodulator");
  3025. MODULE_LICENSE("GPL");