dib7000p.c 64 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472
  1. /*
  2. * Linux-DVB Driver for DiBcom's second generation DiB7000P (PC).
  3. *
  4. * Copyright (C) 2005-7 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 "dib7000p.h"
  17. static int debug;
  18. module_param(debug, int, 0644);
  19. MODULE_PARM_DESC(debug, "turn on debugging (default: 0)");
  20. static int buggy_sfn_workaround;
  21. module_param(buggy_sfn_workaround, int, 0644);
  22. MODULE_PARM_DESC(buggy_sfn_workaround, "Enable work-around for buggy SFNs (default: 0)");
  23. #define dprintk(args...) do { if (debug) { printk(KERN_DEBUG "DiB7000P: "); printk(args); printk("\n"); } } while (0)
  24. struct i2c_device {
  25. struct i2c_adapter *i2c_adap;
  26. u8 i2c_addr;
  27. };
  28. struct dib7000p_state {
  29. struct dvb_frontend demod;
  30. struct dib7000p_config cfg;
  31. u8 i2c_addr;
  32. struct i2c_adapter *i2c_adap;
  33. struct dibx000_i2c_master i2c_master;
  34. u16 wbd_ref;
  35. u8 current_band;
  36. u32 current_bandwidth;
  37. struct dibx000_agc_config *current_agc;
  38. u32 timf;
  39. u8 div_force_off:1;
  40. u8 div_state:1;
  41. u16 div_sync_wait;
  42. u8 agc_state;
  43. u16 gpio_dir;
  44. u16 gpio_val;
  45. u8 sfn_workaround_active:1;
  46. #define SOC7090 0x7090
  47. u16 version;
  48. u16 tuner_enable;
  49. struct i2c_adapter dib7090_tuner_adap;
  50. /* for the I2C transfer */
  51. struct i2c_msg msg[2];
  52. u8 i2c_write_buffer[4];
  53. u8 i2c_read_buffer[2];
  54. struct mutex i2c_buffer_lock;
  55. u8 input_mode_mpeg;
  56. };
  57. enum dib7000p_power_mode {
  58. DIB7000P_POWER_ALL = 0,
  59. DIB7000P_POWER_ANALOG_ADC,
  60. DIB7000P_POWER_INTERFACE_ONLY,
  61. };
  62. /* dib7090 specific fonctions */
  63. static int dib7090_set_output_mode(struct dvb_frontend *fe, int mode);
  64. static int dib7090_set_diversity_in(struct dvb_frontend *fe, int onoff);
  65. static void dib7090_setDibTxMux(struct dib7000p_state *state, int mode);
  66. static void dib7090_setHostBusMux(struct dib7000p_state *state, int mode);
  67. static u16 dib7000p_read_word(struct dib7000p_state *state, u16 reg)
  68. {
  69. u16 ret;
  70. if (mutex_lock_interruptible(&state->i2c_buffer_lock) < 0) {
  71. dprintk("could not acquire lock");
  72. return 0;
  73. }
  74. state->i2c_write_buffer[0] = reg >> 8;
  75. state->i2c_write_buffer[1] = reg & 0xff;
  76. memset(state->msg, 0, 2 * sizeof(struct i2c_msg));
  77. state->msg[0].addr = state->i2c_addr >> 1;
  78. state->msg[0].flags = 0;
  79. state->msg[0].buf = state->i2c_write_buffer;
  80. state->msg[0].len = 2;
  81. state->msg[1].addr = state->i2c_addr >> 1;
  82. state->msg[1].flags = I2C_M_RD;
  83. state->msg[1].buf = state->i2c_read_buffer;
  84. state->msg[1].len = 2;
  85. if (i2c_transfer(state->i2c_adap, state->msg, 2) != 2)
  86. dprintk("i2c read error on %d", reg);
  87. ret = (state->i2c_read_buffer[0] << 8) | state->i2c_read_buffer[1];
  88. mutex_unlock(&state->i2c_buffer_lock);
  89. return ret;
  90. }
  91. static int dib7000p_write_word(struct dib7000p_state *state, u16 reg, u16 val)
  92. {
  93. int ret;
  94. if (mutex_lock_interruptible(&state->i2c_buffer_lock) < 0) {
  95. dprintk("could not acquire lock");
  96. return -EINVAL;
  97. }
  98. state->i2c_write_buffer[0] = (reg >> 8) & 0xff;
  99. state->i2c_write_buffer[1] = reg & 0xff;
  100. state->i2c_write_buffer[2] = (val >> 8) & 0xff;
  101. state->i2c_write_buffer[3] = val & 0xff;
  102. memset(&state->msg[0], 0, sizeof(struct i2c_msg));
  103. state->msg[0].addr = state->i2c_addr >> 1;
  104. state->msg[0].flags = 0;
  105. state->msg[0].buf = state->i2c_write_buffer;
  106. state->msg[0].len = 4;
  107. ret = (i2c_transfer(state->i2c_adap, state->msg, 1) != 1 ?
  108. -EREMOTEIO : 0);
  109. mutex_unlock(&state->i2c_buffer_lock);
  110. return ret;
  111. }
  112. static void dib7000p_write_tab(struct dib7000p_state *state, u16 * buf)
  113. {
  114. u16 l = 0, r, *n;
  115. n = buf;
  116. l = *n++;
  117. while (l) {
  118. r = *n++;
  119. do {
  120. dib7000p_write_word(state, r, *n++);
  121. r++;
  122. } while (--l);
  123. l = *n++;
  124. }
  125. }
  126. static int dib7000p_set_output_mode(struct dib7000p_state *state, int mode)
  127. {
  128. int ret = 0;
  129. u16 outreg, fifo_threshold, smo_mode;
  130. outreg = 0;
  131. fifo_threshold = 1792;
  132. smo_mode = (dib7000p_read_word(state, 235) & 0x0050) | (1 << 1);
  133. dprintk("setting output mode for demod %p to %d", &state->demod, mode);
  134. switch (mode) {
  135. case OUTMODE_MPEG2_PAR_GATED_CLK:
  136. outreg = (1 << 10); /* 0x0400 */
  137. break;
  138. case OUTMODE_MPEG2_PAR_CONT_CLK:
  139. outreg = (1 << 10) | (1 << 6); /* 0x0440 */
  140. break;
  141. case OUTMODE_MPEG2_SERIAL:
  142. outreg = (1 << 10) | (2 << 6) | (0 << 1); /* 0x0480 */
  143. break;
  144. case OUTMODE_DIVERSITY:
  145. if (state->cfg.hostbus_diversity)
  146. outreg = (1 << 10) | (4 << 6); /* 0x0500 */
  147. else
  148. outreg = (1 << 11);
  149. break;
  150. case OUTMODE_MPEG2_FIFO:
  151. smo_mode |= (3 << 1);
  152. fifo_threshold = 512;
  153. outreg = (1 << 10) | (5 << 6);
  154. break;
  155. case OUTMODE_ANALOG_ADC:
  156. outreg = (1 << 10) | (3 << 6);
  157. break;
  158. case OUTMODE_HIGH_Z:
  159. outreg = 0;
  160. break;
  161. default:
  162. dprintk("Unhandled output_mode passed to be set for demod %p", &state->demod);
  163. break;
  164. }
  165. if (state->cfg.output_mpeg2_in_188_bytes)
  166. smo_mode |= (1 << 5);
  167. ret |= dib7000p_write_word(state, 235, smo_mode);
  168. ret |= dib7000p_write_word(state, 236, fifo_threshold); /* synchronous fread */
  169. if (state->version != SOC7090)
  170. ret |= dib7000p_write_word(state, 1286, outreg); /* P_Div_active */
  171. return ret;
  172. }
  173. static int dib7000p_set_diversity_in(struct dvb_frontend *demod, int onoff)
  174. {
  175. struct dib7000p_state *state = demod->demodulator_priv;
  176. if (state->div_force_off) {
  177. dprintk("diversity combination deactivated - forced by COFDM parameters");
  178. onoff = 0;
  179. dib7000p_write_word(state, 207, 0);
  180. } else
  181. dib7000p_write_word(state, 207, (state->div_sync_wait << 4) | (1 << 2) | (2 << 0));
  182. state->div_state = (u8) onoff;
  183. if (onoff) {
  184. dib7000p_write_word(state, 204, 6);
  185. dib7000p_write_word(state, 205, 16);
  186. /* P_dvsy_sync_mode = 0, P_dvsy_sync_enable=1, P_dvcb_comb_mode=2 */
  187. } else {
  188. dib7000p_write_word(state, 204, 1);
  189. dib7000p_write_word(state, 205, 0);
  190. }
  191. return 0;
  192. }
  193. static int dib7000p_set_power_mode(struct dib7000p_state *state, enum dib7000p_power_mode mode)
  194. {
  195. /* by default everything is powered off */
  196. u16 reg_774 = 0x3fff, reg_775 = 0xffff, reg_776 = 0x0007, reg_899 = 0x0003, reg_1280 = (0xfe00) | (dib7000p_read_word(state, 1280) & 0x01ff);
  197. /* now, depending on the requested mode, we power on */
  198. switch (mode) {
  199. /* power up everything in the demod */
  200. case DIB7000P_POWER_ALL:
  201. reg_774 = 0x0000;
  202. reg_775 = 0x0000;
  203. reg_776 = 0x0;
  204. reg_899 = 0x0;
  205. if (state->version == SOC7090)
  206. reg_1280 &= 0x001f;
  207. else
  208. reg_1280 &= 0x01ff;
  209. break;
  210. case DIB7000P_POWER_ANALOG_ADC:
  211. /* dem, cfg, iqc, sad, agc */
  212. reg_774 &= ~((1 << 15) | (1 << 14) | (1 << 11) | (1 << 10) | (1 << 9));
  213. /* nud */
  214. reg_776 &= ~((1 << 0));
  215. /* Dout */
  216. if (state->version != SOC7090)
  217. reg_1280 &= ~((1 << 11));
  218. reg_1280 &= ~(1 << 6);
  219. /* fall through wanted to enable the interfaces */
  220. /* just leave power on the control-interfaces: GPIO and (I2C or SDIO) */
  221. case DIB7000P_POWER_INTERFACE_ONLY: /* TODO power up either SDIO or I2C */
  222. if (state->version == SOC7090)
  223. reg_1280 &= ~((1 << 7) | (1 << 5));
  224. else
  225. reg_1280 &= ~((1 << 14) | (1 << 13) | (1 << 12) | (1 << 10));
  226. break;
  227. /* TODO following stuff is just converted from the dib7000-driver - check when is used what */
  228. }
  229. dib7000p_write_word(state, 774, reg_774);
  230. dib7000p_write_word(state, 775, reg_775);
  231. dib7000p_write_word(state, 776, reg_776);
  232. dib7000p_write_word(state, 1280, reg_1280);
  233. if (state->version != SOC7090)
  234. dib7000p_write_word(state, 899, reg_899);
  235. return 0;
  236. }
  237. static void dib7000p_set_adc_state(struct dib7000p_state *state, enum dibx000_adc_states no)
  238. {
  239. u16 reg_908 = 0, reg_909 = 0;
  240. u16 reg;
  241. if (state->version != SOC7090) {
  242. reg_908 = dib7000p_read_word(state, 908);
  243. reg_909 = dib7000p_read_word(state, 909);
  244. }
  245. switch (no) {
  246. case DIBX000_SLOW_ADC_ON:
  247. if (state->version == SOC7090) {
  248. reg = dib7000p_read_word(state, 1925);
  249. dib7000p_write_word(state, 1925, reg | (1 << 4) | (1 << 2)); /* en_slowAdc = 1 & reset_sladc = 1 */
  250. reg = dib7000p_read_word(state, 1925); /* read acces to make it works... strange ... */
  251. msleep(200);
  252. dib7000p_write_word(state, 1925, reg & ~(1 << 4)); /* en_slowAdc = 1 & reset_sladc = 0 */
  253. reg = dib7000p_read_word(state, 72) & ~((0x3 << 14) | (0x3 << 12));
  254. dib7000p_write_word(state, 72, reg | (1 << 14) | (3 << 12) | 524); /* ref = Vin1 => Vbg ; sel = Vin0 or Vin3 ; (Vin2 = Vcm) */
  255. } else {
  256. reg_909 |= (1 << 1) | (1 << 0);
  257. dib7000p_write_word(state, 909, reg_909);
  258. reg_909 &= ~(1 << 1);
  259. }
  260. break;
  261. case DIBX000_SLOW_ADC_OFF:
  262. if (state->version == SOC7090) {
  263. reg = dib7000p_read_word(state, 1925);
  264. dib7000p_write_word(state, 1925, (reg & ~(1 << 2)) | (1 << 4)); /* reset_sladc = 1 en_slowAdc = 0 */
  265. } else
  266. reg_909 |= (1 << 1) | (1 << 0);
  267. break;
  268. case DIBX000_ADC_ON:
  269. reg_908 &= 0x0fff;
  270. reg_909 &= 0x0003;
  271. break;
  272. case DIBX000_ADC_OFF:
  273. reg_908 |= (1 << 14) | (1 << 13) | (1 << 12);
  274. reg_909 |= (1 << 5) | (1 << 4) | (1 << 3) | (1 << 2);
  275. break;
  276. case DIBX000_VBG_ENABLE:
  277. reg_908 &= ~(1 << 15);
  278. break;
  279. case DIBX000_VBG_DISABLE:
  280. reg_908 |= (1 << 15);
  281. break;
  282. default:
  283. break;
  284. }
  285. // dprintk( "908: %x, 909: %x\n", reg_908, reg_909);
  286. reg_909 |= (state->cfg.disable_sample_and_hold & 1) << 4;
  287. reg_908 |= (state->cfg.enable_current_mirror & 1) << 7;
  288. if (state->version != SOC7090) {
  289. dib7000p_write_word(state, 908, reg_908);
  290. dib7000p_write_word(state, 909, reg_909);
  291. }
  292. }
  293. static int dib7000p_set_bandwidth(struct dib7000p_state *state, u32 bw)
  294. {
  295. u32 timf;
  296. // store the current bandwidth for later use
  297. state->current_bandwidth = bw;
  298. if (state->timf == 0) {
  299. dprintk("using default timf");
  300. timf = state->cfg.bw->timf;
  301. } else {
  302. dprintk("using updated timf");
  303. timf = state->timf;
  304. }
  305. timf = timf * (bw / 50) / 160;
  306. dib7000p_write_word(state, 23, (u16) ((timf >> 16) & 0xffff));
  307. dib7000p_write_word(state, 24, (u16) ((timf) & 0xffff));
  308. return 0;
  309. }
  310. static int dib7000p_sad_calib(struct dib7000p_state *state)
  311. {
  312. /* internal */
  313. dib7000p_write_word(state, 73, (0 << 1) | (0 << 0));
  314. if (state->version == SOC7090)
  315. dib7000p_write_word(state, 74, 2048);
  316. else
  317. dib7000p_write_word(state, 74, 776);
  318. /* do the calibration */
  319. dib7000p_write_word(state, 73, (1 << 0));
  320. dib7000p_write_word(state, 73, (0 << 0));
  321. msleep(1);
  322. return 0;
  323. }
  324. int dib7000p_set_wbd_ref(struct dvb_frontend *demod, u16 value)
  325. {
  326. struct dib7000p_state *state = demod->demodulator_priv;
  327. if (value > 4095)
  328. value = 4095;
  329. state->wbd_ref = value;
  330. return dib7000p_write_word(state, 105, (dib7000p_read_word(state, 105) & 0xf000) | value);
  331. }
  332. EXPORT_SYMBOL(dib7000p_set_wbd_ref);
  333. int dib7000p_get_agc_values(struct dvb_frontend *fe,
  334. u16 *agc_global, u16 *agc1, u16 *agc2, u16 *wbd)
  335. {
  336. struct dib7000p_state *state = fe->demodulator_priv;
  337. if (agc_global != NULL)
  338. *agc_global = dib7000p_read_word(state, 394);
  339. if (agc1 != NULL)
  340. *agc1 = dib7000p_read_word(state, 392);
  341. if (agc2 != NULL)
  342. *agc2 = dib7000p_read_word(state, 393);
  343. if (wbd != NULL)
  344. *wbd = dib7000p_read_word(state, 397);
  345. return 0;
  346. }
  347. EXPORT_SYMBOL(dib7000p_get_agc_values);
  348. int dib7000p_set_agc1_min(struct dvb_frontend *fe, u16 v)
  349. {
  350. struct dib7000p_state *state = fe->demodulator_priv;
  351. return dib7000p_write_word(state, 108, v);
  352. }
  353. EXPORT_SYMBOL(dib7000p_set_agc1_min);
  354. static void dib7000p_reset_pll(struct dib7000p_state *state)
  355. {
  356. struct dibx000_bandwidth_config *bw = &state->cfg.bw[0];
  357. u16 clk_cfg0;
  358. if (state->version == SOC7090) {
  359. dib7000p_write_word(state, 1856, (!bw->pll_reset << 13) | (bw->pll_range << 12) | (bw->pll_ratio << 6) | (bw->pll_prediv));
  360. while (((dib7000p_read_word(state, 1856) >> 15) & 0x1) != 1)
  361. ;
  362. dib7000p_write_word(state, 1857, dib7000p_read_word(state, 1857) | (!bw->pll_bypass << 15));
  363. } else {
  364. /* force PLL bypass */
  365. clk_cfg0 = (1 << 15) | ((bw->pll_ratio & 0x3f) << 9) |
  366. (bw->modulo << 7) | (bw->ADClkSrc << 6) | (bw->IO_CLK_en_core << 5) | (bw->bypclk_div << 2) | (bw->enable_refdiv << 1) | (0 << 0);
  367. dib7000p_write_word(state, 900, clk_cfg0);
  368. /* P_pll_cfg */
  369. dib7000p_write_word(state, 903, (bw->pll_prediv << 5) | (((bw->pll_ratio >> 6) & 0x3) << 3) | (bw->pll_range << 1) | bw->pll_reset);
  370. clk_cfg0 = (bw->pll_bypass << 15) | (clk_cfg0 & 0x7fff);
  371. dib7000p_write_word(state, 900, clk_cfg0);
  372. }
  373. dib7000p_write_word(state, 18, (u16) (((bw->internal * 1000) >> 16) & 0xffff));
  374. dib7000p_write_word(state, 19, (u16) ((bw->internal * 1000) & 0xffff));
  375. dib7000p_write_word(state, 21, (u16) ((bw->ifreq >> 16) & 0xffff));
  376. dib7000p_write_word(state, 22, (u16) ((bw->ifreq) & 0xffff));
  377. dib7000p_write_word(state, 72, bw->sad_cfg);
  378. }
  379. static u32 dib7000p_get_internal_freq(struct dib7000p_state *state)
  380. {
  381. u32 internal = (u32) dib7000p_read_word(state, 18) << 16;
  382. internal |= (u32) dib7000p_read_word(state, 19);
  383. internal /= 1000;
  384. return internal;
  385. }
  386. int dib7000p_update_pll(struct dvb_frontend *fe, struct dibx000_bandwidth_config *bw)
  387. {
  388. struct dib7000p_state *state = fe->demodulator_priv;
  389. u16 reg_1857, reg_1856 = dib7000p_read_word(state, 1856);
  390. u8 loopdiv, prediv;
  391. u32 internal, xtal;
  392. /* get back old values */
  393. prediv = reg_1856 & 0x3f;
  394. loopdiv = (reg_1856 >> 6) & 0x3f;
  395. if ((bw != NULL) && (bw->pll_prediv != prediv || bw->pll_ratio != loopdiv)) {
  396. dprintk("Updating pll (prediv: old = %d new = %d ; loopdiv : old = %d new = %d)", prediv, bw->pll_prediv, loopdiv, bw->pll_ratio);
  397. reg_1856 &= 0xf000;
  398. reg_1857 = dib7000p_read_word(state, 1857);
  399. dib7000p_write_word(state, 1857, reg_1857 & ~(1 << 15));
  400. dib7000p_write_word(state, 1856, reg_1856 | ((bw->pll_ratio & 0x3f) << 6) | (bw->pll_prediv & 0x3f));
  401. /* write new system clk into P_sec_len */
  402. internal = dib7000p_get_internal_freq(state);
  403. xtal = (internal / loopdiv) * prediv;
  404. internal = 1000 * (xtal / bw->pll_prediv) * bw->pll_ratio; /* new internal */
  405. dib7000p_write_word(state, 18, (u16) ((internal >> 16) & 0xffff));
  406. dib7000p_write_word(state, 19, (u16) (internal & 0xffff));
  407. dib7000p_write_word(state, 1857, reg_1857 | (1 << 15));
  408. while (((dib7000p_read_word(state, 1856) >> 15) & 0x1) != 1)
  409. dprintk("Waiting for PLL to lock");
  410. return 0;
  411. }
  412. return -EIO;
  413. }
  414. EXPORT_SYMBOL(dib7000p_update_pll);
  415. static int dib7000p_reset_gpio(struct dib7000p_state *st)
  416. {
  417. /* reset the GPIOs */
  418. dprintk("gpio dir: %x: val: %x, pwm_pos: %x", st->gpio_dir, st->gpio_val, st->cfg.gpio_pwm_pos);
  419. dib7000p_write_word(st, 1029, st->gpio_dir);
  420. dib7000p_write_word(st, 1030, st->gpio_val);
  421. /* TODO 1031 is P_gpio_od */
  422. dib7000p_write_word(st, 1032, st->cfg.gpio_pwm_pos);
  423. dib7000p_write_word(st, 1037, st->cfg.pwm_freq_div);
  424. return 0;
  425. }
  426. static int dib7000p_cfg_gpio(struct dib7000p_state *st, u8 num, u8 dir, u8 val)
  427. {
  428. st->gpio_dir = dib7000p_read_word(st, 1029);
  429. st->gpio_dir &= ~(1 << num); /* reset the direction bit */
  430. st->gpio_dir |= (dir & 0x1) << num; /* set the new direction */
  431. dib7000p_write_word(st, 1029, st->gpio_dir);
  432. st->gpio_val = dib7000p_read_word(st, 1030);
  433. st->gpio_val &= ~(1 << num); /* reset the direction bit */
  434. st->gpio_val |= (val & 0x01) << num; /* set the new value */
  435. dib7000p_write_word(st, 1030, st->gpio_val);
  436. return 0;
  437. }
  438. int dib7000p_set_gpio(struct dvb_frontend *demod, u8 num, u8 dir, u8 val)
  439. {
  440. struct dib7000p_state *state = demod->demodulator_priv;
  441. return dib7000p_cfg_gpio(state, num, dir, val);
  442. }
  443. EXPORT_SYMBOL(dib7000p_set_gpio);
  444. static u16 dib7000p_defaults[] = {
  445. // auto search configuration
  446. 3, 2,
  447. 0x0004,
  448. (1<<3)|(1<<11)|(1<<12)|(1<<13),
  449. 0x0814, /* Equal Lock */
  450. 12, 6,
  451. 0x001b,
  452. 0x7740,
  453. 0x005b,
  454. 0x8d80,
  455. 0x01c9,
  456. 0xc380,
  457. 0x0000,
  458. 0x0080,
  459. 0x0000,
  460. 0x0090,
  461. 0x0001,
  462. 0xd4c0,
  463. 1, 26,
  464. 0x6680,
  465. /* set ADC level to -16 */
  466. 11, 79,
  467. (1 << 13) - 825 - 117,
  468. (1 << 13) - 837 - 117,
  469. (1 << 13) - 811 - 117,
  470. (1 << 13) - 766 - 117,
  471. (1 << 13) - 737 - 117,
  472. (1 << 13) - 693 - 117,
  473. (1 << 13) - 648 - 117,
  474. (1 << 13) - 619 - 117,
  475. (1 << 13) - 575 - 117,
  476. (1 << 13) - 531 - 117,
  477. (1 << 13) - 501 - 117,
  478. 1, 142,
  479. 0x0410,
  480. /* disable power smoothing */
  481. 8, 145,
  482. 0,
  483. 0,
  484. 0,
  485. 0,
  486. 0,
  487. 0,
  488. 0,
  489. 0,
  490. 1, 154,
  491. 1 << 13,
  492. 1, 168,
  493. 0x0ccd,
  494. 1, 183,
  495. 0x200f,
  496. 1, 212,
  497. 0x169,
  498. 5, 187,
  499. 0x023d,
  500. 0x00a4,
  501. 0x00a4,
  502. 0x7ff0,
  503. 0x3ccc,
  504. 1, 198,
  505. 0x800,
  506. 1, 222,
  507. 0x0010,
  508. 1, 235,
  509. 0x0062,
  510. 0,
  511. };
  512. static int dib7000p_demod_reset(struct dib7000p_state *state)
  513. {
  514. dib7000p_set_power_mode(state, DIB7000P_POWER_ALL);
  515. if (state->version == SOC7090)
  516. dibx000_reset_i2c_master(&state->i2c_master);
  517. dib7000p_set_adc_state(state, DIBX000_VBG_ENABLE);
  518. /* restart all parts */
  519. dib7000p_write_word(state, 770, 0xffff);
  520. dib7000p_write_word(state, 771, 0xffff);
  521. dib7000p_write_word(state, 772, 0x001f);
  522. dib7000p_write_word(state, 1280, 0x001f - ((1 << 4) | (1 << 3)));
  523. dib7000p_write_word(state, 770, 0);
  524. dib7000p_write_word(state, 771, 0);
  525. dib7000p_write_word(state, 772, 0);
  526. dib7000p_write_word(state, 1280, 0);
  527. if (state->version != SOC7090) {
  528. dib7000p_write_word(state, 898, 0x0003);
  529. dib7000p_write_word(state, 898, 0);
  530. }
  531. /* default */
  532. dib7000p_reset_pll(state);
  533. if (dib7000p_reset_gpio(state) != 0)
  534. dprintk("GPIO reset was not successful.");
  535. if (state->version == SOC7090) {
  536. dib7000p_write_word(state, 899, 0);
  537. /* impulse noise */
  538. dib7000p_write_word(state, 42, (1<<5) | 3); /* P_iqc_thsat_ipc = 1 ; P_iqc_win2 = 3 */
  539. dib7000p_write_word(state, 43, 0x2d4); /*-300 fag P_iqc_dect_min = -280 */
  540. dib7000p_write_word(state, 44, 300); /* 300 fag P_iqc_dect_min = +280 */
  541. dib7000p_write_word(state, 273, (0<<6) | 30);
  542. }
  543. if (dib7000p_set_output_mode(state, OUTMODE_HIGH_Z) != 0)
  544. dprintk("OUTPUT_MODE could not be reset.");
  545. dib7000p_set_adc_state(state, DIBX000_SLOW_ADC_ON);
  546. dib7000p_sad_calib(state);
  547. dib7000p_set_adc_state(state, DIBX000_SLOW_ADC_OFF);
  548. /* unforce divstr regardless whether i2c enumeration was done or not */
  549. dib7000p_write_word(state, 1285, dib7000p_read_word(state, 1285) & ~(1 << 1));
  550. dib7000p_set_bandwidth(state, 8000);
  551. if (state->version == SOC7090) {
  552. dib7000p_write_word(state, 36, 0x0755);/* P_iqc_impnc_on =1 & P_iqc_corr_inh = 1 for impulsive noise */
  553. } else {
  554. if (state->cfg.tuner_is_baseband)
  555. dib7000p_write_word(state, 36, 0x0755);
  556. else
  557. dib7000p_write_word(state, 36, 0x1f55);
  558. }
  559. dib7000p_write_tab(state, dib7000p_defaults);
  560. if (state->version != SOC7090) {
  561. dib7000p_write_word(state, 901, 0x0006);
  562. dib7000p_write_word(state, 902, (3 << 10) | (1 << 6));
  563. dib7000p_write_word(state, 905, 0x2c8e);
  564. }
  565. dib7000p_set_power_mode(state, DIB7000P_POWER_INTERFACE_ONLY);
  566. return 0;
  567. }
  568. static void dib7000p_pll_clk_cfg(struct dib7000p_state *state)
  569. {
  570. u16 tmp = 0;
  571. tmp = dib7000p_read_word(state, 903);
  572. dib7000p_write_word(state, 903, (tmp | 0x1));
  573. tmp = dib7000p_read_word(state, 900);
  574. dib7000p_write_word(state, 900, (tmp & 0x7fff) | (1 << 6));
  575. }
  576. static void dib7000p_restart_agc(struct dib7000p_state *state)
  577. {
  578. // P_restart_iqc & P_restart_agc
  579. dib7000p_write_word(state, 770, (1 << 11) | (1 << 9));
  580. dib7000p_write_word(state, 770, 0x0000);
  581. }
  582. static int dib7000p_update_lna(struct dib7000p_state *state)
  583. {
  584. u16 dyn_gain;
  585. if (state->cfg.update_lna) {
  586. dyn_gain = dib7000p_read_word(state, 394);
  587. if (state->cfg.update_lna(&state->demod, dyn_gain)) {
  588. dib7000p_restart_agc(state);
  589. return 1;
  590. }
  591. }
  592. return 0;
  593. }
  594. static int dib7000p_set_agc_config(struct dib7000p_state *state, u8 band)
  595. {
  596. struct dibx000_agc_config *agc = NULL;
  597. int i;
  598. if (state->current_band == band && state->current_agc != NULL)
  599. return 0;
  600. state->current_band = band;
  601. for (i = 0; i < state->cfg.agc_config_count; i++)
  602. if (state->cfg.agc[i].band_caps & band) {
  603. agc = &state->cfg.agc[i];
  604. break;
  605. }
  606. if (agc == NULL) {
  607. dprintk("no valid AGC configuration found for band 0x%02x", band);
  608. return -EINVAL;
  609. }
  610. state->current_agc = agc;
  611. /* AGC */
  612. dib7000p_write_word(state, 75, agc->setup);
  613. dib7000p_write_word(state, 76, agc->inv_gain);
  614. dib7000p_write_word(state, 77, agc->time_stabiliz);
  615. dib7000p_write_word(state, 100, (agc->alpha_level << 12) | agc->thlock);
  616. // Demod AGC loop configuration
  617. dib7000p_write_word(state, 101, (agc->alpha_mant << 5) | agc->alpha_exp);
  618. dib7000p_write_word(state, 102, (agc->beta_mant << 6) | agc->beta_exp);
  619. /* AGC continued */
  620. dprintk("WBD: ref: %d, sel: %d, active: %d, alpha: %d",
  621. state->wbd_ref != 0 ? state->wbd_ref : agc->wbd_ref, agc->wbd_sel, !agc->perform_agc_softsplit, agc->wbd_sel);
  622. if (state->wbd_ref != 0)
  623. dib7000p_write_word(state, 105, (agc->wbd_inv << 12) | state->wbd_ref);
  624. else
  625. dib7000p_write_word(state, 105, (agc->wbd_inv << 12) | agc->wbd_ref);
  626. dib7000p_write_word(state, 106, (agc->wbd_sel << 13) | (agc->wbd_alpha << 9) | (agc->perform_agc_softsplit << 8));
  627. dib7000p_write_word(state, 107, agc->agc1_max);
  628. dib7000p_write_word(state, 108, agc->agc1_min);
  629. dib7000p_write_word(state, 109, agc->agc2_max);
  630. dib7000p_write_word(state, 110, agc->agc2_min);
  631. dib7000p_write_word(state, 111, (agc->agc1_pt1 << 8) | agc->agc1_pt2);
  632. dib7000p_write_word(state, 112, agc->agc1_pt3);
  633. dib7000p_write_word(state, 113, (agc->agc1_slope1 << 8) | agc->agc1_slope2);
  634. dib7000p_write_word(state, 114, (agc->agc2_pt1 << 8) | agc->agc2_pt2);
  635. dib7000p_write_word(state, 115, (agc->agc2_slope1 << 8) | agc->agc2_slope2);
  636. return 0;
  637. }
  638. static void dib7000p_set_dds(struct dib7000p_state *state, s32 offset_khz)
  639. {
  640. u32 internal = dib7000p_get_internal_freq(state);
  641. s32 unit_khz_dds_val = 67108864 / (internal); /* 2**26 / Fsampling is the unit 1KHz offset */
  642. u32 abs_offset_khz = ABS(offset_khz);
  643. u32 dds = state->cfg.bw->ifreq & 0x1ffffff;
  644. u8 invert = !!(state->cfg.bw->ifreq & (1 << 25));
  645. dprintk("setting a frequency offset of %dkHz internal freq = %d invert = %d", offset_khz, internal, invert);
  646. if (offset_khz < 0)
  647. unit_khz_dds_val *= -1;
  648. /* IF tuner */
  649. if (invert)
  650. dds -= (abs_offset_khz * unit_khz_dds_val); /* /100 because of /100 on the unit_khz_dds_val line calc for better accuracy */
  651. else
  652. dds += (abs_offset_khz * unit_khz_dds_val);
  653. if (abs_offset_khz <= (internal / 2)) { /* Max dds offset is the half of the demod freq */
  654. dib7000p_write_word(state, 21, (u16) (((dds >> 16) & 0x1ff) | (0 << 10) | (invert << 9)));
  655. dib7000p_write_word(state, 22, (u16) (dds & 0xffff));
  656. }
  657. }
  658. static int dib7000p_agc_startup(struct dvb_frontend *demod)
  659. {
  660. struct dtv_frontend_properties *ch = &demod->dtv_property_cache;
  661. struct dib7000p_state *state = demod->demodulator_priv;
  662. int ret = -1;
  663. u8 *agc_state = &state->agc_state;
  664. u8 agc_split;
  665. u16 reg;
  666. u32 upd_demod_gain_period = 0x1000;
  667. s32 frequency_offset = 0;
  668. switch (state->agc_state) {
  669. case 0:
  670. dib7000p_set_power_mode(state, DIB7000P_POWER_ALL);
  671. if (state->version == SOC7090) {
  672. reg = dib7000p_read_word(state, 0x79b) & 0xff00;
  673. dib7000p_write_word(state, 0x79a, upd_demod_gain_period & 0xFFFF); /* lsb */
  674. dib7000p_write_word(state, 0x79b, reg | (1 << 14) | ((upd_demod_gain_period >> 16) & 0xFF));
  675. /* enable adc i & q */
  676. reg = dib7000p_read_word(state, 0x780);
  677. dib7000p_write_word(state, 0x780, (reg | (0x3)) & (~(1 << 7)));
  678. } else {
  679. dib7000p_set_adc_state(state, DIBX000_ADC_ON);
  680. dib7000p_pll_clk_cfg(state);
  681. }
  682. if (dib7000p_set_agc_config(state, BAND_OF_FREQUENCY(ch->frequency / 1000)) != 0)
  683. return -1;
  684. if (demod->ops.tuner_ops.get_frequency) {
  685. u32 frequency_tuner;
  686. demod->ops.tuner_ops.get_frequency(demod, &frequency_tuner);
  687. frequency_offset = (s32)frequency_tuner / 1000 - ch->frequency / 1000;
  688. }
  689. dib7000p_set_dds(state, frequency_offset);
  690. ret = 7;
  691. (*agc_state)++;
  692. break;
  693. case 1:
  694. if (state->cfg.agc_control)
  695. state->cfg.agc_control(&state->demod, 1);
  696. dib7000p_write_word(state, 78, 32768);
  697. if (!state->current_agc->perform_agc_softsplit) {
  698. /* we are using the wbd - so slow AGC startup */
  699. /* force 0 split on WBD and restart AGC */
  700. dib7000p_write_word(state, 106, (state->current_agc->wbd_sel << 13) | (state->current_agc->wbd_alpha << 9) | (1 << 8));
  701. (*agc_state)++;
  702. ret = 5;
  703. } else {
  704. /* default AGC startup */
  705. (*agc_state) = 4;
  706. /* wait AGC rough lock time */
  707. ret = 7;
  708. }
  709. dib7000p_restart_agc(state);
  710. break;
  711. case 2: /* fast split search path after 5sec */
  712. dib7000p_write_word(state, 75, state->current_agc->setup | (1 << 4)); /* freeze AGC loop */
  713. dib7000p_write_word(state, 106, (state->current_agc->wbd_sel << 13) | (2 << 9) | (0 << 8)); /* fast split search 0.25kHz */
  714. (*agc_state)++;
  715. ret = 14;
  716. break;
  717. case 3: /* split search ended */
  718. agc_split = (u8) dib7000p_read_word(state, 396); /* store the split value for the next time */
  719. dib7000p_write_word(state, 78, dib7000p_read_word(state, 394)); /* set AGC gain start value */
  720. dib7000p_write_word(state, 75, state->current_agc->setup); /* std AGC loop */
  721. dib7000p_write_word(state, 106, (state->current_agc->wbd_sel << 13) | (state->current_agc->wbd_alpha << 9) | agc_split); /* standard split search */
  722. dib7000p_restart_agc(state);
  723. dprintk("SPLIT %p: %hd", demod, agc_split);
  724. (*agc_state)++;
  725. ret = 5;
  726. break;
  727. case 4: /* LNA startup */
  728. ret = 7;
  729. if (dib7000p_update_lna(state))
  730. ret = 5;
  731. else
  732. (*agc_state)++;
  733. break;
  734. case 5:
  735. if (state->cfg.agc_control)
  736. state->cfg.agc_control(&state->demod, 0);
  737. (*agc_state)++;
  738. break;
  739. default:
  740. break;
  741. }
  742. return ret;
  743. }
  744. static void dib7000p_update_timf(struct dib7000p_state *state)
  745. {
  746. u32 timf = (dib7000p_read_word(state, 427) << 16) | dib7000p_read_word(state, 428);
  747. state->timf = timf * 160 / (state->current_bandwidth / 50);
  748. dib7000p_write_word(state, 23, (u16) (timf >> 16));
  749. dib7000p_write_word(state, 24, (u16) (timf & 0xffff));
  750. dprintk("updated timf_frequency: %d (default: %d)", state->timf, state->cfg.bw->timf);
  751. }
  752. u32 dib7000p_ctrl_timf(struct dvb_frontend *fe, u8 op, u32 timf)
  753. {
  754. struct dib7000p_state *state = fe->demodulator_priv;
  755. switch (op) {
  756. case DEMOD_TIMF_SET:
  757. state->timf = timf;
  758. break;
  759. case DEMOD_TIMF_UPDATE:
  760. dib7000p_update_timf(state);
  761. break;
  762. case DEMOD_TIMF_GET:
  763. break;
  764. }
  765. dib7000p_set_bandwidth(state, state->current_bandwidth);
  766. return state->timf;
  767. }
  768. EXPORT_SYMBOL(dib7000p_ctrl_timf);
  769. static void dib7000p_set_channel(struct dib7000p_state *state,
  770. struct dtv_frontend_properties *ch, u8 seq)
  771. {
  772. u16 value, est[4];
  773. dib7000p_set_bandwidth(state, BANDWIDTH_TO_KHZ(ch->bandwidth_hz));
  774. /* nfft, guard, qam, alpha */
  775. value = 0;
  776. switch (ch->transmission_mode) {
  777. case TRANSMISSION_MODE_2K:
  778. value |= (0 << 7);
  779. break;
  780. case TRANSMISSION_MODE_4K:
  781. value |= (2 << 7);
  782. break;
  783. default:
  784. case TRANSMISSION_MODE_8K:
  785. value |= (1 << 7);
  786. break;
  787. }
  788. switch (ch->guard_interval) {
  789. case GUARD_INTERVAL_1_32:
  790. value |= (0 << 5);
  791. break;
  792. case GUARD_INTERVAL_1_16:
  793. value |= (1 << 5);
  794. break;
  795. case GUARD_INTERVAL_1_4:
  796. value |= (3 << 5);
  797. break;
  798. default:
  799. case GUARD_INTERVAL_1_8:
  800. value |= (2 << 5);
  801. break;
  802. }
  803. switch (ch->modulation) {
  804. case QPSK:
  805. value |= (0 << 3);
  806. break;
  807. case QAM_16:
  808. value |= (1 << 3);
  809. break;
  810. default:
  811. case QAM_64:
  812. value |= (2 << 3);
  813. break;
  814. }
  815. switch (HIERARCHY_1) {
  816. case HIERARCHY_2:
  817. value |= 2;
  818. break;
  819. case HIERARCHY_4:
  820. value |= 4;
  821. break;
  822. default:
  823. case HIERARCHY_1:
  824. value |= 1;
  825. break;
  826. }
  827. dib7000p_write_word(state, 0, value);
  828. dib7000p_write_word(state, 5, (seq << 4) | 1); /* do not force tps, search list 0 */
  829. /* P_dintl_native, P_dintlv_inv, P_hrch, P_code_rate, P_select_hp */
  830. value = 0;
  831. if (1 != 0)
  832. value |= (1 << 6);
  833. if (ch->hierarchy == 1)
  834. value |= (1 << 4);
  835. if (1 == 1)
  836. value |= 1;
  837. switch ((ch->hierarchy == 0 || 1 == 1) ? ch->code_rate_HP : ch->code_rate_LP) {
  838. case FEC_2_3:
  839. value |= (2 << 1);
  840. break;
  841. case FEC_3_4:
  842. value |= (3 << 1);
  843. break;
  844. case FEC_5_6:
  845. value |= (5 << 1);
  846. break;
  847. case FEC_7_8:
  848. value |= (7 << 1);
  849. break;
  850. default:
  851. case FEC_1_2:
  852. value |= (1 << 1);
  853. break;
  854. }
  855. dib7000p_write_word(state, 208, value);
  856. /* offset loop parameters */
  857. dib7000p_write_word(state, 26, 0x6680);
  858. dib7000p_write_word(state, 32, 0x0003);
  859. dib7000p_write_word(state, 29, 0x1273);
  860. dib7000p_write_word(state, 33, 0x0005);
  861. /* P_dvsy_sync_wait */
  862. switch (ch->transmission_mode) {
  863. case TRANSMISSION_MODE_8K:
  864. value = 256;
  865. break;
  866. case TRANSMISSION_MODE_4K:
  867. value = 128;
  868. break;
  869. case TRANSMISSION_MODE_2K:
  870. default:
  871. value = 64;
  872. break;
  873. }
  874. switch (ch->guard_interval) {
  875. case GUARD_INTERVAL_1_16:
  876. value *= 2;
  877. break;
  878. case GUARD_INTERVAL_1_8:
  879. value *= 4;
  880. break;
  881. case GUARD_INTERVAL_1_4:
  882. value *= 8;
  883. break;
  884. default:
  885. case GUARD_INTERVAL_1_32:
  886. value *= 1;
  887. break;
  888. }
  889. if (state->cfg.diversity_delay == 0)
  890. state->div_sync_wait = (value * 3) / 2 + 48;
  891. else
  892. state->div_sync_wait = (value * 3) / 2 + state->cfg.diversity_delay;
  893. /* deactive the possibility of diversity reception if extended interleaver */
  894. state->div_force_off = !1 && ch->transmission_mode != TRANSMISSION_MODE_8K;
  895. dib7000p_set_diversity_in(&state->demod, state->div_state);
  896. /* channel estimation fine configuration */
  897. switch (ch->modulation) {
  898. case QAM_64:
  899. est[0] = 0x0148; /* P_adp_regul_cnt 0.04 */
  900. est[1] = 0xfff0; /* P_adp_noise_cnt -0.002 */
  901. est[2] = 0x00a4; /* P_adp_regul_ext 0.02 */
  902. est[3] = 0xfff8; /* P_adp_noise_ext -0.001 */
  903. break;
  904. case QAM_16:
  905. est[0] = 0x023d; /* P_adp_regul_cnt 0.07 */
  906. est[1] = 0xffdf; /* P_adp_noise_cnt -0.004 */
  907. est[2] = 0x00a4; /* P_adp_regul_ext 0.02 */
  908. est[3] = 0xfff0; /* P_adp_noise_ext -0.002 */
  909. break;
  910. default:
  911. est[0] = 0x099a; /* P_adp_regul_cnt 0.3 */
  912. est[1] = 0xffae; /* P_adp_noise_cnt -0.01 */
  913. est[2] = 0x0333; /* P_adp_regul_ext 0.1 */
  914. est[3] = 0xfff8; /* P_adp_noise_ext -0.002 */
  915. break;
  916. }
  917. for (value = 0; value < 4; value++)
  918. dib7000p_write_word(state, 187 + value, est[value]);
  919. }
  920. static int dib7000p_autosearch_start(struct dvb_frontend *demod)
  921. {
  922. struct dtv_frontend_properties *ch = &demod->dtv_property_cache;
  923. struct dib7000p_state *state = demod->demodulator_priv;
  924. struct dtv_frontend_properties schan;
  925. u32 value, factor;
  926. u32 internal = dib7000p_get_internal_freq(state);
  927. schan = *ch;
  928. schan.modulation = QAM_64;
  929. schan.guard_interval = GUARD_INTERVAL_1_32;
  930. schan.transmission_mode = TRANSMISSION_MODE_8K;
  931. schan.code_rate_HP = FEC_2_3;
  932. schan.code_rate_LP = FEC_3_4;
  933. schan.hierarchy = 0;
  934. dib7000p_set_channel(state, &schan, 7);
  935. factor = BANDWIDTH_TO_KHZ(ch->bandwidth_hz);
  936. if (factor >= 5000) {
  937. if (state->version == SOC7090)
  938. factor = 2;
  939. else
  940. factor = 1;
  941. } else
  942. factor = 6;
  943. value = 30 * internal * factor;
  944. dib7000p_write_word(state, 6, (u16) ((value >> 16) & 0xffff));
  945. dib7000p_write_word(state, 7, (u16) (value & 0xffff));
  946. value = 100 * internal * factor;
  947. dib7000p_write_word(state, 8, (u16) ((value >> 16) & 0xffff));
  948. dib7000p_write_word(state, 9, (u16) (value & 0xffff));
  949. value = 500 * internal * factor;
  950. dib7000p_write_word(state, 10, (u16) ((value >> 16) & 0xffff));
  951. dib7000p_write_word(state, 11, (u16) (value & 0xffff));
  952. value = dib7000p_read_word(state, 0);
  953. dib7000p_write_word(state, 0, (u16) ((1 << 9) | value));
  954. dib7000p_read_word(state, 1284);
  955. dib7000p_write_word(state, 0, (u16) value);
  956. return 0;
  957. }
  958. static int dib7000p_autosearch_is_irq(struct dvb_frontend *demod)
  959. {
  960. struct dib7000p_state *state = demod->demodulator_priv;
  961. u16 irq_pending = dib7000p_read_word(state, 1284);
  962. if (irq_pending & 0x1)
  963. return 1;
  964. if (irq_pending & 0x2)
  965. return 2;
  966. return 0;
  967. }
  968. static void dib7000p_spur_protect(struct dib7000p_state *state, u32 rf_khz, u32 bw)
  969. {
  970. static s16 notch[] = { 16143, 14402, 12238, 9713, 6902, 3888, 759, -2392 };
  971. static u8 sine[] = { 0, 2, 3, 5, 6, 8, 9, 11, 13, 14, 16, 17, 19, 20, 22,
  972. 24, 25, 27, 28, 30, 31, 33, 34, 36, 38, 39, 41, 42, 44, 45, 47, 48, 50, 51,
  973. 53, 55, 56, 58, 59, 61, 62, 64, 65, 67, 68, 70, 71, 73, 74, 76, 77, 79, 80,
  974. 82, 83, 85, 86, 88, 89, 91, 92, 94, 95, 97, 98, 99, 101, 102, 104, 105,
  975. 107, 108, 109, 111, 112, 114, 115, 117, 118, 119, 121, 122, 123, 125, 126,
  976. 128, 129, 130, 132, 133, 134, 136, 137, 138, 140, 141, 142, 144, 145, 146,
  977. 147, 149, 150, 151, 152, 154, 155, 156, 157, 159, 160, 161, 162, 164, 165,
  978. 166, 167, 168, 170, 171, 172, 173, 174, 175, 177, 178, 179, 180, 181, 182,
  979. 183, 184, 185, 186, 188, 189, 190, 191, 192, 193, 194, 195, 196, 197, 198,
  980. 199, 200, 201, 202, 203, 204, 205, 206, 207, 207, 208, 209, 210, 211, 212,
  981. 213, 214, 215, 215, 216, 217, 218, 219, 220, 220, 221, 222, 223, 224, 224,
  982. 225, 226, 227, 227, 228, 229, 229, 230, 231, 231, 232, 233, 233, 234, 235,
  983. 235, 236, 237, 237, 238, 238, 239, 239, 240, 241, 241, 242, 242, 243, 243,
  984. 244, 244, 245, 245, 245, 246, 246, 247, 247, 248, 248, 248, 249, 249, 249,
  985. 250, 250, 250, 251, 251, 251, 252, 252, 252, 252, 253, 253, 253, 253, 254,
  986. 254, 254, 254, 254, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
  987. 255, 255, 255, 255, 255, 255
  988. };
  989. u32 xtal = state->cfg.bw->xtal_hz / 1000;
  990. int f_rel = DIV_ROUND_CLOSEST(rf_khz, xtal) * xtal - rf_khz;
  991. int k;
  992. int coef_re[8], coef_im[8];
  993. int bw_khz = bw;
  994. u32 pha;
  995. dprintk("relative position of the Spur: %dk (RF: %dk, XTAL: %dk)", f_rel, rf_khz, xtal);
  996. if (f_rel < -bw_khz / 2 || f_rel > bw_khz / 2)
  997. return;
  998. bw_khz /= 100;
  999. dib7000p_write_word(state, 142, 0x0610);
  1000. for (k = 0; k < 8; k++) {
  1001. pha = ((f_rel * (k + 1) * 112 * 80 / bw_khz) / 1000) & 0x3ff;
  1002. if (pha == 0) {
  1003. coef_re[k] = 256;
  1004. coef_im[k] = 0;
  1005. } else if (pha < 256) {
  1006. coef_re[k] = sine[256 - (pha & 0xff)];
  1007. coef_im[k] = sine[pha & 0xff];
  1008. } else if (pha == 256) {
  1009. coef_re[k] = 0;
  1010. coef_im[k] = 256;
  1011. } else if (pha < 512) {
  1012. coef_re[k] = -sine[pha & 0xff];
  1013. coef_im[k] = sine[256 - (pha & 0xff)];
  1014. } else if (pha == 512) {
  1015. coef_re[k] = -256;
  1016. coef_im[k] = 0;
  1017. } else if (pha < 768) {
  1018. coef_re[k] = -sine[256 - (pha & 0xff)];
  1019. coef_im[k] = -sine[pha & 0xff];
  1020. } else if (pha == 768) {
  1021. coef_re[k] = 0;
  1022. coef_im[k] = -256;
  1023. } else {
  1024. coef_re[k] = sine[pha & 0xff];
  1025. coef_im[k] = -sine[256 - (pha & 0xff)];
  1026. }
  1027. coef_re[k] *= notch[k];
  1028. coef_re[k] += (1 << 14);
  1029. if (coef_re[k] >= (1 << 24))
  1030. coef_re[k] = (1 << 24) - 1;
  1031. coef_re[k] /= (1 << 15);
  1032. coef_im[k] *= notch[k];
  1033. coef_im[k] += (1 << 14);
  1034. if (coef_im[k] >= (1 << 24))
  1035. coef_im[k] = (1 << 24) - 1;
  1036. coef_im[k] /= (1 << 15);
  1037. dprintk("PALF COEF: %d re: %d im: %d", k, coef_re[k], coef_im[k]);
  1038. dib7000p_write_word(state, 143, (0 << 14) | (k << 10) | (coef_re[k] & 0x3ff));
  1039. dib7000p_write_word(state, 144, coef_im[k] & 0x3ff);
  1040. dib7000p_write_word(state, 143, (1 << 14) | (k << 10) | (coef_re[k] & 0x3ff));
  1041. }
  1042. dib7000p_write_word(state, 143, 0);
  1043. }
  1044. static int dib7000p_tune(struct dvb_frontend *demod)
  1045. {
  1046. struct dtv_frontend_properties *ch = &demod->dtv_property_cache;
  1047. struct dib7000p_state *state = demod->demodulator_priv;
  1048. u16 tmp = 0;
  1049. if (ch != NULL)
  1050. dib7000p_set_channel(state, ch, 0);
  1051. else
  1052. return -EINVAL;
  1053. // restart demod
  1054. dib7000p_write_word(state, 770, 0x4000);
  1055. dib7000p_write_word(state, 770, 0x0000);
  1056. msleep(45);
  1057. /* P_ctrl_inh_cor=0, P_ctrl_alpha_cor=4, P_ctrl_inh_isi=0, P_ctrl_alpha_isi=3, P_ctrl_inh_cor4=1, P_ctrl_alpha_cor4=3 */
  1058. tmp = (0 << 14) | (4 << 10) | (0 << 9) | (3 << 5) | (1 << 4) | (0x3);
  1059. if (state->sfn_workaround_active) {
  1060. dprintk("SFN workaround is active");
  1061. tmp |= (1 << 9);
  1062. dib7000p_write_word(state, 166, 0x4000);
  1063. } else {
  1064. dib7000p_write_word(state, 166, 0x0000);
  1065. }
  1066. dib7000p_write_word(state, 29, tmp);
  1067. // never achieved a lock with that bandwidth so far - wait for osc-freq to update
  1068. if (state->timf == 0)
  1069. msleep(200);
  1070. /* offset loop parameters */
  1071. /* P_timf_alpha, P_corm_alpha=6, P_corm_thres=0x80 */
  1072. tmp = (6 << 8) | 0x80;
  1073. switch (ch->transmission_mode) {
  1074. case TRANSMISSION_MODE_2K:
  1075. tmp |= (2 << 12);
  1076. break;
  1077. case TRANSMISSION_MODE_4K:
  1078. tmp |= (3 << 12);
  1079. break;
  1080. default:
  1081. case TRANSMISSION_MODE_8K:
  1082. tmp |= (4 << 12);
  1083. break;
  1084. }
  1085. dib7000p_write_word(state, 26, tmp); /* timf_a(6xxx) */
  1086. /* P_ctrl_freeze_pha_shift=0, P_ctrl_pha_off_max */
  1087. tmp = (0 << 4);
  1088. switch (ch->transmission_mode) {
  1089. case TRANSMISSION_MODE_2K:
  1090. tmp |= 0x6;
  1091. break;
  1092. case TRANSMISSION_MODE_4K:
  1093. tmp |= 0x7;
  1094. break;
  1095. default:
  1096. case TRANSMISSION_MODE_8K:
  1097. tmp |= 0x8;
  1098. break;
  1099. }
  1100. dib7000p_write_word(state, 32, tmp);
  1101. /* P_ctrl_sfreq_inh=0, P_ctrl_sfreq_step */
  1102. tmp = (0 << 4);
  1103. switch (ch->transmission_mode) {
  1104. case TRANSMISSION_MODE_2K:
  1105. tmp |= 0x6;
  1106. break;
  1107. case TRANSMISSION_MODE_4K:
  1108. tmp |= 0x7;
  1109. break;
  1110. default:
  1111. case TRANSMISSION_MODE_8K:
  1112. tmp |= 0x8;
  1113. break;
  1114. }
  1115. dib7000p_write_word(state, 33, tmp);
  1116. tmp = dib7000p_read_word(state, 509);
  1117. if (!((tmp >> 6) & 0x1)) {
  1118. /* restart the fec */
  1119. tmp = dib7000p_read_word(state, 771);
  1120. dib7000p_write_word(state, 771, tmp | (1 << 1));
  1121. dib7000p_write_word(state, 771, tmp);
  1122. msleep(40);
  1123. tmp = dib7000p_read_word(state, 509);
  1124. }
  1125. // we achieved a lock - it's time to update the osc freq
  1126. if ((tmp >> 6) & 0x1) {
  1127. dib7000p_update_timf(state);
  1128. /* P_timf_alpha += 2 */
  1129. tmp = dib7000p_read_word(state, 26);
  1130. dib7000p_write_word(state, 26, (tmp & ~(0xf << 12)) | ((((tmp >> 12) & 0xf) + 5) << 12));
  1131. }
  1132. if (state->cfg.spur_protect)
  1133. dib7000p_spur_protect(state, ch->frequency / 1000, BANDWIDTH_TO_KHZ(ch->bandwidth_hz));
  1134. dib7000p_set_bandwidth(state, BANDWIDTH_TO_KHZ(ch->bandwidth_hz));
  1135. return 0;
  1136. }
  1137. static int dib7000p_wakeup(struct dvb_frontend *demod)
  1138. {
  1139. struct dib7000p_state *state = demod->demodulator_priv;
  1140. dib7000p_set_power_mode(state, DIB7000P_POWER_ALL);
  1141. dib7000p_set_adc_state(state, DIBX000_SLOW_ADC_ON);
  1142. if (state->version == SOC7090)
  1143. dib7000p_sad_calib(state);
  1144. return 0;
  1145. }
  1146. static int dib7000p_sleep(struct dvb_frontend *demod)
  1147. {
  1148. struct dib7000p_state *state = demod->demodulator_priv;
  1149. if (state->version == SOC7090)
  1150. return dib7000p_set_power_mode(state, DIB7000P_POWER_INTERFACE_ONLY);
  1151. return dib7000p_set_output_mode(state, OUTMODE_HIGH_Z) | dib7000p_set_power_mode(state, DIB7000P_POWER_INTERFACE_ONLY);
  1152. }
  1153. static int dib7000p_identify(struct dib7000p_state *st)
  1154. {
  1155. u16 value;
  1156. dprintk("checking demod on I2C address: %d (%x)", st->i2c_addr, st->i2c_addr);
  1157. if ((value = dib7000p_read_word(st, 768)) != 0x01b3) {
  1158. dprintk("wrong Vendor ID (read=0x%x)", value);
  1159. return -EREMOTEIO;
  1160. }
  1161. if ((value = dib7000p_read_word(st, 769)) != 0x4000) {
  1162. dprintk("wrong Device ID (%x)", value);
  1163. return -EREMOTEIO;
  1164. }
  1165. return 0;
  1166. }
  1167. static int dib7000p_get_frontend(struct dvb_frontend *fe)
  1168. {
  1169. struct dtv_frontend_properties *fep = &fe->dtv_property_cache;
  1170. struct dib7000p_state *state = fe->demodulator_priv;
  1171. u16 tps = dib7000p_read_word(state, 463);
  1172. fep->inversion = INVERSION_AUTO;
  1173. fep->bandwidth_hz = BANDWIDTH_TO_HZ(state->current_bandwidth);
  1174. switch ((tps >> 8) & 0x3) {
  1175. case 0:
  1176. fep->transmission_mode = TRANSMISSION_MODE_2K;
  1177. break;
  1178. case 1:
  1179. fep->transmission_mode = TRANSMISSION_MODE_8K;
  1180. break;
  1181. /* case 2: fep->transmission_mode = TRANSMISSION_MODE_4K; break; */
  1182. }
  1183. switch (tps & 0x3) {
  1184. case 0:
  1185. fep->guard_interval = GUARD_INTERVAL_1_32;
  1186. break;
  1187. case 1:
  1188. fep->guard_interval = GUARD_INTERVAL_1_16;
  1189. break;
  1190. case 2:
  1191. fep->guard_interval = GUARD_INTERVAL_1_8;
  1192. break;
  1193. case 3:
  1194. fep->guard_interval = GUARD_INTERVAL_1_4;
  1195. break;
  1196. }
  1197. switch ((tps >> 14) & 0x3) {
  1198. case 0:
  1199. fep->modulation = QPSK;
  1200. break;
  1201. case 1:
  1202. fep->modulation = QAM_16;
  1203. break;
  1204. case 2:
  1205. default:
  1206. fep->modulation = QAM_64;
  1207. break;
  1208. }
  1209. /* as long as the frontend_param structure is fixed for hierarchical transmission I refuse to use it */
  1210. /* (tps >> 13) & 0x1 == hrch is used, (tps >> 10) & 0x7 == alpha */
  1211. fep->hierarchy = HIERARCHY_NONE;
  1212. switch ((tps >> 5) & 0x7) {
  1213. case 1:
  1214. fep->code_rate_HP = FEC_1_2;
  1215. break;
  1216. case 2:
  1217. fep->code_rate_HP = FEC_2_3;
  1218. break;
  1219. case 3:
  1220. fep->code_rate_HP = FEC_3_4;
  1221. break;
  1222. case 5:
  1223. fep->code_rate_HP = FEC_5_6;
  1224. break;
  1225. case 7:
  1226. default:
  1227. fep->code_rate_HP = FEC_7_8;
  1228. break;
  1229. }
  1230. switch ((tps >> 2) & 0x7) {
  1231. case 1:
  1232. fep->code_rate_LP = FEC_1_2;
  1233. break;
  1234. case 2:
  1235. fep->code_rate_LP = FEC_2_3;
  1236. break;
  1237. case 3:
  1238. fep->code_rate_LP = FEC_3_4;
  1239. break;
  1240. case 5:
  1241. fep->code_rate_LP = FEC_5_6;
  1242. break;
  1243. case 7:
  1244. default:
  1245. fep->code_rate_LP = FEC_7_8;
  1246. break;
  1247. }
  1248. /* native interleaver: (dib7000p_read_word(state, 464) >> 5) & 0x1 */
  1249. return 0;
  1250. }
  1251. static int dib7000p_set_frontend(struct dvb_frontend *fe)
  1252. {
  1253. struct dtv_frontend_properties *fep = &fe->dtv_property_cache;
  1254. struct dib7000p_state *state = fe->demodulator_priv;
  1255. int time, ret;
  1256. if (state->version == SOC7090)
  1257. dib7090_set_diversity_in(fe, 0);
  1258. else
  1259. dib7000p_set_output_mode(state, OUTMODE_HIGH_Z);
  1260. /* maybe the parameter has been changed */
  1261. state->sfn_workaround_active = buggy_sfn_workaround;
  1262. if (fe->ops.tuner_ops.set_params)
  1263. fe->ops.tuner_ops.set_params(fe);
  1264. /* start up the AGC */
  1265. state->agc_state = 0;
  1266. do {
  1267. time = dib7000p_agc_startup(fe);
  1268. if (time != -1)
  1269. msleep(time);
  1270. } while (time != -1);
  1271. if (fep->transmission_mode == TRANSMISSION_MODE_AUTO ||
  1272. fep->guard_interval == GUARD_INTERVAL_AUTO || fep->modulation == QAM_AUTO || fep->code_rate_HP == FEC_AUTO) {
  1273. int i = 800, found;
  1274. dib7000p_autosearch_start(fe);
  1275. do {
  1276. msleep(1);
  1277. found = dib7000p_autosearch_is_irq(fe);
  1278. } while (found == 0 && i--);
  1279. dprintk("autosearch returns: %d", found);
  1280. if (found == 0 || found == 1)
  1281. return 0;
  1282. dib7000p_get_frontend(fe);
  1283. }
  1284. ret = dib7000p_tune(fe);
  1285. /* make this a config parameter */
  1286. if (state->version == SOC7090) {
  1287. dib7090_set_output_mode(fe, state->cfg.output_mode);
  1288. if (state->cfg.enMpegOutput == 0) {
  1289. dib7090_setDibTxMux(state, MPEG_ON_DIBTX);
  1290. dib7090_setHostBusMux(state, DIBTX_ON_HOSTBUS);
  1291. }
  1292. } else
  1293. dib7000p_set_output_mode(state, state->cfg.output_mode);
  1294. return ret;
  1295. }
  1296. static int dib7000p_read_status(struct dvb_frontend *fe, fe_status_t * stat)
  1297. {
  1298. struct dib7000p_state *state = fe->demodulator_priv;
  1299. u16 lock = dib7000p_read_word(state, 509);
  1300. *stat = 0;
  1301. if (lock & 0x8000)
  1302. *stat |= FE_HAS_SIGNAL;
  1303. if (lock & 0x3000)
  1304. *stat |= FE_HAS_CARRIER;
  1305. if (lock & 0x0100)
  1306. *stat |= FE_HAS_VITERBI;
  1307. if (lock & 0x0010)
  1308. *stat |= FE_HAS_SYNC;
  1309. if ((lock & 0x0038) == 0x38)
  1310. *stat |= FE_HAS_LOCK;
  1311. return 0;
  1312. }
  1313. static int dib7000p_read_ber(struct dvb_frontend *fe, u32 * ber)
  1314. {
  1315. struct dib7000p_state *state = fe->demodulator_priv;
  1316. *ber = (dib7000p_read_word(state, 500) << 16) | dib7000p_read_word(state, 501);
  1317. return 0;
  1318. }
  1319. static int dib7000p_read_unc_blocks(struct dvb_frontend *fe, u32 * unc)
  1320. {
  1321. struct dib7000p_state *state = fe->demodulator_priv;
  1322. *unc = dib7000p_read_word(state, 506);
  1323. return 0;
  1324. }
  1325. static int dib7000p_read_signal_strength(struct dvb_frontend *fe, u16 * strength)
  1326. {
  1327. struct dib7000p_state *state = fe->demodulator_priv;
  1328. u16 val = dib7000p_read_word(state, 394);
  1329. *strength = 65535 - val;
  1330. return 0;
  1331. }
  1332. static int dib7000p_read_snr(struct dvb_frontend *fe, u16 * snr)
  1333. {
  1334. struct dib7000p_state *state = fe->demodulator_priv;
  1335. u16 val;
  1336. s32 signal_mant, signal_exp, noise_mant, noise_exp;
  1337. u32 result = 0;
  1338. val = dib7000p_read_word(state, 479);
  1339. noise_mant = (val >> 4) & 0xff;
  1340. noise_exp = ((val & 0xf) << 2);
  1341. val = dib7000p_read_word(state, 480);
  1342. noise_exp += ((val >> 14) & 0x3);
  1343. if ((noise_exp & 0x20) != 0)
  1344. noise_exp -= 0x40;
  1345. signal_mant = (val >> 6) & 0xFF;
  1346. signal_exp = (val & 0x3F);
  1347. if ((signal_exp & 0x20) != 0)
  1348. signal_exp -= 0x40;
  1349. if (signal_mant != 0)
  1350. result = intlog10(2) * 10 * signal_exp + 10 * intlog10(signal_mant);
  1351. else
  1352. result = intlog10(2) * 10 * signal_exp - 100;
  1353. if (noise_mant != 0)
  1354. result -= intlog10(2) * 10 * noise_exp + 10 * intlog10(noise_mant);
  1355. else
  1356. result -= intlog10(2) * 10 * noise_exp - 100;
  1357. *snr = result / ((1 << 24) / 10);
  1358. return 0;
  1359. }
  1360. static int dib7000p_fe_get_tune_settings(struct dvb_frontend *fe, struct dvb_frontend_tune_settings *tune)
  1361. {
  1362. tune->min_delay_ms = 1000;
  1363. return 0;
  1364. }
  1365. static void dib7000p_release(struct dvb_frontend *demod)
  1366. {
  1367. struct dib7000p_state *st = demod->demodulator_priv;
  1368. dibx000_exit_i2c_master(&st->i2c_master);
  1369. i2c_del_adapter(&st->dib7090_tuner_adap);
  1370. kfree(st);
  1371. }
  1372. int dib7000pc_detection(struct i2c_adapter *i2c_adap)
  1373. {
  1374. u8 *tx, *rx;
  1375. struct i2c_msg msg[2] = {
  1376. {.addr = 18 >> 1, .flags = 0, .len = 2},
  1377. {.addr = 18 >> 1, .flags = I2C_M_RD, .len = 2},
  1378. };
  1379. int ret = 0;
  1380. tx = kzalloc(2*sizeof(u8), GFP_KERNEL);
  1381. if (!tx)
  1382. return -ENOMEM;
  1383. rx = kzalloc(2*sizeof(u8), GFP_KERNEL);
  1384. if (!rx) {
  1385. ret = -ENOMEM;
  1386. goto rx_memory_error;
  1387. }
  1388. msg[0].buf = tx;
  1389. msg[1].buf = rx;
  1390. tx[0] = 0x03;
  1391. tx[1] = 0x00;
  1392. if (i2c_transfer(i2c_adap, msg, 2) == 2)
  1393. if (rx[0] == 0x01 && rx[1] == 0xb3) {
  1394. dprintk("-D- DiB7000PC detected");
  1395. return 1;
  1396. }
  1397. msg[0].addr = msg[1].addr = 0x40;
  1398. if (i2c_transfer(i2c_adap, msg, 2) == 2)
  1399. if (rx[0] == 0x01 && rx[1] == 0xb3) {
  1400. dprintk("-D- DiB7000PC detected");
  1401. return 1;
  1402. }
  1403. dprintk("-D- DiB7000PC not detected");
  1404. kfree(rx);
  1405. rx_memory_error:
  1406. kfree(tx);
  1407. return ret;
  1408. }
  1409. EXPORT_SYMBOL(dib7000pc_detection);
  1410. struct i2c_adapter *dib7000p_get_i2c_master(struct dvb_frontend *demod, enum dibx000_i2c_interface intf, int gating)
  1411. {
  1412. struct dib7000p_state *st = demod->demodulator_priv;
  1413. return dibx000_get_i2c_adapter(&st->i2c_master, intf, gating);
  1414. }
  1415. EXPORT_SYMBOL(dib7000p_get_i2c_master);
  1416. int dib7000p_pid_filter_ctrl(struct dvb_frontend *fe, u8 onoff)
  1417. {
  1418. struct dib7000p_state *state = fe->demodulator_priv;
  1419. u16 val = dib7000p_read_word(state, 235) & 0xffef;
  1420. val |= (onoff & 0x1) << 4;
  1421. dprintk("PID filter enabled %d", onoff);
  1422. return dib7000p_write_word(state, 235, val);
  1423. }
  1424. EXPORT_SYMBOL(dib7000p_pid_filter_ctrl);
  1425. int dib7000p_pid_filter(struct dvb_frontend *fe, u8 id, u16 pid, u8 onoff)
  1426. {
  1427. struct dib7000p_state *state = fe->demodulator_priv;
  1428. dprintk("PID filter: index %x, PID %d, OnOff %d", id, pid, onoff);
  1429. return dib7000p_write_word(state, 241 + id, onoff ? (1 << 13) | pid : 0);
  1430. }
  1431. EXPORT_SYMBOL(dib7000p_pid_filter);
  1432. int dib7000p_i2c_enumeration(struct i2c_adapter *i2c, int no_of_demods, u8 default_addr, struct dib7000p_config cfg[])
  1433. {
  1434. struct dib7000p_state *dpst;
  1435. int k = 0;
  1436. u8 new_addr = 0;
  1437. dpst = kzalloc(sizeof(struct dib7000p_state), GFP_KERNEL);
  1438. if (!dpst)
  1439. return -ENOMEM;
  1440. dpst->i2c_adap = i2c;
  1441. mutex_init(&dpst->i2c_buffer_lock);
  1442. for (k = no_of_demods - 1; k >= 0; k--) {
  1443. dpst->cfg = cfg[k];
  1444. /* designated i2c address */
  1445. if (cfg[k].default_i2c_addr != 0)
  1446. new_addr = cfg[k].default_i2c_addr + (k << 1);
  1447. else
  1448. new_addr = (0x40 + k) << 1;
  1449. dpst->i2c_addr = new_addr;
  1450. dib7000p_write_word(dpst, 1287, 0x0003); /* sram lead in, rdy */
  1451. if (dib7000p_identify(dpst) != 0) {
  1452. dpst->i2c_addr = default_addr;
  1453. dib7000p_write_word(dpst, 1287, 0x0003); /* sram lead in, rdy */
  1454. if (dib7000p_identify(dpst) != 0) {
  1455. dprintk("DiB7000P #%d: not identified\n", k);
  1456. kfree(dpst);
  1457. return -EIO;
  1458. }
  1459. }
  1460. /* start diversity to pull_down div_str - just for i2c-enumeration */
  1461. dib7000p_set_output_mode(dpst, OUTMODE_DIVERSITY);
  1462. /* set new i2c address and force divstart */
  1463. dib7000p_write_word(dpst, 1285, (new_addr << 2) | 0x2);
  1464. dprintk("IC %d initialized (to i2c_address 0x%x)", k, new_addr);
  1465. }
  1466. for (k = 0; k < no_of_demods; k++) {
  1467. dpst->cfg = cfg[k];
  1468. if (cfg[k].default_i2c_addr != 0)
  1469. dpst->i2c_addr = (cfg[k].default_i2c_addr + k) << 1;
  1470. else
  1471. dpst->i2c_addr = (0x40 + k) << 1;
  1472. // unforce divstr
  1473. dib7000p_write_word(dpst, 1285, dpst->i2c_addr << 2);
  1474. /* deactivate div - it was just for i2c-enumeration */
  1475. dib7000p_set_output_mode(dpst, OUTMODE_HIGH_Z);
  1476. }
  1477. kfree(dpst);
  1478. return 0;
  1479. }
  1480. EXPORT_SYMBOL(dib7000p_i2c_enumeration);
  1481. static const s32 lut_1000ln_mant[] = {
  1482. 6908, 6956, 7003, 7047, 7090, 7131, 7170, 7208, 7244, 7279, 7313, 7346, 7377, 7408, 7438, 7467, 7495, 7523, 7549, 7575, 7600
  1483. };
  1484. static s32 dib7000p_get_adc_power(struct dvb_frontend *fe)
  1485. {
  1486. struct dib7000p_state *state = fe->demodulator_priv;
  1487. u32 tmp_val = 0, exp = 0, mant = 0;
  1488. s32 pow_i;
  1489. u16 buf[2];
  1490. u8 ix = 0;
  1491. buf[0] = dib7000p_read_word(state, 0x184);
  1492. buf[1] = dib7000p_read_word(state, 0x185);
  1493. pow_i = (buf[0] << 16) | buf[1];
  1494. dprintk("raw pow_i = %d", pow_i);
  1495. tmp_val = pow_i;
  1496. while (tmp_val >>= 1)
  1497. exp++;
  1498. mant = (pow_i * 1000 / (1 << exp));
  1499. dprintk(" mant = %d exp = %d", mant / 1000, exp);
  1500. ix = (u8) ((mant - 1000) / 100); /* index of the LUT */
  1501. dprintk(" ix = %d", ix);
  1502. pow_i = (lut_1000ln_mant[ix] + 693 * (exp - 20) - 6908);
  1503. pow_i = (pow_i << 8) / 1000;
  1504. dprintk(" pow_i = %d", pow_i);
  1505. return pow_i;
  1506. }
  1507. static int map_addr_to_serpar_number(struct i2c_msg *msg)
  1508. {
  1509. if ((msg->buf[0] <= 15))
  1510. msg->buf[0] -= 1;
  1511. else if (msg->buf[0] == 17)
  1512. msg->buf[0] = 15;
  1513. else if (msg->buf[0] == 16)
  1514. msg->buf[0] = 17;
  1515. else if (msg->buf[0] == 19)
  1516. msg->buf[0] = 16;
  1517. else if (msg->buf[0] >= 21 && msg->buf[0] <= 25)
  1518. msg->buf[0] -= 3;
  1519. else if (msg->buf[0] == 28)
  1520. msg->buf[0] = 23;
  1521. else
  1522. return -EINVAL;
  1523. return 0;
  1524. }
  1525. static int w7090p_tuner_write_serpar(struct i2c_adapter *i2c_adap, struct i2c_msg msg[], int num)
  1526. {
  1527. struct dib7000p_state *state = i2c_get_adapdata(i2c_adap);
  1528. u8 n_overflow = 1;
  1529. u16 i = 1000;
  1530. u16 serpar_num = msg[0].buf[0];
  1531. while (n_overflow == 1 && i) {
  1532. n_overflow = (dib7000p_read_word(state, 1984) >> 1) & 0x1;
  1533. i--;
  1534. if (i == 0)
  1535. dprintk("Tuner ITF: write busy (overflow)");
  1536. }
  1537. dib7000p_write_word(state, 1985, (1 << 6) | (serpar_num & 0x3f));
  1538. dib7000p_write_word(state, 1986, (msg[0].buf[1] << 8) | msg[0].buf[2]);
  1539. return num;
  1540. }
  1541. static int w7090p_tuner_read_serpar(struct i2c_adapter *i2c_adap, struct i2c_msg msg[], int num)
  1542. {
  1543. struct dib7000p_state *state = i2c_get_adapdata(i2c_adap);
  1544. u8 n_overflow = 1, n_empty = 1;
  1545. u16 i = 1000;
  1546. u16 serpar_num = msg[0].buf[0];
  1547. u16 read_word;
  1548. while (n_overflow == 1 && i) {
  1549. n_overflow = (dib7000p_read_word(state, 1984) >> 1) & 0x1;
  1550. i--;
  1551. if (i == 0)
  1552. dprintk("TunerITF: read busy (overflow)");
  1553. }
  1554. dib7000p_write_word(state, 1985, (0 << 6) | (serpar_num & 0x3f));
  1555. i = 1000;
  1556. while (n_empty == 1 && i) {
  1557. n_empty = dib7000p_read_word(state, 1984) & 0x1;
  1558. i--;
  1559. if (i == 0)
  1560. dprintk("TunerITF: read busy (empty)");
  1561. }
  1562. read_word = dib7000p_read_word(state, 1987);
  1563. msg[1].buf[0] = (read_word >> 8) & 0xff;
  1564. msg[1].buf[1] = (read_word) & 0xff;
  1565. return num;
  1566. }
  1567. static int w7090p_tuner_rw_serpar(struct i2c_adapter *i2c_adap, struct i2c_msg msg[], int num)
  1568. {
  1569. if (map_addr_to_serpar_number(&msg[0]) == 0) { /* else = Tuner regs to ignore : DIG_CFG, CTRL_RF_LT, PLL_CFG, PWM1_REG, ADCCLK, DIG_CFG_3; SLEEP_EN... */
  1570. if (num == 1) { /* write */
  1571. return w7090p_tuner_write_serpar(i2c_adap, msg, 1);
  1572. } else { /* read */
  1573. return w7090p_tuner_read_serpar(i2c_adap, msg, 2);
  1574. }
  1575. }
  1576. return num;
  1577. }
  1578. static int dib7090p_rw_on_apb(struct i2c_adapter *i2c_adap,
  1579. struct i2c_msg msg[], int num, u16 apb_address)
  1580. {
  1581. struct dib7000p_state *state = i2c_get_adapdata(i2c_adap);
  1582. u16 word;
  1583. if (num == 1) { /* write */
  1584. dib7000p_write_word(state, apb_address, ((msg[0].buf[1] << 8) | (msg[0].buf[2])));
  1585. } else {
  1586. word = dib7000p_read_word(state, apb_address);
  1587. msg[1].buf[0] = (word >> 8) & 0xff;
  1588. msg[1].buf[1] = (word) & 0xff;
  1589. }
  1590. return num;
  1591. }
  1592. static int dib7090_tuner_xfer(struct i2c_adapter *i2c_adap, struct i2c_msg msg[], int num)
  1593. {
  1594. struct dib7000p_state *state = i2c_get_adapdata(i2c_adap);
  1595. u16 apb_address = 0, word;
  1596. int i = 0;
  1597. switch (msg[0].buf[0]) {
  1598. case 0x12:
  1599. apb_address = 1920;
  1600. break;
  1601. case 0x14:
  1602. apb_address = 1921;
  1603. break;
  1604. case 0x24:
  1605. apb_address = 1922;
  1606. break;
  1607. case 0x1a:
  1608. apb_address = 1923;
  1609. break;
  1610. case 0x22:
  1611. apb_address = 1924;
  1612. break;
  1613. case 0x33:
  1614. apb_address = 1926;
  1615. break;
  1616. case 0x34:
  1617. apb_address = 1927;
  1618. break;
  1619. case 0x35:
  1620. apb_address = 1928;
  1621. break;
  1622. case 0x36:
  1623. apb_address = 1929;
  1624. break;
  1625. case 0x37:
  1626. apb_address = 1930;
  1627. break;
  1628. case 0x38:
  1629. apb_address = 1931;
  1630. break;
  1631. case 0x39:
  1632. apb_address = 1932;
  1633. break;
  1634. case 0x2a:
  1635. apb_address = 1935;
  1636. break;
  1637. case 0x2b:
  1638. apb_address = 1936;
  1639. break;
  1640. case 0x2c:
  1641. apb_address = 1937;
  1642. break;
  1643. case 0x2d:
  1644. apb_address = 1938;
  1645. break;
  1646. case 0x2e:
  1647. apb_address = 1939;
  1648. break;
  1649. case 0x2f:
  1650. apb_address = 1940;
  1651. break;
  1652. case 0x30:
  1653. apb_address = 1941;
  1654. break;
  1655. case 0x31:
  1656. apb_address = 1942;
  1657. break;
  1658. case 0x32:
  1659. apb_address = 1943;
  1660. break;
  1661. case 0x3e:
  1662. apb_address = 1944;
  1663. break;
  1664. case 0x3f:
  1665. apb_address = 1945;
  1666. break;
  1667. case 0x40:
  1668. apb_address = 1948;
  1669. break;
  1670. case 0x25:
  1671. apb_address = 914;
  1672. break;
  1673. case 0x26:
  1674. apb_address = 915;
  1675. break;
  1676. case 0x27:
  1677. apb_address = 917;
  1678. break;
  1679. case 0x28:
  1680. apb_address = 916;
  1681. break;
  1682. case 0x1d:
  1683. i = ((dib7000p_read_word(state, 72) >> 12) & 0x3);
  1684. word = dib7000p_read_word(state, 384 + i);
  1685. msg[1].buf[0] = (word >> 8) & 0xff;
  1686. msg[1].buf[1] = (word) & 0xff;
  1687. return num;
  1688. case 0x1f:
  1689. if (num == 1) { /* write */
  1690. word = (u16) ((msg[0].buf[1] << 8) | msg[0].buf[2]);
  1691. word &= 0x3;
  1692. word = (dib7000p_read_word(state, 72) & ~(3 << 12)) | (word << 12);
  1693. dib7000p_write_word(state, 72, word); /* Set the proper input */
  1694. return num;
  1695. }
  1696. }
  1697. if (apb_address != 0) /* R/W acces via APB */
  1698. return dib7090p_rw_on_apb(i2c_adap, msg, num, apb_address);
  1699. else /* R/W access via SERPAR */
  1700. return w7090p_tuner_rw_serpar(i2c_adap, msg, num);
  1701. return 0;
  1702. }
  1703. static u32 dib7000p_i2c_func(struct i2c_adapter *adapter)
  1704. {
  1705. return I2C_FUNC_I2C;
  1706. }
  1707. static struct i2c_algorithm dib7090_tuner_xfer_algo = {
  1708. .master_xfer = dib7090_tuner_xfer,
  1709. .functionality = dib7000p_i2c_func,
  1710. };
  1711. struct i2c_adapter *dib7090_get_i2c_tuner(struct dvb_frontend *fe)
  1712. {
  1713. struct dib7000p_state *st = fe->demodulator_priv;
  1714. return &st->dib7090_tuner_adap;
  1715. }
  1716. EXPORT_SYMBOL(dib7090_get_i2c_tuner);
  1717. static int dib7090_host_bus_drive(struct dib7000p_state *state, u8 drive)
  1718. {
  1719. u16 reg;
  1720. /* drive host bus 2, 3, 4 */
  1721. reg = dib7000p_read_word(state, 1798) & ~((0x7) | (0x7 << 6) | (0x7 << 12));
  1722. reg |= (drive << 12) | (drive << 6) | drive;
  1723. dib7000p_write_word(state, 1798, reg);
  1724. /* drive host bus 5,6 */
  1725. reg = dib7000p_read_word(state, 1799) & ~((0x7 << 2) | (0x7 << 8));
  1726. reg |= (drive << 8) | (drive << 2);
  1727. dib7000p_write_word(state, 1799, reg);
  1728. /* drive host bus 7, 8, 9 */
  1729. reg = dib7000p_read_word(state, 1800) & ~((0x7) | (0x7 << 6) | (0x7 << 12));
  1730. reg |= (drive << 12) | (drive << 6) | drive;
  1731. dib7000p_write_word(state, 1800, reg);
  1732. /* drive host bus 10, 11 */
  1733. reg = dib7000p_read_word(state, 1801) & ~((0x7 << 2) | (0x7 << 8));
  1734. reg |= (drive << 8) | (drive << 2);
  1735. dib7000p_write_word(state, 1801, reg);
  1736. /* drive host bus 12, 13, 14 */
  1737. reg = dib7000p_read_word(state, 1802) & ~((0x7) | (0x7 << 6) | (0x7 << 12));
  1738. reg |= (drive << 12) | (drive << 6) | drive;
  1739. dib7000p_write_word(state, 1802, reg);
  1740. return 0;
  1741. }
  1742. static u32 dib7090_calcSyncFreq(u32 P_Kin, u32 P_Kout, u32 insertExtSynchro, u32 syncSize)
  1743. {
  1744. u32 quantif = 3;
  1745. u32 nom = (insertExtSynchro * P_Kin + syncSize);
  1746. u32 denom = P_Kout;
  1747. u32 syncFreq = ((nom << quantif) / denom);
  1748. if ((syncFreq & ((1 << quantif) - 1)) != 0)
  1749. syncFreq = (syncFreq >> quantif) + 1;
  1750. else
  1751. syncFreq = (syncFreq >> quantif);
  1752. if (syncFreq != 0)
  1753. syncFreq = syncFreq - 1;
  1754. return syncFreq;
  1755. }
  1756. static int dib7090_cfg_DibTx(struct dib7000p_state *state, u32 P_Kin, u32 P_Kout, u32 insertExtSynchro, u32 synchroMode, u32 syncWord, u32 syncSize)
  1757. {
  1758. dprintk("Configure DibStream Tx");
  1759. dib7000p_write_word(state, 1615, 1);
  1760. dib7000p_write_word(state, 1603, P_Kin);
  1761. dib7000p_write_word(state, 1605, P_Kout);
  1762. dib7000p_write_word(state, 1606, insertExtSynchro);
  1763. dib7000p_write_word(state, 1608, synchroMode);
  1764. dib7000p_write_word(state, 1609, (syncWord >> 16) & 0xffff);
  1765. dib7000p_write_word(state, 1610, syncWord & 0xffff);
  1766. dib7000p_write_word(state, 1612, syncSize);
  1767. dib7000p_write_word(state, 1615, 0);
  1768. return 0;
  1769. }
  1770. static int dib7090_cfg_DibRx(struct dib7000p_state *state, u32 P_Kin, u32 P_Kout, u32 synchroMode, u32 insertExtSynchro, u32 syncWord, u32 syncSize,
  1771. u32 dataOutRate)
  1772. {
  1773. u32 syncFreq;
  1774. dprintk("Configure DibStream Rx");
  1775. if ((P_Kin != 0) && (P_Kout != 0)) {
  1776. syncFreq = dib7090_calcSyncFreq(P_Kin, P_Kout, insertExtSynchro, syncSize);
  1777. dib7000p_write_word(state, 1542, syncFreq);
  1778. }
  1779. dib7000p_write_word(state, 1554, 1);
  1780. dib7000p_write_word(state, 1536, P_Kin);
  1781. dib7000p_write_word(state, 1537, P_Kout);
  1782. dib7000p_write_word(state, 1539, synchroMode);
  1783. dib7000p_write_word(state, 1540, (syncWord >> 16) & 0xffff);
  1784. dib7000p_write_word(state, 1541, syncWord & 0xffff);
  1785. dib7000p_write_word(state, 1543, syncSize);
  1786. dib7000p_write_word(state, 1544, dataOutRate);
  1787. dib7000p_write_word(state, 1554, 0);
  1788. return 0;
  1789. }
  1790. static void dib7090_enMpegMux(struct dib7000p_state *state, int onoff)
  1791. {
  1792. u16 reg_1287 = dib7000p_read_word(state, 1287);
  1793. switch (onoff) {
  1794. case 1:
  1795. reg_1287 &= ~(1<<7);
  1796. break;
  1797. case 0:
  1798. reg_1287 |= (1<<7);
  1799. break;
  1800. }
  1801. dib7000p_write_word(state, 1287, reg_1287);
  1802. }
  1803. static void dib7090_configMpegMux(struct dib7000p_state *state,
  1804. u16 pulseWidth, u16 enSerialMode, u16 enSerialClkDiv2)
  1805. {
  1806. dprintk("Enable Mpeg mux");
  1807. dib7090_enMpegMux(state, 0);
  1808. /* If the input mode is MPEG do not divide the serial clock */
  1809. if ((enSerialMode == 1) && (state->input_mode_mpeg == 1))
  1810. enSerialClkDiv2 = 0;
  1811. dib7000p_write_word(state, 1287, ((pulseWidth & 0x1f) << 2)
  1812. | ((enSerialMode & 0x1) << 1)
  1813. | (enSerialClkDiv2 & 0x1));
  1814. dib7090_enMpegMux(state, 1);
  1815. }
  1816. static void dib7090_setDibTxMux(struct dib7000p_state *state, int mode)
  1817. {
  1818. u16 reg_1288 = dib7000p_read_word(state, 1288) & ~(0x7 << 7);
  1819. switch (mode) {
  1820. case MPEG_ON_DIBTX:
  1821. dprintk("SET MPEG ON DIBSTREAM TX");
  1822. dib7090_cfg_DibTx(state, 8, 5, 0, 0, 0, 0);
  1823. reg_1288 |= (1<<9);
  1824. break;
  1825. case DIV_ON_DIBTX:
  1826. dprintk("SET DIV_OUT ON DIBSTREAM TX");
  1827. dib7090_cfg_DibTx(state, 5, 5, 0, 0, 0, 0);
  1828. reg_1288 |= (1<<8);
  1829. break;
  1830. case ADC_ON_DIBTX:
  1831. dprintk("SET ADC_OUT ON DIBSTREAM TX");
  1832. dib7090_cfg_DibTx(state, 20, 5, 10, 0, 0, 0);
  1833. reg_1288 |= (1<<7);
  1834. break;
  1835. default:
  1836. break;
  1837. }
  1838. dib7000p_write_word(state, 1288, reg_1288);
  1839. }
  1840. static void dib7090_setHostBusMux(struct dib7000p_state *state, int mode)
  1841. {
  1842. u16 reg_1288 = dib7000p_read_word(state, 1288) & ~(0x7 << 4);
  1843. switch (mode) {
  1844. case DEMOUT_ON_HOSTBUS:
  1845. dprintk("SET DEM OUT OLD INTERF ON HOST BUS");
  1846. dib7090_enMpegMux(state, 0);
  1847. reg_1288 |= (1<<6);
  1848. break;
  1849. case DIBTX_ON_HOSTBUS:
  1850. dprintk("SET DIBSTREAM TX ON HOST BUS");
  1851. dib7090_enMpegMux(state, 0);
  1852. reg_1288 |= (1<<5);
  1853. break;
  1854. case MPEG_ON_HOSTBUS:
  1855. dprintk("SET MPEG MUX ON HOST BUS");
  1856. reg_1288 |= (1<<4);
  1857. break;
  1858. default:
  1859. break;
  1860. }
  1861. dib7000p_write_word(state, 1288, reg_1288);
  1862. }
  1863. int dib7090_set_diversity_in(struct dvb_frontend *fe, int onoff)
  1864. {
  1865. struct dib7000p_state *state = fe->demodulator_priv;
  1866. u16 reg_1287;
  1867. switch (onoff) {
  1868. case 0: /* only use the internal way - not the diversity input */
  1869. dprintk("%s mode OFF : by default Enable Mpeg INPUT", __func__);
  1870. dib7090_cfg_DibRx(state, 8, 5, 0, 0, 0, 8, 0);
  1871. /* Do not divide the serial clock of MPEG MUX */
  1872. /* in SERIAL MODE in case input mode MPEG is used */
  1873. reg_1287 = dib7000p_read_word(state, 1287);
  1874. /* enSerialClkDiv2 == 1 ? */
  1875. if ((reg_1287 & 0x1) == 1) {
  1876. /* force enSerialClkDiv2 = 0 */
  1877. reg_1287 &= ~0x1;
  1878. dib7000p_write_word(state, 1287, reg_1287);
  1879. }
  1880. state->input_mode_mpeg = 1;
  1881. break;
  1882. case 1: /* both ways */
  1883. case 2: /* only the diversity input */
  1884. dprintk("%s ON : Enable diversity INPUT", __func__);
  1885. dib7090_cfg_DibRx(state, 5, 5, 0, 0, 0, 0, 0);
  1886. state->input_mode_mpeg = 0;
  1887. break;
  1888. }
  1889. dib7000p_set_diversity_in(&state->demod, onoff);
  1890. return 0;
  1891. }
  1892. static int dib7090_set_output_mode(struct dvb_frontend *fe, int mode)
  1893. {
  1894. struct dib7000p_state *state = fe->demodulator_priv;
  1895. u16 outreg, smo_mode, fifo_threshold;
  1896. u8 prefer_mpeg_mux_use = 1;
  1897. int ret = 0;
  1898. dib7090_host_bus_drive(state, 1);
  1899. fifo_threshold = 1792;
  1900. smo_mode = (dib7000p_read_word(state, 235) & 0x0050) | (1 << 1);
  1901. outreg = dib7000p_read_word(state, 1286) & ~((1 << 10) | (0x7 << 6) | (1 << 1));
  1902. switch (mode) {
  1903. case OUTMODE_HIGH_Z:
  1904. outreg = 0;
  1905. break;
  1906. case OUTMODE_MPEG2_SERIAL:
  1907. if (prefer_mpeg_mux_use) {
  1908. dprintk("setting output mode TS_SERIAL using Mpeg Mux");
  1909. dib7090_configMpegMux(state, 3, 1, 1);
  1910. dib7090_setHostBusMux(state, MPEG_ON_HOSTBUS);
  1911. } else {/* Use Smooth block */
  1912. dprintk("setting output mode TS_SERIAL using Smooth bloc");
  1913. dib7090_setHostBusMux(state, DEMOUT_ON_HOSTBUS);
  1914. outreg |= (2<<6) | (0 << 1);
  1915. }
  1916. break;
  1917. case OUTMODE_MPEG2_PAR_GATED_CLK:
  1918. if (prefer_mpeg_mux_use) {
  1919. dprintk("setting output mode TS_PARALLEL_GATED using Mpeg Mux");
  1920. dib7090_configMpegMux(state, 2, 0, 0);
  1921. dib7090_setHostBusMux(state, MPEG_ON_HOSTBUS);
  1922. } else { /* Use Smooth block */
  1923. dprintk("setting output mode TS_PARALLEL_GATED using Smooth block");
  1924. dib7090_setHostBusMux(state, DEMOUT_ON_HOSTBUS);
  1925. outreg |= (0<<6);
  1926. }
  1927. break;
  1928. case OUTMODE_MPEG2_PAR_CONT_CLK: /* Using Smooth block only */
  1929. dprintk("setting output mode TS_PARALLEL_CONT using Smooth block");
  1930. dib7090_setHostBusMux(state, DEMOUT_ON_HOSTBUS);
  1931. outreg |= (1<<6);
  1932. break;
  1933. case OUTMODE_MPEG2_FIFO: /* Using Smooth block because not supported by new Mpeg Mux bloc */
  1934. dprintk("setting output mode TS_FIFO using Smooth block");
  1935. dib7090_setHostBusMux(state, DEMOUT_ON_HOSTBUS);
  1936. outreg |= (5<<6);
  1937. smo_mode |= (3 << 1);
  1938. fifo_threshold = 512;
  1939. break;
  1940. case OUTMODE_DIVERSITY:
  1941. dprintk("setting output mode MODE_DIVERSITY");
  1942. dib7090_setDibTxMux(state, DIV_ON_DIBTX);
  1943. dib7090_setHostBusMux(state, DIBTX_ON_HOSTBUS);
  1944. break;
  1945. case OUTMODE_ANALOG_ADC:
  1946. dprintk("setting output mode MODE_ANALOG_ADC");
  1947. dib7090_setDibTxMux(state, ADC_ON_DIBTX);
  1948. dib7090_setHostBusMux(state, DIBTX_ON_HOSTBUS);
  1949. break;
  1950. }
  1951. if (mode != OUTMODE_HIGH_Z)
  1952. outreg |= (1 << 10);
  1953. if (state->cfg.output_mpeg2_in_188_bytes)
  1954. smo_mode |= (1 << 5);
  1955. ret |= dib7000p_write_word(state, 235, smo_mode);
  1956. ret |= dib7000p_write_word(state, 236, fifo_threshold); /* synchronous fread */
  1957. ret |= dib7000p_write_word(state, 1286, outreg);
  1958. return ret;
  1959. }
  1960. int dib7090_tuner_sleep(struct dvb_frontend *fe, int onoff)
  1961. {
  1962. struct dib7000p_state *state = fe->demodulator_priv;
  1963. u16 en_cur_state;
  1964. dprintk("sleep dib7090: %d", onoff);
  1965. en_cur_state = dib7000p_read_word(state, 1922);
  1966. if (en_cur_state > 0xff)
  1967. state->tuner_enable = en_cur_state;
  1968. if (onoff)
  1969. en_cur_state &= 0x00ff;
  1970. else {
  1971. if (state->tuner_enable != 0)
  1972. en_cur_state = state->tuner_enable;
  1973. }
  1974. dib7000p_write_word(state, 1922, en_cur_state);
  1975. return 0;
  1976. }
  1977. EXPORT_SYMBOL(dib7090_tuner_sleep);
  1978. int dib7090_get_adc_power(struct dvb_frontend *fe)
  1979. {
  1980. return dib7000p_get_adc_power(fe);
  1981. }
  1982. EXPORT_SYMBOL(dib7090_get_adc_power);
  1983. int dib7090_slave_reset(struct dvb_frontend *fe)
  1984. {
  1985. struct dib7000p_state *state = fe->demodulator_priv;
  1986. u16 reg;
  1987. reg = dib7000p_read_word(state, 1794);
  1988. dib7000p_write_word(state, 1794, reg | (4 << 12));
  1989. dib7000p_write_word(state, 1032, 0xffff);
  1990. return 0;
  1991. }
  1992. EXPORT_SYMBOL(dib7090_slave_reset);
  1993. static struct dvb_frontend_ops dib7000p_ops;
  1994. struct dvb_frontend *dib7000p_attach(struct i2c_adapter *i2c_adap, u8 i2c_addr, struct dib7000p_config *cfg)
  1995. {
  1996. struct dvb_frontend *demod;
  1997. struct dib7000p_state *st;
  1998. st = kzalloc(sizeof(struct dib7000p_state), GFP_KERNEL);
  1999. if (st == NULL)
  2000. return NULL;
  2001. memcpy(&st->cfg, cfg, sizeof(struct dib7000p_config));
  2002. st->i2c_adap = i2c_adap;
  2003. st->i2c_addr = i2c_addr;
  2004. st->gpio_val = cfg->gpio_val;
  2005. st->gpio_dir = cfg->gpio_dir;
  2006. /* Ensure the output mode remains at the previous default if it's
  2007. * not specifically set by the caller.
  2008. */
  2009. if ((st->cfg.output_mode != OUTMODE_MPEG2_SERIAL) && (st->cfg.output_mode != OUTMODE_MPEG2_PAR_GATED_CLK))
  2010. st->cfg.output_mode = OUTMODE_MPEG2_FIFO;
  2011. demod = &st->demod;
  2012. demod->demodulator_priv = st;
  2013. memcpy(&st->demod.ops, &dib7000p_ops, sizeof(struct dvb_frontend_ops));
  2014. mutex_init(&st->i2c_buffer_lock);
  2015. dib7000p_write_word(st, 1287, 0x0003); /* sram lead in, rdy */
  2016. if (dib7000p_identify(st) != 0)
  2017. goto error;
  2018. st->version = dib7000p_read_word(st, 897);
  2019. /* FIXME: make sure the dev.parent field is initialized, or else
  2020. request_firmware() will hit an OOPS (this should be moved somewhere
  2021. more common) */
  2022. st->i2c_master.gated_tuner_i2c_adap.dev.parent = i2c_adap->dev.parent;
  2023. dibx000_init_i2c_master(&st->i2c_master, DIB7000P, st->i2c_adap, st->i2c_addr);
  2024. /* init 7090 tuner adapter */
  2025. strncpy(st->dib7090_tuner_adap.name, "DiB7090 tuner interface", sizeof(st->dib7090_tuner_adap.name));
  2026. st->dib7090_tuner_adap.algo = &dib7090_tuner_xfer_algo;
  2027. st->dib7090_tuner_adap.algo_data = NULL;
  2028. st->dib7090_tuner_adap.dev.parent = st->i2c_adap->dev.parent;
  2029. i2c_set_adapdata(&st->dib7090_tuner_adap, st);
  2030. i2c_add_adapter(&st->dib7090_tuner_adap);
  2031. dib7000p_demod_reset(st);
  2032. if (st->version == SOC7090) {
  2033. dib7090_set_output_mode(demod, st->cfg.output_mode);
  2034. dib7090_set_diversity_in(demod, 0);
  2035. }
  2036. return demod;
  2037. error:
  2038. kfree(st);
  2039. return NULL;
  2040. }
  2041. EXPORT_SYMBOL(dib7000p_attach);
  2042. static struct dvb_frontend_ops dib7000p_ops = {
  2043. .delsys = { SYS_DVBT },
  2044. .info = {
  2045. .name = "DiBcom 7000PC",
  2046. .frequency_min = 44250000,
  2047. .frequency_max = 867250000,
  2048. .frequency_stepsize = 62500,
  2049. .caps = FE_CAN_INVERSION_AUTO |
  2050. FE_CAN_FEC_1_2 | FE_CAN_FEC_2_3 | FE_CAN_FEC_3_4 |
  2051. FE_CAN_FEC_5_6 | FE_CAN_FEC_7_8 | FE_CAN_FEC_AUTO |
  2052. FE_CAN_QPSK | FE_CAN_QAM_16 | FE_CAN_QAM_64 | FE_CAN_QAM_AUTO |
  2053. FE_CAN_TRANSMISSION_MODE_AUTO | FE_CAN_GUARD_INTERVAL_AUTO | FE_CAN_RECOVER | FE_CAN_HIERARCHY_AUTO,
  2054. },
  2055. .release = dib7000p_release,
  2056. .init = dib7000p_wakeup,
  2057. .sleep = dib7000p_sleep,
  2058. .set_frontend = dib7000p_set_frontend,
  2059. .get_tune_settings = dib7000p_fe_get_tune_settings,
  2060. .get_frontend = dib7000p_get_frontend,
  2061. .read_status = dib7000p_read_status,
  2062. .read_ber = dib7000p_read_ber,
  2063. .read_signal_strength = dib7000p_read_signal_strength,
  2064. .read_snr = dib7000p_read_snr,
  2065. .read_ucblocks = dib7000p_read_unc_blocks,
  2066. };
  2067. MODULE_AUTHOR("Olivier Grenie <ogrenie@dibcom.fr>");
  2068. MODULE_AUTHOR("Patrick Boettcher <pboettcher@dibcom.fr>");
  2069. MODULE_DESCRIPTION("Driver for the DiBcom 7000PC COFDM demodulator");
  2070. MODULE_LICENSE("GPL");