dib8000.c 107 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560
  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;
  2290. // we are already tuned - just resuming from suspend
  2291. if (state == NULL)
  2292. return -EINVAL;
  2293. mode = fft_to_mode(state);
  2294. dib8000_set_bandwidth(fe, state->fe[0]->dtv_property_cache.bandwidth_hz / 1000);
  2295. dib8000_set_channel(state, 0, 0);
  2296. // restart demod
  2297. ret |= dib8000_write_word(state, 770, 0x4000);
  2298. ret |= dib8000_write_word(state, 770, 0x0000);
  2299. msleep(45);
  2300. /* P_ctrl_inh_cor=0, P_ctrl_alpha_cor=4, P_ctrl_inh_isi=0, P_ctrl_alpha_isi=3 */
  2301. /* ret |= dib8000_write_word(state, 29, (0 << 9) | (4 << 5) | (0 << 4) | (3 << 0) ); workaround inh_isi stays at 1 */
  2302. // never achieved a lock before - wait for timfreq to update
  2303. if (state->timf == 0) {
  2304. if (state->fe[0]->dtv_property_cache.isdbt_sb_mode == 1) {
  2305. if (state->fe[0]->dtv_property_cache.isdbt_partial_reception == 0)
  2306. msleep(300);
  2307. else // Sound Broadcasting mode 3 seg
  2308. msleep(500);
  2309. } else // 13 seg
  2310. msleep(200);
  2311. }
  2312. if (state->fe[0]->dtv_property_cache.isdbt_sb_mode == 1) {
  2313. if (state->fe[0]->dtv_property_cache.isdbt_partial_reception == 0) {
  2314. /* P_timf_alpha = (13-P_mode) , P_corm_alpha=6, P_corm_thres=0x40 alpha to check on board */
  2315. dib8000_write_word(state, 32, ((13 - mode) << 12) | (6 << 8) | 0x40);
  2316. //dib8000_write_word(state, 32, (8 << 12) | (6 << 8) | 0x80);
  2317. /* P_ctrl_sfreq_step= (12-P_mode) P_ctrl_sfreq_inh =0 P_ctrl_pha_off_max */
  2318. ret |= dib8000_write_word(state, 37, (12 - mode) | ((5 + mode) << 5));
  2319. } else { // Sound Broadcasting mode 3 seg
  2320. /* P_timf_alpha = (12-P_mode) , P_corm_alpha=6, P_corm_thres=0x60 alpha to check on board */
  2321. dib8000_write_word(state, 32, ((12 - mode) << 12) | (6 << 8) | 0x60);
  2322. ret |= dib8000_write_word(state, 37, (11 - mode) | ((5 + mode) << 5));
  2323. }
  2324. } else { // 13 seg
  2325. /* P_timf_alpha = 8 , P_corm_alpha=6, P_corm_thres=0x80 alpha to check on board */
  2326. dib8000_write_word(state, 32, ((11 - mode) << 12) | (6 << 8) | 0x80);
  2327. ret |= dib8000_write_word(state, 37, (10 - mode) | ((5 + mode) << 5));
  2328. }
  2329. // we achieved a coff_cpil_lock - it's time to update the timf
  2330. if (state->revision != 0x8090)
  2331. lock = dib8000_read_word(state, 568);
  2332. else
  2333. lock = dib8000_read_word(state, 570);
  2334. if ((lock >> 11) & 0x1)
  2335. dib8000_update_timf(state);
  2336. //now that tune is finished, lock0 should lock on fec_mpeg to output this lock on MP_LOCK. It's changed in autosearch start
  2337. dib8000_write_word(state, 6, 0x200);
  2338. if (state->revision == 0x8002) {
  2339. value = dib8000_read_word(state, 903);
  2340. dib8000_write_word(state, 903, value & ~(1 << 3));
  2341. msleep(1);
  2342. dib8000_write_word(state, 903, value | (1 << 3));
  2343. }
  2344. return ret;
  2345. }
  2346. static int dib8000_wakeup(struct dvb_frontend *fe)
  2347. {
  2348. struct dib8000_state *state = fe->demodulator_priv;
  2349. u8 index_frontend;
  2350. int ret;
  2351. dib8000_set_power_mode(state, DIB8000_POWER_ALL);
  2352. dib8000_set_adc_state(state, DIBX000_ADC_ON);
  2353. if (dib8000_set_adc_state(state, DIBX000_SLOW_ADC_ON) != 0)
  2354. dprintk("could not start Slow ADC");
  2355. if (state->revision != 0x8090)
  2356. dib8000_sad_calib(state);
  2357. for (index_frontend = 1; (index_frontend < MAX_NUMBER_OF_FRONTENDS) && (state->fe[index_frontend] != NULL); index_frontend++) {
  2358. ret = state->fe[index_frontend]->ops.init(state->fe[index_frontend]);
  2359. if (ret < 0)
  2360. return ret;
  2361. }
  2362. return 0;
  2363. }
  2364. static int dib8000_sleep(struct dvb_frontend *fe)
  2365. {
  2366. struct dib8000_state *state = fe->demodulator_priv;
  2367. u8 index_frontend;
  2368. int ret;
  2369. for (index_frontend = 1; (index_frontend < MAX_NUMBER_OF_FRONTENDS) && (state->fe[index_frontend] != NULL); index_frontend++) {
  2370. ret = state->fe[index_frontend]->ops.sleep(state->fe[index_frontend]);
  2371. if (ret < 0)
  2372. return ret;
  2373. }
  2374. if (state->revision != 0x8090)
  2375. dib8000_set_output_mode(fe, OUTMODE_HIGH_Z);
  2376. dib8000_set_power_mode(state, DIB8000_POWER_INTERFACE_ONLY);
  2377. return dib8000_set_adc_state(state, DIBX000_SLOW_ADC_OFF) | dib8000_set_adc_state(state, DIBX000_ADC_OFF);
  2378. }
  2379. enum frontend_tune_state dib8000_get_tune_state(struct dvb_frontend *fe)
  2380. {
  2381. struct dib8000_state *state = fe->demodulator_priv;
  2382. return state->tune_state;
  2383. }
  2384. EXPORT_SYMBOL(dib8000_get_tune_state);
  2385. int dib8000_set_tune_state(struct dvb_frontend *fe, enum frontend_tune_state tune_state)
  2386. {
  2387. struct dib8000_state *state = fe->demodulator_priv;
  2388. state->tune_state = tune_state;
  2389. return 0;
  2390. }
  2391. EXPORT_SYMBOL(dib8000_set_tune_state);
  2392. static int dib8000_get_frontend(struct dvb_frontend *fe)
  2393. {
  2394. struct dib8000_state *state = fe->demodulator_priv;
  2395. u16 i, val = 0;
  2396. fe_status_t stat;
  2397. u8 index_frontend, sub_index_frontend;
  2398. fe->dtv_property_cache.bandwidth_hz = 6000000;
  2399. for (index_frontend = 1; (index_frontend < MAX_NUMBER_OF_FRONTENDS) && (state->fe[index_frontend] != NULL); index_frontend++) {
  2400. state->fe[index_frontend]->ops.read_status(state->fe[index_frontend], &stat);
  2401. if (stat&FE_HAS_SYNC) {
  2402. dprintk("TMCC lock on the slave%i", index_frontend);
  2403. /* synchronize the cache with the other frontends */
  2404. state->fe[index_frontend]->ops.get_frontend(state->fe[index_frontend]);
  2405. for (sub_index_frontend = 0; (sub_index_frontend < MAX_NUMBER_OF_FRONTENDS) && (state->fe[sub_index_frontend] != NULL); sub_index_frontend++) {
  2406. if (sub_index_frontend != index_frontend) {
  2407. state->fe[sub_index_frontend]->dtv_property_cache.isdbt_sb_mode = state->fe[index_frontend]->dtv_property_cache.isdbt_sb_mode;
  2408. state->fe[sub_index_frontend]->dtv_property_cache.inversion = state->fe[index_frontend]->dtv_property_cache.inversion;
  2409. state->fe[sub_index_frontend]->dtv_property_cache.transmission_mode = state->fe[index_frontend]->dtv_property_cache.transmission_mode;
  2410. state->fe[sub_index_frontend]->dtv_property_cache.guard_interval = state->fe[index_frontend]->dtv_property_cache.guard_interval;
  2411. state->fe[sub_index_frontend]->dtv_property_cache.isdbt_partial_reception = state->fe[index_frontend]->dtv_property_cache.isdbt_partial_reception;
  2412. for (i = 0; i < 3; i++) {
  2413. state->fe[sub_index_frontend]->dtv_property_cache.layer[i].segment_count = state->fe[index_frontend]->dtv_property_cache.layer[i].segment_count;
  2414. state->fe[sub_index_frontend]->dtv_property_cache.layer[i].interleaving = state->fe[index_frontend]->dtv_property_cache.layer[i].interleaving;
  2415. state->fe[sub_index_frontend]->dtv_property_cache.layer[i].fec = state->fe[index_frontend]->dtv_property_cache.layer[i].fec;
  2416. state->fe[sub_index_frontend]->dtv_property_cache.layer[i].modulation = state->fe[index_frontend]->dtv_property_cache.layer[i].modulation;
  2417. }
  2418. }
  2419. }
  2420. return 0;
  2421. }
  2422. }
  2423. fe->dtv_property_cache.isdbt_sb_mode = dib8000_read_word(state, 508) & 0x1;
  2424. if (state->revision == 0x8090)
  2425. val = dib8000_read_word(state, 572);
  2426. else
  2427. val = dib8000_read_word(state, 570);
  2428. fe->dtv_property_cache.inversion = (val & 0x40) >> 6;
  2429. switch ((val & 0x30) >> 4) {
  2430. case 1:
  2431. fe->dtv_property_cache.transmission_mode = TRANSMISSION_MODE_2K;
  2432. break;
  2433. case 3:
  2434. default:
  2435. fe->dtv_property_cache.transmission_mode = TRANSMISSION_MODE_8K;
  2436. break;
  2437. }
  2438. switch (val & 0x3) {
  2439. case 0:
  2440. fe->dtv_property_cache.guard_interval = GUARD_INTERVAL_1_32;
  2441. dprintk("dib8000_get_frontend GI = 1/32 ");
  2442. break;
  2443. case 1:
  2444. fe->dtv_property_cache.guard_interval = GUARD_INTERVAL_1_16;
  2445. dprintk("dib8000_get_frontend GI = 1/16 ");
  2446. break;
  2447. case 2:
  2448. dprintk("dib8000_get_frontend GI = 1/8 ");
  2449. fe->dtv_property_cache.guard_interval = GUARD_INTERVAL_1_8;
  2450. break;
  2451. case 3:
  2452. dprintk("dib8000_get_frontend GI = 1/4 ");
  2453. fe->dtv_property_cache.guard_interval = GUARD_INTERVAL_1_4;
  2454. break;
  2455. }
  2456. val = dib8000_read_word(state, 505);
  2457. fe->dtv_property_cache.isdbt_partial_reception = val & 1;
  2458. dprintk("dib8000_get_frontend : partial_reception = %d ", fe->dtv_property_cache.isdbt_partial_reception);
  2459. for (i = 0; i < 3; i++) {
  2460. val = dib8000_read_word(state, 493 + i);
  2461. fe->dtv_property_cache.layer[i].segment_count = val & 0x0F;
  2462. dprintk("dib8000_get_frontend : Layer %d segments = %d ", i, fe->dtv_property_cache.layer[i].segment_count);
  2463. val = dib8000_read_word(state, 499 + i);
  2464. fe->dtv_property_cache.layer[i].interleaving = val & 0x3;
  2465. dprintk("dib8000_get_frontend : Layer %d time_intlv = %d ", i, fe->dtv_property_cache.layer[i].interleaving);
  2466. val = dib8000_read_word(state, 481 + i);
  2467. switch (val & 0x7) {
  2468. case 1:
  2469. fe->dtv_property_cache.layer[i].fec = FEC_1_2;
  2470. dprintk("dib8000_get_frontend : Layer %d Code Rate = 1/2 ", i);
  2471. break;
  2472. case 2:
  2473. fe->dtv_property_cache.layer[i].fec = FEC_2_3;
  2474. dprintk("dib8000_get_frontend : Layer %d Code Rate = 2/3 ", i);
  2475. break;
  2476. case 3:
  2477. fe->dtv_property_cache.layer[i].fec = FEC_3_4;
  2478. dprintk("dib8000_get_frontend : Layer %d Code Rate = 3/4 ", i);
  2479. break;
  2480. case 5:
  2481. fe->dtv_property_cache.layer[i].fec = FEC_5_6;
  2482. dprintk("dib8000_get_frontend : Layer %d Code Rate = 5/6 ", i);
  2483. break;
  2484. default:
  2485. fe->dtv_property_cache.layer[i].fec = FEC_7_8;
  2486. dprintk("dib8000_get_frontend : Layer %d Code Rate = 7/8 ", i);
  2487. break;
  2488. }
  2489. val = dib8000_read_word(state, 487 + i);
  2490. switch (val & 0x3) {
  2491. case 0:
  2492. dprintk("dib8000_get_frontend : Layer %d DQPSK ", i);
  2493. fe->dtv_property_cache.layer[i].modulation = DQPSK;
  2494. break;
  2495. case 1:
  2496. fe->dtv_property_cache.layer[i].modulation = QPSK;
  2497. dprintk("dib8000_get_frontend : Layer %d QPSK ", i);
  2498. break;
  2499. case 2:
  2500. fe->dtv_property_cache.layer[i].modulation = QAM_16;
  2501. dprintk("dib8000_get_frontend : Layer %d QAM16 ", i);
  2502. break;
  2503. case 3:
  2504. default:
  2505. dprintk("dib8000_get_frontend : Layer %d QAM64 ", i);
  2506. fe->dtv_property_cache.layer[i].modulation = QAM_64;
  2507. break;
  2508. }
  2509. }
  2510. /* synchronize the cache with the other frontends */
  2511. for (index_frontend = 1; (index_frontend < MAX_NUMBER_OF_FRONTENDS) && (state->fe[index_frontend] != NULL); index_frontend++) {
  2512. state->fe[index_frontend]->dtv_property_cache.isdbt_sb_mode = fe->dtv_property_cache.isdbt_sb_mode;
  2513. state->fe[index_frontend]->dtv_property_cache.inversion = fe->dtv_property_cache.inversion;
  2514. state->fe[index_frontend]->dtv_property_cache.transmission_mode = fe->dtv_property_cache.transmission_mode;
  2515. state->fe[index_frontend]->dtv_property_cache.guard_interval = fe->dtv_property_cache.guard_interval;
  2516. state->fe[index_frontend]->dtv_property_cache.isdbt_partial_reception = fe->dtv_property_cache.isdbt_partial_reception;
  2517. for (i = 0; i < 3; i++) {
  2518. state->fe[index_frontend]->dtv_property_cache.layer[i].segment_count = fe->dtv_property_cache.layer[i].segment_count;
  2519. state->fe[index_frontend]->dtv_property_cache.layer[i].interleaving = fe->dtv_property_cache.layer[i].interleaving;
  2520. state->fe[index_frontend]->dtv_property_cache.layer[i].fec = fe->dtv_property_cache.layer[i].fec;
  2521. state->fe[index_frontend]->dtv_property_cache.layer[i].modulation = fe->dtv_property_cache.layer[i].modulation;
  2522. }
  2523. }
  2524. return 0;
  2525. }
  2526. static int dib8000_set_frontend(struct dvb_frontend *fe)
  2527. {
  2528. struct dib8000_state *state = fe->demodulator_priv;
  2529. u8 nbr_pending, exit_condition, index_frontend;
  2530. s8 index_frontend_success = -1;
  2531. int time, ret;
  2532. int time_slave = FE_CALLBACK_TIME_NEVER;
  2533. if (state->fe[0]->dtv_property_cache.frequency == 0) {
  2534. dprintk("dib8000: must at least specify frequency ");
  2535. return 0;
  2536. }
  2537. if (state->fe[0]->dtv_property_cache.bandwidth_hz == 0) {
  2538. dprintk("dib8000: no bandwidth specified, set to default ");
  2539. state->fe[0]->dtv_property_cache.bandwidth_hz = 6000000;
  2540. }
  2541. for (index_frontend = 0; (index_frontend < MAX_NUMBER_OF_FRONTENDS) && (state->fe[index_frontend] != NULL); index_frontend++) {
  2542. /* synchronization of the cache */
  2543. state->fe[index_frontend]->dtv_property_cache.delivery_system = SYS_ISDBT;
  2544. memcpy(&state->fe[index_frontend]->dtv_property_cache, &fe->dtv_property_cache, sizeof(struct dtv_frontend_properties));
  2545. if (state->revision != 0x8090)
  2546. dib8000_set_output_mode(state->fe[index_frontend],
  2547. OUTMODE_HIGH_Z);
  2548. else
  2549. dib8096p_set_output_mode(state->fe[index_frontend],
  2550. OUTMODE_HIGH_Z);
  2551. if (state->fe[index_frontend]->ops.tuner_ops.set_params)
  2552. state->fe[index_frontend]->ops.tuner_ops.set_params(state->fe[index_frontend]);
  2553. dib8000_set_tune_state(state->fe[index_frontend], CT_AGC_START);
  2554. }
  2555. /* start up the AGC */
  2556. do {
  2557. time = dib8000_agc_startup(state->fe[0]);
  2558. for (index_frontend = 1; (index_frontend < MAX_NUMBER_OF_FRONTENDS) && (state->fe[index_frontend] != NULL); index_frontend++) {
  2559. time_slave = dib8000_agc_startup(state->fe[index_frontend]);
  2560. if (time == FE_CALLBACK_TIME_NEVER)
  2561. time = time_slave;
  2562. else if ((time_slave != FE_CALLBACK_TIME_NEVER) && (time_slave > time))
  2563. time = time_slave;
  2564. }
  2565. if (time != FE_CALLBACK_TIME_NEVER)
  2566. msleep(time / 10);
  2567. else
  2568. break;
  2569. exit_condition = 1;
  2570. for (index_frontend = 0; (index_frontend < MAX_NUMBER_OF_FRONTENDS) && (state->fe[index_frontend] != NULL); index_frontend++) {
  2571. if (dib8000_get_tune_state(state->fe[index_frontend]) != CT_AGC_STOP) {
  2572. exit_condition = 0;
  2573. break;
  2574. }
  2575. }
  2576. } while (exit_condition == 0);
  2577. for (index_frontend = 0; (index_frontend < MAX_NUMBER_OF_FRONTENDS) && (state->fe[index_frontend] != NULL); index_frontend++)
  2578. dib8000_set_tune_state(state->fe[index_frontend], CT_DEMOD_START);
  2579. if ((state->fe[0]->dtv_property_cache.delivery_system != SYS_ISDBT) ||
  2580. (state->fe[0]->dtv_property_cache.inversion == INVERSION_AUTO) ||
  2581. (state->fe[0]->dtv_property_cache.transmission_mode == TRANSMISSION_MODE_AUTO) ||
  2582. (state->fe[0]->dtv_property_cache.guard_interval == GUARD_INTERVAL_AUTO) ||
  2583. (((state->fe[0]->dtv_property_cache.isdbt_layer_enabled & (1 << 0)) != 0) &&
  2584. (state->fe[0]->dtv_property_cache.layer[0].segment_count != 0xff) &&
  2585. (state->fe[0]->dtv_property_cache.layer[0].segment_count != 0) &&
  2586. ((state->fe[0]->dtv_property_cache.layer[0].modulation == QAM_AUTO) ||
  2587. (state->fe[0]->dtv_property_cache.layer[0].fec == FEC_AUTO))) ||
  2588. (((state->fe[0]->dtv_property_cache.isdbt_layer_enabled & (1 << 1)) != 0) &&
  2589. (state->fe[0]->dtv_property_cache.layer[1].segment_count != 0xff) &&
  2590. (state->fe[0]->dtv_property_cache.layer[1].segment_count != 0) &&
  2591. ((state->fe[0]->dtv_property_cache.layer[1].modulation == QAM_AUTO) ||
  2592. (state->fe[0]->dtv_property_cache.layer[1].fec == FEC_AUTO))) ||
  2593. (((state->fe[0]->dtv_property_cache.isdbt_layer_enabled & (1 << 2)) != 0) &&
  2594. (state->fe[0]->dtv_property_cache.layer[2].segment_count != 0xff) &&
  2595. (state->fe[0]->dtv_property_cache.layer[2].segment_count != 0) &&
  2596. ((state->fe[0]->dtv_property_cache.layer[2].modulation == QAM_AUTO) ||
  2597. (state->fe[0]->dtv_property_cache.layer[2].fec == FEC_AUTO))) ||
  2598. (((state->fe[0]->dtv_property_cache.layer[0].segment_count == 0) ||
  2599. ((state->fe[0]->dtv_property_cache.isdbt_layer_enabled & (1 << 0)) == 0)) &&
  2600. ((state->fe[0]->dtv_property_cache.layer[1].segment_count == 0) ||
  2601. ((state->fe[0]->dtv_property_cache.isdbt_layer_enabled & (2 << 0)) == 0)) &&
  2602. ((state->fe[0]->dtv_property_cache.layer[2].segment_count == 0) || ((state->fe[0]->dtv_property_cache.isdbt_layer_enabled & (3 << 0)) == 0)))) {
  2603. int i = 100;
  2604. u8 found = 0;
  2605. u8 tune_failed = 0;
  2606. for (index_frontend = 0; (index_frontend < MAX_NUMBER_OF_FRONTENDS) && (state->fe[index_frontend] != NULL); index_frontend++) {
  2607. dib8000_set_bandwidth(state->fe[index_frontend], fe->dtv_property_cache.bandwidth_hz / 1000);
  2608. dib8000_autosearch_start(state->fe[index_frontend]);
  2609. }
  2610. do {
  2611. msleep(20);
  2612. nbr_pending = 0;
  2613. exit_condition = 0; /* 0: tune pending; 1: tune failed; 2:tune success */
  2614. for (index_frontend = 0; (index_frontend < MAX_NUMBER_OF_FRONTENDS) && (state->fe[index_frontend] != NULL); index_frontend++) {
  2615. if (((tune_failed >> index_frontend) & 0x1) == 0) {
  2616. found = dib8000_autosearch_irq(state->fe[index_frontend]);
  2617. switch (found) {
  2618. case 0: /* tune pending */
  2619. nbr_pending++;
  2620. break;
  2621. case 2:
  2622. dprintk("autosearch succeed on the frontend%i", index_frontend);
  2623. exit_condition = 2;
  2624. index_frontend_success = index_frontend;
  2625. break;
  2626. default:
  2627. dprintk("unhandled autosearch result");
  2628. case 1:
  2629. tune_failed |= (1 << index_frontend);
  2630. dprintk("autosearch failed for the frontend%i", index_frontend);
  2631. break;
  2632. }
  2633. }
  2634. }
  2635. /* if all tune are done and no success, exit: tune failed */
  2636. if ((nbr_pending == 0) && (exit_condition == 0))
  2637. exit_condition = 1;
  2638. } while ((exit_condition == 0) && i--);
  2639. if (exit_condition == 1) { /* tune failed */
  2640. dprintk("tune failed");
  2641. return 0;
  2642. }
  2643. dprintk("tune success on frontend%i", index_frontend_success);
  2644. dib8000_get_frontend(fe);
  2645. }
  2646. for (index_frontend = 0, ret = 0; (ret >= 0) && (index_frontend < MAX_NUMBER_OF_FRONTENDS) && (state->fe[index_frontend] != NULL); index_frontend++)
  2647. ret = dib8000_tune(state->fe[index_frontend]);
  2648. /* set output mode and diversity input */
  2649. if (state->revision != 0x8090) {
  2650. dib8000_set_output_mode(state->fe[0], state->cfg.output_mode);
  2651. for (index_frontend = 1;
  2652. (index_frontend < MAX_NUMBER_OF_FRONTENDS) &&
  2653. (state->fe[index_frontend] != NULL);
  2654. index_frontend++) {
  2655. dib8000_set_output_mode(state->fe[index_frontend],
  2656. OUTMODE_DIVERSITY);
  2657. dib8000_set_diversity_in(state->fe[index_frontend-1], 1);
  2658. }
  2659. /* turn off the diversity of the last chip */
  2660. dib8000_set_diversity_in(state->fe[index_frontend-1], 0);
  2661. } else {
  2662. dib8096p_set_output_mode(state->fe[0], state->cfg.output_mode);
  2663. if (state->cfg.enMpegOutput == 0) {
  2664. dib8096p_setDibTxMux(state, MPEG_ON_DIBTX);
  2665. dib8096p_setHostBusMux(state, DIBTX_ON_HOSTBUS);
  2666. }
  2667. for (index_frontend = 1;
  2668. (index_frontend < MAX_NUMBER_OF_FRONTENDS) &&
  2669. (state->fe[index_frontend] != NULL);
  2670. index_frontend++) {
  2671. dib8096p_set_output_mode(state->fe[index_frontend],
  2672. OUTMODE_DIVERSITY);
  2673. dib8096p_set_diversity_in(state->fe[index_frontend-1], 1);
  2674. }
  2675. /* turn off the diversity of the last chip */
  2676. dib8096p_set_diversity_in(state->fe[index_frontend-1], 0);
  2677. }
  2678. return ret;
  2679. }
  2680. static u16 dib8000_read_lock(struct dvb_frontend *fe)
  2681. {
  2682. struct dib8000_state *state = fe->demodulator_priv;
  2683. if (state->revision == 0x8090)
  2684. return dib8000_read_word(state, 570);
  2685. return dib8000_read_word(state, 568);
  2686. }
  2687. static int dib8000_read_status(struct dvb_frontend *fe, fe_status_t * stat)
  2688. {
  2689. struct dib8000_state *state = fe->demodulator_priv;
  2690. u16 lock_slave = 0, lock;
  2691. u8 index_frontend;
  2692. if (state->revision == 0x8090)
  2693. lock = dib8000_read_word(state, 570);
  2694. else
  2695. lock = dib8000_read_word(state, 568);
  2696. for (index_frontend = 1; (index_frontend < MAX_NUMBER_OF_FRONTENDS) && (state->fe[index_frontend] != NULL); index_frontend++)
  2697. lock_slave |= dib8000_read_lock(state->fe[index_frontend]);
  2698. *stat = 0;
  2699. if (((lock >> 13) & 1) || ((lock_slave >> 13) & 1))
  2700. *stat |= FE_HAS_SIGNAL;
  2701. if (((lock >> 8) & 1) || ((lock_slave >> 8) & 1)) /* Equal */
  2702. *stat |= FE_HAS_CARRIER;
  2703. if ((((lock >> 1) & 0xf) == 0xf) || (((lock_slave >> 1) & 0xf) == 0xf)) /* TMCC_SYNC */
  2704. *stat |= FE_HAS_SYNC;
  2705. if ((((lock >> 12) & 1) || ((lock_slave >> 12) & 1)) && ((lock >> 5) & 7)) /* FEC MPEG */
  2706. *stat |= FE_HAS_LOCK;
  2707. if (((lock >> 12) & 1) || ((lock_slave >> 12) & 1)) {
  2708. lock = dib8000_read_word(state, 554); /* Viterbi Layer A */
  2709. if (lock & 0x01)
  2710. *stat |= FE_HAS_VITERBI;
  2711. lock = dib8000_read_word(state, 555); /* Viterbi Layer B */
  2712. if (lock & 0x01)
  2713. *stat |= FE_HAS_VITERBI;
  2714. lock = dib8000_read_word(state, 556); /* Viterbi Layer C */
  2715. if (lock & 0x01)
  2716. *stat |= FE_HAS_VITERBI;
  2717. }
  2718. return 0;
  2719. }
  2720. static int dib8000_read_ber(struct dvb_frontend *fe, u32 * ber)
  2721. {
  2722. struct dib8000_state *state = fe->demodulator_priv;
  2723. /* 13 segments */
  2724. if (state->revision == 0x8090)
  2725. *ber = (dib8000_read_word(state, 562) << 16) |
  2726. dib8000_read_word(state, 563);
  2727. else
  2728. *ber = (dib8000_read_word(state, 560) << 16) |
  2729. dib8000_read_word(state, 561);
  2730. return 0;
  2731. }
  2732. static int dib8000_read_unc_blocks(struct dvb_frontend *fe, u32 * unc)
  2733. {
  2734. struct dib8000_state *state = fe->demodulator_priv;
  2735. /* packet error on 13 seg */
  2736. if (state->revision == 0x8090)
  2737. *unc = dib8000_read_word(state, 567);
  2738. else
  2739. *unc = dib8000_read_word(state, 565);
  2740. return 0;
  2741. }
  2742. static int dib8000_read_signal_strength(struct dvb_frontend *fe, u16 * strength)
  2743. {
  2744. struct dib8000_state *state = fe->demodulator_priv;
  2745. u8 index_frontend;
  2746. u16 val;
  2747. *strength = 0;
  2748. for (index_frontend = 1; (index_frontend < MAX_NUMBER_OF_FRONTENDS) && (state->fe[index_frontend] != NULL); index_frontend++) {
  2749. state->fe[index_frontend]->ops.read_signal_strength(state->fe[index_frontend], &val);
  2750. if (val > 65535 - *strength)
  2751. *strength = 65535;
  2752. else
  2753. *strength += val;
  2754. }
  2755. val = 65535 - dib8000_read_word(state, 390);
  2756. if (val > 65535 - *strength)
  2757. *strength = 65535;
  2758. else
  2759. *strength += val;
  2760. return 0;
  2761. }
  2762. static u32 dib8000_get_snr(struct dvb_frontend *fe)
  2763. {
  2764. struct dib8000_state *state = fe->demodulator_priv;
  2765. u32 n, s, exp;
  2766. u16 val;
  2767. if (state->revision != 0x8090)
  2768. val = dib8000_read_word(state, 542);
  2769. else
  2770. val = dib8000_read_word(state, 544);
  2771. n = (val >> 6) & 0xff;
  2772. exp = (val & 0x3f);
  2773. if ((exp & 0x20) != 0)
  2774. exp -= 0x40;
  2775. n <<= exp+16;
  2776. if (state->revision != 0x8090)
  2777. val = dib8000_read_word(state, 543);
  2778. else
  2779. val = dib8000_read_word(state, 545);
  2780. s = (val >> 6) & 0xff;
  2781. exp = (val & 0x3f);
  2782. if ((exp & 0x20) != 0)
  2783. exp -= 0x40;
  2784. s <<= exp+16;
  2785. if (n > 0) {
  2786. u32 t = (s/n) << 16;
  2787. return t + ((s << 16) - n*t) / n;
  2788. }
  2789. return 0xffffffff;
  2790. }
  2791. static int dib8000_read_snr(struct dvb_frontend *fe, u16 * snr)
  2792. {
  2793. struct dib8000_state *state = fe->demodulator_priv;
  2794. u8 index_frontend;
  2795. u32 snr_master;
  2796. snr_master = dib8000_get_snr(fe);
  2797. for (index_frontend = 1; (index_frontend < MAX_NUMBER_OF_FRONTENDS) && (state->fe[index_frontend] != NULL); index_frontend++)
  2798. snr_master += dib8000_get_snr(state->fe[index_frontend]);
  2799. if ((snr_master >> 16) != 0) {
  2800. snr_master = 10*intlog10(snr_master>>16);
  2801. *snr = snr_master / ((1 << 24) / 10);
  2802. }
  2803. else
  2804. *snr = 0;
  2805. return 0;
  2806. }
  2807. int dib8000_set_slave_frontend(struct dvb_frontend *fe, struct dvb_frontend *fe_slave)
  2808. {
  2809. struct dib8000_state *state = fe->demodulator_priv;
  2810. u8 index_frontend = 1;
  2811. while ((index_frontend < MAX_NUMBER_OF_FRONTENDS) && (state->fe[index_frontend] != NULL))
  2812. index_frontend++;
  2813. if (index_frontend < MAX_NUMBER_OF_FRONTENDS) {
  2814. dprintk("set slave fe %p to index %i", fe_slave, index_frontend);
  2815. state->fe[index_frontend] = fe_slave;
  2816. return 0;
  2817. }
  2818. dprintk("too many slave frontend");
  2819. return -ENOMEM;
  2820. }
  2821. EXPORT_SYMBOL(dib8000_set_slave_frontend);
  2822. int dib8000_remove_slave_frontend(struct dvb_frontend *fe)
  2823. {
  2824. struct dib8000_state *state = fe->demodulator_priv;
  2825. u8 index_frontend = 1;
  2826. while ((index_frontend < MAX_NUMBER_OF_FRONTENDS) && (state->fe[index_frontend] != NULL))
  2827. index_frontend++;
  2828. if (index_frontend != 1) {
  2829. dprintk("remove slave fe %p (index %i)", state->fe[index_frontend-1], index_frontend-1);
  2830. state->fe[index_frontend] = NULL;
  2831. return 0;
  2832. }
  2833. dprintk("no frontend to be removed");
  2834. return -ENODEV;
  2835. }
  2836. EXPORT_SYMBOL(dib8000_remove_slave_frontend);
  2837. struct dvb_frontend *dib8000_get_slave_frontend(struct dvb_frontend *fe, int slave_index)
  2838. {
  2839. struct dib8000_state *state = fe->demodulator_priv;
  2840. if (slave_index >= MAX_NUMBER_OF_FRONTENDS)
  2841. return NULL;
  2842. return state->fe[slave_index];
  2843. }
  2844. EXPORT_SYMBOL(dib8000_get_slave_frontend);
  2845. int dib8000_i2c_enumeration(struct i2c_adapter *host, int no_of_demods,
  2846. u8 default_addr, u8 first_addr, u8 is_dib8096p)
  2847. {
  2848. int k = 0, ret = 0;
  2849. u8 new_addr = 0;
  2850. struct i2c_device client = {.adap = host };
  2851. client.i2c_write_buffer = kzalloc(4 * sizeof(u8), GFP_KERNEL);
  2852. if (!client.i2c_write_buffer) {
  2853. dprintk("%s: not enough memory", __func__);
  2854. return -ENOMEM;
  2855. }
  2856. client.i2c_read_buffer = kzalloc(4 * sizeof(u8), GFP_KERNEL);
  2857. if (!client.i2c_read_buffer) {
  2858. dprintk("%s: not enough memory", __func__);
  2859. ret = -ENOMEM;
  2860. goto error_memory_read;
  2861. }
  2862. client.i2c_buffer_lock = kzalloc(sizeof(struct mutex), GFP_KERNEL);
  2863. if (!client.i2c_buffer_lock) {
  2864. dprintk("%s: not enough memory", __func__);
  2865. ret = -ENOMEM;
  2866. goto error_memory_lock;
  2867. }
  2868. mutex_init(client.i2c_buffer_lock);
  2869. for (k = no_of_demods - 1; k >= 0; k--) {
  2870. /* designated i2c address */
  2871. new_addr = first_addr + (k << 1);
  2872. client.addr = new_addr;
  2873. if (!is_dib8096p)
  2874. dib8000_i2c_write16(&client, 1287, 0x0003); /* sram lead in, rdy */
  2875. if (dib8000_identify(&client) == 0) {
  2876. /* sram lead in, rdy */
  2877. if (!is_dib8096p)
  2878. dib8000_i2c_write16(&client, 1287, 0x0003);
  2879. client.addr = default_addr;
  2880. if (dib8000_identify(&client) == 0) {
  2881. dprintk("#%d: not identified", k);
  2882. ret = -EINVAL;
  2883. goto error;
  2884. }
  2885. }
  2886. /* start diversity to pull_down div_str - just for i2c-enumeration */
  2887. dib8000_i2c_write16(&client, 1286, (1 << 10) | (4 << 6));
  2888. /* set new i2c address and force divstart */
  2889. dib8000_i2c_write16(&client, 1285, (new_addr << 2) | 0x2);
  2890. client.addr = new_addr;
  2891. dib8000_identify(&client);
  2892. dprintk("IC %d initialized (to i2c_address 0x%x)", k, new_addr);
  2893. }
  2894. for (k = 0; k < no_of_demods; k++) {
  2895. new_addr = first_addr | (k << 1);
  2896. client.addr = new_addr;
  2897. // unforce divstr
  2898. dib8000_i2c_write16(&client, 1285, new_addr << 2);
  2899. /* deactivate div - it was just for i2c-enumeration */
  2900. dib8000_i2c_write16(&client, 1286, 0);
  2901. }
  2902. error:
  2903. kfree(client.i2c_buffer_lock);
  2904. error_memory_lock:
  2905. kfree(client.i2c_read_buffer);
  2906. error_memory_read:
  2907. kfree(client.i2c_write_buffer);
  2908. return ret;
  2909. }
  2910. EXPORT_SYMBOL(dib8000_i2c_enumeration);
  2911. static int dib8000_fe_get_tune_settings(struct dvb_frontend *fe, struct dvb_frontend_tune_settings *tune)
  2912. {
  2913. tune->min_delay_ms = 1000;
  2914. tune->step_size = 0;
  2915. tune->max_drift = 0;
  2916. return 0;
  2917. }
  2918. static void dib8000_release(struct dvb_frontend *fe)
  2919. {
  2920. struct dib8000_state *st = fe->demodulator_priv;
  2921. u8 index_frontend;
  2922. for (index_frontend = 1; (index_frontend < MAX_NUMBER_OF_FRONTENDS) && (st->fe[index_frontend] != NULL); index_frontend++)
  2923. dvb_frontend_detach(st->fe[index_frontend]);
  2924. dibx000_exit_i2c_master(&st->i2c_master);
  2925. i2c_del_adapter(&st->dib8096p_tuner_adap);
  2926. kfree(st->fe[0]);
  2927. kfree(st);
  2928. }
  2929. struct i2c_adapter *dib8000_get_i2c_master(struct dvb_frontend *fe, enum dibx000_i2c_interface intf, int gating)
  2930. {
  2931. struct dib8000_state *st = fe->demodulator_priv;
  2932. return dibx000_get_i2c_adapter(&st->i2c_master, intf, gating);
  2933. }
  2934. EXPORT_SYMBOL(dib8000_get_i2c_master);
  2935. int dib8000_pid_filter_ctrl(struct dvb_frontend *fe, u8 onoff)
  2936. {
  2937. struct dib8000_state *st = fe->demodulator_priv;
  2938. u16 val = dib8000_read_word(st, 299) & 0xffef;
  2939. val |= (onoff & 0x1) << 4;
  2940. dprintk("pid filter enabled %d", onoff);
  2941. return dib8000_write_word(st, 299, val);
  2942. }
  2943. EXPORT_SYMBOL(dib8000_pid_filter_ctrl);
  2944. int dib8000_pid_filter(struct dvb_frontend *fe, u8 id, u16 pid, u8 onoff)
  2945. {
  2946. struct dib8000_state *st = fe->demodulator_priv;
  2947. dprintk("Index %x, PID %d, OnOff %d", id, pid, onoff);
  2948. return dib8000_write_word(st, 305 + id, onoff ? (1 << 13) | pid : 0);
  2949. }
  2950. EXPORT_SYMBOL(dib8000_pid_filter);
  2951. static const struct dvb_frontend_ops dib8000_ops = {
  2952. .delsys = { SYS_ISDBT },
  2953. .info = {
  2954. .name = "DiBcom 8000 ISDB-T",
  2955. .frequency_min = 44250000,
  2956. .frequency_max = 867250000,
  2957. .frequency_stepsize = 62500,
  2958. .caps = FE_CAN_INVERSION_AUTO |
  2959. FE_CAN_FEC_1_2 | FE_CAN_FEC_2_3 | FE_CAN_FEC_3_4 |
  2960. FE_CAN_FEC_5_6 | FE_CAN_FEC_7_8 | FE_CAN_FEC_AUTO |
  2961. FE_CAN_QPSK | FE_CAN_QAM_16 | FE_CAN_QAM_64 | FE_CAN_QAM_AUTO |
  2962. FE_CAN_TRANSMISSION_MODE_AUTO | FE_CAN_GUARD_INTERVAL_AUTO | FE_CAN_RECOVER | FE_CAN_HIERARCHY_AUTO,
  2963. },
  2964. .release = dib8000_release,
  2965. .init = dib8000_wakeup,
  2966. .sleep = dib8000_sleep,
  2967. .set_frontend = dib8000_set_frontend,
  2968. .get_tune_settings = dib8000_fe_get_tune_settings,
  2969. .get_frontend = dib8000_get_frontend,
  2970. .read_status = dib8000_read_status,
  2971. .read_ber = dib8000_read_ber,
  2972. .read_signal_strength = dib8000_read_signal_strength,
  2973. .read_snr = dib8000_read_snr,
  2974. .read_ucblocks = dib8000_read_unc_blocks,
  2975. };
  2976. struct dvb_frontend *dib8000_attach(struct i2c_adapter *i2c_adap, u8 i2c_addr, struct dib8000_config *cfg)
  2977. {
  2978. struct dvb_frontend *fe;
  2979. struct dib8000_state *state;
  2980. dprintk("dib8000_attach");
  2981. state = kzalloc(sizeof(struct dib8000_state), GFP_KERNEL);
  2982. if (state == NULL)
  2983. return NULL;
  2984. fe = kzalloc(sizeof(struct dvb_frontend), GFP_KERNEL);
  2985. if (fe == NULL)
  2986. goto error;
  2987. memcpy(&state->cfg, cfg, sizeof(struct dib8000_config));
  2988. state->i2c.adap = i2c_adap;
  2989. state->i2c.addr = i2c_addr;
  2990. state->i2c.i2c_write_buffer = state->i2c_write_buffer;
  2991. state->i2c.i2c_read_buffer = state->i2c_read_buffer;
  2992. mutex_init(&state->i2c_buffer_lock);
  2993. state->i2c.i2c_buffer_lock = &state->i2c_buffer_lock;
  2994. state->gpio_val = cfg->gpio_val;
  2995. state->gpio_dir = cfg->gpio_dir;
  2996. /* Ensure the output mode remains at the previous default if it's
  2997. * not specifically set by the caller.
  2998. */
  2999. if ((state->cfg.output_mode != OUTMODE_MPEG2_SERIAL) && (state->cfg.output_mode != OUTMODE_MPEG2_PAR_GATED_CLK))
  3000. state->cfg.output_mode = OUTMODE_MPEG2_FIFO;
  3001. state->fe[0] = fe;
  3002. fe->demodulator_priv = state;
  3003. memcpy(&state->fe[0]->ops, &dib8000_ops, sizeof(struct dvb_frontend_ops));
  3004. state->timf_default = cfg->pll->timf;
  3005. if (dib8000_identify(&state->i2c) == 0)
  3006. goto error;
  3007. dibx000_init_i2c_master(&state->i2c_master, DIB8000, state->i2c.adap, state->i2c.addr);
  3008. /* init 8096p tuner adapter */
  3009. strncpy(state->dib8096p_tuner_adap.name, "DiB8096P tuner interface",
  3010. sizeof(state->dib8096p_tuner_adap.name));
  3011. state->dib8096p_tuner_adap.algo = &dib8096p_tuner_xfer_algo;
  3012. state->dib8096p_tuner_adap.algo_data = NULL;
  3013. state->dib8096p_tuner_adap.dev.parent = state->i2c.adap->dev.parent;
  3014. i2c_set_adapdata(&state->dib8096p_tuner_adap, state);
  3015. i2c_add_adapter(&state->dib8096p_tuner_adap);
  3016. dib8000_reset(fe);
  3017. dib8000_write_word(state, 285, (dib8000_read_word(state, 285) & ~0x60) | (3 << 5)); /* ber_rs_len = 3 */
  3018. return fe;
  3019. error:
  3020. kfree(state);
  3021. return NULL;
  3022. }
  3023. EXPORT_SYMBOL(dib8000_attach);
  3024. MODULE_AUTHOR("Olivier Grenie <Olivier.Grenie@dibcom.fr, " "Patrick Boettcher <pboettcher@dibcom.fr>");
  3025. MODULE_DESCRIPTION("Driver for the DiBcom 8000 ISDB-T demodulator");
  3026. MODULE_LICENSE("GPL");