dib8000.c 71 KB

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