dib8000.c 125 KB

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