dib7000p.c 42 KB

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