dib7000p.c 42 KB

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