dib7000p.c 42 KB

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