dib8000.c 133 KB

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