dib8000.c 85 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670
  1. /*
  2. * Linux-DVB Driver for DiBcom's DiB8000 chip (ISDB-T).
  3. *
  4. * Copyright (C) 2009 DiBcom (http://www.dibcom.fr/)
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License as
  8. * published by the Free Software Foundation, version 2.
  9. */
  10. #include <linux/kernel.h>
  11. #include <linux/slab.h>
  12. #include <linux/i2c.h>
  13. #include <linux/mutex.h>
  14. #include "dvb_math.h"
  15. #include "dvb_frontend.h"
  16. #include "dib8000.h"
  17. #define LAYER_ALL -1
  18. #define LAYER_A 1
  19. #define LAYER_B 2
  20. #define LAYER_C 3
  21. #define FE_CALLBACK_TIME_NEVER 0xffffffff
  22. #define MAX_NUMBER_OF_FRONTENDS 6
  23. static int debug;
  24. module_param(debug, int, 0644);
  25. MODULE_PARM_DESC(debug, "turn on debugging (default: 0)");
  26. #define dprintk(args...) do { if (debug) { printk(KERN_DEBUG "DiB8000: "); printk(args); printk("\n"); } } while (0)
  27. #define FE_STATUS_TUNE_FAILED 0
  28. struct i2c_device {
  29. struct i2c_adapter *adap;
  30. u8 addr;
  31. u8 *i2c_write_buffer;
  32. u8 *i2c_read_buffer;
  33. struct mutex *i2c_buffer_lock;
  34. };
  35. struct dib8000_state {
  36. struct dib8000_config cfg;
  37. struct i2c_device i2c;
  38. struct dibx000_i2c_master i2c_master;
  39. u16 wbd_ref;
  40. u8 current_band;
  41. u32 current_bandwidth;
  42. struct dibx000_agc_config *current_agc;
  43. u32 timf;
  44. u32 timf_default;
  45. u8 div_force_off:1;
  46. u8 div_state:1;
  47. u16 div_sync_wait;
  48. u8 agc_state;
  49. u8 differential_constellation;
  50. u8 diversity_onoff;
  51. s16 ber_monitored_layer;
  52. u16 gpio_dir;
  53. u16 gpio_val;
  54. u16 revision;
  55. u8 isdbt_cfg_loaded;
  56. enum frontend_tune_state tune_state;
  57. u32 status;
  58. struct dvb_frontend *fe[MAX_NUMBER_OF_FRONTENDS];
  59. /* for the I2C transfer */
  60. struct i2c_msg msg[2];
  61. u8 i2c_write_buffer[4];
  62. u8 i2c_read_buffer[2];
  63. struct mutex i2c_buffer_lock;
  64. };
  65. enum dib8000_power_mode {
  66. DIB8000M_POWER_ALL = 0,
  67. DIB8000M_POWER_INTERFACE_ONLY,
  68. };
  69. static u16 dib8000_i2c_read16(struct i2c_device *i2c, u16 reg)
  70. {
  71. u16 ret;
  72. struct i2c_msg msg[2] = {
  73. {.addr = i2c->addr >> 1, .flags = 0, .len = 2},
  74. {.addr = i2c->addr >> 1, .flags = I2C_M_RD, .len = 2},
  75. };
  76. if (mutex_lock_interruptible(i2c->i2c_buffer_lock) < 0) {
  77. dprintk("could not acquire lock");
  78. return 0;
  79. }
  80. msg[0].buf = i2c->i2c_write_buffer;
  81. msg[0].buf[0] = reg >> 8;
  82. msg[0].buf[1] = reg & 0xff;
  83. msg[1].buf = i2c->i2c_read_buffer;
  84. if (i2c_transfer(i2c->adap, msg, 2) != 2)
  85. dprintk("i2c read error on %d", reg);
  86. ret = (msg[1].buf[0] << 8) | msg[1].buf[1];
  87. mutex_unlock(i2c->i2c_buffer_lock);
  88. return ret;
  89. }
  90. static u16 dib8000_read_word(struct dib8000_state *state, u16 reg)
  91. {
  92. u16 ret;
  93. if (mutex_lock_interruptible(&state->i2c_buffer_lock) < 0) {
  94. dprintk("could not acquire lock");
  95. return 0;
  96. }
  97. state->i2c_write_buffer[0] = reg >> 8;
  98. state->i2c_write_buffer[1] = reg & 0xff;
  99. memset(state->msg, 0, 2 * sizeof(struct i2c_msg));
  100. state->msg[0].addr = state->i2c.addr >> 1;
  101. state->msg[0].flags = 0;
  102. state->msg[0].buf = state->i2c_write_buffer;
  103. state->msg[0].len = 2;
  104. state->msg[1].addr = state->i2c.addr >> 1;
  105. state->msg[1].flags = I2C_M_RD;
  106. state->msg[1].buf = state->i2c_read_buffer;
  107. state->msg[1].len = 2;
  108. if (i2c_transfer(state->i2c.adap, state->msg, 2) != 2)
  109. dprintk("i2c read error on %d", reg);
  110. ret = (state->i2c_read_buffer[0] << 8) | state->i2c_read_buffer[1];
  111. mutex_unlock(&state->i2c_buffer_lock);
  112. return ret;
  113. }
  114. static u32 dib8000_read32(struct dib8000_state *state, u16 reg)
  115. {
  116. u16 rw[2];
  117. rw[0] = dib8000_read_word(state, reg + 0);
  118. rw[1] = dib8000_read_word(state, reg + 1);
  119. return ((rw[0] << 16) | (rw[1]));
  120. }
  121. static int dib8000_i2c_write16(struct i2c_device *i2c, u16 reg, u16 val)
  122. {
  123. struct i2c_msg msg = {.addr = i2c->addr >> 1, .flags = 0, .len = 4};
  124. int ret = 0;
  125. if (mutex_lock_interruptible(i2c->i2c_buffer_lock) < 0) {
  126. dprintk("could not acquire lock");
  127. return -EINVAL;
  128. }
  129. msg.buf = i2c->i2c_write_buffer;
  130. msg.buf[0] = (reg >> 8) & 0xff;
  131. msg.buf[1] = reg & 0xff;
  132. msg.buf[2] = (val >> 8) & 0xff;
  133. msg.buf[3] = val & 0xff;
  134. ret = i2c_transfer(i2c->adap, &msg, 1) != 1 ? -EREMOTEIO : 0;
  135. mutex_unlock(i2c->i2c_buffer_lock);
  136. return ret;
  137. }
  138. static int dib8000_write_word(struct dib8000_state *state, u16 reg, u16 val)
  139. {
  140. int ret;
  141. if (mutex_lock_interruptible(&state->i2c_buffer_lock) < 0) {
  142. dprintk("could not acquire lock");
  143. return -EINVAL;
  144. }
  145. state->i2c_write_buffer[0] = (reg >> 8) & 0xff;
  146. state->i2c_write_buffer[1] = reg & 0xff;
  147. state->i2c_write_buffer[2] = (val >> 8) & 0xff;
  148. state->i2c_write_buffer[3] = val & 0xff;
  149. memset(&state->msg[0], 0, sizeof(struct i2c_msg));
  150. state->msg[0].addr = state->i2c.addr >> 1;
  151. state->msg[0].flags = 0;
  152. state->msg[0].buf = state->i2c_write_buffer;
  153. state->msg[0].len = 4;
  154. ret = (i2c_transfer(state->i2c.adap, state->msg, 1) != 1 ?
  155. -EREMOTEIO : 0);
  156. mutex_unlock(&state->i2c_buffer_lock);
  157. return ret;
  158. }
  159. static const s16 coeff_2k_sb_1seg_dqpsk[8] = {
  160. (769 << 5) | 0x0a, (745 << 5) | 0x03, (595 << 5) | 0x0d, (769 << 5) | 0x0a, (920 << 5) | 0x09, (784 << 5) | 0x02, (519 << 5) | 0x0c,
  161. (920 << 5) | 0x09
  162. };
  163. static const s16 coeff_2k_sb_1seg[8] = {
  164. (692 << 5) | 0x0b, (683 << 5) | 0x01, (519 << 5) | 0x09, (692 << 5) | 0x0b, 0 | 0x1f, 0 | 0x1f, 0 | 0x1f, 0 | 0x1f
  165. };
  166. static const s16 coeff_2k_sb_3seg_0dqpsk_1dqpsk[8] = {
  167. (832 << 5) | 0x10, (912 << 5) | 0x05, (900 << 5) | 0x12, (832 << 5) | 0x10, (-931 << 5) | 0x0f, (912 << 5) | 0x04, (807 << 5) | 0x11,
  168. (-931 << 5) | 0x0f
  169. };
  170. static const s16 coeff_2k_sb_3seg_0dqpsk[8] = {
  171. (622 << 5) | 0x0c, (941 << 5) | 0x04, (796 << 5) | 0x10, (622 << 5) | 0x0c, (982 << 5) | 0x0c, (519 << 5) | 0x02, (572 << 5) | 0x0e,
  172. (982 << 5) | 0x0c
  173. };
  174. static const s16 coeff_2k_sb_3seg_1dqpsk[8] = {
  175. (699 << 5) | 0x14, (607 << 5) | 0x04, (944 << 5) | 0x13, (699 << 5) | 0x14, (-720 << 5) | 0x0d, (640 << 5) | 0x03, (866 << 5) | 0x12,
  176. (-720 << 5) | 0x0d
  177. };
  178. static const s16 coeff_2k_sb_3seg[8] = {
  179. (664 << 5) | 0x0c, (925 << 5) | 0x03, (937 << 5) | 0x10, (664 << 5) | 0x0c, (-610 << 5) | 0x0a, (697 << 5) | 0x01, (836 << 5) | 0x0e,
  180. (-610 << 5) | 0x0a
  181. };
  182. static const s16 coeff_4k_sb_1seg_dqpsk[8] = {
  183. (-955 << 5) | 0x0e, (687 << 5) | 0x04, (818 << 5) | 0x10, (-955 << 5) | 0x0e, (-922 << 5) | 0x0d, (750 << 5) | 0x03, (665 << 5) | 0x0f,
  184. (-922 << 5) | 0x0d
  185. };
  186. static const s16 coeff_4k_sb_1seg[8] = {
  187. (638 << 5) | 0x0d, (683 << 5) | 0x02, (638 << 5) | 0x0d, (638 << 5) | 0x0d, (-655 << 5) | 0x0a, (517 << 5) | 0x00, (698 << 5) | 0x0d,
  188. (-655 << 5) | 0x0a
  189. };
  190. static const s16 coeff_4k_sb_3seg_0dqpsk_1dqpsk[8] = {
  191. (-707 << 5) | 0x14, (910 << 5) | 0x06, (889 << 5) | 0x16, (-707 << 5) | 0x14, (-958 << 5) | 0x13, (993 << 5) | 0x05, (523 << 5) | 0x14,
  192. (-958 << 5) | 0x13
  193. };
  194. static const s16 coeff_4k_sb_3seg_0dqpsk[8] = {
  195. (-723 << 5) | 0x13, (910 << 5) | 0x05, (777 << 5) | 0x14, (-723 << 5) | 0x13, (-568 << 5) | 0x0f, (547 << 5) | 0x03, (696 << 5) | 0x12,
  196. (-568 << 5) | 0x0f
  197. };
  198. static const s16 coeff_4k_sb_3seg_1dqpsk[8] = {
  199. (-940 << 5) | 0x15, (607 << 5) | 0x05, (915 << 5) | 0x16, (-940 << 5) | 0x15, (-848 << 5) | 0x13, (683 << 5) | 0x04, (543 << 5) | 0x14,
  200. (-848 << 5) | 0x13
  201. };
  202. static const s16 coeff_4k_sb_3seg[8] = {
  203. (612 << 5) | 0x12, (910 << 5) | 0x04, (864 << 5) | 0x14, (612 << 5) | 0x12, (-869 << 5) | 0x13, (683 << 5) | 0x02, (869 << 5) | 0x12,
  204. (-869 << 5) | 0x13
  205. };
  206. static const s16 coeff_8k_sb_1seg_dqpsk[8] = {
  207. (-835 << 5) | 0x12, (684 << 5) | 0x05, (735 << 5) | 0x14, (-835 << 5) | 0x12, (-598 << 5) | 0x10, (781 << 5) | 0x04, (739 << 5) | 0x13,
  208. (-598 << 5) | 0x10
  209. };
  210. static const s16 coeff_8k_sb_1seg[8] = {
  211. (673 << 5) | 0x0f, (683 << 5) | 0x03, (808 << 5) | 0x12, (673 << 5) | 0x0f, (585 << 5) | 0x0f, (512 << 5) | 0x01, (780 << 5) | 0x0f,
  212. (585 << 5) | 0x0f
  213. };
  214. static const s16 coeff_8k_sb_3seg_0dqpsk_1dqpsk[8] = {
  215. (863 << 5) | 0x17, (930 << 5) | 0x07, (878 << 5) | 0x19, (863 << 5) | 0x17, (0 << 5) | 0x14, (521 << 5) | 0x05, (980 << 5) | 0x18,
  216. (0 << 5) | 0x14
  217. };
  218. static const s16 coeff_8k_sb_3seg_0dqpsk[8] = {
  219. (-924 << 5) | 0x17, (910 << 5) | 0x06, (774 << 5) | 0x17, (-924 << 5) | 0x17, (-877 << 5) | 0x15, (565 << 5) | 0x04, (553 << 5) | 0x15,
  220. (-877 << 5) | 0x15
  221. };
  222. static const s16 coeff_8k_sb_3seg_1dqpsk[8] = {
  223. (-921 << 5) | 0x19, (607 << 5) | 0x06, (881 << 5) | 0x19, (-921 << 5) | 0x19, (-921 << 5) | 0x14, (713 << 5) | 0x05, (1018 << 5) | 0x18,
  224. (-921 << 5) | 0x14
  225. };
  226. static const s16 coeff_8k_sb_3seg[8] = {
  227. (514 << 5) | 0x14, (910 << 5) | 0x05, (861 << 5) | 0x17, (514 << 5) | 0x14, (690 << 5) | 0x14, (683 << 5) | 0x03, (662 << 5) | 0x15,
  228. (690 << 5) | 0x14
  229. };
  230. static const s16 ana_fe_coeff_3seg[24] = {
  231. 81, 80, 78, 74, 68, 61, 54, 45, 37, 28, 19, 11, 4, 1022, 1017, 1013, 1010, 1008, 1008, 1008, 1008, 1010, 1014, 1017
  232. };
  233. static const s16 ana_fe_coeff_1seg[24] = {
  234. 249, 226, 164, 82, 5, 981, 970, 988, 1018, 20, 31, 26, 8, 1012, 1000, 1018, 1012, 8, 15, 14, 9, 3, 1017, 1003
  235. };
  236. static const s16 ana_fe_coeff_13seg[24] = {
  237. 396, 305, 105, -51, -77, -12, 41, 31, -11, -30, -11, 14, 15, -2, -13, -7, 5, 8, 1, -6, -7, -3, 0, 1
  238. };
  239. static u16 fft_to_mode(struct dib8000_state *state)
  240. {
  241. u16 mode;
  242. switch (state->fe[0]->dtv_property_cache.transmission_mode) {
  243. case TRANSMISSION_MODE_2K:
  244. mode = 1;
  245. break;
  246. case TRANSMISSION_MODE_4K:
  247. mode = 2;
  248. break;
  249. default:
  250. case TRANSMISSION_MODE_AUTO:
  251. case TRANSMISSION_MODE_8K:
  252. mode = 3;
  253. break;
  254. }
  255. return mode;
  256. }
  257. static void dib8000_set_acquisition_mode(struct dib8000_state *state)
  258. {
  259. u16 nud = dib8000_read_word(state, 298);
  260. nud |= (1 << 3) | (1 << 0);
  261. dprintk("acquisition mode activated");
  262. dib8000_write_word(state, 298, nud);
  263. }
  264. static int dib8000_set_output_mode(struct dvb_frontend *fe, int mode)
  265. {
  266. struct dib8000_state *state = fe->demodulator_priv;
  267. u16 outreg, fifo_threshold, smo_mode, sram = 0x0205; /* by default SDRAM deintlv is enabled */
  268. outreg = 0;
  269. fifo_threshold = 1792;
  270. smo_mode = (dib8000_read_word(state, 299) & 0x0050) | (1 << 1);
  271. dprintk("-I- Setting output mode for demod %p to %d",
  272. &state->fe[0], mode);
  273. switch (mode) {
  274. case OUTMODE_MPEG2_PAR_GATED_CLK: // STBs with parallel gated clock
  275. outreg = (1 << 10); /* 0x0400 */
  276. break;
  277. case OUTMODE_MPEG2_PAR_CONT_CLK: // STBs with parallel continues clock
  278. outreg = (1 << 10) | (1 << 6); /* 0x0440 */
  279. break;
  280. case OUTMODE_MPEG2_SERIAL: // STBs with serial input
  281. outreg = (1 << 10) | (2 << 6) | (0 << 1); /* 0x0482 */
  282. break;
  283. case OUTMODE_DIVERSITY:
  284. if (state->cfg.hostbus_diversity) {
  285. outreg = (1 << 10) | (4 << 6); /* 0x0500 */
  286. sram &= 0xfdff;
  287. } else
  288. sram |= 0x0c00;
  289. break;
  290. case OUTMODE_MPEG2_FIFO: // e.g. USB feeding
  291. smo_mode |= (3 << 1);
  292. fifo_threshold = 512;
  293. outreg = (1 << 10) | (5 << 6);
  294. break;
  295. case OUTMODE_HIGH_Z: // disable
  296. outreg = 0;
  297. break;
  298. case OUTMODE_ANALOG_ADC:
  299. outreg = (1 << 10) | (3 << 6);
  300. dib8000_set_acquisition_mode(state);
  301. break;
  302. default:
  303. dprintk("Unhandled output_mode passed to be set for demod %p",
  304. &state->fe[0]);
  305. return -EINVAL;
  306. }
  307. if (state->cfg.output_mpeg2_in_188_bytes)
  308. smo_mode |= (1 << 5);
  309. dib8000_write_word(state, 299, smo_mode);
  310. dib8000_write_word(state, 300, fifo_threshold); /* synchronous fread */
  311. dib8000_write_word(state, 1286, outreg);
  312. dib8000_write_word(state, 1291, sram);
  313. return 0;
  314. }
  315. static int dib8000_set_diversity_in(struct dvb_frontend *fe, int onoff)
  316. {
  317. struct dib8000_state *state = fe->demodulator_priv;
  318. u16 sync_wait = dib8000_read_word(state, 273) & 0xfff0;
  319. if (!state->differential_constellation) {
  320. dib8000_write_word(state, 272, 1 << 9); //dvsy_off_lmod4 = 1
  321. dib8000_write_word(state, 273, sync_wait | (1 << 2) | 2); // sync_enable = 1; comb_mode = 2
  322. } else {
  323. dib8000_write_word(state, 272, 0); //dvsy_off_lmod4 = 0
  324. dib8000_write_word(state, 273, sync_wait); // sync_enable = 0; comb_mode = 0
  325. }
  326. state->diversity_onoff = onoff;
  327. switch (onoff) {
  328. case 0: /* only use the internal way - not the diversity input */
  329. dib8000_write_word(state, 270, 1);
  330. dib8000_write_word(state, 271, 0);
  331. break;
  332. case 1: /* both ways */
  333. dib8000_write_word(state, 270, 6);
  334. dib8000_write_word(state, 271, 6);
  335. break;
  336. case 2: /* only the diversity input */
  337. dib8000_write_word(state, 270, 0);
  338. dib8000_write_word(state, 271, 1);
  339. break;
  340. }
  341. return 0;
  342. }
  343. static void dib8000_set_power_mode(struct dib8000_state *state, enum dib8000_power_mode mode)
  344. {
  345. /* by default everything is going to be powered off */
  346. u16 reg_774 = 0x3fff, reg_775 = 0xffff, reg_776 = 0xffff,
  347. reg_900 = (dib8000_read_word(state, 900) & 0xfffc) | 0x3,
  348. reg_1280 = (dib8000_read_word(state, 1280) & 0x00ff) | 0xff00;
  349. /* now, depending on the requested mode, we power on */
  350. switch (mode) {
  351. /* power up everything in the demod */
  352. case DIB8000M_POWER_ALL:
  353. reg_774 = 0x0000;
  354. reg_775 = 0x0000;
  355. reg_776 = 0x0000;
  356. reg_900 &= 0xfffc;
  357. reg_1280 &= 0x00ff;
  358. break;
  359. case DIB8000M_POWER_INTERFACE_ONLY:
  360. reg_1280 &= 0x00ff;
  361. break;
  362. }
  363. dprintk("powermode : 774 : %x ; 775 : %x; 776 : %x ; 900 : %x; 1280 : %x", reg_774, reg_775, reg_776, reg_900, reg_1280);
  364. dib8000_write_word(state, 774, reg_774);
  365. dib8000_write_word(state, 775, reg_775);
  366. dib8000_write_word(state, 776, reg_776);
  367. dib8000_write_word(state, 900, reg_900);
  368. dib8000_write_word(state, 1280, reg_1280);
  369. }
  370. static int dib8000_set_adc_state(struct dib8000_state *state, enum dibx000_adc_states no)
  371. {
  372. int ret = 0;
  373. u16 reg_907 = dib8000_read_word(state, 907), reg_908 = dib8000_read_word(state, 908);
  374. switch (no) {
  375. case DIBX000_SLOW_ADC_ON:
  376. reg_908 |= (1 << 1) | (1 << 0);
  377. ret |= dib8000_write_word(state, 908, reg_908);
  378. reg_908 &= ~(1 << 1);
  379. break;
  380. case DIBX000_SLOW_ADC_OFF:
  381. reg_908 |= (1 << 1) | (1 << 0);
  382. break;
  383. case DIBX000_ADC_ON:
  384. reg_907 &= 0x0fff;
  385. reg_908 &= 0x0003;
  386. break;
  387. case DIBX000_ADC_OFF: // leave the VBG voltage on
  388. reg_907 |= (1 << 14) | (1 << 13) | (1 << 12);
  389. reg_908 |= (1 << 5) | (1 << 4) | (1 << 3) | (1 << 2);
  390. break;
  391. case DIBX000_VBG_ENABLE:
  392. reg_907 &= ~(1 << 15);
  393. break;
  394. case DIBX000_VBG_DISABLE:
  395. reg_907 |= (1 << 15);
  396. break;
  397. default:
  398. break;
  399. }
  400. ret |= dib8000_write_word(state, 907, reg_907);
  401. ret |= dib8000_write_word(state, 908, reg_908);
  402. return ret;
  403. }
  404. static int dib8000_set_bandwidth(struct dvb_frontend *fe, u32 bw)
  405. {
  406. struct dib8000_state *state = fe->demodulator_priv;
  407. u32 timf;
  408. if (bw == 0)
  409. bw = 6000;
  410. if (state->timf == 0) {
  411. dprintk("using default timf");
  412. timf = state->timf_default;
  413. } else {
  414. dprintk("using updated timf");
  415. timf = state->timf;
  416. }
  417. dib8000_write_word(state, 29, (u16) ((timf >> 16) & 0xffff));
  418. dib8000_write_word(state, 30, (u16) ((timf) & 0xffff));
  419. return 0;
  420. }
  421. static int dib8000_sad_calib(struct dib8000_state *state)
  422. {
  423. /* internal */
  424. dib8000_write_word(state, 923, (0 << 1) | (0 << 0));
  425. dib8000_write_word(state, 924, 776); // 0.625*3.3 / 4096
  426. /* do the calibration */
  427. dib8000_write_word(state, 923, (1 << 0));
  428. dib8000_write_word(state, 923, (0 << 0));
  429. msleep(1);
  430. return 0;
  431. }
  432. int dib8000_set_wbd_ref(struct dvb_frontend *fe, u16 value)
  433. {
  434. struct dib8000_state *state = fe->demodulator_priv;
  435. if (value > 4095)
  436. value = 4095;
  437. state->wbd_ref = value;
  438. return dib8000_write_word(state, 106, value);
  439. }
  440. EXPORT_SYMBOL(dib8000_set_wbd_ref);
  441. static void dib8000_reset_pll_common(struct dib8000_state *state, const struct dibx000_bandwidth_config *bw)
  442. {
  443. dprintk("ifreq: %d %x, inversion: %d", bw->ifreq, bw->ifreq, bw->ifreq >> 25);
  444. dib8000_write_word(state, 23, (u16) (((bw->internal * 1000) >> 16) & 0xffff)); /* P_sec_len */
  445. dib8000_write_word(state, 24, (u16) ((bw->internal * 1000) & 0xffff));
  446. dib8000_write_word(state, 27, (u16) ((bw->ifreq >> 16) & 0x01ff));
  447. dib8000_write_word(state, 28, (u16) (bw->ifreq & 0xffff));
  448. dib8000_write_word(state, 26, (u16) ((bw->ifreq >> 25) & 0x0003));
  449. dib8000_write_word(state, 922, bw->sad_cfg);
  450. }
  451. static void dib8000_reset_pll(struct dib8000_state *state)
  452. {
  453. const struct dibx000_bandwidth_config *pll = state->cfg.pll;
  454. u16 clk_cfg1;
  455. // clk_cfg0
  456. dib8000_write_word(state, 901, (pll->pll_prediv << 8) | (pll->pll_ratio << 0));
  457. // clk_cfg1
  458. clk_cfg1 = (1 << 10) | (0 << 9) | (pll->IO_CLK_en_core << 8) |
  459. (pll->bypclk_div << 5) | (pll->enable_refdiv << 4) | (1 << 3) |
  460. (pll->pll_range << 1) | (pll->pll_reset << 0);
  461. dib8000_write_word(state, 902, clk_cfg1);
  462. clk_cfg1 = (clk_cfg1 & 0xfff7) | (pll->pll_bypass << 3);
  463. dib8000_write_word(state, 902, clk_cfg1);
  464. dprintk("clk_cfg1: 0x%04x", clk_cfg1); /* 0x507 1 0 1 000 0 0 11 1 */
  465. /* smpl_cfg: P_refclksel=2, P_ensmplsel=1 nodivsmpl=1 */
  466. if (state->cfg.pll->ADClkSrc == 0)
  467. dib8000_write_word(state, 904, (0 << 15) | (0 << 12) | (0 << 10) |
  468. (pll->modulo << 8) | (pll->ADClkSrc << 7) | (0 << 1));
  469. else if (state->cfg.refclksel != 0)
  470. dib8000_write_word(state, 904, (0 << 15) | (1 << 12) |
  471. ((state->cfg.refclksel & 0x3) << 10) | (pll->modulo << 8) |
  472. (pll->ADClkSrc << 7) | (0 << 1));
  473. else
  474. dib8000_write_word(state, 904, (0 << 15) | (1 << 12) | (3 << 10) | (pll->modulo << 8) | (pll->ADClkSrc << 7) | (0 << 1));
  475. dib8000_reset_pll_common(state, pll);
  476. }
  477. static int dib8000_reset_gpio(struct dib8000_state *st)
  478. {
  479. /* reset the GPIOs */
  480. dib8000_write_word(st, 1029, st->cfg.gpio_dir);
  481. dib8000_write_word(st, 1030, st->cfg.gpio_val);
  482. /* TODO 782 is P_gpio_od */
  483. dib8000_write_word(st, 1032, st->cfg.gpio_pwm_pos);
  484. dib8000_write_word(st, 1037, st->cfg.pwm_freq_div);
  485. return 0;
  486. }
  487. static int dib8000_cfg_gpio(struct dib8000_state *st, u8 num, u8 dir, u8 val)
  488. {
  489. st->cfg.gpio_dir = dib8000_read_word(st, 1029);
  490. st->cfg.gpio_dir &= ~(1 << num); /* reset the direction bit */
  491. st->cfg.gpio_dir |= (dir & 0x1) << num; /* set the new direction */
  492. dib8000_write_word(st, 1029, st->cfg.gpio_dir);
  493. st->cfg.gpio_val = dib8000_read_word(st, 1030);
  494. st->cfg.gpio_val &= ~(1 << num); /* reset the direction bit */
  495. st->cfg.gpio_val |= (val & 0x01) << num; /* set the new value */
  496. dib8000_write_word(st, 1030, st->cfg.gpio_val);
  497. dprintk("gpio dir: %x: gpio val: %x", st->cfg.gpio_dir, st->cfg.gpio_val);
  498. return 0;
  499. }
  500. int dib8000_set_gpio(struct dvb_frontend *fe, u8 num, u8 dir, u8 val)
  501. {
  502. struct dib8000_state *state = fe->demodulator_priv;
  503. return dib8000_cfg_gpio(state, num, dir, val);
  504. }
  505. EXPORT_SYMBOL(dib8000_set_gpio);
  506. static const u16 dib8000_defaults[] = {
  507. /* auto search configuration - lock0 by default waiting
  508. * for cpil_lock; lock1 cpil_lock; lock2 tmcc_sync_lock */
  509. 3, 7,
  510. 0x0004,
  511. 0x0400,
  512. 0x0814,
  513. 12, 11,
  514. 0x001b,
  515. 0x7740,
  516. 0x005b,
  517. 0x8d80,
  518. 0x01c9,
  519. 0xc380,
  520. 0x0000,
  521. 0x0080,
  522. 0x0000,
  523. 0x0090,
  524. 0x0001,
  525. 0xd4c0,
  526. /*1, 32,
  527. 0x6680 // P_corm_thres Lock algorithms configuration */
  528. 11, 80, /* set ADC level to -16 */
  529. (1 << 13) - 825 - 117,
  530. (1 << 13) - 837 - 117,
  531. (1 << 13) - 811 - 117,
  532. (1 << 13) - 766 - 117,
  533. (1 << 13) - 737 - 117,
  534. (1 << 13) - 693 - 117,
  535. (1 << 13) - 648 - 117,
  536. (1 << 13) - 619 - 117,
  537. (1 << 13) - 575 - 117,
  538. (1 << 13) - 531 - 117,
  539. (1 << 13) - 501 - 117,
  540. 4, 108,
  541. 0,
  542. 0,
  543. 0,
  544. 0,
  545. 1, 175,
  546. 0x0410,
  547. 1, 179,
  548. 8192, // P_fft_nb_to_cut
  549. 6, 181,
  550. 0x2800, // P_coff_corthres_ ( 2k 4k 8k ) 0x2800
  551. 0x2800,
  552. 0x2800,
  553. 0x2800, // P_coff_cpilthres_ ( 2k 4k 8k ) 0x2800
  554. 0x2800,
  555. 0x2800,
  556. 2, 193,
  557. 0x0666, // P_pha3_thres
  558. 0x0000, // P_cti_use_cpe, P_cti_use_prog
  559. 2, 205,
  560. 0x200f, // P_cspu_regul, P_cspu_win_cut
  561. 0x000f, // P_des_shift_work
  562. 5, 215,
  563. 0x023d, // P_adp_regul_cnt
  564. 0x00a4, // P_adp_noise_cnt
  565. 0x00a4, // P_adp_regul_ext
  566. 0x7ff0, // P_adp_noise_ext
  567. 0x3ccc, // P_adp_fil
  568. 1, 230,
  569. 0x0000, // P_2d_byp_ti_num
  570. 1, 263,
  571. 0x800, //P_equal_thres_wgn
  572. 1, 268,
  573. (2 << 9) | 39, // P_equal_ctrl_synchro, P_equal_speedmode
  574. 1, 270,
  575. 0x0001, // P_div_lock0_wait
  576. 1, 285,
  577. 0x0020, //p_fec_
  578. 1, 299,
  579. 0x0062, /* P_smo_mode, P_smo_rs_discard, P_smo_fifo_flush, P_smo_pid_parse, P_smo_error_discard */
  580. 1, 338,
  581. (1 << 12) | // P_ctrl_corm_thres4pre_freq_inh=1
  582. (1 << 10) |
  583. (0 << 9) | /* P_ctrl_pre_freq_inh=0 */
  584. (3 << 5) | /* P_ctrl_pre_freq_step=3 */
  585. (1 << 0), /* P_pre_freq_win_len=1 */
  586. 1, 903,
  587. (0 << 4) | 2, // P_divclksel=0 P_divbitsel=2 (was clk=3,bit=1 for MPW)
  588. 0,
  589. };
  590. static u16 dib8000_identify(struct i2c_device *client)
  591. {
  592. u16 value;
  593. //because of glitches sometimes
  594. value = dib8000_i2c_read16(client, 896);
  595. if ((value = dib8000_i2c_read16(client, 896)) != 0x01b3) {
  596. dprintk("wrong Vendor ID (read=0x%x)", value);
  597. return 0;
  598. }
  599. value = dib8000_i2c_read16(client, 897);
  600. if (value != 0x8000 && value != 0x8001 && value != 0x8002) {
  601. dprintk("wrong Device ID (%x)", value);
  602. return 0;
  603. }
  604. switch (value) {
  605. case 0x8000:
  606. dprintk("found DiB8000A");
  607. break;
  608. case 0x8001:
  609. dprintk("found DiB8000B");
  610. break;
  611. case 0x8002:
  612. dprintk("found DiB8000C");
  613. break;
  614. }
  615. return value;
  616. }
  617. static int dib8000_reset(struct dvb_frontend *fe)
  618. {
  619. struct dib8000_state *state = fe->demodulator_priv;
  620. dib8000_write_word(state, 1287, 0x0003); /* sram lead in, rdy */
  621. if ((state->revision = dib8000_identify(&state->i2c)) == 0)
  622. return -EINVAL;
  623. if (state->revision == 0x8000)
  624. dprintk("error : dib8000 MA not supported");
  625. dibx000_reset_i2c_master(&state->i2c_master);
  626. dib8000_set_power_mode(state, DIB8000M_POWER_ALL);
  627. /* always leave the VBG voltage on - it consumes almost nothing but takes a long time to start */
  628. dib8000_set_adc_state(state, DIBX000_VBG_ENABLE);
  629. /* restart all parts */
  630. dib8000_write_word(state, 770, 0xffff);
  631. dib8000_write_word(state, 771, 0xffff);
  632. dib8000_write_word(state, 772, 0xfffc);
  633. dib8000_write_word(state, 898, 0x000c); // sad
  634. dib8000_write_word(state, 1280, 0x004d);
  635. dib8000_write_word(state, 1281, 0x000c);
  636. dib8000_write_word(state, 770, 0x0000);
  637. dib8000_write_word(state, 771, 0x0000);
  638. dib8000_write_word(state, 772, 0x0000);
  639. dib8000_write_word(state, 898, 0x0004); // sad
  640. dib8000_write_word(state, 1280, 0x0000);
  641. dib8000_write_word(state, 1281, 0x0000);
  642. /* drives */
  643. if (state->cfg.drives)
  644. dib8000_write_word(state, 906, state->cfg.drives);
  645. else {
  646. dprintk("using standard PAD-drive-settings, please adjust settings in config-struct to be optimal.");
  647. dib8000_write_word(state, 906, 0x2d98); // min drive SDRAM - not optimal - adjust
  648. }
  649. dib8000_reset_pll(state);
  650. if (dib8000_reset_gpio(state) != 0)
  651. dprintk("GPIO reset was not successful.");
  652. if (dib8000_set_output_mode(fe, OUTMODE_HIGH_Z) != 0)
  653. dprintk("OUTPUT_MODE could not be resetted.");
  654. state->current_agc = NULL;
  655. // P_iqc_alpha_pha, P_iqc_alpha_amp, P_iqc_dcc_alpha, ...
  656. /* P_iqc_ca2 = 0; P_iqc_impnc_on = 0; P_iqc_mode = 0; */
  657. if (state->cfg.pll->ifreq == 0)
  658. dib8000_write_word(state, 40, 0x0755); /* P_iqc_corr_inh = 0 enable IQcorr block */
  659. else
  660. dib8000_write_word(state, 40, 0x1f55); /* P_iqc_corr_inh = 1 disable IQcorr block */
  661. {
  662. u16 l = 0, r;
  663. const u16 *n;
  664. n = dib8000_defaults;
  665. l = *n++;
  666. while (l) {
  667. r = *n++;
  668. do {
  669. dib8000_write_word(state, r, *n++);
  670. r++;
  671. } while (--l);
  672. l = *n++;
  673. }
  674. }
  675. state->isdbt_cfg_loaded = 0;
  676. //div_cfg override for special configs
  677. if (state->cfg.div_cfg != 0)
  678. dib8000_write_word(state, 903, state->cfg.div_cfg);
  679. /* unforce divstr regardless whether i2c enumeration was done or not */
  680. dib8000_write_word(state, 1285, dib8000_read_word(state, 1285) & ~(1 << 1));
  681. dib8000_set_bandwidth(fe, 6000);
  682. dib8000_set_adc_state(state, DIBX000_SLOW_ADC_ON);
  683. dib8000_sad_calib(state);
  684. dib8000_set_adc_state(state, DIBX000_SLOW_ADC_OFF);
  685. dib8000_set_power_mode(state, DIB8000M_POWER_INTERFACE_ONLY);
  686. return 0;
  687. }
  688. static void dib8000_restart_agc(struct dib8000_state *state)
  689. {
  690. // P_restart_iqc & P_restart_agc
  691. dib8000_write_word(state, 770, 0x0a00);
  692. dib8000_write_word(state, 770, 0x0000);
  693. }
  694. static int dib8000_update_lna(struct dib8000_state *state)
  695. {
  696. u16 dyn_gain;
  697. if (state->cfg.update_lna) {
  698. // read dyn_gain here (because it is demod-dependent and not tuner)
  699. dyn_gain = dib8000_read_word(state, 390);
  700. if (state->cfg.update_lna(state->fe[0], dyn_gain)) {
  701. dib8000_restart_agc(state);
  702. return 1;
  703. }
  704. }
  705. return 0;
  706. }
  707. static int dib8000_set_agc_config(struct dib8000_state *state, u8 band)
  708. {
  709. struct dibx000_agc_config *agc = NULL;
  710. int i;
  711. if (state->current_band == band && state->current_agc != NULL)
  712. return 0;
  713. state->current_band = band;
  714. for (i = 0; i < state->cfg.agc_config_count; i++)
  715. if (state->cfg.agc[i].band_caps & band) {
  716. agc = &state->cfg.agc[i];
  717. break;
  718. }
  719. if (agc == NULL) {
  720. dprintk("no valid AGC configuration found for band 0x%02x", band);
  721. return -EINVAL;
  722. }
  723. state->current_agc = agc;
  724. /* AGC */
  725. dib8000_write_word(state, 76, agc->setup);
  726. dib8000_write_word(state, 77, agc->inv_gain);
  727. dib8000_write_word(state, 78, agc->time_stabiliz);
  728. dib8000_write_word(state, 101, (agc->alpha_level << 12) | agc->thlock);
  729. // Demod AGC loop configuration
  730. dib8000_write_word(state, 102, (agc->alpha_mant << 5) | agc->alpha_exp);
  731. dib8000_write_word(state, 103, (agc->beta_mant << 6) | agc->beta_exp);
  732. dprintk("WBD: ref: %d, sel: %d, active: %d, alpha: %d",
  733. state->wbd_ref != 0 ? state->wbd_ref : agc->wbd_ref, agc->wbd_sel, !agc->perform_agc_softsplit, agc->wbd_sel);
  734. /* AGC continued */
  735. if (state->wbd_ref != 0)
  736. dib8000_write_word(state, 106, state->wbd_ref);
  737. else // use default
  738. dib8000_write_word(state, 106, agc->wbd_ref);
  739. dib8000_write_word(state, 107, (agc->wbd_alpha << 9) | (agc->perform_agc_softsplit << 8));
  740. dib8000_write_word(state, 108, agc->agc1_max);
  741. dib8000_write_word(state, 109, agc->agc1_min);
  742. dib8000_write_word(state, 110, agc->agc2_max);
  743. dib8000_write_word(state, 111, agc->agc2_min);
  744. dib8000_write_word(state, 112, (agc->agc1_pt1 << 8) | agc->agc1_pt2);
  745. dib8000_write_word(state, 113, (agc->agc1_slope1 << 8) | agc->agc1_slope2);
  746. dib8000_write_word(state, 114, (agc->agc2_pt1 << 8) | agc->agc2_pt2);
  747. dib8000_write_word(state, 115, (agc->agc2_slope1 << 8) | agc->agc2_slope2);
  748. dib8000_write_word(state, 75, agc->agc1_pt3);
  749. dib8000_write_word(state, 923, (dib8000_read_word(state, 923) & 0xffe3) | (agc->wbd_inv << 4) | (agc->wbd_sel << 2)); /*LB : 929 -> 923 */
  750. return 0;
  751. }
  752. void dib8000_pwm_agc_reset(struct dvb_frontend *fe)
  753. {
  754. struct dib8000_state *state = fe->demodulator_priv;
  755. dib8000_set_adc_state(state, DIBX000_ADC_ON);
  756. dib8000_set_agc_config(state, (unsigned char)(BAND_OF_FREQUENCY(fe->dtv_property_cache.frequency / 1000)));
  757. }
  758. EXPORT_SYMBOL(dib8000_pwm_agc_reset);
  759. static int dib8000_agc_soft_split(struct dib8000_state *state)
  760. {
  761. u16 agc, split_offset;
  762. if (!state->current_agc || !state->current_agc->perform_agc_softsplit || state->current_agc->split.max == 0)
  763. return FE_CALLBACK_TIME_NEVER;
  764. // n_agc_global
  765. agc = dib8000_read_word(state, 390);
  766. if (agc > state->current_agc->split.min_thres)
  767. split_offset = state->current_agc->split.min;
  768. else if (agc < state->current_agc->split.max_thres)
  769. split_offset = state->current_agc->split.max;
  770. else
  771. split_offset = state->current_agc->split.max *
  772. (agc - state->current_agc->split.min_thres) /
  773. (state->current_agc->split.max_thres - state->current_agc->split.min_thres);
  774. dprintk("AGC split_offset: %d", split_offset);
  775. // P_agc_force_split and P_agc_split_offset
  776. dib8000_write_word(state, 107, (dib8000_read_word(state, 107) & 0xff00) | split_offset);
  777. return 5000;
  778. }
  779. static int dib8000_agc_startup(struct dvb_frontend *fe)
  780. {
  781. struct dib8000_state *state = fe->demodulator_priv;
  782. enum frontend_tune_state *tune_state = &state->tune_state;
  783. int ret = 0;
  784. switch (*tune_state) {
  785. case CT_AGC_START:
  786. // set power-up level: interf+analog+AGC
  787. dib8000_set_adc_state(state, DIBX000_ADC_ON);
  788. if (dib8000_set_agc_config(state, (unsigned char)(BAND_OF_FREQUENCY(fe->dtv_property_cache.frequency / 1000))) != 0) {
  789. *tune_state = CT_AGC_STOP;
  790. state->status = FE_STATUS_TUNE_FAILED;
  791. break;
  792. }
  793. ret = 70;
  794. *tune_state = CT_AGC_STEP_0;
  795. break;
  796. case CT_AGC_STEP_0:
  797. //AGC initialization
  798. if (state->cfg.agc_control)
  799. state->cfg.agc_control(fe, 1);
  800. dib8000_restart_agc(state);
  801. // wait AGC rough lock time
  802. ret = 50;
  803. *tune_state = CT_AGC_STEP_1;
  804. break;
  805. case CT_AGC_STEP_1:
  806. // wait AGC accurate lock time
  807. ret = 70;
  808. if (dib8000_update_lna(state))
  809. // wait only AGC rough lock time
  810. ret = 50;
  811. else
  812. *tune_state = CT_AGC_STEP_2;
  813. break;
  814. case CT_AGC_STEP_2:
  815. dib8000_agc_soft_split(state);
  816. if (state->cfg.agc_control)
  817. state->cfg.agc_control(fe, 0);
  818. *tune_state = CT_AGC_STOP;
  819. break;
  820. default:
  821. ret = dib8000_agc_soft_split(state);
  822. break;
  823. }
  824. return ret;
  825. }
  826. static const s32 lut_1000ln_mant[] =
  827. {
  828. 908, 7003, 7090, 7170, 7244, 7313, 7377, 7438, 7495, 7549, 7600
  829. };
  830. s32 dib8000_get_adc_power(struct dvb_frontend *fe, u8 mode)
  831. {
  832. struct dib8000_state *state = fe->demodulator_priv;
  833. u32 ix = 0, tmp_val = 0, exp = 0, mant = 0;
  834. s32 val;
  835. val = dib8000_read32(state, 384);
  836. if (mode) {
  837. tmp_val = val;
  838. while (tmp_val >>= 1)
  839. exp++;
  840. mant = (val * 1000 / (1<<exp));
  841. ix = (u8)((mant-1000)/100); /* index of the LUT */
  842. val = (lut_1000ln_mant[ix] + 693*(exp-20) - 6908);
  843. val = (val*256)/1000;
  844. }
  845. return val;
  846. }
  847. EXPORT_SYMBOL(dib8000_get_adc_power);
  848. static void dib8000_update_timf(struct dib8000_state *state)
  849. {
  850. u32 timf = state->timf = dib8000_read32(state, 435);
  851. dib8000_write_word(state, 29, (u16) (timf >> 16));
  852. dib8000_write_word(state, 30, (u16) (timf & 0xffff));
  853. dprintk("Updated timing frequency: %d (default: %d)", state->timf, state->timf_default);
  854. }
  855. static const u16 adc_target_16dB[11] = {
  856. (1 << 13) - 825 - 117,
  857. (1 << 13) - 837 - 117,
  858. (1 << 13) - 811 - 117,
  859. (1 << 13) - 766 - 117,
  860. (1 << 13) - 737 - 117,
  861. (1 << 13) - 693 - 117,
  862. (1 << 13) - 648 - 117,
  863. (1 << 13) - 619 - 117,
  864. (1 << 13) - 575 - 117,
  865. (1 << 13) - 531 - 117,
  866. (1 << 13) - 501 - 117
  867. };
  868. static const u8 permu_seg[] = { 6, 5, 7, 4, 8, 3, 9, 2, 10, 1, 11, 0, 12 };
  869. static void dib8000_set_channel(struct dib8000_state *state, u8 seq, u8 autosearching)
  870. {
  871. u16 mode, max_constellation, seg_diff_mask = 0, nbseg_diff = 0;
  872. u8 guard, crate, constellation, timeI;
  873. u16 i, coeff[4], P_cfr_left_edge = 0, P_cfr_right_edge = 0, seg_mask13 = 0x1fff; // All 13 segments enabled
  874. const s16 *ncoeff = NULL, *ana_fe;
  875. u16 tmcc_pow = 0;
  876. u16 coff_pow = 0x2800;
  877. u16 init_prbs = 0xfff;
  878. u16 ana_gain = 0;
  879. if (state->ber_monitored_layer != LAYER_ALL)
  880. dib8000_write_word(state, 285, (dib8000_read_word(state, 285) & 0x60) | state->ber_monitored_layer);
  881. else
  882. dib8000_write_word(state, 285, dib8000_read_word(state, 285) & 0x60);
  883. i = dib8000_read_word(state, 26) & 1; // P_dds_invspec
  884. dib8000_write_word(state, 26, state->fe[0]->dtv_property_cache.inversion^i);
  885. if (state->fe[0]->dtv_property_cache.isdbt_sb_mode) {
  886. //compute new dds_freq for the seg and adjust prbs
  887. int seg_offset =
  888. state->fe[0]->dtv_property_cache.isdbt_sb_segment_idx -
  889. (state->fe[0]->dtv_property_cache.isdbt_sb_segment_count / 2) -
  890. (state->fe[0]->dtv_property_cache.isdbt_sb_segment_count % 2);
  891. int clk = state->cfg.pll->internal;
  892. u32 segtodds = ((u32) (430 << 23) / clk) << 3; // segtodds = SegBW / Fclk * pow(2,26)
  893. int dds_offset = seg_offset * segtodds;
  894. int new_dds, sub_channel;
  895. if ((state->fe[0]->dtv_property_cache.isdbt_sb_segment_count % 2) == 0)
  896. dds_offset -= (int)(segtodds / 2);
  897. if (state->cfg.pll->ifreq == 0) {
  898. if ((state->fe[0]->dtv_property_cache.inversion ^ i) == 0) {
  899. dib8000_write_word(state, 26, dib8000_read_word(state, 26) | 1);
  900. new_dds = dds_offset;
  901. } else
  902. new_dds = dds_offset;
  903. // We shift tuning frequency if the wanted segment is :
  904. // - the segment of center frequency with an odd total number of segments
  905. // - the segment to the left of center frequency with an even total number of segments
  906. // - the segment to the right of center frequency with an even total number of segments
  907. if ((state->fe[0]->dtv_property_cache.delivery_system == SYS_ISDBT)
  908. && (state->fe[0]->dtv_property_cache.isdbt_sb_mode == 1)
  909. && (((state->fe[0]->dtv_property_cache.isdbt_sb_segment_count % 2)
  910. && (state->fe[0]->dtv_property_cache.isdbt_sb_segment_idx ==
  911. ((state->fe[0]->dtv_property_cache.isdbt_sb_segment_count / 2) + 1)))
  912. || (((state->fe[0]->dtv_property_cache.isdbt_sb_segment_count % 2) == 0)
  913. && (state->fe[0]->dtv_property_cache.isdbt_sb_segment_idx == (state->fe[0]->dtv_property_cache.isdbt_sb_segment_count / 2)))
  914. || (((state->fe[0]->dtv_property_cache.isdbt_sb_segment_count % 2) == 0)
  915. && (state->fe[0]->dtv_property_cache.isdbt_sb_segment_idx ==
  916. ((state->fe[0]->dtv_property_cache.isdbt_sb_segment_count / 2) + 1)))
  917. )) {
  918. new_dds -= ((u32) (850 << 22) / clk) << 4; // new_dds = 850 (freq shift in KHz) / Fclk * pow(2,26)
  919. }
  920. } else {
  921. if ((state->fe[0]->dtv_property_cache.inversion ^ i) == 0)
  922. new_dds = state->cfg.pll->ifreq - dds_offset;
  923. else
  924. new_dds = state->cfg.pll->ifreq + dds_offset;
  925. }
  926. dib8000_write_word(state, 27, (u16) ((new_dds >> 16) & 0x01ff));
  927. dib8000_write_word(state, 28, (u16) (new_dds & 0xffff));
  928. if (state->fe[0]->dtv_property_cache.isdbt_sb_segment_count % 2)
  929. sub_channel = ((state->fe[0]->dtv_property_cache.isdbt_sb_subchannel + (3 * seg_offset) + 1) % 41) / 3;
  930. else
  931. sub_channel = ((state->fe[0]->dtv_property_cache.isdbt_sb_subchannel + (3 * seg_offset)) % 41) / 3;
  932. sub_channel -= 6;
  933. if (state->fe[0]->dtv_property_cache.transmission_mode == TRANSMISSION_MODE_2K
  934. || state->fe[0]->dtv_property_cache.transmission_mode == TRANSMISSION_MODE_4K) {
  935. dib8000_write_word(state, 219, dib8000_read_word(state, 219) | 0x1); //adp_pass =1
  936. dib8000_write_word(state, 190, dib8000_read_word(state, 190) | (0x1 << 14)); //pha3_force_pha_shift = 1
  937. } else {
  938. dib8000_write_word(state, 219, dib8000_read_word(state, 219) & 0xfffe); //adp_pass =0
  939. dib8000_write_word(state, 190, dib8000_read_word(state, 190) & 0xbfff); //pha3_force_pha_shift = 0
  940. }
  941. switch (state->fe[0]->dtv_property_cache.transmission_mode) {
  942. case TRANSMISSION_MODE_2K:
  943. switch (sub_channel) {
  944. case -6:
  945. init_prbs = 0x0;
  946. break; // 41, 0, 1
  947. case -5:
  948. init_prbs = 0x423;
  949. break; // 02~04
  950. case -4:
  951. init_prbs = 0x9;
  952. break; // 05~07
  953. case -3:
  954. init_prbs = 0x5C7;
  955. break; // 08~10
  956. case -2:
  957. init_prbs = 0x7A6;
  958. break; // 11~13
  959. case -1:
  960. init_prbs = 0x3D8;
  961. break; // 14~16
  962. case 0:
  963. init_prbs = 0x527;
  964. break; // 17~19
  965. case 1:
  966. init_prbs = 0x7FF;
  967. break; // 20~22
  968. case 2:
  969. init_prbs = 0x79B;
  970. break; // 23~25
  971. case 3:
  972. init_prbs = 0x3D6;
  973. break; // 26~28
  974. case 4:
  975. init_prbs = 0x3A2;
  976. break; // 29~31
  977. case 5:
  978. init_prbs = 0x53B;
  979. break; // 32~34
  980. case 6:
  981. init_prbs = 0x2F4;
  982. break; // 35~37
  983. default:
  984. case 7:
  985. init_prbs = 0x213;
  986. break; // 38~40
  987. }
  988. break;
  989. case TRANSMISSION_MODE_4K:
  990. switch (sub_channel) {
  991. case -6:
  992. init_prbs = 0x0;
  993. break; // 41, 0, 1
  994. case -5:
  995. init_prbs = 0x208;
  996. break; // 02~04
  997. case -4:
  998. init_prbs = 0xC3;
  999. break; // 05~07
  1000. case -3:
  1001. init_prbs = 0x7B9;
  1002. break; // 08~10
  1003. case -2:
  1004. init_prbs = 0x423;
  1005. break; // 11~13
  1006. case -1:
  1007. init_prbs = 0x5C7;
  1008. break; // 14~16
  1009. case 0:
  1010. init_prbs = 0x3D8;
  1011. break; // 17~19
  1012. case 1:
  1013. init_prbs = 0x7FF;
  1014. break; // 20~22
  1015. case 2:
  1016. init_prbs = 0x3D6;
  1017. break; // 23~25
  1018. case 3:
  1019. init_prbs = 0x53B;
  1020. break; // 26~28
  1021. case 4:
  1022. init_prbs = 0x213;
  1023. break; // 29~31
  1024. case 5:
  1025. init_prbs = 0x29;
  1026. break; // 32~34
  1027. case 6:
  1028. init_prbs = 0xD0;
  1029. break; // 35~37
  1030. default:
  1031. case 7:
  1032. init_prbs = 0x48E;
  1033. break; // 38~40
  1034. }
  1035. break;
  1036. default:
  1037. case TRANSMISSION_MODE_8K:
  1038. switch (sub_channel) {
  1039. case -6:
  1040. init_prbs = 0x0;
  1041. break; // 41, 0, 1
  1042. case -5:
  1043. init_prbs = 0x740;
  1044. break; // 02~04
  1045. case -4:
  1046. init_prbs = 0x069;
  1047. break; // 05~07
  1048. case -3:
  1049. init_prbs = 0x7DD;
  1050. break; // 08~10
  1051. case -2:
  1052. init_prbs = 0x208;
  1053. break; // 11~13
  1054. case -1:
  1055. init_prbs = 0x7B9;
  1056. break; // 14~16
  1057. case 0:
  1058. init_prbs = 0x5C7;
  1059. break; // 17~19
  1060. case 1:
  1061. init_prbs = 0x7FF;
  1062. break; // 20~22
  1063. case 2:
  1064. init_prbs = 0x53B;
  1065. break; // 23~25
  1066. case 3:
  1067. init_prbs = 0x29;
  1068. break; // 26~28
  1069. case 4:
  1070. init_prbs = 0x48E;
  1071. break; // 29~31
  1072. case 5:
  1073. init_prbs = 0x4C4;
  1074. break; // 32~34
  1075. case 6:
  1076. init_prbs = 0x367;
  1077. break; // 33~37
  1078. default:
  1079. case 7:
  1080. init_prbs = 0x684;
  1081. break; // 38~40
  1082. }
  1083. break;
  1084. }
  1085. } else {
  1086. dib8000_write_word(state, 27, (u16) ((state->cfg.pll->ifreq >> 16) & 0x01ff));
  1087. dib8000_write_word(state, 28, (u16) (state->cfg.pll->ifreq & 0xffff));
  1088. dib8000_write_word(state, 26, (u16) ((state->cfg.pll->ifreq >> 25) & 0x0003));
  1089. }
  1090. /*P_mode == ?? */
  1091. dib8000_write_word(state, 10, (seq << 4));
  1092. // dib8000_write_word(state, 287, (dib8000_read_word(state, 287) & 0xe000) | 0x1000);
  1093. switch (state->fe[0]->dtv_property_cache.guard_interval) {
  1094. case GUARD_INTERVAL_1_32:
  1095. guard = 0;
  1096. break;
  1097. case GUARD_INTERVAL_1_16:
  1098. guard = 1;
  1099. break;
  1100. case GUARD_INTERVAL_1_8:
  1101. guard = 2;
  1102. break;
  1103. case GUARD_INTERVAL_1_4:
  1104. default:
  1105. guard = 3;
  1106. break;
  1107. }
  1108. dib8000_write_word(state, 1, (init_prbs << 2) | (guard & 0x3)); // ADDR 1
  1109. max_constellation = DQPSK;
  1110. for (i = 0; i < 3; i++) {
  1111. switch (state->fe[0]->dtv_property_cache.layer[i].modulation) {
  1112. case DQPSK:
  1113. constellation = 0;
  1114. break;
  1115. case QPSK:
  1116. constellation = 1;
  1117. break;
  1118. case QAM_16:
  1119. constellation = 2;
  1120. break;
  1121. case QAM_64:
  1122. default:
  1123. constellation = 3;
  1124. break;
  1125. }
  1126. switch (state->fe[0]->dtv_property_cache.layer[i].fec) {
  1127. case FEC_1_2:
  1128. crate = 1;
  1129. break;
  1130. case FEC_2_3:
  1131. crate = 2;
  1132. break;
  1133. case FEC_3_4:
  1134. crate = 3;
  1135. break;
  1136. case FEC_5_6:
  1137. crate = 5;
  1138. break;
  1139. case FEC_7_8:
  1140. default:
  1141. crate = 7;
  1142. break;
  1143. }
  1144. if ((state->fe[0]->dtv_property_cache.layer[i].interleaving > 0) &&
  1145. ((state->fe[0]->dtv_property_cache.layer[i].interleaving <= 3) ||
  1146. (state->fe[0]->dtv_property_cache.layer[i].interleaving == 4 && state->fe[0]->dtv_property_cache.isdbt_sb_mode == 1))
  1147. )
  1148. timeI = state->fe[0]->dtv_property_cache.layer[i].interleaving;
  1149. else
  1150. timeI = 0;
  1151. dib8000_write_word(state, 2 + i, (constellation << 10) | ((state->fe[0]->dtv_property_cache.layer[i].segment_count & 0xf) << 6) |
  1152. (crate << 3) | timeI);
  1153. if (state->fe[0]->dtv_property_cache.layer[i].segment_count > 0) {
  1154. switch (max_constellation) {
  1155. case DQPSK:
  1156. case QPSK:
  1157. if (state->fe[0]->dtv_property_cache.layer[i].modulation == QAM_16 ||
  1158. state->fe[0]->dtv_property_cache.layer[i].modulation == QAM_64)
  1159. max_constellation = state->fe[0]->dtv_property_cache.layer[i].modulation;
  1160. break;
  1161. case QAM_16:
  1162. if (state->fe[0]->dtv_property_cache.layer[i].modulation == QAM_64)
  1163. max_constellation = state->fe[0]->dtv_property_cache.layer[i].modulation;
  1164. break;
  1165. }
  1166. }
  1167. }
  1168. mode = fft_to_mode(state);
  1169. //dib8000_write_word(state, 5, 13); /*p_last_seg = 13*/
  1170. dib8000_write_word(state, 274, (dib8000_read_word(state, 274) & 0xffcf) |
  1171. ((state->fe[0]->dtv_property_cache.isdbt_partial_reception & 1) << 5) | ((state->fe[0]->dtv_property_cache.
  1172. isdbt_sb_mode & 1) << 4));
  1173. dprintk("mode = %d ; guard = %d", mode, state->fe[0]->dtv_property_cache.guard_interval);
  1174. /* signal optimization parameter */
  1175. if (state->fe[0]->dtv_property_cache.isdbt_partial_reception) {
  1176. seg_diff_mask = (state->fe[0]->dtv_property_cache.layer[0].modulation == DQPSK) << permu_seg[0];
  1177. for (i = 1; i < 3; i++)
  1178. nbseg_diff +=
  1179. (state->fe[0]->dtv_property_cache.layer[i].modulation == DQPSK) * state->fe[0]->dtv_property_cache.layer[i].segment_count;
  1180. for (i = 0; i < nbseg_diff; i++)
  1181. seg_diff_mask |= 1 << permu_seg[i + 1];
  1182. } else {
  1183. for (i = 0; i < 3; i++)
  1184. nbseg_diff +=
  1185. (state->fe[0]->dtv_property_cache.layer[i].modulation == DQPSK) * state->fe[0]->dtv_property_cache.layer[i].segment_count;
  1186. for (i = 0; i < nbseg_diff; i++)
  1187. seg_diff_mask |= 1 << permu_seg[i];
  1188. }
  1189. dprintk("nbseg_diff = %X (%d)", seg_diff_mask, seg_diff_mask);
  1190. state->differential_constellation = (seg_diff_mask != 0);
  1191. dib8000_set_diversity_in(state->fe[0], state->diversity_onoff);
  1192. if (state->fe[0]->dtv_property_cache.isdbt_sb_mode == 1) {
  1193. if (state->fe[0]->dtv_property_cache.isdbt_partial_reception == 1)
  1194. seg_mask13 = 0x00E0;
  1195. else // 1-segment
  1196. seg_mask13 = 0x0040;
  1197. } else
  1198. seg_mask13 = 0x1fff;
  1199. // WRITE: Mode & Diff mask
  1200. dib8000_write_word(state, 0, (mode << 13) | seg_diff_mask);
  1201. if ((seg_diff_mask) || (state->fe[0]->dtv_property_cache.isdbt_sb_mode))
  1202. dib8000_write_word(state, 268, (dib8000_read_word(state, 268) & 0xF9FF) | 0x0200);
  1203. else
  1204. dib8000_write_word(state, 268, (2 << 9) | 39); //init value
  1205. // ---- SMALL ----
  1206. // P_small_seg_diff
  1207. dib8000_write_word(state, 352, seg_diff_mask); // ADDR 352
  1208. dib8000_write_word(state, 353, seg_mask13); // ADDR 353
  1209. /* // P_small_narrow_band=0, P_small_last_seg=13, P_small_offset_num_car=5 */
  1210. // ---- SMALL ----
  1211. if (state->fe[0]->dtv_property_cache.isdbt_sb_mode == 1) {
  1212. switch (state->fe[0]->dtv_property_cache.transmission_mode) {
  1213. case TRANSMISSION_MODE_2K:
  1214. if (state->fe[0]->dtv_property_cache.isdbt_partial_reception == 0) {
  1215. if (state->fe[0]->dtv_property_cache.layer[0].modulation == DQPSK)
  1216. ncoeff = coeff_2k_sb_1seg_dqpsk;
  1217. else // QPSK or QAM
  1218. ncoeff = coeff_2k_sb_1seg;
  1219. } else { // 3-segments
  1220. if (state->fe[0]->dtv_property_cache.layer[0].modulation == DQPSK) {
  1221. if (state->fe[0]->dtv_property_cache.layer[1].modulation == DQPSK)
  1222. ncoeff = coeff_2k_sb_3seg_0dqpsk_1dqpsk;
  1223. else // QPSK or QAM on external segments
  1224. ncoeff = coeff_2k_sb_3seg_0dqpsk;
  1225. } else { // QPSK or QAM on central segment
  1226. if (state->fe[0]->dtv_property_cache.layer[1].modulation == DQPSK)
  1227. ncoeff = coeff_2k_sb_3seg_1dqpsk;
  1228. else // QPSK or QAM on external segments
  1229. ncoeff = coeff_2k_sb_3seg;
  1230. }
  1231. }
  1232. break;
  1233. case TRANSMISSION_MODE_4K:
  1234. if (state->fe[0]->dtv_property_cache.isdbt_partial_reception == 0) {
  1235. if (state->fe[0]->dtv_property_cache.layer[0].modulation == DQPSK)
  1236. ncoeff = coeff_4k_sb_1seg_dqpsk;
  1237. else // QPSK or QAM
  1238. ncoeff = coeff_4k_sb_1seg;
  1239. } else { // 3-segments
  1240. if (state->fe[0]->dtv_property_cache.layer[0].modulation == DQPSK) {
  1241. if (state->fe[0]->dtv_property_cache.layer[1].modulation == DQPSK) {
  1242. ncoeff = coeff_4k_sb_3seg_0dqpsk_1dqpsk;
  1243. } else { // QPSK or QAM on external segments
  1244. ncoeff = coeff_4k_sb_3seg_0dqpsk;
  1245. }
  1246. } else { // QPSK or QAM on central segment
  1247. if (state->fe[0]->dtv_property_cache.layer[1].modulation == DQPSK) {
  1248. ncoeff = coeff_4k_sb_3seg_1dqpsk;
  1249. } else // QPSK or QAM on external segments
  1250. ncoeff = coeff_4k_sb_3seg;
  1251. }
  1252. }
  1253. break;
  1254. case TRANSMISSION_MODE_AUTO:
  1255. case TRANSMISSION_MODE_8K:
  1256. default:
  1257. if (state->fe[0]->dtv_property_cache.isdbt_partial_reception == 0) {
  1258. if (state->fe[0]->dtv_property_cache.layer[0].modulation == DQPSK)
  1259. ncoeff = coeff_8k_sb_1seg_dqpsk;
  1260. else // QPSK or QAM
  1261. ncoeff = coeff_8k_sb_1seg;
  1262. } else { // 3-segments
  1263. if (state->fe[0]->dtv_property_cache.layer[0].modulation == DQPSK) {
  1264. if (state->fe[0]->dtv_property_cache.layer[1].modulation == DQPSK) {
  1265. ncoeff = coeff_8k_sb_3seg_0dqpsk_1dqpsk;
  1266. } else { // QPSK or QAM on external segments
  1267. ncoeff = coeff_8k_sb_3seg_0dqpsk;
  1268. }
  1269. } else { // QPSK or QAM on central segment
  1270. if (state->fe[0]->dtv_property_cache.layer[1].modulation == DQPSK) {
  1271. ncoeff = coeff_8k_sb_3seg_1dqpsk;
  1272. } else // QPSK or QAM on external segments
  1273. ncoeff = coeff_8k_sb_3seg;
  1274. }
  1275. }
  1276. break;
  1277. }
  1278. for (i = 0; i < 8; i++)
  1279. dib8000_write_word(state, 343 + i, ncoeff[i]);
  1280. }
  1281. // P_small_coef_ext_enable=ISDB-Tsb, P_small_narrow_band=ISDB-Tsb, P_small_last_seg=13, P_small_offset_num_car=5
  1282. dib8000_write_word(state, 351,
  1283. (state->fe[0]->dtv_property_cache.isdbt_sb_mode << 9) | (state->fe[0]->dtv_property_cache.isdbt_sb_mode << 8) | (13 << 4) | 5);
  1284. // ---- COFF ----
  1285. // Carloff, the most robust
  1286. if (state->fe[0]->dtv_property_cache.isdbt_sb_mode == 1) {
  1287. // P_coff_cpil_alpha=4, P_coff_inh=0, P_coff_cpil_winlen=64
  1288. // 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
  1289. dib8000_write_word(state, 187,
  1290. (4 << 12) | (0 << 11) | (63 << 5) | (0x3 << 3) | ((~state->fe[0]->dtv_property_cache.isdbt_partial_reception & 1) << 2)
  1291. | 0x3);
  1292. /* // P_small_coef_ext_enable = 1 */
  1293. /* dib8000_write_word(state, 351, dib8000_read_word(state, 351) | 0x200); */
  1294. if (state->fe[0]->dtv_property_cache.isdbt_partial_reception == 0) {
  1295. // 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)
  1296. if (mode == 3)
  1297. dib8000_write_word(state, 180, 0x1fcf | ((mode - 1) << 14));
  1298. else
  1299. dib8000_write_word(state, 180, 0x0fcf | ((mode - 1) << 14));
  1300. // P_ctrl_corm_thres4pre_freq_inh=1,P_ctrl_pre_freq_mode_sat=1,
  1301. // P_ctrl_pre_freq_inh=0, P_ctrl_pre_freq_step = 5, P_pre_freq_win_len=4
  1302. dib8000_write_word(state, 338, (1 << 12) | (1 << 10) | (0 << 9) | (5 << 5) | 4);
  1303. // P_ctrl_pre_freq_win_len=16, P_ctrl_pre_freq_thres_lockin=8
  1304. dib8000_write_word(state, 340, (16 << 6) | (8 << 0));
  1305. // P_ctrl_pre_freq_thres_lockout=6, P_small_use_tmcc/ac/cp=1
  1306. dib8000_write_word(state, 341, (6 << 3) | (1 << 2) | (1 << 1) | (1 << 0));
  1307. // P_coff_corthres_8k, 4k, 2k and P_coff_cpilthres_8k, 4k, 2k
  1308. dib8000_write_word(state, 181, 300);
  1309. dib8000_write_word(state, 182, 150);
  1310. dib8000_write_word(state, 183, 80);
  1311. dib8000_write_word(state, 184, 300);
  1312. dib8000_write_word(state, 185, 150);
  1313. dib8000_write_word(state, 186, 80);
  1314. } else { // Sound Broadcasting mode 3 seg
  1315. // P_coff_one_seg_sym= 1, P_coff_one_seg_width= 1, P_coff_winlen=63, P_coff_thres_lock=15
  1316. /* if (mode == 3) */
  1317. /* dib8000_write_word(state, 180, 0x2fca | ((0) << 14)); */
  1318. /* else */
  1319. /* dib8000_write_word(state, 180, 0x2fca | ((1) << 14)); */
  1320. dib8000_write_word(state, 180, 0x1fcf | (1 << 14));
  1321. // P_ctrl_corm_thres4pre_freq_inh = 1, P_ctrl_pre_freq_mode_sat=1,
  1322. // P_ctrl_pre_freq_inh=0, P_ctrl_pre_freq_step = 4, P_pre_freq_win_len=4
  1323. dib8000_write_word(state, 338, (1 << 12) | (1 << 10) | (0 << 9) | (4 << 5) | 4);
  1324. // P_ctrl_pre_freq_win_len=16, P_ctrl_pre_freq_thres_lockin=8
  1325. dib8000_write_word(state, 340, (16 << 6) | (8 << 0));
  1326. //P_ctrl_pre_freq_thres_lockout=6, P_small_use_tmcc/ac/cp=1
  1327. dib8000_write_word(state, 341, (6 << 3) | (1 << 2) | (1 << 1) | (1 << 0));
  1328. // P_coff_corthres_8k, 4k, 2k and P_coff_cpilthres_8k, 4k, 2k
  1329. dib8000_write_word(state, 181, 350);
  1330. dib8000_write_word(state, 182, 300);
  1331. dib8000_write_word(state, 183, 250);
  1332. dib8000_write_word(state, 184, 350);
  1333. dib8000_write_word(state, 185, 300);
  1334. dib8000_write_word(state, 186, 250);
  1335. }
  1336. } else if (state->isdbt_cfg_loaded == 0) { // if not Sound Broadcasting mode : put default values for 13 segments
  1337. dib8000_write_word(state, 180, (16 << 6) | 9);
  1338. dib8000_write_word(state, 187, (4 << 12) | (8 << 5) | 0x2);
  1339. coff_pow = 0x2800;
  1340. for (i = 0; i < 6; i++)
  1341. dib8000_write_word(state, 181 + i, coff_pow);
  1342. // P_ctrl_corm_thres4pre_freq_inh=1, P_ctrl_pre_freq_mode_sat=1,
  1343. // 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
  1344. dib8000_write_word(state, 338, (1 << 12) | (1 << 10) | (0 << 9) | (3 << 5) | 1);
  1345. // P_ctrl_pre_freq_win_len=8, P_ctrl_pre_freq_thres_lockin=6
  1346. dib8000_write_word(state, 340, (8 << 6) | (6 << 0));
  1347. // P_ctrl_pre_freq_thres_lockout=4, P_small_use_tmcc/ac/cp=1
  1348. dib8000_write_word(state, 341, (4 << 3) | (1 << 2) | (1 << 1) | (1 << 0));
  1349. }
  1350. // ---- FFT ----
  1351. if (state->fe[0]->dtv_property_cache.isdbt_sb_mode == 1 && state->fe[0]->dtv_property_cache.isdbt_partial_reception == 0)
  1352. dib8000_write_word(state, 178, 64); // P_fft_powrange=64
  1353. else
  1354. dib8000_write_word(state, 178, 32); // P_fft_powrange=32
  1355. /* make the cpil_coff_lock more robust but slower p_coff_winlen
  1356. * 6bits; p_coff_thres_lock 6bits (for coff lock if needed)
  1357. */
  1358. /* if ( ( nbseg_diff>0)&&(nbseg_diff<13))
  1359. dib8000_write_word(state, 187, (dib8000_read_word(state, 187) & 0xfffb) | (1 << 3)); */
  1360. dib8000_write_word(state, 189, ~seg_mask13 | seg_diff_mask); /* P_lmod4_seg_inh */
  1361. dib8000_write_word(state, 192, ~seg_mask13 | seg_diff_mask); /* P_pha3_seg_inh */
  1362. dib8000_write_word(state, 225, ~seg_mask13 | seg_diff_mask); /* P_tac_seg_inh */
  1363. if ((!state->fe[0]->dtv_property_cache.isdbt_sb_mode) && (state->cfg.pll->ifreq == 0))
  1364. dib8000_write_word(state, 266, ~seg_mask13 | seg_diff_mask | 0x40); /* P_equal_noise_seg_inh */
  1365. else
  1366. dib8000_write_word(state, 266, ~seg_mask13 | seg_diff_mask); /* P_equal_noise_seg_inh */
  1367. dib8000_write_word(state, 287, ~seg_mask13 | 0x1000); /* P_tmcc_seg_inh */
  1368. //dib8000_write_word(state, 288, ~seg_mask13 | seg_diff_mask); /* P_tmcc_seg_eq_inh */
  1369. if (!autosearching)
  1370. dib8000_write_word(state, 288, (~seg_mask13 | seg_diff_mask) & 0x1fff); /* P_tmcc_seg_eq_inh */
  1371. else
  1372. dib8000_write_word(state, 288, 0x1fff); //disable equalisation of the tmcc when autosearch to be able to find the DQPSK channels.
  1373. dprintk("287 = %X (%d)", ~seg_mask13 | 0x1000, ~seg_mask13 | 0x1000);
  1374. dib8000_write_word(state, 211, seg_mask13 & (~seg_diff_mask)); /* P_des_seg_enabled */
  1375. /* offset loop parameters */
  1376. if (state->fe[0]->dtv_property_cache.isdbt_sb_mode == 1) {
  1377. if (state->fe[0]->dtv_property_cache.isdbt_partial_reception == 0)
  1378. /* P_timf_alpha = (11-P_mode), P_corm_alpha=6, P_corm_thres=0x80 */
  1379. dib8000_write_word(state, 32, ((11 - mode) << 12) | (6 << 8) | 0x40);
  1380. else // Sound Broadcasting mode 3 seg
  1381. /* P_timf_alpha = (10-P_mode), P_corm_alpha=6, P_corm_thres=0x80 */
  1382. dib8000_write_word(state, 32, ((10 - mode) << 12) | (6 << 8) | 0x60);
  1383. } else
  1384. // TODO in 13 seg, timf_alpha can always be the same or not ?
  1385. /* P_timf_alpha = (9-P_mode, P_corm_alpha=6, P_corm_thres=0x80 */
  1386. dib8000_write_word(state, 32, ((9 - mode) << 12) | (6 << 8) | 0x80);
  1387. if (state->fe[0]->dtv_property_cache.isdbt_sb_mode == 1) {
  1388. if (state->fe[0]->dtv_property_cache.isdbt_partial_reception == 0)
  1389. /* P_ctrl_pha_off_max=3 P_ctrl_sfreq_inh =0 P_ctrl_sfreq_step = (11-P_mode) */
  1390. dib8000_write_word(state, 37, (3 << 5) | (0 << 4) | (10 - mode));
  1391. else // Sound Broadcasting mode 3 seg
  1392. /* P_ctrl_pha_off_max=3 P_ctrl_sfreq_inh =0 P_ctrl_sfreq_step = (10-P_mode) */
  1393. dib8000_write_word(state, 37, (3 << 5) | (0 << 4) | (9 - mode));
  1394. } else
  1395. /* P_ctrl_pha_off_max=3 P_ctrl_sfreq_inh =0 P_ctrl_sfreq_step = 9 */
  1396. dib8000_write_word(state, 37, (3 << 5) | (0 << 4) | (8 - mode));
  1397. /* P_dvsy_sync_wait - reuse mode */
  1398. switch (state->fe[0]->dtv_property_cache.transmission_mode) {
  1399. case TRANSMISSION_MODE_8K:
  1400. mode = 256;
  1401. break;
  1402. case TRANSMISSION_MODE_4K:
  1403. mode = 128;
  1404. break;
  1405. default:
  1406. case TRANSMISSION_MODE_2K:
  1407. mode = 64;
  1408. break;
  1409. }
  1410. if (state->cfg.diversity_delay == 0)
  1411. mode = (mode * (1 << (guard)) * 3) / 2 + 48; // add 50% SFN margin + compensate for one DVSY-fifo
  1412. else
  1413. mode = (mode * (1 << (guard)) * 3) / 2 + state->cfg.diversity_delay; // add 50% SFN margin + compensate for DVSY-fifo
  1414. mode <<= 4;
  1415. dib8000_write_word(state, 273, (dib8000_read_word(state, 273) & 0x000f) | mode);
  1416. /* channel estimation fine configuration */
  1417. switch (max_constellation) {
  1418. case QAM_64:
  1419. ana_gain = 0x7; // -1 : avoid def_est saturation when ADC target is -16dB
  1420. coeff[0] = 0x0148; /* P_adp_regul_cnt 0.04 */
  1421. coeff[1] = 0xfff0; /* P_adp_noise_cnt -0.002 */
  1422. coeff[2] = 0x00a4; /* P_adp_regul_ext 0.02 */
  1423. coeff[3] = 0xfff8; /* P_adp_noise_ext -0.001 */
  1424. //if (!state->cfg.hostbus_diversity) //if diversity, we should prehaps use the configuration of the max_constallation -1
  1425. break;
  1426. case QAM_16:
  1427. ana_gain = 0x7; // -1 : avoid def_est saturation when ADC target is -16dB
  1428. coeff[0] = 0x023d; /* P_adp_regul_cnt 0.07 */
  1429. coeff[1] = 0xffdf; /* P_adp_noise_cnt -0.004 */
  1430. coeff[2] = 0x00a4; /* P_adp_regul_ext 0.02 */
  1431. coeff[3] = 0xfff0; /* P_adp_noise_ext -0.002 */
  1432. //if (!((state->cfg.hostbus_diversity) && (max_constellation == QAM_16)))
  1433. break;
  1434. default:
  1435. ana_gain = 0; // 0 : goes along with ADC target at -22dB to keep good mobile performance and lock at sensitivity level
  1436. coeff[0] = 0x099a; /* P_adp_regul_cnt 0.3 */
  1437. coeff[1] = 0xffae; /* P_adp_noise_cnt -0.01 */
  1438. coeff[2] = 0x0333; /* P_adp_regul_ext 0.1 */
  1439. coeff[3] = 0xfff8; /* P_adp_noise_ext -0.002 */
  1440. break;
  1441. }
  1442. for (mode = 0; mode < 4; mode++)
  1443. dib8000_write_word(state, 215 + mode, coeff[mode]);
  1444. // update ana_gain depending on max constellation
  1445. dib8000_write_word(state, 116, ana_gain);
  1446. // update ADC target depending on ana_gain
  1447. if (ana_gain) { // set -16dB ADC target for ana_gain=-1
  1448. for (i = 0; i < 10; i++)
  1449. dib8000_write_word(state, 80 + i, adc_target_16dB[i]);
  1450. } else { // set -22dB ADC target for ana_gain=0
  1451. for (i = 0; i < 10; i++)
  1452. dib8000_write_word(state, 80 + i, adc_target_16dB[i] - 355);
  1453. }
  1454. // ---- ANA_FE ----
  1455. if (state->fe[0]->dtv_property_cache.isdbt_sb_mode) {
  1456. if (state->fe[0]->dtv_property_cache.isdbt_partial_reception == 1)
  1457. ana_fe = ana_fe_coeff_3seg;
  1458. else // 1-segment
  1459. ana_fe = ana_fe_coeff_1seg;
  1460. } else
  1461. ana_fe = ana_fe_coeff_13seg;
  1462. if (state->fe[0]->dtv_property_cache.isdbt_sb_mode == 1 || state->isdbt_cfg_loaded == 0)
  1463. for (mode = 0; mode < 24; mode++)
  1464. dib8000_write_word(state, 117 + mode, ana_fe[mode]);
  1465. // ---- CHAN_BLK ----
  1466. for (i = 0; i < 13; i++) {
  1467. if ((((~seg_diff_mask) >> i) & 1) == 1) {
  1468. P_cfr_left_edge += (1 << i) * ((i == 0) || ((((seg_mask13 & (~seg_diff_mask)) >> (i - 1)) & 1) == 0));
  1469. P_cfr_right_edge += (1 << i) * ((i == 12) || ((((seg_mask13 & (~seg_diff_mask)) >> (i + 1)) & 1) == 0));
  1470. }
  1471. }
  1472. dib8000_write_word(state, 222, P_cfr_left_edge); // P_cfr_left_edge
  1473. dib8000_write_word(state, 223, P_cfr_right_edge); // P_cfr_right_edge
  1474. // "P_cspu_left_edge" not used => do not care
  1475. // "P_cspu_right_edge" not used => do not care
  1476. if (state->fe[0]->dtv_property_cache.isdbt_sb_mode == 1) {
  1477. dib8000_write_word(state, 228, 1); // P_2d_mode_byp=1
  1478. dib8000_write_word(state, 205, dib8000_read_word(state, 205) & 0xfff0); // P_cspu_win_cut = 0
  1479. if (state->fe[0]->dtv_property_cache.isdbt_partial_reception == 0
  1480. && state->fe[0]->dtv_property_cache.transmission_mode == TRANSMISSION_MODE_2K) {
  1481. //dib8000_write_word(state, 219, dib8000_read_word(state, 219) & 0xfffe); // P_adp_pass = 0
  1482. dib8000_write_word(state, 265, 15); // P_equal_noise_sel = 15
  1483. }
  1484. } else if (state->isdbt_cfg_loaded == 0) {
  1485. dib8000_write_word(state, 228, 0); // default value
  1486. dib8000_write_word(state, 265, 31); // default value
  1487. dib8000_write_word(state, 205, 0x200f); // init value
  1488. }
  1489. // ---- TMCC ----
  1490. for (i = 0; i < 3; i++)
  1491. tmcc_pow +=
  1492. (((state->fe[0]->dtv_property_cache.layer[i].modulation == DQPSK) * 4 + 1) * state->fe[0]->dtv_property_cache.layer[i].segment_count);
  1493. // Quantif of "P_tmcc_dec_thres_?k" is (0, 5+mode, 9);
  1494. // Threshold is set at 1/4 of max power.
  1495. tmcc_pow *= (1 << (9 - 2));
  1496. dib8000_write_word(state, 290, tmcc_pow); // P_tmcc_dec_thres_2k
  1497. dib8000_write_word(state, 291, tmcc_pow); // P_tmcc_dec_thres_4k
  1498. dib8000_write_word(state, 292, tmcc_pow); // P_tmcc_dec_thres_8k
  1499. //dib8000_write_word(state, 287, (1 << 13) | 0x1000 );
  1500. // ---- PHA3 ----
  1501. if (state->isdbt_cfg_loaded == 0)
  1502. dib8000_write_word(state, 250, 3285); /*p_2d_hspeed_thr0 */
  1503. if (state->fe[0]->dtv_property_cache.isdbt_sb_mode == 1)
  1504. state->isdbt_cfg_loaded = 0;
  1505. else
  1506. state->isdbt_cfg_loaded = 1;
  1507. }
  1508. static int dib8000_autosearch_start(struct dvb_frontend *fe)
  1509. {
  1510. u8 factor;
  1511. u32 value;
  1512. struct dib8000_state *state = fe->demodulator_priv;
  1513. int slist = 0;
  1514. state->fe[0]->dtv_property_cache.inversion = 0;
  1515. if (!state->fe[0]->dtv_property_cache.isdbt_sb_mode)
  1516. state->fe[0]->dtv_property_cache.layer[0].segment_count = 13;
  1517. state->fe[0]->dtv_property_cache.layer[0].modulation = QAM_64;
  1518. state->fe[0]->dtv_property_cache.layer[0].fec = FEC_2_3;
  1519. state->fe[0]->dtv_property_cache.layer[0].interleaving = 0;
  1520. //choose the right list, in sb, always do everything
  1521. if (state->fe[0]->dtv_property_cache.isdbt_sb_mode) {
  1522. state->fe[0]->dtv_property_cache.transmission_mode = TRANSMISSION_MODE_8K;
  1523. state->fe[0]->dtv_property_cache.guard_interval = GUARD_INTERVAL_1_8;
  1524. slist = 7;
  1525. dib8000_write_word(state, 0, (dib8000_read_word(state, 0) & 0x9fff) | (1 << 13));
  1526. } else {
  1527. if (state->fe[0]->dtv_property_cache.guard_interval == GUARD_INTERVAL_AUTO) {
  1528. if (state->fe[0]->dtv_property_cache.transmission_mode == TRANSMISSION_MODE_AUTO) {
  1529. slist = 7;
  1530. dib8000_write_word(state, 0, (dib8000_read_word(state, 0) & 0x9fff) | (1 << 13)); // P_mode = 1 to have autosearch start ok with mode2
  1531. } else
  1532. slist = 3;
  1533. } else {
  1534. if (state->fe[0]->dtv_property_cache.transmission_mode == TRANSMISSION_MODE_AUTO) {
  1535. slist = 2;
  1536. dib8000_write_word(state, 0, (dib8000_read_word(state, 0) & 0x9fff) | (1 << 13)); // P_mode = 1
  1537. } else
  1538. slist = 0;
  1539. }
  1540. if (state->fe[0]->dtv_property_cache.transmission_mode == TRANSMISSION_MODE_AUTO)
  1541. state->fe[0]->dtv_property_cache.transmission_mode = TRANSMISSION_MODE_8K;
  1542. if (state->fe[0]->dtv_property_cache.guard_interval == GUARD_INTERVAL_AUTO)
  1543. state->fe[0]->dtv_property_cache.guard_interval = GUARD_INTERVAL_1_8;
  1544. dprintk("using list for autosearch : %d", slist);
  1545. dib8000_set_channel(state, (unsigned char)slist, 1);
  1546. //dib8000_write_word(state, 0, (dib8000_read_word(state, 0) & 0x9fff) | (1 << 13)); // P_mode = 1
  1547. factor = 1;
  1548. //set lock_mask values
  1549. dib8000_write_word(state, 6, 0x4);
  1550. dib8000_write_word(state, 7, 0x8);
  1551. dib8000_write_word(state, 8, 0x1000);
  1552. //set lock_mask wait time values
  1553. value = 50 * state->cfg.pll->internal * factor;
  1554. dib8000_write_word(state, 11, (u16) ((value >> 16) & 0xffff)); // lock0 wait time
  1555. dib8000_write_word(state, 12, (u16) (value & 0xffff)); // lock0 wait time
  1556. value = 100 * state->cfg.pll->internal * factor;
  1557. dib8000_write_word(state, 13, (u16) ((value >> 16) & 0xffff)); // lock1 wait time
  1558. dib8000_write_word(state, 14, (u16) (value & 0xffff)); // lock1 wait time
  1559. value = 1000 * state->cfg.pll->internal * factor;
  1560. dib8000_write_word(state, 15, (u16) ((value >> 16) & 0xffff)); // lock2 wait time
  1561. dib8000_write_word(state, 16, (u16) (value & 0xffff)); // lock2 wait time
  1562. value = dib8000_read_word(state, 0);
  1563. dib8000_write_word(state, 0, (u16) ((1 << 15) | value));
  1564. dib8000_read_word(state, 1284); // reset the INT. n_irq_pending
  1565. dib8000_write_word(state, 0, (u16) value);
  1566. }
  1567. return 0;
  1568. }
  1569. static int dib8000_autosearch_irq(struct dvb_frontend *fe)
  1570. {
  1571. struct dib8000_state *state = fe->demodulator_priv;
  1572. u16 irq_pending = dib8000_read_word(state, 1284);
  1573. if (irq_pending & 0x1) { // failed
  1574. dprintk("dib8000_autosearch_irq failed");
  1575. return 1;
  1576. }
  1577. if (irq_pending & 0x2) { // succeeded
  1578. dprintk("dib8000_autosearch_irq succeeded");
  1579. return 2;
  1580. }
  1581. return 0; // still pending
  1582. }
  1583. static int dib8000_tune(struct dvb_frontend *fe)
  1584. {
  1585. struct dib8000_state *state = fe->demodulator_priv;
  1586. int ret = 0;
  1587. u16 value, mode = fft_to_mode(state);
  1588. // we are already tuned - just resuming from suspend
  1589. if (state == NULL)
  1590. return -EINVAL;
  1591. dib8000_set_bandwidth(fe, state->fe[0]->dtv_property_cache.bandwidth_hz / 1000);
  1592. dib8000_set_channel(state, 0, 0);
  1593. // restart demod
  1594. ret |= dib8000_write_word(state, 770, 0x4000);
  1595. ret |= dib8000_write_word(state, 770, 0x0000);
  1596. msleep(45);
  1597. /* P_ctrl_inh_cor=0, P_ctrl_alpha_cor=4, P_ctrl_inh_isi=0, P_ctrl_alpha_isi=3 */
  1598. /* ret |= dib8000_write_word(state, 29, (0 << 9) | (4 << 5) | (0 << 4) | (3 << 0) ); workaround inh_isi stays at 1 */
  1599. // never achieved a lock before - wait for timfreq to update
  1600. if (state->timf == 0) {
  1601. if (state->fe[0]->dtv_property_cache.isdbt_sb_mode == 1) {
  1602. if (state->fe[0]->dtv_property_cache.isdbt_partial_reception == 0)
  1603. msleep(300);
  1604. else // Sound Broadcasting mode 3 seg
  1605. msleep(500);
  1606. } else // 13 seg
  1607. msleep(200);
  1608. }
  1609. if (state->fe[0]->dtv_property_cache.isdbt_sb_mode == 1) {
  1610. if (state->fe[0]->dtv_property_cache.isdbt_partial_reception == 0) {
  1611. /* P_timf_alpha = (13-P_mode) , P_corm_alpha=6, P_corm_thres=0x40 alpha to check on board */
  1612. dib8000_write_word(state, 32, ((13 - mode) << 12) | (6 << 8) | 0x40);
  1613. //dib8000_write_word(state, 32, (8 << 12) | (6 << 8) | 0x80);
  1614. /* P_ctrl_sfreq_step= (12-P_mode) P_ctrl_sfreq_inh =0 P_ctrl_pha_off_max */
  1615. ret |= dib8000_write_word(state, 37, (12 - mode) | ((5 + mode) << 5));
  1616. } else { // Sound Broadcasting mode 3 seg
  1617. /* P_timf_alpha = (12-P_mode) , P_corm_alpha=6, P_corm_thres=0x60 alpha to check on board */
  1618. dib8000_write_word(state, 32, ((12 - mode) << 12) | (6 << 8) | 0x60);
  1619. ret |= dib8000_write_word(state, 37, (11 - mode) | ((5 + mode) << 5));
  1620. }
  1621. } else { // 13 seg
  1622. /* P_timf_alpha = 8 , P_corm_alpha=6, P_corm_thres=0x80 alpha to check on board */
  1623. dib8000_write_word(state, 32, ((11 - mode) << 12) | (6 << 8) | 0x80);
  1624. ret |= dib8000_write_word(state, 37, (10 - mode) | ((5 + mode) << 5));
  1625. }
  1626. // we achieved a coff_cpil_lock - it's time to update the timf
  1627. if ((dib8000_read_word(state, 568) >> 11) & 0x1)
  1628. dib8000_update_timf(state);
  1629. //now that tune is finished, lock0 should lock on fec_mpeg to output this lock on MP_LOCK. It's changed in autosearch start
  1630. dib8000_write_word(state, 6, 0x200);
  1631. if (state->revision == 0x8002) {
  1632. value = dib8000_read_word(state, 903);
  1633. dib8000_write_word(state, 903, value & ~(1 << 3));
  1634. msleep(1);
  1635. dib8000_write_word(state, 903, value | (1 << 3));
  1636. }
  1637. return ret;
  1638. }
  1639. static int dib8000_wakeup(struct dvb_frontend *fe)
  1640. {
  1641. struct dib8000_state *state = fe->demodulator_priv;
  1642. u8 index_frontend;
  1643. int ret;
  1644. dib8000_set_power_mode(state, DIB8000M_POWER_ALL);
  1645. dib8000_set_adc_state(state, DIBX000_ADC_ON);
  1646. if (dib8000_set_adc_state(state, DIBX000_SLOW_ADC_ON) != 0)
  1647. dprintk("could not start Slow ADC");
  1648. for (index_frontend = 1; (index_frontend < MAX_NUMBER_OF_FRONTENDS) && (state->fe[index_frontend] != NULL); index_frontend++) {
  1649. ret = state->fe[index_frontend]->ops.init(state->fe[index_frontend]);
  1650. if (ret < 0)
  1651. return ret;
  1652. }
  1653. return 0;
  1654. }
  1655. static int dib8000_sleep(struct dvb_frontend *fe)
  1656. {
  1657. struct dib8000_state *state = fe->demodulator_priv;
  1658. u8 index_frontend;
  1659. int ret;
  1660. for (index_frontend = 1; (index_frontend < MAX_NUMBER_OF_FRONTENDS) && (state->fe[index_frontend] != NULL); index_frontend++) {
  1661. ret = state->fe[index_frontend]->ops.sleep(state->fe[index_frontend]);
  1662. if (ret < 0)
  1663. return ret;
  1664. }
  1665. dib8000_set_output_mode(fe, OUTMODE_HIGH_Z);
  1666. dib8000_set_power_mode(state, DIB8000M_POWER_INTERFACE_ONLY);
  1667. return dib8000_set_adc_state(state, DIBX000_SLOW_ADC_OFF) | dib8000_set_adc_state(state, DIBX000_ADC_OFF);
  1668. }
  1669. enum frontend_tune_state dib8000_get_tune_state(struct dvb_frontend *fe)
  1670. {
  1671. struct dib8000_state *state = fe->demodulator_priv;
  1672. return state->tune_state;
  1673. }
  1674. EXPORT_SYMBOL(dib8000_get_tune_state);
  1675. int dib8000_set_tune_state(struct dvb_frontend *fe, enum frontend_tune_state tune_state)
  1676. {
  1677. struct dib8000_state *state = fe->demodulator_priv;
  1678. state->tune_state = tune_state;
  1679. return 0;
  1680. }
  1681. EXPORT_SYMBOL(dib8000_set_tune_state);
  1682. static int dib8000_get_frontend(struct dvb_frontend *fe, struct dvb_frontend_parameters *fep)
  1683. {
  1684. struct dib8000_state *state = fe->demodulator_priv;
  1685. u16 i, val = 0;
  1686. fe_status_t stat;
  1687. u8 index_frontend, sub_index_frontend;
  1688. fe->dtv_property_cache.bandwidth_hz = 6000000;
  1689. for (index_frontend = 1; (index_frontend < MAX_NUMBER_OF_FRONTENDS) && (state->fe[index_frontend] != NULL); index_frontend++) {
  1690. state->fe[index_frontend]->ops.read_status(state->fe[index_frontend], &stat);
  1691. if (stat&FE_HAS_SYNC) {
  1692. dprintk("TMCC lock on the slave%i", index_frontend);
  1693. /* synchronize the cache with the other frontends */
  1694. state->fe[index_frontend]->ops.get_frontend(state->fe[index_frontend], fep);
  1695. for (sub_index_frontend = 0; (sub_index_frontend < MAX_NUMBER_OF_FRONTENDS) && (state->fe[sub_index_frontend] != NULL); sub_index_frontend++) {
  1696. if (sub_index_frontend != index_frontend) {
  1697. state->fe[sub_index_frontend]->dtv_property_cache.isdbt_sb_mode = state->fe[index_frontend]->dtv_property_cache.isdbt_sb_mode;
  1698. state->fe[sub_index_frontend]->dtv_property_cache.inversion = state->fe[index_frontend]->dtv_property_cache.inversion;
  1699. state->fe[sub_index_frontend]->dtv_property_cache.transmission_mode = state->fe[index_frontend]->dtv_property_cache.transmission_mode;
  1700. state->fe[sub_index_frontend]->dtv_property_cache.guard_interval = state->fe[index_frontend]->dtv_property_cache.guard_interval;
  1701. state->fe[sub_index_frontend]->dtv_property_cache.isdbt_partial_reception = state->fe[index_frontend]->dtv_property_cache.isdbt_partial_reception;
  1702. for (i = 0; i < 3; i++) {
  1703. state->fe[sub_index_frontend]->dtv_property_cache.layer[i].segment_count = state->fe[index_frontend]->dtv_property_cache.layer[i].segment_count;
  1704. state->fe[sub_index_frontend]->dtv_property_cache.layer[i].interleaving = state->fe[index_frontend]->dtv_property_cache.layer[i].interleaving;
  1705. state->fe[sub_index_frontend]->dtv_property_cache.layer[i].fec = state->fe[index_frontend]->dtv_property_cache.layer[i].fec;
  1706. state->fe[sub_index_frontend]->dtv_property_cache.layer[i].modulation = state->fe[index_frontend]->dtv_property_cache.layer[i].modulation;
  1707. }
  1708. }
  1709. }
  1710. return 0;
  1711. }
  1712. }
  1713. fe->dtv_property_cache.isdbt_sb_mode = dib8000_read_word(state, 508) & 0x1;
  1714. val = dib8000_read_word(state, 570);
  1715. fe->dtv_property_cache.inversion = (val & 0x40) >> 6;
  1716. switch ((val & 0x30) >> 4) {
  1717. case 1:
  1718. fe->dtv_property_cache.transmission_mode = TRANSMISSION_MODE_2K;
  1719. break;
  1720. case 3:
  1721. default:
  1722. fe->dtv_property_cache.transmission_mode = TRANSMISSION_MODE_8K;
  1723. break;
  1724. }
  1725. switch (val & 0x3) {
  1726. case 0:
  1727. fe->dtv_property_cache.guard_interval = GUARD_INTERVAL_1_32;
  1728. dprintk("dib8000_get_frontend GI = 1/32 ");
  1729. break;
  1730. case 1:
  1731. fe->dtv_property_cache.guard_interval = GUARD_INTERVAL_1_16;
  1732. dprintk("dib8000_get_frontend GI = 1/16 ");
  1733. break;
  1734. case 2:
  1735. dprintk("dib8000_get_frontend GI = 1/8 ");
  1736. fe->dtv_property_cache.guard_interval = GUARD_INTERVAL_1_8;
  1737. break;
  1738. case 3:
  1739. dprintk("dib8000_get_frontend GI = 1/4 ");
  1740. fe->dtv_property_cache.guard_interval = GUARD_INTERVAL_1_4;
  1741. break;
  1742. }
  1743. val = dib8000_read_word(state, 505);
  1744. fe->dtv_property_cache.isdbt_partial_reception = val & 1;
  1745. dprintk("dib8000_get_frontend : partial_reception = %d ", fe->dtv_property_cache.isdbt_partial_reception);
  1746. for (i = 0; i < 3; i++) {
  1747. val = dib8000_read_word(state, 493 + i);
  1748. fe->dtv_property_cache.layer[i].segment_count = val & 0x0F;
  1749. dprintk("dib8000_get_frontend : Layer %d segments = %d ", i, fe->dtv_property_cache.layer[i].segment_count);
  1750. val = dib8000_read_word(state, 499 + i);
  1751. fe->dtv_property_cache.layer[i].interleaving = val & 0x3;
  1752. dprintk("dib8000_get_frontend : Layer %d time_intlv = %d ", i, fe->dtv_property_cache.layer[i].interleaving);
  1753. val = dib8000_read_word(state, 481 + i);
  1754. switch (val & 0x7) {
  1755. case 1:
  1756. fe->dtv_property_cache.layer[i].fec = FEC_1_2;
  1757. dprintk("dib8000_get_frontend : Layer %d Code Rate = 1/2 ", i);
  1758. break;
  1759. case 2:
  1760. fe->dtv_property_cache.layer[i].fec = FEC_2_3;
  1761. dprintk("dib8000_get_frontend : Layer %d Code Rate = 2/3 ", i);
  1762. break;
  1763. case 3:
  1764. fe->dtv_property_cache.layer[i].fec = FEC_3_4;
  1765. dprintk("dib8000_get_frontend : Layer %d Code Rate = 3/4 ", i);
  1766. break;
  1767. case 5:
  1768. fe->dtv_property_cache.layer[i].fec = FEC_5_6;
  1769. dprintk("dib8000_get_frontend : Layer %d Code Rate = 5/6 ", i);
  1770. break;
  1771. default:
  1772. fe->dtv_property_cache.layer[i].fec = FEC_7_8;
  1773. dprintk("dib8000_get_frontend : Layer %d Code Rate = 7/8 ", i);
  1774. break;
  1775. }
  1776. val = dib8000_read_word(state, 487 + i);
  1777. switch (val & 0x3) {
  1778. case 0:
  1779. dprintk("dib8000_get_frontend : Layer %d DQPSK ", i);
  1780. fe->dtv_property_cache.layer[i].modulation = DQPSK;
  1781. break;
  1782. case 1:
  1783. fe->dtv_property_cache.layer[i].modulation = QPSK;
  1784. dprintk("dib8000_get_frontend : Layer %d QPSK ", i);
  1785. break;
  1786. case 2:
  1787. fe->dtv_property_cache.layer[i].modulation = QAM_16;
  1788. dprintk("dib8000_get_frontend : Layer %d QAM16 ", i);
  1789. break;
  1790. case 3:
  1791. default:
  1792. dprintk("dib8000_get_frontend : Layer %d QAM64 ", i);
  1793. fe->dtv_property_cache.layer[i].modulation = QAM_64;
  1794. break;
  1795. }
  1796. }
  1797. /* synchronize the cache with the other frontends */
  1798. for (index_frontend = 1; (index_frontend < MAX_NUMBER_OF_FRONTENDS) && (state->fe[index_frontend] != NULL); index_frontend++) {
  1799. state->fe[index_frontend]->dtv_property_cache.isdbt_sb_mode = fe->dtv_property_cache.isdbt_sb_mode;
  1800. state->fe[index_frontend]->dtv_property_cache.inversion = fe->dtv_property_cache.inversion;
  1801. state->fe[index_frontend]->dtv_property_cache.transmission_mode = fe->dtv_property_cache.transmission_mode;
  1802. state->fe[index_frontend]->dtv_property_cache.guard_interval = fe->dtv_property_cache.guard_interval;
  1803. state->fe[index_frontend]->dtv_property_cache.isdbt_partial_reception = fe->dtv_property_cache.isdbt_partial_reception;
  1804. for (i = 0; i < 3; i++) {
  1805. state->fe[index_frontend]->dtv_property_cache.layer[i].segment_count = fe->dtv_property_cache.layer[i].segment_count;
  1806. state->fe[index_frontend]->dtv_property_cache.layer[i].interleaving = fe->dtv_property_cache.layer[i].interleaving;
  1807. state->fe[index_frontend]->dtv_property_cache.layer[i].fec = fe->dtv_property_cache.layer[i].fec;
  1808. state->fe[index_frontend]->dtv_property_cache.layer[i].modulation = fe->dtv_property_cache.layer[i].modulation;
  1809. }
  1810. }
  1811. return 0;
  1812. }
  1813. static int dib8000_set_frontend(struct dvb_frontend *fe, struct dvb_frontend_parameters *fep)
  1814. {
  1815. struct dib8000_state *state = fe->demodulator_priv;
  1816. u8 nbr_pending, exit_condition, index_frontend;
  1817. s8 index_frontend_success = -1;
  1818. int time, ret;
  1819. int time_slave = FE_CALLBACK_TIME_NEVER;
  1820. if (state->fe[0]->dtv_property_cache.frequency == 0) {
  1821. dprintk("dib8000: must at least specify frequency ");
  1822. return 0;
  1823. }
  1824. if (state->fe[0]->dtv_property_cache.bandwidth_hz == 0) {
  1825. dprintk("dib8000: no bandwidth specified, set to default ");
  1826. state->fe[0]->dtv_property_cache.bandwidth_hz = 6000000;
  1827. }
  1828. for (index_frontend = 0; (index_frontend < MAX_NUMBER_OF_FRONTENDS) && (state->fe[index_frontend] != NULL); index_frontend++) {
  1829. /* synchronization of the cache */
  1830. state->fe[index_frontend]->dtv_property_cache.delivery_system = SYS_ISDBT;
  1831. memcpy(&state->fe[index_frontend]->dtv_property_cache, &fe->dtv_property_cache, sizeof(struct dtv_frontend_properties));
  1832. dib8000_set_output_mode(state->fe[index_frontend], OUTMODE_HIGH_Z);
  1833. if (state->fe[index_frontend]->ops.tuner_ops.set_params)
  1834. state->fe[index_frontend]->ops.tuner_ops.set_params(state->fe[index_frontend], fep);
  1835. dib8000_set_tune_state(state->fe[index_frontend], CT_AGC_START);
  1836. }
  1837. /* start up the AGC */
  1838. do {
  1839. time = dib8000_agc_startup(state->fe[0]);
  1840. for (index_frontend = 1; (index_frontend < MAX_NUMBER_OF_FRONTENDS) && (state->fe[index_frontend] != NULL); index_frontend++) {
  1841. time_slave = dib8000_agc_startup(state->fe[index_frontend]);
  1842. if (time == FE_CALLBACK_TIME_NEVER)
  1843. time = time_slave;
  1844. else if ((time_slave != FE_CALLBACK_TIME_NEVER) && (time_slave > time))
  1845. time = time_slave;
  1846. }
  1847. if (time != FE_CALLBACK_TIME_NEVER)
  1848. msleep(time / 10);
  1849. else
  1850. break;
  1851. exit_condition = 1;
  1852. for (index_frontend = 0; (index_frontend < MAX_NUMBER_OF_FRONTENDS) && (state->fe[index_frontend] != NULL); index_frontend++) {
  1853. if (dib8000_get_tune_state(state->fe[index_frontend]) != CT_AGC_STOP) {
  1854. exit_condition = 0;
  1855. break;
  1856. }
  1857. }
  1858. } while (exit_condition == 0);
  1859. for (index_frontend = 0; (index_frontend < MAX_NUMBER_OF_FRONTENDS) && (state->fe[index_frontend] != NULL); index_frontend++)
  1860. dib8000_set_tune_state(state->fe[index_frontend], CT_DEMOD_START);
  1861. if ((state->fe[0]->dtv_property_cache.delivery_system != SYS_ISDBT) ||
  1862. (state->fe[0]->dtv_property_cache.inversion == INVERSION_AUTO) ||
  1863. (state->fe[0]->dtv_property_cache.transmission_mode == TRANSMISSION_MODE_AUTO) ||
  1864. (state->fe[0]->dtv_property_cache.guard_interval == GUARD_INTERVAL_AUTO) ||
  1865. (((state->fe[0]->dtv_property_cache.isdbt_layer_enabled & (1 << 0)) != 0) &&
  1866. (state->fe[0]->dtv_property_cache.layer[0].segment_count != 0xff) &&
  1867. (state->fe[0]->dtv_property_cache.layer[0].segment_count != 0) &&
  1868. ((state->fe[0]->dtv_property_cache.layer[0].modulation == QAM_AUTO) ||
  1869. (state->fe[0]->dtv_property_cache.layer[0].fec == FEC_AUTO))) ||
  1870. (((state->fe[0]->dtv_property_cache.isdbt_layer_enabled & (1 << 1)) != 0) &&
  1871. (state->fe[0]->dtv_property_cache.layer[1].segment_count != 0xff) &&
  1872. (state->fe[0]->dtv_property_cache.layer[1].segment_count != 0) &&
  1873. ((state->fe[0]->dtv_property_cache.layer[1].modulation == QAM_AUTO) ||
  1874. (state->fe[0]->dtv_property_cache.layer[1].fec == FEC_AUTO))) ||
  1875. (((state->fe[0]->dtv_property_cache.isdbt_layer_enabled & (1 << 2)) != 0) &&
  1876. (state->fe[0]->dtv_property_cache.layer[2].segment_count != 0xff) &&
  1877. (state->fe[0]->dtv_property_cache.layer[2].segment_count != 0) &&
  1878. ((state->fe[0]->dtv_property_cache.layer[2].modulation == QAM_AUTO) ||
  1879. (state->fe[0]->dtv_property_cache.layer[2].fec == FEC_AUTO))) ||
  1880. (((state->fe[0]->dtv_property_cache.layer[0].segment_count == 0) ||
  1881. ((state->fe[0]->dtv_property_cache.isdbt_layer_enabled & (1 << 0)) == 0)) &&
  1882. ((state->fe[0]->dtv_property_cache.layer[1].segment_count == 0) ||
  1883. ((state->fe[0]->dtv_property_cache.isdbt_layer_enabled & (2 << 0)) == 0)) &&
  1884. ((state->fe[0]->dtv_property_cache.layer[2].segment_count == 0) || ((state->fe[0]->dtv_property_cache.isdbt_layer_enabled & (3 << 0)) == 0)))) {
  1885. int i = 80000;
  1886. u8 found = 0;
  1887. u8 tune_failed = 0;
  1888. for (index_frontend = 0; (index_frontend < MAX_NUMBER_OF_FRONTENDS) && (state->fe[index_frontend] != NULL); index_frontend++) {
  1889. dib8000_set_bandwidth(state->fe[index_frontend], fe->dtv_property_cache.bandwidth_hz / 1000);
  1890. dib8000_autosearch_start(state->fe[index_frontend]);
  1891. }
  1892. do {
  1893. msleep(20);
  1894. nbr_pending = 0;
  1895. exit_condition = 0; /* 0: tune pending; 1: tune failed; 2:tune success */
  1896. for (index_frontend = 0; (index_frontend < MAX_NUMBER_OF_FRONTENDS) && (state->fe[index_frontend] != NULL); index_frontend++) {
  1897. if (((tune_failed >> index_frontend) & 0x1) == 0) {
  1898. found = dib8000_autosearch_irq(state->fe[index_frontend]);
  1899. switch (found) {
  1900. case 0: /* tune pending */
  1901. nbr_pending++;
  1902. break;
  1903. case 2:
  1904. dprintk("autosearch succeed on the frontend%i", index_frontend);
  1905. exit_condition = 2;
  1906. index_frontend_success = index_frontend;
  1907. break;
  1908. default:
  1909. dprintk("unhandled autosearch result");
  1910. case 1:
  1911. dprintk("autosearch failed for the frontend%i", index_frontend);
  1912. break;
  1913. }
  1914. }
  1915. }
  1916. /* if all tune are done and no success, exit: tune failed */
  1917. if ((nbr_pending == 0) && (exit_condition == 0))
  1918. exit_condition = 1;
  1919. } while ((exit_condition == 0) && i--);
  1920. if (exit_condition == 1) { /* tune failed */
  1921. dprintk("tune failed");
  1922. return 0;
  1923. }
  1924. dprintk("tune success on frontend%i", index_frontend_success);
  1925. dib8000_get_frontend(fe, fep);
  1926. }
  1927. for (index_frontend = 0, ret = 0; (ret >= 0) && (index_frontend < MAX_NUMBER_OF_FRONTENDS) && (state->fe[index_frontend] != NULL); index_frontend++)
  1928. ret = dib8000_tune(state->fe[index_frontend]);
  1929. /* set output mode and diversity input */
  1930. dib8000_set_output_mode(state->fe[0], state->cfg.output_mode);
  1931. for (index_frontend = 1; (index_frontend < MAX_NUMBER_OF_FRONTENDS) && (state->fe[index_frontend] != NULL); index_frontend++) {
  1932. dib8000_set_output_mode(state->fe[index_frontend], OUTMODE_DIVERSITY);
  1933. dib8000_set_diversity_in(state->fe[index_frontend-1], 1);
  1934. }
  1935. /* turn off the diversity of the last chip */
  1936. dib8000_set_diversity_in(state->fe[index_frontend-1], 0);
  1937. return ret;
  1938. }
  1939. static u16 dib8000_read_lock(struct dvb_frontend *fe)
  1940. {
  1941. struct dib8000_state *state = fe->demodulator_priv;
  1942. return dib8000_read_word(state, 568);
  1943. }
  1944. static int dib8000_read_status(struct dvb_frontend *fe, fe_status_t * stat)
  1945. {
  1946. struct dib8000_state *state = fe->demodulator_priv;
  1947. u16 lock_slave = 0, lock = dib8000_read_word(state, 568);
  1948. u8 index_frontend;
  1949. for (index_frontend = 1; (index_frontend < MAX_NUMBER_OF_FRONTENDS) && (state->fe[index_frontend] != NULL); index_frontend++)
  1950. lock_slave |= dib8000_read_lock(state->fe[index_frontend]);
  1951. *stat = 0;
  1952. if (((lock >> 13) & 1) || ((lock_slave >> 13) & 1))
  1953. *stat |= FE_HAS_SIGNAL;
  1954. if (((lock >> 8) & 1) || ((lock_slave >> 8) & 1)) /* Equal */
  1955. *stat |= FE_HAS_CARRIER;
  1956. if ((((lock >> 1) & 0xf) == 0xf) || (((lock_slave >> 1) & 0xf) == 0xf)) /* TMCC_SYNC */
  1957. *stat |= FE_HAS_SYNC;
  1958. if ((((lock >> 12) & 1) || ((lock_slave >> 12) & 1)) && ((lock >> 5) & 7)) /* FEC MPEG */
  1959. *stat |= FE_HAS_LOCK;
  1960. if (((lock >> 12) & 1) || ((lock_slave >> 12) & 1)) {
  1961. lock = dib8000_read_word(state, 554); /* Viterbi Layer A */
  1962. if (lock & 0x01)
  1963. *stat |= FE_HAS_VITERBI;
  1964. lock = dib8000_read_word(state, 555); /* Viterbi Layer B */
  1965. if (lock & 0x01)
  1966. *stat |= FE_HAS_VITERBI;
  1967. lock = dib8000_read_word(state, 556); /* Viterbi Layer C */
  1968. if (lock & 0x01)
  1969. *stat |= FE_HAS_VITERBI;
  1970. }
  1971. return 0;
  1972. }
  1973. static int dib8000_read_ber(struct dvb_frontend *fe, u32 * ber)
  1974. {
  1975. struct dib8000_state *state = fe->demodulator_priv;
  1976. *ber = (dib8000_read_word(state, 560) << 16) | dib8000_read_word(state, 561); // 13 segments
  1977. return 0;
  1978. }
  1979. static int dib8000_read_unc_blocks(struct dvb_frontend *fe, u32 * unc)
  1980. {
  1981. struct dib8000_state *state = fe->demodulator_priv;
  1982. *unc = dib8000_read_word(state, 565); // packet error on 13 seg
  1983. return 0;
  1984. }
  1985. static int dib8000_read_signal_strength(struct dvb_frontend *fe, u16 * strength)
  1986. {
  1987. struct dib8000_state *state = fe->demodulator_priv;
  1988. u8 index_frontend;
  1989. u16 val;
  1990. *strength = 0;
  1991. for (index_frontend = 1; (index_frontend < MAX_NUMBER_OF_FRONTENDS) && (state->fe[index_frontend] != NULL); index_frontend++) {
  1992. state->fe[index_frontend]->ops.read_signal_strength(state->fe[index_frontend], &val);
  1993. if (val > 65535 - *strength)
  1994. *strength = 65535;
  1995. else
  1996. *strength += val;
  1997. }
  1998. val = 65535 - dib8000_read_word(state, 390);
  1999. if (val > 65535 - *strength)
  2000. *strength = 65535;
  2001. else
  2002. *strength += val;
  2003. return 0;
  2004. }
  2005. static u32 dib8000_get_snr(struct dvb_frontend *fe)
  2006. {
  2007. struct dib8000_state *state = fe->demodulator_priv;
  2008. u32 n, s, exp;
  2009. u16 val;
  2010. val = dib8000_read_word(state, 542);
  2011. n = (val >> 6) & 0xff;
  2012. exp = (val & 0x3f);
  2013. if ((exp & 0x20) != 0)
  2014. exp -= 0x40;
  2015. n <<= exp+16;
  2016. val = dib8000_read_word(state, 543);
  2017. s = (val >> 6) & 0xff;
  2018. exp = (val & 0x3f);
  2019. if ((exp & 0x20) != 0)
  2020. exp -= 0x40;
  2021. s <<= exp+16;
  2022. if (n > 0) {
  2023. u32 t = (s/n) << 16;
  2024. return t + ((s << 16) - n*t) / n;
  2025. }
  2026. return 0xffffffff;
  2027. }
  2028. static int dib8000_read_snr(struct dvb_frontend *fe, u16 * snr)
  2029. {
  2030. struct dib8000_state *state = fe->demodulator_priv;
  2031. u8 index_frontend;
  2032. u32 snr_master;
  2033. snr_master = dib8000_get_snr(fe);
  2034. for (index_frontend = 1; (index_frontend < MAX_NUMBER_OF_FRONTENDS) && (state->fe[index_frontend] != NULL); index_frontend++)
  2035. snr_master += dib8000_get_snr(state->fe[index_frontend]);
  2036. if (snr_master != 0) {
  2037. snr_master = 10*intlog10(snr_master>>16);
  2038. *snr = snr_master / ((1 << 24) / 10);
  2039. }
  2040. else
  2041. *snr = 0;
  2042. return 0;
  2043. }
  2044. int dib8000_set_slave_frontend(struct dvb_frontend *fe, struct dvb_frontend *fe_slave)
  2045. {
  2046. struct dib8000_state *state = fe->demodulator_priv;
  2047. u8 index_frontend = 1;
  2048. while ((index_frontend < MAX_NUMBER_OF_FRONTENDS) && (state->fe[index_frontend] != NULL))
  2049. index_frontend++;
  2050. if (index_frontend < MAX_NUMBER_OF_FRONTENDS) {
  2051. dprintk("set slave fe %p to index %i", fe_slave, index_frontend);
  2052. state->fe[index_frontend] = fe_slave;
  2053. return 0;
  2054. }
  2055. dprintk("too many slave frontend");
  2056. return -ENOMEM;
  2057. }
  2058. EXPORT_SYMBOL(dib8000_set_slave_frontend);
  2059. int dib8000_remove_slave_frontend(struct dvb_frontend *fe)
  2060. {
  2061. struct dib8000_state *state = fe->demodulator_priv;
  2062. u8 index_frontend = 1;
  2063. while ((index_frontend < MAX_NUMBER_OF_FRONTENDS) && (state->fe[index_frontend] != NULL))
  2064. index_frontend++;
  2065. if (index_frontend != 1) {
  2066. dprintk("remove slave fe %p (index %i)", state->fe[index_frontend-1], index_frontend-1);
  2067. state->fe[index_frontend] = NULL;
  2068. return 0;
  2069. }
  2070. dprintk("no frontend to be removed");
  2071. return -ENODEV;
  2072. }
  2073. EXPORT_SYMBOL(dib8000_remove_slave_frontend);
  2074. struct dvb_frontend *dib8000_get_slave_frontend(struct dvb_frontend *fe, int slave_index)
  2075. {
  2076. struct dib8000_state *state = fe->demodulator_priv;
  2077. if (slave_index >= MAX_NUMBER_OF_FRONTENDS)
  2078. return NULL;
  2079. return state->fe[slave_index];
  2080. }
  2081. EXPORT_SYMBOL(dib8000_get_slave_frontend);
  2082. int dib8000_i2c_enumeration(struct i2c_adapter *host, int no_of_demods, u8 default_addr, u8 first_addr)
  2083. {
  2084. int k = 0, ret = 0;
  2085. u8 new_addr = 0;
  2086. struct i2c_device client = {.adap = host };
  2087. client.i2c_write_buffer = kzalloc(4 * sizeof(u8), GFP_KERNEL);
  2088. if (!client.i2c_write_buffer) {
  2089. dprintk("%s: not enough memory", __func__);
  2090. return -ENOMEM;
  2091. }
  2092. client.i2c_read_buffer = kzalloc(4 * sizeof(u8), GFP_KERNEL);
  2093. if (!client.i2c_read_buffer) {
  2094. dprintk("%s: not enough memory", __func__);
  2095. ret = -ENOMEM;
  2096. goto error_memory_read;
  2097. }
  2098. client.i2c_buffer_lock = kzalloc(sizeof(struct mutex), GFP_KERNEL);
  2099. if (!client.i2c_buffer_lock) {
  2100. dprintk("%s: not enough memory", __func__);
  2101. ret = -ENOMEM;
  2102. goto error_memory_lock;
  2103. }
  2104. mutex_init(client.i2c_buffer_lock);
  2105. for (k = no_of_demods - 1; k >= 0; k--) {
  2106. /* designated i2c address */
  2107. new_addr = first_addr + (k << 1);
  2108. client.addr = new_addr;
  2109. dib8000_i2c_write16(&client, 1287, 0x0003); /* sram lead in, rdy */
  2110. if (dib8000_identify(&client) == 0) {
  2111. dib8000_i2c_write16(&client, 1287, 0x0003); /* sram lead in, rdy */
  2112. client.addr = default_addr;
  2113. if (dib8000_identify(&client) == 0) {
  2114. dprintk("#%d: not identified", k);
  2115. ret = -EINVAL;
  2116. goto error;
  2117. }
  2118. }
  2119. /* start diversity to pull_down div_str - just for i2c-enumeration */
  2120. dib8000_i2c_write16(&client, 1286, (1 << 10) | (4 << 6));
  2121. /* set new i2c address and force divstart */
  2122. dib8000_i2c_write16(&client, 1285, (new_addr << 2) | 0x2);
  2123. client.addr = new_addr;
  2124. dib8000_identify(&client);
  2125. dprintk("IC %d initialized (to i2c_address 0x%x)", k, new_addr);
  2126. }
  2127. for (k = 0; k < no_of_demods; k++) {
  2128. new_addr = first_addr | (k << 1);
  2129. client.addr = new_addr;
  2130. // unforce divstr
  2131. dib8000_i2c_write16(&client, 1285, new_addr << 2);
  2132. /* deactivate div - it was just for i2c-enumeration */
  2133. dib8000_i2c_write16(&client, 1286, 0);
  2134. }
  2135. error:
  2136. kfree(client.i2c_buffer_lock);
  2137. error_memory_lock:
  2138. kfree(client.i2c_read_buffer);
  2139. error_memory_read:
  2140. kfree(client.i2c_write_buffer);
  2141. return ret;
  2142. }
  2143. EXPORT_SYMBOL(dib8000_i2c_enumeration);
  2144. static int dib8000_fe_get_tune_settings(struct dvb_frontend *fe, struct dvb_frontend_tune_settings *tune)
  2145. {
  2146. tune->min_delay_ms = 1000;
  2147. tune->step_size = 0;
  2148. tune->max_drift = 0;
  2149. return 0;
  2150. }
  2151. static void dib8000_release(struct dvb_frontend *fe)
  2152. {
  2153. struct dib8000_state *st = fe->demodulator_priv;
  2154. u8 index_frontend;
  2155. for (index_frontend = 1; (index_frontend < MAX_NUMBER_OF_FRONTENDS) && (st->fe[index_frontend] != NULL); index_frontend++)
  2156. dvb_frontend_detach(st->fe[index_frontend]);
  2157. dibx000_exit_i2c_master(&st->i2c_master);
  2158. kfree(st->fe[0]);
  2159. kfree(st);
  2160. }
  2161. struct i2c_adapter *dib8000_get_i2c_master(struct dvb_frontend *fe, enum dibx000_i2c_interface intf, int gating)
  2162. {
  2163. struct dib8000_state *st = fe->demodulator_priv;
  2164. return dibx000_get_i2c_adapter(&st->i2c_master, intf, gating);
  2165. }
  2166. EXPORT_SYMBOL(dib8000_get_i2c_master);
  2167. int dib8000_pid_filter_ctrl(struct dvb_frontend *fe, u8 onoff)
  2168. {
  2169. struct dib8000_state *st = fe->demodulator_priv;
  2170. u16 val = dib8000_read_word(st, 299) & 0xffef;
  2171. val |= (onoff & 0x1) << 4;
  2172. dprintk("pid filter enabled %d", onoff);
  2173. return dib8000_write_word(st, 299, val);
  2174. }
  2175. EXPORT_SYMBOL(dib8000_pid_filter_ctrl);
  2176. int dib8000_pid_filter(struct dvb_frontend *fe, u8 id, u16 pid, u8 onoff)
  2177. {
  2178. struct dib8000_state *st = fe->demodulator_priv;
  2179. dprintk("Index %x, PID %d, OnOff %d", id, pid, onoff);
  2180. return dib8000_write_word(st, 305 + id, onoff ? (1 << 13) | pid : 0);
  2181. }
  2182. EXPORT_SYMBOL(dib8000_pid_filter);
  2183. static const struct dvb_frontend_ops dib8000_ops = {
  2184. .info = {
  2185. .name = "DiBcom 8000 ISDB-T",
  2186. .type = FE_OFDM,
  2187. .frequency_min = 44250000,
  2188. .frequency_max = 867250000,
  2189. .frequency_stepsize = 62500,
  2190. .caps = FE_CAN_INVERSION_AUTO |
  2191. FE_CAN_FEC_1_2 | FE_CAN_FEC_2_3 | FE_CAN_FEC_3_4 |
  2192. FE_CAN_FEC_5_6 | FE_CAN_FEC_7_8 | FE_CAN_FEC_AUTO |
  2193. FE_CAN_QPSK | FE_CAN_QAM_16 | FE_CAN_QAM_64 | FE_CAN_QAM_AUTO |
  2194. FE_CAN_TRANSMISSION_MODE_AUTO | FE_CAN_GUARD_INTERVAL_AUTO | FE_CAN_RECOVER | FE_CAN_HIERARCHY_AUTO,
  2195. },
  2196. .release = dib8000_release,
  2197. .init = dib8000_wakeup,
  2198. .sleep = dib8000_sleep,
  2199. .set_frontend = dib8000_set_frontend,
  2200. .get_tune_settings = dib8000_fe_get_tune_settings,
  2201. .get_frontend = dib8000_get_frontend,
  2202. .read_status = dib8000_read_status,
  2203. .read_ber = dib8000_read_ber,
  2204. .read_signal_strength = dib8000_read_signal_strength,
  2205. .read_snr = dib8000_read_snr,
  2206. .read_ucblocks = dib8000_read_unc_blocks,
  2207. };
  2208. struct dvb_frontend *dib8000_attach(struct i2c_adapter *i2c_adap, u8 i2c_addr, struct dib8000_config *cfg)
  2209. {
  2210. struct dvb_frontend *fe;
  2211. struct dib8000_state *state;
  2212. dprintk("dib8000_attach");
  2213. state = kzalloc(sizeof(struct dib8000_state), GFP_KERNEL);
  2214. if (state == NULL)
  2215. return NULL;
  2216. fe = kzalloc(sizeof(struct dvb_frontend), GFP_KERNEL);
  2217. if (fe == NULL)
  2218. goto error;
  2219. memcpy(&state->cfg, cfg, sizeof(struct dib8000_config));
  2220. state->i2c.adap = i2c_adap;
  2221. state->i2c.addr = i2c_addr;
  2222. state->i2c.i2c_write_buffer = state->i2c_write_buffer;
  2223. state->i2c.i2c_read_buffer = state->i2c_read_buffer;
  2224. mutex_init(&state->i2c_buffer_lock);
  2225. state->i2c.i2c_buffer_lock = &state->i2c_buffer_lock;
  2226. state->gpio_val = cfg->gpio_val;
  2227. state->gpio_dir = cfg->gpio_dir;
  2228. /* Ensure the output mode remains at the previous default if it's
  2229. * not specifically set by the caller.
  2230. */
  2231. if ((state->cfg.output_mode != OUTMODE_MPEG2_SERIAL) && (state->cfg.output_mode != OUTMODE_MPEG2_PAR_GATED_CLK))
  2232. state->cfg.output_mode = OUTMODE_MPEG2_FIFO;
  2233. state->fe[0] = fe;
  2234. fe->demodulator_priv = state;
  2235. memcpy(&state->fe[0]->ops, &dib8000_ops, sizeof(struct dvb_frontend_ops));
  2236. state->timf_default = cfg->pll->timf;
  2237. if (dib8000_identify(&state->i2c) == 0)
  2238. goto error;
  2239. dibx000_init_i2c_master(&state->i2c_master, DIB8000, state->i2c.adap, state->i2c.addr);
  2240. dib8000_reset(fe);
  2241. dib8000_write_word(state, 285, (dib8000_read_word(state, 285) & ~0x60) | (3 << 5)); /* ber_rs_len = 3 */
  2242. return fe;
  2243. error:
  2244. kfree(state);
  2245. return NULL;
  2246. }
  2247. EXPORT_SYMBOL(dib8000_attach);
  2248. MODULE_AUTHOR("Olivier Grenie <Olivier.Grenie@dibcom.fr, " "Patrick Boettcher <pboettcher@dibcom.fr>");
  2249. MODULE_DESCRIPTION("Driver for the DiBcom 8000 ISDB-T demodulator");
  2250. MODULE_LICENSE("GPL");