dib7000p.c 42 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464
  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 "dvb_math.h"
  14. #include "dvb_frontend.h"
  15. #include "dib7000p.h"
  16. static int debug;
  17. module_param(debug, int, 0644);
  18. MODULE_PARM_DESC(debug, "turn on debugging (default: 0)");
  19. static int buggy_sfn_workaround;
  20. module_param(buggy_sfn_workaround, int, 0644);
  21. MODULE_PARM_DESC(buggy_sfn_workaround, "Enable work-around for buggy SFNs (default: 0)");
  22. #define dprintk(args...) do { if (debug) { printk(KERN_DEBUG "DiB7000P: "); printk(args); printk("\n"); } } while (0)
  23. struct dib7000p_state {
  24. struct dvb_frontend demod;
  25. struct dib7000p_config cfg;
  26. u8 i2c_addr;
  27. struct i2c_adapter *i2c_adap;
  28. struct dibx000_i2c_master i2c_master;
  29. u16 wbd_ref;
  30. u8 current_band;
  31. u32 current_bandwidth;
  32. struct dibx000_agc_config *current_agc;
  33. u32 timf;
  34. u8 div_force_off : 1;
  35. u8 div_state : 1;
  36. u16 div_sync_wait;
  37. u8 agc_state;
  38. u16 gpio_dir;
  39. u16 gpio_val;
  40. u8 sfn_workaround_active :1;
  41. };
  42. enum dib7000p_power_mode {
  43. DIB7000P_POWER_ALL = 0,
  44. DIB7000P_POWER_ANALOG_ADC,
  45. DIB7000P_POWER_INTERFACE_ONLY,
  46. };
  47. static u16 dib7000p_read_word(struct dib7000p_state *state, u16 reg)
  48. {
  49. u8 wb[2] = { reg >> 8, reg & 0xff };
  50. u8 rb[2];
  51. struct i2c_msg msg[2] = {
  52. { .addr = state->i2c_addr >> 1, .flags = 0, .buf = wb, .len = 2 },
  53. { .addr = state->i2c_addr >> 1, .flags = I2C_M_RD, .buf = rb, .len = 2 },
  54. };
  55. if (i2c_transfer(state->i2c_adap, msg, 2) != 2)
  56. dprintk("i2c read error on %d",reg);
  57. return (rb[0] << 8) | rb[1];
  58. }
  59. static int dib7000p_write_word(struct dib7000p_state *state, u16 reg, u16 val)
  60. {
  61. u8 b[4] = {
  62. (reg >> 8) & 0xff, reg & 0xff,
  63. (val >> 8) & 0xff, val & 0xff,
  64. };
  65. struct i2c_msg msg = {
  66. .addr = state->i2c_addr >> 1, .flags = 0, .buf = b, .len = 4
  67. };
  68. return i2c_transfer(state->i2c_adap, &msg, 1) != 1 ? -EREMOTEIO : 0;
  69. }
  70. static void dib7000p_write_tab(struct dib7000p_state *state, u16 *buf)
  71. {
  72. u16 l = 0, r, *n;
  73. n = buf;
  74. l = *n++;
  75. while (l) {
  76. r = *n++;
  77. do {
  78. dib7000p_write_word(state, r, *n++);
  79. r++;
  80. } while (--l);
  81. l = *n++;
  82. }
  83. }
  84. static int dib7000p_set_output_mode(struct dib7000p_state *state, int mode)
  85. {
  86. int ret = 0;
  87. u16 outreg, fifo_threshold, smo_mode;
  88. outreg = 0;
  89. fifo_threshold = 1792;
  90. smo_mode = (dib7000p_read_word(state, 235) & 0x0050) | (1 << 1);
  91. dprintk( "setting output mode for demod %p to %d",
  92. &state->demod, mode);
  93. switch (mode) {
  94. case OUTMODE_MPEG2_PAR_GATED_CLK: // STBs with parallel gated clock
  95. outreg = (1 << 10); /* 0x0400 */
  96. break;
  97. case OUTMODE_MPEG2_PAR_CONT_CLK: // STBs with parallel continues clock
  98. outreg = (1 << 10) | (1 << 6); /* 0x0440 */
  99. break;
  100. case OUTMODE_MPEG2_SERIAL: // STBs with serial input
  101. outreg = (1 << 10) | (2 << 6) | (0 << 1); /* 0x0480 */
  102. break;
  103. case OUTMODE_DIVERSITY:
  104. if (state->cfg.hostbus_diversity)
  105. outreg = (1 << 10) | (4 << 6); /* 0x0500 */
  106. else
  107. outreg = (1 << 11);
  108. break;
  109. case OUTMODE_MPEG2_FIFO: // e.g. USB feeding
  110. smo_mode |= (3 << 1);
  111. fifo_threshold = 512;
  112. outreg = (1 << 10) | (5 << 6);
  113. break;
  114. case OUTMODE_ANALOG_ADC:
  115. outreg = (1 << 10) | (3 << 6);
  116. break;
  117. case OUTMODE_HIGH_Z: // disable
  118. outreg = 0;
  119. break;
  120. default:
  121. dprintk( "Unhandled output_mode passed to be set for demod %p",&state->demod);
  122. break;
  123. }
  124. if (state->cfg.output_mpeg2_in_188_bytes)
  125. smo_mode |= (1 << 5) ;
  126. ret |= dib7000p_write_word(state, 235, smo_mode);
  127. ret |= dib7000p_write_word(state, 236, fifo_threshold); /* synchronous fread */
  128. ret |= dib7000p_write_word(state, 1286, outreg); /* P_Div_active */
  129. return ret;
  130. }
  131. static int dib7000p_set_diversity_in(struct dvb_frontend *demod, int onoff)
  132. {
  133. struct dib7000p_state *state = demod->demodulator_priv;
  134. if (state->div_force_off) {
  135. dprintk( "diversity combination deactivated - forced by COFDM parameters");
  136. onoff = 0;
  137. dib7000p_write_word(state, 207, 0);
  138. } else
  139. dib7000p_write_word(state, 207, (state->div_sync_wait << 4) | (1 << 2) | (2 << 0));
  140. state->div_state = (u8)onoff;
  141. if (onoff) {
  142. dib7000p_write_word(state, 204, 6);
  143. dib7000p_write_word(state, 205, 16);
  144. /* P_dvsy_sync_mode = 0, P_dvsy_sync_enable=1, P_dvcb_comb_mode=2 */
  145. } else {
  146. dib7000p_write_word(state, 204, 1);
  147. dib7000p_write_word(state, 205, 0);
  148. }
  149. return 0;
  150. }
  151. static int dib7000p_set_power_mode(struct dib7000p_state *state, enum dib7000p_power_mode mode)
  152. {
  153. /* by default everything is powered off */
  154. u16 reg_774 = 0xffff, reg_775 = 0xffff, reg_776 = 0x0007, reg_899 = 0x0003,
  155. reg_1280 = (0xfe00) | (dib7000p_read_word(state, 1280) & 0x01ff);
  156. /* now, depending on the requested mode, we power on */
  157. switch (mode) {
  158. /* power up everything in the demod */
  159. case DIB7000P_POWER_ALL:
  160. reg_774 = 0x0000; reg_775 = 0x0000; reg_776 = 0x0; reg_899 = 0x0; reg_1280 &= 0x01ff;
  161. break;
  162. case DIB7000P_POWER_ANALOG_ADC:
  163. /* dem, cfg, iqc, sad, agc */
  164. reg_774 &= ~((1 << 15) | (1 << 14) | (1 << 11) | (1 << 10) | (1 << 9));
  165. /* nud */
  166. reg_776 &= ~((1 << 0));
  167. /* Dout */
  168. reg_1280 &= ~((1 << 11));
  169. /* fall through wanted to enable the interfaces */
  170. /* just leave power on the control-interfaces: GPIO and (I2C or SDIO) */
  171. case DIB7000P_POWER_INTERFACE_ONLY: /* TODO power up either SDIO or I2C */
  172. reg_1280 &= ~((1 << 14) | (1 << 13) | (1 << 12) | (1 << 10));
  173. break;
  174. /* TODO following stuff is just converted from the dib7000-driver - check when is used what */
  175. }
  176. dib7000p_write_word(state, 774, reg_774);
  177. dib7000p_write_word(state, 775, reg_775);
  178. dib7000p_write_word(state, 776, reg_776);
  179. dib7000p_write_word(state, 899, reg_899);
  180. dib7000p_write_word(state, 1280, reg_1280);
  181. return 0;
  182. }
  183. static void dib7000p_set_adc_state(struct dib7000p_state *state, enum dibx000_adc_states no)
  184. {
  185. u16 reg_908 = dib7000p_read_word(state, 908),
  186. reg_909 = dib7000p_read_word(state, 909);
  187. switch (no) {
  188. case DIBX000_SLOW_ADC_ON:
  189. reg_909 |= (1 << 1) | (1 << 0);
  190. dib7000p_write_word(state, 909, reg_909);
  191. reg_909 &= ~(1 << 1);
  192. break;
  193. case DIBX000_SLOW_ADC_OFF:
  194. reg_909 |= (1 << 1) | (1 << 0);
  195. break;
  196. case DIBX000_ADC_ON:
  197. reg_908 &= 0x0fff;
  198. reg_909 &= 0x0003;
  199. break;
  200. case DIBX000_ADC_OFF: // leave the VBG voltage on
  201. reg_908 |= (1 << 14) | (1 << 13) | (1 << 12);
  202. reg_909 |= (1 << 5) | (1 << 4) | (1 << 3) | (1 << 2);
  203. break;
  204. case DIBX000_VBG_ENABLE:
  205. reg_908 &= ~(1 << 15);
  206. break;
  207. case DIBX000_VBG_DISABLE:
  208. reg_908 |= (1 << 15);
  209. break;
  210. default:
  211. break;
  212. }
  213. // dprintk( "908: %x, 909: %x\n", reg_908, reg_909);
  214. reg_908 |= (state->cfg.enable_current_mirror & 1) << 7;
  215. dib7000p_write_word(state, 908, reg_908);
  216. dib7000p_write_word(state, 909, reg_909);
  217. }
  218. static int dib7000p_set_bandwidth(struct dib7000p_state *state, u32 bw)
  219. {
  220. u32 timf;
  221. // store the current bandwidth for later use
  222. state->current_bandwidth = bw;
  223. if (state->timf == 0) {
  224. dprintk( "using default timf");
  225. timf = state->cfg.bw->timf;
  226. } else {
  227. dprintk( "using updated timf");
  228. timf = state->timf;
  229. }
  230. timf = timf * (bw / 50) / 160;
  231. dib7000p_write_word(state, 23, (u16) ((timf >> 16) & 0xffff));
  232. dib7000p_write_word(state, 24, (u16) ((timf ) & 0xffff));
  233. return 0;
  234. }
  235. static int dib7000p_sad_calib(struct dib7000p_state *state)
  236. {
  237. /* internal */
  238. // dib7000p_write_word(state, 72, (3 << 14) | (1 << 12) | (524 << 0)); // sampling clock of the SAD is writting in set_bandwidth
  239. dib7000p_write_word(state, 73, (0 << 1) | (0 << 0));
  240. dib7000p_write_word(state, 74, 776); // 0.625*3.3 / 4096
  241. /* do the calibration */
  242. dib7000p_write_word(state, 73, (1 << 0));
  243. dib7000p_write_word(state, 73, (0 << 0));
  244. msleep(1);
  245. return 0;
  246. }
  247. int dib7000p_set_wbd_ref(struct dvb_frontend *demod, u16 value)
  248. {
  249. struct dib7000p_state *state = demod->demodulator_priv;
  250. if (value > 4095)
  251. value = 4095;
  252. state->wbd_ref = value;
  253. return dib7000p_write_word(state, 105, (dib7000p_read_word(state, 105) & 0xf000) | value);
  254. }
  255. EXPORT_SYMBOL(dib7000p_set_wbd_ref);
  256. static void dib7000p_reset_pll(struct dib7000p_state *state)
  257. {
  258. struct dibx000_bandwidth_config *bw = &state->cfg.bw[0];
  259. u16 clk_cfg0;
  260. /* force PLL bypass */
  261. clk_cfg0 = (1 << 15) | ((bw->pll_ratio & 0x3f) << 9) |
  262. (bw->modulo << 7) | (bw->ADClkSrc << 6) | (bw->IO_CLK_en_core << 5) |
  263. (bw->bypclk_div << 2) | (bw->enable_refdiv << 1) | (0 << 0);
  264. dib7000p_write_word(state, 900, clk_cfg0);
  265. /* P_pll_cfg */
  266. dib7000p_write_word(state, 903, (bw->pll_prediv << 5) | (((bw->pll_ratio >> 6) & 0x3) << 3) | (bw->pll_range << 1) | bw->pll_reset);
  267. clk_cfg0 = (bw->pll_bypass << 15) | (clk_cfg0 & 0x7fff);
  268. dib7000p_write_word(state, 900, clk_cfg0);
  269. dib7000p_write_word(state, 18, (u16) (((bw->internal*1000) >> 16) & 0xffff));
  270. dib7000p_write_word(state, 19, (u16) ( (bw->internal*1000 ) & 0xffff));
  271. dib7000p_write_word(state, 21, (u16) ( (bw->ifreq >> 16) & 0xffff));
  272. dib7000p_write_word(state, 22, (u16) ( (bw->ifreq ) & 0xffff));
  273. dib7000p_write_word(state, 72, bw->sad_cfg);
  274. }
  275. static int dib7000p_reset_gpio(struct dib7000p_state *st)
  276. {
  277. /* reset the GPIOs */
  278. dprintk( "gpio dir: %x: val: %x, pwm_pos: %x",st->gpio_dir, st->gpio_val,st->cfg.gpio_pwm_pos);
  279. dib7000p_write_word(st, 1029, st->gpio_dir);
  280. dib7000p_write_word(st, 1030, st->gpio_val);
  281. /* TODO 1031 is P_gpio_od */
  282. dib7000p_write_word(st, 1032, st->cfg.gpio_pwm_pos);
  283. dib7000p_write_word(st, 1037, st->cfg.pwm_freq_div);
  284. return 0;
  285. }
  286. static int dib7000p_cfg_gpio(struct dib7000p_state *st, u8 num, u8 dir, u8 val)
  287. {
  288. st->gpio_dir = dib7000p_read_word(st, 1029);
  289. st->gpio_dir &= ~(1 << num); /* reset the direction bit */
  290. st->gpio_dir |= (dir & 0x1) << num; /* set the new direction */
  291. dib7000p_write_word(st, 1029, st->gpio_dir);
  292. st->gpio_val = dib7000p_read_word(st, 1030);
  293. st->gpio_val &= ~(1 << num); /* reset the direction bit */
  294. st->gpio_val |= (val & 0x01) << num; /* set the new value */
  295. dib7000p_write_word(st, 1030, st->gpio_val);
  296. return 0;
  297. }
  298. int dib7000p_set_gpio(struct dvb_frontend *demod, u8 num, u8 dir, u8 val)
  299. {
  300. struct dib7000p_state *state = demod->demodulator_priv;
  301. return dib7000p_cfg_gpio(state, num, dir, val);
  302. }
  303. EXPORT_SYMBOL(dib7000p_set_gpio);
  304. static u16 dib7000p_defaults[] =
  305. {
  306. // auto search configuration
  307. 3, 2,
  308. 0x0004,
  309. 0x1000,
  310. 0x0814, /* Equal Lock */
  311. 12, 6,
  312. 0x001b,
  313. 0x7740,
  314. 0x005b,
  315. 0x8d80,
  316. 0x01c9,
  317. 0xc380,
  318. 0x0000,
  319. 0x0080,
  320. 0x0000,
  321. 0x0090,
  322. 0x0001,
  323. 0xd4c0,
  324. 1, 26,
  325. 0x6680, // P_timf_alpha=6, P_corm_alpha=6, P_corm_thres=128 default: 6,4,26
  326. /* set ADC level to -16 */
  327. 11, 79,
  328. (1 << 13) - 825 - 117,
  329. (1 << 13) - 837 - 117,
  330. (1 << 13) - 811 - 117,
  331. (1 << 13) - 766 - 117,
  332. (1 << 13) - 737 - 117,
  333. (1 << 13) - 693 - 117,
  334. (1 << 13) - 648 - 117,
  335. (1 << 13) - 619 - 117,
  336. (1 << 13) - 575 - 117,
  337. (1 << 13) - 531 - 117,
  338. (1 << 13) - 501 - 117,
  339. 1, 142,
  340. 0x0410, // P_palf_filter_on=1, P_palf_filter_freeze=0, P_palf_alpha_regul=16
  341. /* disable power smoothing */
  342. 8, 145,
  343. 0,
  344. 0,
  345. 0,
  346. 0,
  347. 0,
  348. 0,
  349. 0,
  350. 0,
  351. 1, 154,
  352. 1 << 13, // P_fft_freq_dir=1, P_fft_nb_to_cut=0
  353. 1, 168,
  354. 0x0ccd, // P_pha3_thres, default 0x3000
  355. // 1, 169,
  356. // 0x0010, // P_cti_use_cpe=0, P_cti_use_prog=0, P_cti_win_len=16, default: 0x0010
  357. 1, 183,
  358. 0x200f, // P_cspu_regul=512, P_cspu_win_cut=15, default: 0x2005
  359. 5, 187,
  360. 0x023d, // P_adp_regul_cnt=573, default: 410
  361. 0x00a4, // P_adp_noise_cnt=
  362. 0x00a4, // P_adp_regul_ext
  363. 0x7ff0, // P_adp_noise_ext
  364. 0x3ccc, // P_adp_fil
  365. 1, 198,
  366. 0x800, // P_equal_thres_wgn
  367. 1, 222,
  368. 0x0010, // P_fec_ber_rs_len=2
  369. 1, 235,
  370. 0x0062, // P_smo_mode, P_smo_rs_discard, P_smo_fifo_flush, P_smo_pid_parse, P_smo_error_discard
  371. 2, 901,
  372. 0x0006, // P_clk_cfg1
  373. (3 << 10) | (1 << 6), // P_divclksel=3 P_divbitsel=1
  374. 1, 905,
  375. 0x2c8e, // Tuner IO bank: max drive (14mA) + divout pads max drive
  376. 0,
  377. };
  378. static int dib7000p_demod_reset(struct dib7000p_state *state)
  379. {
  380. dib7000p_set_power_mode(state, DIB7000P_POWER_ALL);
  381. dib7000p_set_adc_state(state, DIBX000_VBG_ENABLE);
  382. /* restart all parts */
  383. dib7000p_write_word(state, 770, 0xffff);
  384. dib7000p_write_word(state, 771, 0xffff);
  385. dib7000p_write_word(state, 772, 0x001f);
  386. dib7000p_write_word(state, 898, 0x0003);
  387. /* except i2c, sdio, gpio - control interfaces */
  388. dib7000p_write_word(state, 1280, 0x01fc - ((1 << 7) | (1 << 6) | (1 << 5)) );
  389. dib7000p_write_word(state, 770, 0);
  390. dib7000p_write_word(state, 771, 0);
  391. dib7000p_write_word(state, 772, 0);
  392. dib7000p_write_word(state, 898, 0);
  393. dib7000p_write_word(state, 1280, 0);
  394. /* default */
  395. dib7000p_reset_pll(state);
  396. if (dib7000p_reset_gpio(state) != 0)
  397. dprintk( "GPIO reset was not successful.");
  398. if (dib7000p_set_output_mode(state, OUTMODE_HIGH_Z) != 0)
  399. dprintk( "OUTPUT_MODE could not be reset.");
  400. /* unforce divstr regardless whether i2c enumeration was done or not */
  401. dib7000p_write_word(state, 1285, dib7000p_read_word(state, 1285) & ~(1 << 1) );
  402. dib7000p_set_bandwidth(state, 8000);
  403. dib7000p_set_adc_state(state, DIBX000_SLOW_ADC_ON);
  404. dib7000p_sad_calib(state);
  405. dib7000p_set_adc_state(state, DIBX000_SLOW_ADC_OFF);
  406. // P_iqc_alpha_pha, P_iqc_alpha_amp_dcc_alpha, ...
  407. if(state->cfg.tuner_is_baseband)
  408. dib7000p_write_word(state, 36,0x0755);
  409. else
  410. dib7000p_write_word(state, 36,0x1f55);
  411. dib7000p_write_tab(state, dib7000p_defaults);
  412. dib7000p_set_power_mode(state, DIB7000P_POWER_INTERFACE_ONLY);
  413. return 0;
  414. }
  415. static void dib7000p_pll_clk_cfg(struct dib7000p_state *state)
  416. {
  417. u16 tmp = 0;
  418. tmp = dib7000p_read_word(state, 903);
  419. dib7000p_write_word(state, 903, (tmp | 0x1)); //pwr-up pll
  420. tmp = dib7000p_read_word(state, 900);
  421. dib7000p_write_word(state, 900, (tmp & 0x7fff) | (1 << 6)); //use High freq clock
  422. }
  423. static void dib7000p_restart_agc(struct dib7000p_state *state)
  424. {
  425. // P_restart_iqc & P_restart_agc
  426. dib7000p_write_word(state, 770, (1 << 11) | (1 << 9));
  427. dib7000p_write_word(state, 770, 0x0000);
  428. }
  429. static int dib7000p_update_lna(struct dib7000p_state *state)
  430. {
  431. u16 dyn_gain;
  432. // when there is no LNA to program return immediatly
  433. if (state->cfg.update_lna) {
  434. // read dyn_gain here (because it is demod-dependent and not fe)
  435. dyn_gain = dib7000p_read_word(state, 394);
  436. if (state->cfg.update_lna(&state->demod,dyn_gain)) { // LNA has changed
  437. dib7000p_restart_agc(state);
  438. return 1;
  439. }
  440. }
  441. return 0;
  442. }
  443. static int dib7000p_set_agc_config(struct dib7000p_state *state, u8 band)
  444. {
  445. struct dibx000_agc_config *agc = NULL;
  446. int i;
  447. if (state->current_band == band && state->current_agc != NULL)
  448. return 0;
  449. state->current_band = band;
  450. for (i = 0; i < state->cfg.agc_config_count; i++)
  451. if (state->cfg.agc[i].band_caps & band) {
  452. agc = &state->cfg.agc[i];
  453. break;
  454. }
  455. if (agc == NULL) {
  456. dprintk( "no valid AGC configuration found for band 0x%02x",band);
  457. return -EINVAL;
  458. }
  459. state->current_agc = agc;
  460. /* AGC */
  461. dib7000p_write_word(state, 75 , agc->setup );
  462. dib7000p_write_word(state, 76 , agc->inv_gain );
  463. dib7000p_write_word(state, 77 , agc->time_stabiliz );
  464. dib7000p_write_word(state, 100, (agc->alpha_level << 12) | agc->thlock);
  465. // Demod AGC loop configuration
  466. dib7000p_write_word(state, 101, (agc->alpha_mant << 5) | agc->alpha_exp);
  467. dib7000p_write_word(state, 102, (agc->beta_mant << 6) | agc->beta_exp);
  468. /* AGC continued */
  469. dprintk( "WBD: ref: %d, sel: %d, active: %d, alpha: %d",
  470. state->wbd_ref != 0 ? state->wbd_ref : agc->wbd_ref, agc->wbd_sel, !agc->perform_agc_softsplit, agc->wbd_sel);
  471. if (state->wbd_ref != 0)
  472. dib7000p_write_word(state, 105, (agc->wbd_inv << 12) | state->wbd_ref);
  473. else
  474. dib7000p_write_word(state, 105, (agc->wbd_inv << 12) | agc->wbd_ref);
  475. dib7000p_write_word(state, 106, (agc->wbd_sel << 13) | (agc->wbd_alpha << 9) | (agc->perform_agc_softsplit << 8));
  476. dib7000p_write_word(state, 107, agc->agc1_max);
  477. dib7000p_write_word(state, 108, agc->agc1_min);
  478. dib7000p_write_word(state, 109, agc->agc2_max);
  479. dib7000p_write_word(state, 110, agc->agc2_min);
  480. dib7000p_write_word(state, 111, (agc->agc1_pt1 << 8) | agc->agc1_pt2);
  481. dib7000p_write_word(state, 112, agc->agc1_pt3);
  482. dib7000p_write_word(state, 113, (agc->agc1_slope1 << 8) | agc->agc1_slope2);
  483. dib7000p_write_word(state, 114, (agc->agc2_pt1 << 8) | agc->agc2_pt2);
  484. dib7000p_write_word(state, 115, (agc->agc2_slope1 << 8) | agc->agc2_slope2);
  485. return 0;
  486. }
  487. static int dib7000p_agc_startup(struct dvb_frontend *demod, struct dvb_frontend_parameters *ch)
  488. {
  489. struct dib7000p_state *state = demod->demodulator_priv;
  490. int ret = -1;
  491. u8 *agc_state = &state->agc_state;
  492. u8 agc_split;
  493. switch (state->agc_state) {
  494. case 0:
  495. // set power-up level: interf+analog+AGC
  496. dib7000p_set_power_mode(state, DIB7000P_POWER_ALL);
  497. dib7000p_set_adc_state(state, DIBX000_ADC_ON);
  498. dib7000p_pll_clk_cfg(state);
  499. if (dib7000p_set_agc_config(state, BAND_OF_FREQUENCY(ch->frequency/1000)) != 0)
  500. return -1;
  501. ret = 7;
  502. (*agc_state)++;
  503. break;
  504. case 1:
  505. // AGC initialization
  506. if (state->cfg.agc_control)
  507. state->cfg.agc_control(&state->demod, 1);
  508. dib7000p_write_word(state, 78, 32768);
  509. if (!state->current_agc->perform_agc_softsplit) {
  510. /* we are using the wbd - so slow AGC startup */
  511. /* force 0 split on WBD and restart AGC */
  512. dib7000p_write_word(state, 106, (state->current_agc->wbd_sel << 13) | (state->current_agc->wbd_alpha << 9) | (1 << 8));
  513. (*agc_state)++;
  514. ret = 5;
  515. } else {
  516. /* default AGC startup */
  517. (*agc_state) = 4;
  518. /* wait AGC rough lock time */
  519. ret = 7;
  520. }
  521. dib7000p_restart_agc(state);
  522. break;
  523. case 2: /* fast split search path after 5sec */
  524. dib7000p_write_word(state, 75, state->current_agc->setup | (1 << 4)); /* freeze AGC loop */
  525. dib7000p_write_word(state, 106, (state->current_agc->wbd_sel << 13) | (2 << 9) | (0 << 8)); /* fast split search 0.25kHz */
  526. (*agc_state)++;
  527. ret = 14;
  528. break;
  529. case 3: /* split search ended */
  530. agc_split = (u8)dib7000p_read_word(state, 396); /* store the split value for the next time */
  531. dib7000p_write_word(state, 78, dib7000p_read_word(state, 394)); /* set AGC gain start value */
  532. dib7000p_write_word(state, 75, state->current_agc->setup); /* std AGC loop */
  533. dib7000p_write_word(state, 106, (state->current_agc->wbd_sel << 13) | (state->current_agc->wbd_alpha << 9) | agc_split); /* standard split search */
  534. dib7000p_restart_agc(state);
  535. dprintk( "SPLIT %p: %hd", demod, agc_split);
  536. (*agc_state)++;
  537. ret = 5;
  538. break;
  539. case 4: /* LNA startup */
  540. // wait AGC accurate lock time
  541. ret = 7;
  542. if (dib7000p_update_lna(state))
  543. // wait only AGC rough lock time
  544. ret = 5;
  545. else // nothing was done, go to the next state
  546. (*agc_state)++;
  547. break;
  548. case 5:
  549. if (state->cfg.agc_control)
  550. state->cfg.agc_control(&state->demod, 0);
  551. (*agc_state)++;
  552. break;
  553. default:
  554. break;
  555. }
  556. return ret;
  557. }
  558. static void dib7000p_update_timf(struct dib7000p_state *state)
  559. {
  560. u32 timf = (dib7000p_read_word(state, 427) << 16) | dib7000p_read_word(state, 428);
  561. state->timf = timf * 160 / (state->current_bandwidth / 50);
  562. dib7000p_write_word(state, 23, (u16) (timf >> 16));
  563. dib7000p_write_word(state, 24, (u16) (timf & 0xffff));
  564. dprintk( "updated timf_frequency: %d (default: %d)",state->timf, state->cfg.bw->timf);
  565. }
  566. static void dib7000p_set_channel(struct dib7000p_state *state, struct dvb_frontend_parameters *ch, u8 seq)
  567. {
  568. u16 value, est[4];
  569. dib7000p_set_bandwidth(state, BANDWIDTH_TO_KHZ(ch->u.ofdm.bandwidth));
  570. /* nfft, guard, qam, alpha */
  571. value = 0;
  572. switch (ch->u.ofdm.transmission_mode) {
  573. case TRANSMISSION_MODE_2K: value |= (0 << 7); break;
  574. case /* 4K MODE */ 255: value |= (2 << 7); break;
  575. default:
  576. case TRANSMISSION_MODE_8K: value |= (1 << 7); break;
  577. }
  578. switch (ch->u.ofdm.guard_interval) {
  579. case GUARD_INTERVAL_1_32: value |= (0 << 5); break;
  580. case GUARD_INTERVAL_1_16: value |= (1 << 5); break;
  581. case GUARD_INTERVAL_1_4: value |= (3 << 5); break;
  582. default:
  583. case GUARD_INTERVAL_1_8: value |= (2 << 5); break;
  584. }
  585. switch (ch->u.ofdm.constellation) {
  586. case QPSK: value |= (0 << 3); break;
  587. case QAM_16: value |= (1 << 3); break;
  588. default:
  589. case QAM_64: value |= (2 << 3); break;
  590. }
  591. switch (HIERARCHY_1) {
  592. case HIERARCHY_2: value |= 2; break;
  593. case HIERARCHY_4: value |= 4; break;
  594. default:
  595. case HIERARCHY_1: value |= 1; break;
  596. }
  597. dib7000p_write_word(state, 0, value);
  598. dib7000p_write_word(state, 5, (seq << 4) | 1); /* do not force tps, search list 0 */
  599. /* P_dintl_native, P_dintlv_inv, P_hrch, P_code_rate, P_select_hp */
  600. value = 0;
  601. if (1 != 0)
  602. value |= (1 << 6);
  603. if (ch->u.ofdm.hierarchy_information == 1)
  604. value |= (1 << 4);
  605. if (1 == 1)
  606. value |= 1;
  607. switch ((ch->u.ofdm.hierarchy_information == 0 || 1 == 1) ? ch->u.ofdm.code_rate_HP : ch->u.ofdm.code_rate_LP) {
  608. case FEC_2_3: value |= (2 << 1); break;
  609. case FEC_3_4: value |= (3 << 1); break;
  610. case FEC_5_6: value |= (5 << 1); break;
  611. case FEC_7_8: value |= (7 << 1); break;
  612. default:
  613. case FEC_1_2: value |= (1 << 1); break;
  614. }
  615. dib7000p_write_word(state, 208, value);
  616. /* offset loop parameters */
  617. dib7000p_write_word(state, 26, 0x6680); // timf(6xxx)
  618. dib7000p_write_word(state, 32, 0x0003); // pha_off_max(xxx3)
  619. dib7000p_write_word(state, 29, 0x1273); // isi
  620. dib7000p_write_word(state, 33, 0x0005); // sfreq(xxx5)
  621. /* P_dvsy_sync_wait */
  622. switch (ch->u.ofdm.transmission_mode) {
  623. case TRANSMISSION_MODE_8K: value = 256; break;
  624. case /* 4K MODE */ 255: value = 128; break;
  625. case TRANSMISSION_MODE_2K:
  626. default: value = 64; break;
  627. }
  628. switch (ch->u.ofdm.guard_interval) {
  629. case GUARD_INTERVAL_1_16: value *= 2; break;
  630. case GUARD_INTERVAL_1_8: value *= 4; break;
  631. case GUARD_INTERVAL_1_4: value *= 8; break;
  632. default:
  633. case GUARD_INTERVAL_1_32: value *= 1; break;
  634. }
  635. state->div_sync_wait = (value * 3) / 2 + 32; // add 50% SFN margin + compensate for one DVSY-fifo TODO
  636. /* deactive the possibility of diversity reception if extended interleaver */
  637. state->div_force_off = !1 && ch->u.ofdm.transmission_mode != TRANSMISSION_MODE_8K;
  638. dib7000p_set_diversity_in(&state->demod, state->div_state);
  639. /* channel estimation fine configuration */
  640. switch (ch->u.ofdm.constellation) {
  641. case QAM_64:
  642. est[0] = 0x0148; /* P_adp_regul_cnt 0.04 */
  643. est[1] = 0xfff0; /* P_adp_noise_cnt -0.002 */
  644. est[2] = 0x00a4; /* P_adp_regul_ext 0.02 */
  645. est[3] = 0xfff8; /* P_adp_noise_ext -0.001 */
  646. break;
  647. case QAM_16:
  648. est[0] = 0x023d; /* P_adp_regul_cnt 0.07 */
  649. est[1] = 0xffdf; /* P_adp_noise_cnt -0.004 */
  650. est[2] = 0x00a4; /* P_adp_regul_ext 0.02 */
  651. est[3] = 0xfff0; /* P_adp_noise_ext -0.002 */
  652. break;
  653. default:
  654. est[0] = 0x099a; /* P_adp_regul_cnt 0.3 */
  655. est[1] = 0xffae; /* P_adp_noise_cnt -0.01 */
  656. est[2] = 0x0333; /* P_adp_regul_ext 0.1 */
  657. est[3] = 0xfff8; /* P_adp_noise_ext -0.002 */
  658. break;
  659. }
  660. for (value = 0; value < 4; value++)
  661. dib7000p_write_word(state, 187 + value, est[value]);
  662. }
  663. static int dib7000p_autosearch_start(struct dvb_frontend *demod, struct dvb_frontend_parameters *ch)
  664. {
  665. struct dib7000p_state *state = demod->demodulator_priv;
  666. struct dvb_frontend_parameters schan;
  667. u32 value, factor;
  668. schan = *ch;
  669. schan.u.ofdm.constellation = QAM_64;
  670. schan.u.ofdm.guard_interval = GUARD_INTERVAL_1_32;
  671. schan.u.ofdm.transmission_mode = TRANSMISSION_MODE_8K;
  672. schan.u.ofdm.code_rate_HP = FEC_2_3;
  673. schan.u.ofdm.code_rate_LP = FEC_3_4;
  674. schan.u.ofdm.hierarchy_information = 0;
  675. dib7000p_set_channel(state, &schan, 7);
  676. factor = BANDWIDTH_TO_KHZ(ch->u.ofdm.bandwidth);
  677. if (factor >= 5000)
  678. factor = 1;
  679. else
  680. factor = 6;
  681. // always use the setting for 8MHz here lock_time for 7,6 MHz are longer
  682. value = 30 * state->cfg.bw->internal * factor;
  683. dib7000p_write_word(state, 6, (u16) ((value >> 16) & 0xffff)); // lock0 wait time
  684. dib7000p_write_word(state, 7, (u16) (value & 0xffff)); // lock0 wait time
  685. value = 100 * state->cfg.bw->internal * factor;
  686. dib7000p_write_word(state, 8, (u16) ((value >> 16) & 0xffff)); // lock1 wait time
  687. dib7000p_write_word(state, 9, (u16) (value & 0xffff)); // lock1 wait time
  688. value = 500 * state->cfg.bw->internal * factor;
  689. dib7000p_write_word(state, 10, (u16) ((value >> 16) & 0xffff)); // lock2 wait time
  690. dib7000p_write_word(state, 11, (u16) (value & 0xffff)); // lock2 wait time
  691. value = dib7000p_read_word(state, 0);
  692. dib7000p_write_word(state, 0, (u16) ((1 << 9) | value));
  693. dib7000p_read_word(state, 1284);
  694. dib7000p_write_word(state, 0, (u16) value);
  695. return 0;
  696. }
  697. static int dib7000p_autosearch_is_irq(struct dvb_frontend *demod)
  698. {
  699. struct dib7000p_state *state = demod->demodulator_priv;
  700. u16 irq_pending = dib7000p_read_word(state, 1284);
  701. if (irq_pending & 0x1) // failed
  702. return 1;
  703. if (irq_pending & 0x2) // succeeded
  704. return 2;
  705. return 0; // still pending
  706. }
  707. static void dib7000p_spur_protect(struct dib7000p_state *state, u32 rf_khz, u32 bw)
  708. {
  709. static s16 notch[]={16143, 14402, 12238, 9713, 6902, 3888, 759, -2392};
  710. static u8 sine [] ={0, 2, 3, 5, 6, 8, 9, 11, 13, 14, 16, 17, 19, 20, 22,
  711. 24, 25, 27, 28, 30, 31, 33, 34, 36, 38, 39, 41, 42, 44, 45, 47, 48, 50, 51,
  712. 53, 55, 56, 58, 59, 61, 62, 64, 65, 67, 68, 70, 71, 73, 74, 76, 77, 79, 80,
  713. 82, 83, 85, 86, 88, 89, 91, 92, 94, 95, 97, 98, 99, 101, 102, 104, 105,
  714. 107, 108, 109, 111, 112, 114, 115, 117, 118, 119, 121, 122, 123, 125, 126,
  715. 128, 129, 130, 132, 133, 134, 136, 137, 138, 140, 141, 142, 144, 145, 146,
  716. 147, 149, 150, 151, 152, 154, 155, 156, 157, 159, 160, 161, 162, 164, 165,
  717. 166, 167, 168, 170, 171, 172, 173, 174, 175, 177, 178, 179, 180, 181, 182,
  718. 183, 184, 185, 186, 188, 189, 190, 191, 192, 193, 194, 195, 196, 197, 198,
  719. 199, 200, 201, 202, 203, 204, 205, 206, 207, 207, 208, 209, 210, 211, 212,
  720. 213, 214, 215, 215, 216, 217, 218, 219, 220, 220, 221, 222, 223, 224, 224,
  721. 225, 226, 227, 227, 228, 229, 229, 230, 231, 231, 232, 233, 233, 234, 235,
  722. 235, 236, 237, 237, 238, 238, 239, 239, 240, 241, 241, 242, 242, 243, 243,
  723. 244, 244, 245, 245, 245, 246, 246, 247, 247, 248, 248, 248, 249, 249, 249,
  724. 250, 250, 250, 251, 251, 251, 252, 252, 252, 252, 253, 253, 253, 253, 254,
  725. 254, 254, 254, 254, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
  726. 255, 255, 255, 255, 255, 255};
  727. u32 xtal = state->cfg.bw->xtal_hz / 1000;
  728. int f_rel = DIV_ROUND_CLOSEST(rf_khz, xtal) * xtal - rf_khz;
  729. int k;
  730. int coef_re[8],coef_im[8];
  731. int bw_khz = bw;
  732. u32 pha;
  733. dprintk( "relative position of the Spur: %dk (RF: %dk, XTAL: %dk)", f_rel, rf_khz, xtal);
  734. if (f_rel < -bw_khz/2 || f_rel > bw_khz/2)
  735. return;
  736. bw_khz /= 100;
  737. dib7000p_write_word(state, 142 ,0x0610);
  738. for (k = 0; k < 8; k++) {
  739. pha = ((f_rel * (k+1) * 112 * 80/bw_khz) /1000) & 0x3ff;
  740. if (pha==0) {
  741. coef_re[k] = 256;
  742. coef_im[k] = 0;
  743. } else if(pha < 256) {
  744. coef_re[k] = sine[256-(pha&0xff)];
  745. coef_im[k] = sine[pha&0xff];
  746. } else if (pha == 256) {
  747. coef_re[k] = 0;
  748. coef_im[k] = 256;
  749. } else if (pha < 512) {
  750. coef_re[k] = -sine[pha&0xff];
  751. coef_im[k] = sine[256 - (pha&0xff)];
  752. } else if (pha == 512) {
  753. coef_re[k] = -256;
  754. coef_im[k] = 0;
  755. } else if (pha < 768) {
  756. coef_re[k] = -sine[256-(pha&0xff)];
  757. coef_im[k] = -sine[pha&0xff];
  758. } else if (pha == 768) {
  759. coef_re[k] = 0;
  760. coef_im[k] = -256;
  761. } else {
  762. coef_re[k] = sine[pha&0xff];
  763. coef_im[k] = -sine[256 - (pha&0xff)];
  764. }
  765. coef_re[k] *= notch[k];
  766. coef_re[k] += (1<<14);
  767. if (coef_re[k] >= (1<<24))
  768. coef_re[k] = (1<<24) - 1;
  769. coef_re[k] /= (1<<15);
  770. coef_im[k] *= notch[k];
  771. coef_im[k] += (1<<14);
  772. if (coef_im[k] >= (1<<24))
  773. coef_im[k] = (1<<24)-1;
  774. coef_im[k] /= (1<<15);
  775. dprintk( "PALF COEF: %d re: %d im: %d", k, coef_re[k], coef_im[k]);
  776. dib7000p_write_word(state, 143, (0 << 14) | (k << 10) | (coef_re[k] & 0x3ff));
  777. dib7000p_write_word(state, 144, coef_im[k] & 0x3ff);
  778. dib7000p_write_word(state, 143, (1 << 14) | (k << 10) | (coef_re[k] & 0x3ff));
  779. }
  780. dib7000p_write_word(state,143 ,0);
  781. }
  782. static int dib7000p_tune(struct dvb_frontend *demod, struct dvb_frontend_parameters *ch)
  783. {
  784. struct dib7000p_state *state = demod->demodulator_priv;
  785. u16 tmp = 0;
  786. if (ch != NULL)
  787. dib7000p_set_channel(state, ch, 0);
  788. else
  789. return -EINVAL;
  790. // restart demod
  791. dib7000p_write_word(state, 770, 0x4000);
  792. dib7000p_write_word(state, 770, 0x0000);
  793. msleep(45);
  794. /* 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 */
  795. tmp = (0 << 14) | (4 << 10) | (0 << 9) | (3 << 5) | (1 << 4) | (0x3);
  796. if (state->sfn_workaround_active) {
  797. dprintk( "SFN workaround is active");
  798. tmp |= (1 << 9);
  799. dib7000p_write_word(state, 166, 0x4000); // P_pha3_force_pha_shift
  800. } else {
  801. dib7000p_write_word(state, 166, 0x0000); // P_pha3_force_pha_shift
  802. }
  803. dib7000p_write_word(state, 29, tmp);
  804. // never achieved a lock with that bandwidth so far - wait for osc-freq to update
  805. if (state->timf == 0)
  806. msleep(200);
  807. /* offset loop parameters */
  808. /* P_timf_alpha, P_corm_alpha=6, P_corm_thres=0x80 */
  809. tmp = (6 << 8) | 0x80;
  810. switch (ch->u.ofdm.transmission_mode) {
  811. case TRANSMISSION_MODE_2K: tmp |= (7 << 12); break;
  812. case /* 4K MODE */ 255: tmp |= (8 << 12); break;
  813. default:
  814. case TRANSMISSION_MODE_8K: tmp |= (9 << 12); break;
  815. }
  816. dib7000p_write_word(state, 26, tmp); /* timf_a(6xxx) */
  817. /* P_ctrl_freeze_pha_shift=0, P_ctrl_pha_off_max */
  818. tmp = (0 << 4);
  819. switch (ch->u.ofdm.transmission_mode) {
  820. case TRANSMISSION_MODE_2K: tmp |= 0x6; break;
  821. case /* 4K MODE */ 255: tmp |= 0x7; break;
  822. default:
  823. case TRANSMISSION_MODE_8K: tmp |= 0x8; break;
  824. }
  825. dib7000p_write_word(state, 32, tmp);
  826. /* P_ctrl_sfreq_inh=0, P_ctrl_sfreq_step */
  827. tmp = (0 << 4);
  828. switch (ch->u.ofdm.transmission_mode) {
  829. case TRANSMISSION_MODE_2K: tmp |= 0x6; break;
  830. case /* 4K MODE */ 255: tmp |= 0x7; break;
  831. default:
  832. case TRANSMISSION_MODE_8K: tmp |= 0x8; break;
  833. }
  834. dib7000p_write_word(state, 33, tmp);
  835. tmp = dib7000p_read_word(state,509);
  836. if (!((tmp >> 6) & 0x1)) {
  837. /* restart the fec */
  838. tmp = dib7000p_read_word(state,771);
  839. dib7000p_write_word(state, 771, tmp | (1 << 1));
  840. dib7000p_write_word(state, 771, tmp);
  841. msleep(10);
  842. tmp = dib7000p_read_word(state,509);
  843. }
  844. // we achieved a lock - it's time to update the osc freq
  845. if ((tmp >> 6) & 0x1)
  846. dib7000p_update_timf(state);
  847. if (state->cfg.spur_protect)
  848. dib7000p_spur_protect(state, ch->frequency/1000, BANDWIDTH_TO_KHZ(ch->u.ofdm.bandwidth));
  849. dib7000p_set_bandwidth(state, BANDWIDTH_TO_KHZ(ch->u.ofdm.bandwidth));
  850. return 0;
  851. }
  852. static int dib7000p_wakeup(struct dvb_frontend *demod)
  853. {
  854. struct dib7000p_state *state = demod->demodulator_priv;
  855. dib7000p_set_power_mode(state, DIB7000P_POWER_ALL);
  856. dib7000p_set_adc_state(state, DIBX000_SLOW_ADC_ON);
  857. return 0;
  858. }
  859. static int dib7000p_sleep(struct dvb_frontend *demod)
  860. {
  861. struct dib7000p_state *state = demod->demodulator_priv;
  862. return dib7000p_set_output_mode(state, OUTMODE_HIGH_Z) | dib7000p_set_power_mode(state, DIB7000P_POWER_INTERFACE_ONLY);
  863. }
  864. static int dib7000p_identify(struct dib7000p_state *st)
  865. {
  866. u16 value;
  867. dprintk( "checking demod on I2C address: %d (%x)",
  868. st->i2c_addr, st->i2c_addr);
  869. if ((value = dib7000p_read_word(st, 768)) != 0x01b3) {
  870. dprintk( "wrong Vendor ID (read=0x%x)",value);
  871. return -EREMOTEIO;
  872. }
  873. if ((value = dib7000p_read_word(st, 769)) != 0x4000) {
  874. dprintk( "wrong Device ID (%x)",value);
  875. return -EREMOTEIO;
  876. }
  877. return 0;
  878. }
  879. static int dib7000p_get_frontend(struct dvb_frontend* fe,
  880. struct dvb_frontend_parameters *fep)
  881. {
  882. struct dib7000p_state *state = fe->demodulator_priv;
  883. u16 tps = dib7000p_read_word(state,463);
  884. fep->inversion = INVERSION_AUTO;
  885. fep->u.ofdm.bandwidth = BANDWIDTH_TO_INDEX(state->current_bandwidth);
  886. switch ((tps >> 8) & 0x3) {
  887. case 0: fep->u.ofdm.transmission_mode = TRANSMISSION_MODE_2K; break;
  888. case 1: fep->u.ofdm.transmission_mode = TRANSMISSION_MODE_8K; break;
  889. /* case 2: fep->u.ofdm.transmission_mode = TRANSMISSION_MODE_4K; break; */
  890. }
  891. switch (tps & 0x3) {
  892. case 0: fep->u.ofdm.guard_interval = GUARD_INTERVAL_1_32; break;
  893. case 1: fep->u.ofdm.guard_interval = GUARD_INTERVAL_1_16; break;
  894. case 2: fep->u.ofdm.guard_interval = GUARD_INTERVAL_1_8; break;
  895. case 3: fep->u.ofdm.guard_interval = GUARD_INTERVAL_1_4; break;
  896. }
  897. switch ((tps >> 14) & 0x3) {
  898. case 0: fep->u.ofdm.constellation = QPSK; break;
  899. case 1: fep->u.ofdm.constellation = QAM_16; break;
  900. case 2:
  901. default: fep->u.ofdm.constellation = QAM_64; break;
  902. }
  903. /* as long as the frontend_param structure is fixed for hierarchical transmission I refuse to use it */
  904. /* (tps >> 13) & 0x1 == hrch is used, (tps >> 10) & 0x7 == alpha */
  905. fep->u.ofdm.hierarchy_information = HIERARCHY_NONE;
  906. switch ((tps >> 5) & 0x7) {
  907. case 1: fep->u.ofdm.code_rate_HP = FEC_1_2; break;
  908. case 2: fep->u.ofdm.code_rate_HP = FEC_2_3; break;
  909. case 3: fep->u.ofdm.code_rate_HP = FEC_3_4; break;
  910. case 5: fep->u.ofdm.code_rate_HP = FEC_5_6; break;
  911. case 7:
  912. default: fep->u.ofdm.code_rate_HP = FEC_7_8; break;
  913. }
  914. switch ((tps >> 2) & 0x7) {
  915. case 1: fep->u.ofdm.code_rate_LP = FEC_1_2; break;
  916. case 2: fep->u.ofdm.code_rate_LP = FEC_2_3; break;
  917. case 3: fep->u.ofdm.code_rate_LP = FEC_3_4; break;
  918. case 5: fep->u.ofdm.code_rate_LP = FEC_5_6; break;
  919. case 7:
  920. default: fep->u.ofdm.code_rate_LP = FEC_7_8; break;
  921. }
  922. /* native interleaver: (dib7000p_read_word(state, 464) >> 5) & 0x1 */
  923. return 0;
  924. }
  925. static int dib7000p_set_frontend(struct dvb_frontend* fe,
  926. struct dvb_frontend_parameters *fep)
  927. {
  928. struct dib7000p_state *state = fe->demodulator_priv;
  929. int time, ret;
  930. dib7000p_set_output_mode(state, OUTMODE_HIGH_Z);
  931. /* maybe the parameter has been changed */
  932. state->sfn_workaround_active = buggy_sfn_workaround;
  933. if (fe->ops.tuner_ops.set_params)
  934. fe->ops.tuner_ops.set_params(fe, fep);
  935. /* start up the AGC */
  936. state->agc_state = 0;
  937. do {
  938. time = dib7000p_agc_startup(fe, fep);
  939. if (time != -1)
  940. msleep(time);
  941. } while (time != -1);
  942. if (fep->u.ofdm.transmission_mode == TRANSMISSION_MODE_AUTO ||
  943. fep->u.ofdm.guard_interval == GUARD_INTERVAL_AUTO ||
  944. fep->u.ofdm.constellation == QAM_AUTO ||
  945. fep->u.ofdm.code_rate_HP == FEC_AUTO) {
  946. int i = 800, found;
  947. dib7000p_autosearch_start(fe, fep);
  948. do {
  949. msleep(1);
  950. found = dib7000p_autosearch_is_irq(fe);
  951. } while (found == 0 && i--);
  952. dprintk("autosearch returns: %d",found);
  953. if (found == 0 || found == 1)
  954. return 0; // no channel found
  955. dib7000p_get_frontend(fe, fep);
  956. }
  957. ret = dib7000p_tune(fe, fep);
  958. /* make this a config parameter */
  959. dib7000p_set_output_mode(state, state->cfg.output_mode);
  960. return ret;
  961. }
  962. static int dib7000p_read_status(struct dvb_frontend *fe, fe_status_t *stat)
  963. {
  964. struct dib7000p_state *state = fe->demodulator_priv;
  965. u16 lock = dib7000p_read_word(state, 509);
  966. *stat = 0;
  967. if (lock & 0x8000)
  968. *stat |= FE_HAS_SIGNAL;
  969. if (lock & 0x3000)
  970. *stat |= FE_HAS_CARRIER;
  971. if (lock & 0x0100)
  972. *stat |= FE_HAS_VITERBI;
  973. if (lock & 0x0010)
  974. *stat |= FE_HAS_SYNC;
  975. if ((lock & 0x0038) == 0x38)
  976. *stat |= FE_HAS_LOCK;
  977. return 0;
  978. }
  979. static int dib7000p_read_ber(struct dvb_frontend *fe, u32 *ber)
  980. {
  981. struct dib7000p_state *state = fe->demodulator_priv;
  982. *ber = (dib7000p_read_word(state, 500) << 16) | dib7000p_read_word(state, 501);
  983. return 0;
  984. }
  985. static int dib7000p_read_unc_blocks(struct dvb_frontend *fe, u32 *unc)
  986. {
  987. struct dib7000p_state *state = fe->demodulator_priv;
  988. *unc = dib7000p_read_word(state, 506);
  989. return 0;
  990. }
  991. static int dib7000p_read_signal_strength(struct dvb_frontend *fe, u16 *strength)
  992. {
  993. struct dib7000p_state *state = fe->demodulator_priv;
  994. u16 val = dib7000p_read_word(state, 394);
  995. *strength = 65535 - val;
  996. return 0;
  997. }
  998. static int dib7000p_read_snr(struct dvb_frontend* fe, u16 *snr)
  999. {
  1000. struct dib7000p_state *state = fe->demodulator_priv;
  1001. u16 val;
  1002. s32 signal_mant, signal_exp, noise_mant, noise_exp;
  1003. u32 result = 0;
  1004. val = dib7000p_read_word(state, 479);
  1005. noise_mant = (val >> 4) & 0xff;
  1006. noise_exp = ((val & 0xf) << 2);
  1007. val = dib7000p_read_word(state, 480);
  1008. noise_exp += ((val >> 14) & 0x3);
  1009. if ((noise_exp & 0x20) != 0)
  1010. noise_exp -= 0x40;
  1011. signal_mant = (val >> 6) & 0xFF;
  1012. signal_exp = (val & 0x3F);
  1013. if ((signal_exp & 0x20) != 0)
  1014. signal_exp -= 0x40;
  1015. if (signal_mant != 0)
  1016. result = intlog10(2) * 10 * signal_exp + 10 *
  1017. intlog10(signal_mant);
  1018. else
  1019. result = intlog10(2) * 10 * signal_exp - 100;
  1020. if (noise_mant != 0)
  1021. result -= intlog10(2) * 10 * noise_exp + 10 *
  1022. intlog10(noise_mant);
  1023. else
  1024. result -= intlog10(2) * 10 * noise_exp - 100;
  1025. *snr = result / ((1 << 24) / 10);
  1026. return 0;
  1027. }
  1028. static int dib7000p_fe_get_tune_settings(struct dvb_frontend* fe, struct dvb_frontend_tune_settings *tune)
  1029. {
  1030. tune->min_delay_ms = 1000;
  1031. return 0;
  1032. }
  1033. static void dib7000p_release(struct dvb_frontend *demod)
  1034. {
  1035. struct dib7000p_state *st = demod->demodulator_priv;
  1036. dibx000_exit_i2c_master(&st->i2c_master);
  1037. kfree(st);
  1038. }
  1039. int dib7000pc_detection(struct i2c_adapter *i2c_adap)
  1040. {
  1041. u8 tx[2], rx[2];
  1042. struct i2c_msg msg[2] = {
  1043. { .addr = 18 >> 1, .flags = 0, .buf = tx, .len = 2 },
  1044. { .addr = 18 >> 1, .flags = I2C_M_RD, .buf = rx, .len = 2 },
  1045. };
  1046. tx[0] = 0x03;
  1047. tx[1] = 0x00;
  1048. if (i2c_transfer(i2c_adap, msg, 2) == 2)
  1049. if (rx[0] == 0x01 && rx[1] == 0xb3) {
  1050. dprintk("-D- DiB7000PC detected");
  1051. return 1;
  1052. }
  1053. msg[0].addr = msg[1].addr = 0x40;
  1054. if (i2c_transfer(i2c_adap, msg, 2) == 2)
  1055. if (rx[0] == 0x01 && rx[1] == 0xb3) {
  1056. dprintk("-D- DiB7000PC detected");
  1057. return 1;
  1058. }
  1059. dprintk("-D- DiB7000PC not detected");
  1060. return 0;
  1061. }
  1062. EXPORT_SYMBOL(dib7000pc_detection);
  1063. struct i2c_adapter * dib7000p_get_i2c_master(struct dvb_frontend *demod, enum dibx000_i2c_interface intf, int gating)
  1064. {
  1065. struct dib7000p_state *st = demod->demodulator_priv;
  1066. return dibx000_get_i2c_adapter(&st->i2c_master, intf, gating);
  1067. }
  1068. EXPORT_SYMBOL(dib7000p_get_i2c_master);
  1069. int dib7000p_pid_filter_ctrl(struct dvb_frontend *fe, u8 onoff)
  1070. {
  1071. struct dib7000p_state *state = fe->demodulator_priv;
  1072. u16 val = dib7000p_read_word(state, 235) & 0xffef;
  1073. val |= (onoff & 0x1) << 4;
  1074. dprintk("PID filter enabled %d", onoff);
  1075. return dib7000p_write_word(state, 235, val);
  1076. }
  1077. EXPORT_SYMBOL(dib7000p_pid_filter_ctrl);
  1078. int dib7000p_pid_filter(struct dvb_frontend *fe, u8 id, u16 pid, u8 onoff)
  1079. {
  1080. struct dib7000p_state *state = fe->demodulator_priv;
  1081. dprintk("PID filter: index %x, PID %d, OnOff %d", id, pid, onoff);
  1082. return dib7000p_write_word(state, 241 + id, onoff ? (1 << 13) | pid : 0);
  1083. }
  1084. EXPORT_SYMBOL(dib7000p_pid_filter);
  1085. int dib7000p_i2c_enumeration(struct i2c_adapter *i2c, int no_of_demods, u8 default_addr, struct dib7000p_config cfg[])
  1086. {
  1087. struct dib7000p_state *dpst;
  1088. int k = 0;
  1089. u8 new_addr = 0;
  1090. dpst = kzalloc(sizeof(struct dib7000p_state), GFP_KERNEL);
  1091. if (!dpst)
  1092. return -ENOMEM;
  1093. dpst->i2c_adap = i2c;
  1094. for (k = no_of_demods-1; k >= 0; k--) {
  1095. dpst->cfg = cfg[k];
  1096. /* designated i2c address */
  1097. new_addr = (0x40 + k) << 1;
  1098. dpst->i2c_addr = new_addr;
  1099. dib7000p_write_word(dpst, 1287, 0x0003); /* sram lead in, rdy */
  1100. if (dib7000p_identify(dpst) != 0) {
  1101. dpst->i2c_addr = default_addr;
  1102. dib7000p_write_word(dpst, 1287, 0x0003); /* sram lead in, rdy */
  1103. if (dib7000p_identify(dpst) != 0) {
  1104. dprintk("DiB7000P #%d: not identified\n", k);
  1105. kfree(dpst);
  1106. return -EIO;
  1107. }
  1108. }
  1109. /* start diversity to pull_down div_str - just for i2c-enumeration */
  1110. dib7000p_set_output_mode(dpst, OUTMODE_DIVERSITY);
  1111. /* set new i2c address and force divstart */
  1112. dib7000p_write_word(dpst, 1285, (new_addr << 2) | 0x2);
  1113. dprintk("IC %d initialized (to i2c_address 0x%x)", k, new_addr);
  1114. }
  1115. for (k = 0; k < no_of_demods; k++) {
  1116. dpst->cfg = cfg[k];
  1117. dpst->i2c_addr = (0x40 + k) << 1;
  1118. // unforce divstr
  1119. dib7000p_write_word(dpst, 1285, dpst->i2c_addr << 2);
  1120. /* deactivate div - it was just for i2c-enumeration */
  1121. dib7000p_set_output_mode(dpst, OUTMODE_HIGH_Z);
  1122. }
  1123. kfree(dpst);
  1124. return 0;
  1125. }
  1126. EXPORT_SYMBOL(dib7000p_i2c_enumeration);
  1127. static struct dvb_frontend_ops dib7000p_ops;
  1128. struct dvb_frontend * dib7000p_attach(struct i2c_adapter *i2c_adap, u8 i2c_addr, struct dib7000p_config *cfg)
  1129. {
  1130. struct dvb_frontend *demod;
  1131. struct dib7000p_state *st;
  1132. st = kzalloc(sizeof(struct dib7000p_state), GFP_KERNEL);
  1133. if (st == NULL)
  1134. return NULL;
  1135. memcpy(&st->cfg, cfg, sizeof(struct dib7000p_config));
  1136. st->i2c_adap = i2c_adap;
  1137. st->i2c_addr = i2c_addr;
  1138. st->gpio_val = cfg->gpio_val;
  1139. st->gpio_dir = cfg->gpio_dir;
  1140. /* Ensure the output mode remains at the previous default if it's
  1141. * not specifically set by the caller.
  1142. */
  1143. if ((st->cfg.output_mode != OUTMODE_MPEG2_SERIAL) &&
  1144. (st->cfg.output_mode != OUTMODE_MPEG2_PAR_GATED_CLK))
  1145. st->cfg.output_mode = OUTMODE_MPEG2_FIFO;
  1146. demod = &st->demod;
  1147. demod->demodulator_priv = st;
  1148. memcpy(&st->demod.ops, &dib7000p_ops, sizeof(struct dvb_frontend_ops));
  1149. dib7000p_write_word(st, 1287, 0x0003); /* sram lead in, rdy */
  1150. if (dib7000p_identify(st) != 0)
  1151. goto error;
  1152. /* FIXME: make sure the dev.parent field is initialized, or else
  1153. request_firmware() will hit an OOPS (this should be moved somewhere
  1154. more common) */
  1155. st->i2c_master.gated_tuner_i2c_adap.dev.parent = i2c_adap->dev.parent;
  1156. dibx000_init_i2c_master(&st->i2c_master, DIB7000P, st->i2c_adap, st->i2c_addr);
  1157. dib7000p_demod_reset(st);
  1158. return demod;
  1159. error:
  1160. kfree(st);
  1161. return NULL;
  1162. }
  1163. EXPORT_SYMBOL(dib7000p_attach);
  1164. static struct dvb_frontend_ops dib7000p_ops = {
  1165. .info = {
  1166. .name = "DiBcom 7000PC",
  1167. .type = FE_OFDM,
  1168. .frequency_min = 44250000,
  1169. .frequency_max = 867250000,
  1170. .frequency_stepsize = 62500,
  1171. .caps = FE_CAN_INVERSION_AUTO |
  1172. FE_CAN_FEC_1_2 | FE_CAN_FEC_2_3 | FE_CAN_FEC_3_4 |
  1173. FE_CAN_FEC_5_6 | FE_CAN_FEC_7_8 | FE_CAN_FEC_AUTO |
  1174. FE_CAN_QPSK | FE_CAN_QAM_16 | FE_CAN_QAM_64 | FE_CAN_QAM_AUTO |
  1175. FE_CAN_TRANSMISSION_MODE_AUTO |
  1176. FE_CAN_GUARD_INTERVAL_AUTO |
  1177. FE_CAN_RECOVER |
  1178. FE_CAN_HIERARCHY_AUTO,
  1179. },
  1180. .release = dib7000p_release,
  1181. .init = dib7000p_wakeup,
  1182. .sleep = dib7000p_sleep,
  1183. .set_frontend = dib7000p_set_frontend,
  1184. .get_tune_settings = dib7000p_fe_get_tune_settings,
  1185. .get_frontend = dib7000p_get_frontend,
  1186. .read_status = dib7000p_read_status,
  1187. .read_ber = dib7000p_read_ber,
  1188. .read_signal_strength = dib7000p_read_signal_strength,
  1189. .read_snr = dib7000p_read_snr,
  1190. .read_ucblocks = dib7000p_read_unc_blocks,
  1191. };
  1192. MODULE_AUTHOR("Patrick Boettcher <pboettcher@dibcom.fr>");
  1193. MODULE_DESCRIPTION("Driver for the DiBcom 7000PC COFDM demodulator");
  1194. MODULE_LICENSE("GPL");