dib7000p.c 30 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024
  1. /*
  2. * Linux-DVB Driver for DiBcom's second generation DiB7000P (PC).
  3. *
  4. * Copyright (C) 2005-6 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_frontend.h"
  13. #include "dib7000p.h"
  14. static int debug;
  15. module_param(debug, int, 0644);
  16. MODULE_PARM_DESC(debug, "turn on debugging (default: 0)");
  17. #define dprintk(args...) do { if (debug) { printk(KERN_DEBUG "DiB7000P:"); printk(args); } } while (0)
  18. struct dib7000p_state {
  19. struct dvb_frontend demod;
  20. struct dib7000p_config cfg;
  21. u8 i2c_addr;
  22. struct i2c_adapter *i2c_adap;
  23. struct dibx000_i2c_master i2c_master;
  24. u16 wbd_ref;
  25. u8 current_band;
  26. fe_bandwidth_t current_bandwidth;
  27. struct dibx000_agc_config *current_agc;
  28. u32 timf;
  29. u16 gpio_dir;
  30. u16 gpio_val;
  31. };
  32. enum dib7000p_power_mode {
  33. DIB7000P_POWER_ALL = 0,
  34. DIB7000P_POWER_INTERFACE_ONLY,
  35. };
  36. static u16 dib7000p_read_word(struct dib7000p_state *state, u16 reg)
  37. {
  38. u8 wb[2] = { reg >> 8, reg & 0xff };
  39. u8 rb[2];
  40. struct i2c_msg msg[2] = {
  41. { .addr = state->i2c_addr >> 1, .flags = 0, .buf = wb, .len = 2 },
  42. { .addr = state->i2c_addr >> 1, .flags = I2C_M_RD, .buf = rb, .len = 2 },
  43. };
  44. if (i2c_transfer(state->i2c_adap, msg, 2) != 2)
  45. dprintk("i2c read error on %d\n",reg);
  46. return (rb[0] << 8) | rb[1];
  47. }
  48. static int dib7000p_write_word(struct dib7000p_state *state, u16 reg, u16 val)
  49. {
  50. u8 b[4] = {
  51. (reg >> 8) & 0xff, reg & 0xff,
  52. (val >> 8) & 0xff, val & 0xff,
  53. };
  54. struct i2c_msg msg = {
  55. .addr = state->i2c_addr >> 1, .flags = 0, .buf = b, .len = 4
  56. };
  57. return i2c_transfer(state->i2c_adap, &msg, 1) != 1 ? -EREMOTEIO : 0;
  58. }
  59. static int dib7000p_set_output_mode(struct dib7000p_state *state, int mode)
  60. {
  61. int ret = 0;
  62. u16 outreg, fifo_threshold, smo_mode;
  63. outreg = 0;
  64. fifo_threshold = 1792;
  65. smo_mode = (dib7000p_read_word(state, 235) & 0x0010) | (1 << 1);
  66. dprintk("-I- Setting output mode for demod %p to %d\n",
  67. &state->demod, mode);
  68. switch (mode) {
  69. case OUTMODE_MPEG2_PAR_GATED_CLK: // STBs with parallel gated clock
  70. outreg = (1 << 10); /* 0x0400 */
  71. break;
  72. case OUTMODE_MPEG2_PAR_CONT_CLK: // STBs with parallel continues clock
  73. outreg = (1 << 10) | (1 << 6); /* 0x0440 */
  74. break;
  75. case OUTMODE_MPEG2_SERIAL: // STBs with serial input
  76. outreg = (1 << 10) | (2 << 6) | (0 << 1); /* 0x0480 */
  77. break;
  78. case OUTMODE_DIVERSITY:
  79. if (state->cfg.hostbus_diversity)
  80. outreg = (1 << 10) | (4 << 6); /* 0x0500 */
  81. else
  82. outreg = (1 << 11);
  83. break;
  84. case OUTMODE_MPEG2_FIFO: // e.g. USB feeding
  85. smo_mode |= (3 << 1);
  86. fifo_threshold = 512;
  87. outreg = (1 << 10) | (5 << 6);
  88. break;
  89. case OUTMODE_HIGH_Z: // disable
  90. outreg = 0;
  91. break;
  92. default:
  93. dprintk("Unhandled output_mode passed to be set for demod %p\n",&state->demod);
  94. break;
  95. }
  96. if (state->cfg.hostbus_diversity) {
  97. ret |= dib7000p_write_word(state, 204, 1); // Diversity ?
  98. ret |= dib7000p_write_word(state, 205, 0); // Diversity ?
  99. }
  100. if (state->cfg.output_mpeg2_in_188_bytes)
  101. smo_mode |= (1 << 5) ;
  102. ret |= dib7000p_write_word(state, 235, smo_mode);
  103. ret |= dib7000p_write_word(state, 236, fifo_threshold); /* synchronous fread */
  104. ret |= dib7000p_write_word(state, 1286, outreg); /* P_Div_active */
  105. return ret;
  106. }
  107. static int dib7000p_set_power_mode(struct dib7000p_state *state, enum dib7000p_power_mode mode)
  108. {
  109. /* by default everything is powered off */
  110. u16 reg_774 = 0xffff, reg_775 = 0xffff, reg_776 = 0x0007, reg_899 = 0x0003,
  111. reg_1280 = (0xfe00) | (dib7000p_read_word(state, 1280) & 0x01ff);
  112. /* now, depending on the requested mode, we power on */
  113. switch (mode) {
  114. /* power up everything in the demod */
  115. case DIB7000P_POWER_ALL:
  116. reg_774 = 0x0000; reg_775 = 0x0000; reg_776 = 0x0; reg_899 = 0x0; reg_1280 &= 0x01ff;
  117. break;
  118. /* just leave power on the control-interfaces: GPIO and (I2C or SDIO) */
  119. case DIB7000P_POWER_INTERFACE_ONLY: /* TODO power up either SDIO or I2C */
  120. reg_1280 &= ~((1 << 14) | (1 << 13) | (1 << 12) | (1 << 10));
  121. break;
  122. /* TODO following stuff is just converted from the dib7000-driver - check when is used what */
  123. }
  124. dib7000p_write_word(state, 774, reg_774);
  125. dib7000p_write_word(state, 775, reg_775);
  126. dib7000p_write_word(state, 776, reg_776);
  127. dib7000p_write_word(state, 899, reg_899);
  128. dib7000p_write_word(state, 1280, reg_1280);
  129. return 0;
  130. }
  131. static void dib7000p_set_adc_state(struct dib7000p_state *state, enum dibx000_adc_states no)
  132. {
  133. u16 reg_908 = dib7000p_read_word(state, 908),
  134. reg_909 = dib7000p_read_word(state, 909);
  135. switch (no) {
  136. case DIBX000_SLOW_ADC_ON:
  137. reg_909 |= (1 << 1) | (1 << 0);
  138. dib7000p_write_word(state, 909, reg_909);
  139. reg_909 &= ~(1 << 1);
  140. break;
  141. case DIBX000_SLOW_ADC_OFF:
  142. reg_909 |= (1 << 1) | (1 << 0);
  143. break;
  144. case DIBX000_ADC_ON:
  145. reg_908 &= 0x0fff;
  146. reg_909 &= 0x0003;
  147. break;
  148. case DIBX000_ADC_OFF: // leave the VBG voltage on
  149. reg_908 |= (1 << 14) | (1 << 13) | (1 << 12);
  150. reg_909 |= (1 << 5) | (1 << 4) | (1 << 3) | (1 << 2);
  151. break;
  152. case DIBX000_VBG_ENABLE:
  153. reg_908 &= ~(1 << 15);
  154. break;
  155. case DIBX000_VBG_DISABLE:
  156. reg_908 |= (1 << 15);
  157. break;
  158. default:
  159. break;
  160. }
  161. // dprintk("908: %x, 909: %x\n", reg_908, reg_909);
  162. dib7000p_write_word(state, 908, reg_908);
  163. dib7000p_write_word(state, 909, reg_909);
  164. }
  165. static int dib7000p_set_bandwidth(struct dvb_frontend *demod, u8 BW_Idx)
  166. {
  167. struct dib7000p_state *state = demod->demodulator_priv;
  168. u32 timf;
  169. // store the current bandwidth for later use
  170. state->current_bandwidth = BW_Idx;
  171. if (state->timf == 0) {
  172. dprintk("-D- Using default timf\n");
  173. timf = state->cfg.bw->timf;
  174. } else {
  175. dprintk("-D- Using updated timf\n");
  176. timf = state->timf;
  177. }
  178. timf = timf * (BW_INDEX_TO_KHZ(BW_Idx) / 100) / 80;
  179. dprintk("timf: %d\n",timf);
  180. dib7000p_write_word(state, 23, (timf >> 16) & 0xffff);
  181. dib7000p_write_word(state, 24, (timf ) & 0xffff);
  182. return 0;
  183. }
  184. static int dib7000p_sad_calib(struct dib7000p_state *state)
  185. {
  186. /* internal */
  187. // dib7000p_write_word(state, 72, (3 << 14) | (1 << 12) | (524 << 0)); // sampling clock of the SAD is written in set_bandwidth
  188. dib7000p_write_word(state, 73, (0 << 1) | (0 << 0));
  189. dib7000p_write_word(state, 74, 776); // 0.625*3.3 / 4096
  190. /* do the calibration */
  191. dib7000p_write_word(state, 73, (1 << 0));
  192. dib7000p_write_word(state, 73, (0 << 0));
  193. msleep(1);
  194. return 0;
  195. }
  196. static void dib7000p_reset_pll(struct dib7000p_state *state)
  197. {
  198. struct dibx000_bandwidth_config *bw = &state->cfg.bw[0];
  199. dib7000p_write_word(state, 903, (bw->pll_prediv << 5) | (((bw->pll_ratio >> 6) & 0x3) << 3) | (bw->pll_range << 1) | bw->pll_reset);
  200. dib7000p_write_word(state, 900, ((bw->pll_ratio & 0x3f) << 9) | (bw->pll_bypass << 15) | (bw->modulo << 7) | (bw->ADClkSrc << 6) |
  201. (bw->IO_CLK_en_core << 5) | (bw->bypclk_div << 2) | (bw->enable_refdiv << 1) | (0 << 0));
  202. dib7000p_write_word(state, 18, ((bw->internal*1000) >> 16) & 0xffff);
  203. dib7000p_write_word(state, 19, (bw->internal*1000 ) & 0xffff);
  204. dib7000p_write_word(state, 21, (bw->ifreq >> 16) & 0xffff);
  205. dib7000p_write_word(state, 22, (bw->ifreq ) & 0xffff);
  206. dib7000p_write_word(state, 72, bw->sad_cfg);
  207. }
  208. static int dib7000p_reset_gpio(struct dib7000p_state *st)
  209. {
  210. /* reset the GPIOs */
  211. dprintk("-D- gpio dir: %x: gpio val: %x, gpio pwm pos: %x\n",st->gpio_dir, st->gpio_val,st->cfg.gpio_pwm_pos);
  212. dib7000p_write_word(st, 1029, st->gpio_dir);
  213. dib7000p_write_word(st, 1030, st->gpio_val);
  214. /* TODO 1031 is P_gpio_od */
  215. dib7000p_write_word(st, 1032, st->cfg.gpio_pwm_pos);
  216. dib7000p_write_word(st, 1037, st->cfg.pwm_freq_div);
  217. return 0;
  218. }
  219. static int dib7000p_demod_reset(struct dib7000p_state *state)
  220. {
  221. dib7000p_set_power_mode(state, DIB7000P_POWER_ALL);
  222. dib7000p_set_adc_state(state, DIBX000_VBG_ENABLE);
  223. /* restart all parts */
  224. dib7000p_write_word(state, 770, 0xffff);
  225. dib7000p_write_word(state, 771, 0xffff);
  226. dib7000p_write_word(state, 772, 0x001f);
  227. dib7000p_write_word(state, 898, 0x0003);
  228. /* except i2c, sdio, gpio - control interfaces */
  229. dib7000p_write_word(state, 1280, 0x01fc - ((1 << 7) | (1 << 6) | (1 << 5)) );
  230. dib7000p_write_word(state, 770, 0);
  231. dib7000p_write_word(state, 771, 0);
  232. dib7000p_write_word(state, 772, 0);
  233. dib7000p_write_word(state, 898, 0);
  234. dib7000p_write_word(state, 1280, 0);
  235. /* default */
  236. dib7000p_reset_pll(state);
  237. if (dib7000p_reset_gpio(state) != 0)
  238. dprintk("-E- GPIO reset was not successful.\n");
  239. if (dib7000p_set_output_mode(state, OUTMODE_HIGH_Z) != 0)
  240. dprintk("-E- OUTPUT_MODE could not be resetted.\n");
  241. /* unforce divstr regardless whether i2c enumeration was done or not */
  242. dib7000p_write_word(state, 1285, dib7000p_read_word(state, 1285) & ~(1 << 1) );
  243. dib7000p_set_power_mode(state, DIB7000P_POWER_INTERFACE_ONLY);
  244. return 0;
  245. }
  246. static void dib7000p_restart_agc(struct dib7000p_state *state)
  247. {
  248. // P_restart_iqc & P_restart_agc
  249. dib7000p_write_word(state, 770, 0x0c00);
  250. dib7000p_write_word(state, 770, 0x0000);
  251. }
  252. static void dib7000p_update_lna(struct dib7000p_state *state)
  253. {
  254. int i;
  255. u16 dyn_gain;
  256. // when there is no LNA to program return immediatly
  257. if (state->cfg.update_lna == NULL)
  258. return;
  259. for (i = 0; i < 5; i++) {
  260. // read dyn_gain here (because it is demod-dependent and not tuner)
  261. dyn_gain = dib7000p_read_word(state, 394);
  262. if (state->cfg.update_lna(&state->demod,dyn_gain)) { // LNA has changed
  263. dib7000p_restart_agc(state);
  264. msleep(5);
  265. } else
  266. break;
  267. }
  268. }
  269. static void dib7000p_pll_clk_cfg(struct dib7000p_state *state)
  270. {
  271. u16 tmp = 0;
  272. tmp = dib7000p_read_word(state, 903);
  273. dib7000p_write_word(state, 903, (tmp | 0x1)); //pwr-up pll
  274. tmp = dib7000p_read_word(state, 900);
  275. dib7000p_write_word(state, 900, (tmp & 0x7fff) | (1 << 6)); //use High freq clock
  276. }
  277. static void dib7000p_update_timf_freq(struct dib7000p_state *state)
  278. {
  279. u32 timf = (dib7000p_read_word(state, 427) << 16) | dib7000p_read_word(state, 428);
  280. state->timf = timf * 80 / (BW_INDEX_TO_KHZ(state->current_bandwidth) / 100);
  281. dib7000p_write_word(state, 23, (u16) (timf >> 16));
  282. dib7000p_write_word(state, 24, (u16) (timf & 0xffff));
  283. dprintk("-D- Updated timf_frequency: %d (default: %d)\n",state->timf, state->cfg.bw->timf);
  284. }
  285. static void dib7000p_set_channel(struct dib7000p_state *state, struct dibx000_ofdm_channel *ch, u8 seq)
  286. {
  287. u16 tmp, est[4]; // reg_26, reg_32, reg_33, reg_187, reg_188, reg_189, reg_190, reg_207, reg_208;
  288. /* nfft, guard, qam, alpha */
  289. dib7000p_write_word(state, 0, (ch->nfft << 7) | (ch->guard << 5) | (ch->nqam << 3) | (ch->vit_alpha));
  290. dib7000p_write_word(state, 5, (seq << 4) | 1); /* do not force tps, search list 0 */
  291. /* P_dintl_native, P_dintlv_inv, P_vit_hrch, P_vit_code_rate, P_vit_select_hp */
  292. tmp = (ch->intlv_native << 6) | (ch->vit_hrch << 4) | (ch->vit_select_hp & 0x1);
  293. if (ch->vit_hrch == 0 || ch->vit_select_hp == 1)
  294. tmp |= (ch->vit_code_rate_hp << 1);
  295. else
  296. tmp |= (ch->vit_code_rate_lp << 1);
  297. dib7000p_write_word(state, 208, tmp);
  298. /* P_dvsy_sync_wait */
  299. switch (ch->nfft) {
  300. case 1: tmp = 256; break;
  301. case 2: tmp = 128; break;
  302. case 0:
  303. default: tmp = 64; break;
  304. }
  305. tmp *= ((1 << (ch->guard)) * 3 / 2); // add 50% SFN margin
  306. tmp <<= 4;
  307. /* deactive the possibility of diversity reception if extended interleave */
  308. /* P_dvsy_sync_mode = 0, P_dvsy_sync_enable=1, P_dvcb_comb_mode=2 */
  309. if (ch->intlv_native || ch->nfft == 1)
  310. tmp |= (1 << 2) | (2 << 0);
  311. dib7000p_write_word(state, 207, tmp);
  312. dib7000p_write_word(state, 26, 0x6680); // timf(6xxx)
  313. dib7000p_write_word(state, 29, 0x1273); // isi inh1273 on1073
  314. dib7000p_write_word(state, 32, 0x0003); // pha_off_max(xxx3)
  315. dib7000p_write_word(state, 33, 0x0005); // sfreq(xxx5)
  316. /* channel estimation fine configuration */
  317. switch (ch->nqam) {
  318. case 2:
  319. est[0] = 0x0148; /* P_adp_regul_cnt 0.04 */
  320. est[1] = 0xfff0; /* P_adp_noise_cnt -0.002 */
  321. est[2] = 0x00a4; /* P_adp_regul_ext 0.02 */
  322. est[3] = 0xfff8; /* P_adp_noise_ext -0.001 */
  323. break;
  324. case 1:
  325. est[0] = 0x023d; /* P_adp_regul_cnt 0.07 */
  326. est[1] = 0xffdf; /* P_adp_noise_cnt -0.004 */
  327. est[2] = 0x00a4; /* P_adp_regul_ext 0.02 */
  328. est[3] = 0xfff0; /* P_adp_noise_ext -0.002 */
  329. break;
  330. default:
  331. est[0] = 0x099a; /* P_adp_regul_cnt 0.3 */
  332. est[1] = 0xffae; /* P_adp_noise_cnt -0.01 */
  333. est[2] = 0x0333; /* P_adp_regul_ext 0.1 */
  334. est[3] = 0xfff8; /* P_adp_noise_ext -0.002 */
  335. break;
  336. }
  337. for (tmp = 0; tmp < 4; tmp++)
  338. dib7000p_write_word(state, 187 + tmp, est[tmp]);
  339. // set power-up level: interf+analog+AGC
  340. dib7000p_set_power_mode(state, DIB7000P_POWER_ALL);
  341. dib7000p_set_adc_state(state, DIBX000_ADC_ON);
  342. dib7000p_pll_clk_cfg(state);
  343. msleep(7);
  344. // AGC initialization
  345. if (state->cfg.agc_control)
  346. state->cfg.agc_control(&state->demod, 1);
  347. dib7000p_restart_agc(state);
  348. // wait AGC rough lock time
  349. msleep(5);
  350. dib7000p_update_lna(state);
  351. // wait AGC accurate lock time
  352. msleep(7);
  353. if (state->cfg.agc_control)
  354. state->cfg.agc_control(&state->demod, 0);
  355. }
  356. static int dib7000p_autosearch_start(struct dvb_frontend *demod, struct dibx000_ofdm_channel *ch)
  357. {
  358. struct dib7000p_state *state = demod->demodulator_priv;
  359. struct dibx000_ofdm_channel auto_ch;
  360. u32 value;
  361. INIT_OFDM_CHANNEL(&auto_ch);
  362. auto_ch.RF_kHz = ch->RF_kHz;
  363. auto_ch.Bw = ch->Bw;
  364. auto_ch.nqam = 2;
  365. auto_ch.guard = 0;
  366. auto_ch.nfft = 1;
  367. auto_ch.vit_alpha = 1;
  368. auto_ch.vit_select_hp = 1;
  369. auto_ch.vit_code_rate_hp = 2;
  370. auto_ch.vit_code_rate_lp = 3;
  371. auto_ch.vit_hrch = 0;
  372. auto_ch.intlv_native = 1;
  373. dib7000p_set_channel(state, &auto_ch, 7);
  374. // always use the setting for 8MHz here lock_time for 7,6 MHz are longer
  375. value = 30 * state->cfg.bw->internal;
  376. dib7000p_write_word(state, 6, (u16) ((value >> 16) & 0xffff)); // lock0 wait time
  377. dib7000p_write_word(state, 7, (u16) (value & 0xffff)); // lock0 wait time
  378. value = 100 * state->cfg.bw->internal;
  379. dib7000p_write_word(state, 8, (u16) ((value >> 16) & 0xffff)); // lock1 wait time
  380. dib7000p_write_word(state, 9, (u16) (value & 0xffff)); // lock1 wait time
  381. value = 500 * state->cfg.bw->internal;
  382. dib7000p_write_word(state, 10, (u16) ((value >> 16) & 0xffff)); // lock2 wait time
  383. dib7000p_write_word(state, 11, (u16) (value & 0xffff)); // lock2 wait time
  384. value = dib7000p_read_word(state, 0);
  385. dib7000p_write_word(state, 0, (1 << 9) | value);
  386. dib7000p_read_word(state, 1284);
  387. dib7000p_write_word(state, 0, (u16) value);
  388. return 0;
  389. }
  390. static int dib7000p_autosearch_is_irq(struct dvb_frontend *demod)
  391. {
  392. struct dib7000p_state *state = demod->demodulator_priv;
  393. u16 irq_pending = dib7000p_read_word(state, 1284);
  394. if (irq_pending & 0x1) // failed
  395. return 1;
  396. if (irq_pending & 0x2) // succeeded
  397. return 2;
  398. return 0; // still pending
  399. }
  400. static int dib7000p_tune(struct dvb_frontend *demod, struct dibx000_ofdm_channel *ch)
  401. {
  402. struct dib7000p_state *state = demod->demodulator_priv;
  403. u16 tmp = 0;
  404. if (ch != NULL)
  405. dib7000p_set_channel(state, ch, 0);
  406. else
  407. return -EINVAL;
  408. // restart demod
  409. dib7000p_write_word(state, 770, 0x4000);
  410. dib7000p_write_word(state, 770, 0x0000);
  411. msleep(45);
  412. /* 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 */
  413. dib7000p_write_word(state, 29, (0 << 14) | (4 << 10) | (0 << 9) | (3 << 5) | (1 << 4) | (0x3));
  414. // never achieved a lock with that bandwidth so far - wait for osc-freq to update
  415. if (state->timf == 0)
  416. msleep(200);
  417. /* offset loop parameters */
  418. /* P_timf_alpha, P_corm_alpha=6, P_corm_thres=0x80 */
  419. tmp = (6 << 8) | 0x80;
  420. switch (ch->nfft) {
  421. case 0: tmp |= (7 << 12); break;
  422. case 1: tmp |= (9 << 12); break;
  423. case 2: tmp |= (8 << 12); break;
  424. }
  425. dib7000p_write_word(state, 26, tmp); /* timf_a(6xxx) */
  426. /* P_ctrl_freeze_pha_shift=0, P_ctrl_pha_off_max */
  427. tmp = (0 << 4);
  428. switch (ch->nfft) {
  429. case 0: tmp |= 0x6; break;
  430. case 1: tmp |= 0x8; break;
  431. case 2: tmp |= 0x7; break;
  432. }
  433. dib7000p_write_word(state, 32, tmp);
  434. /* P_ctrl_sfreq_inh=0, P_ctrl_sfreq_step */
  435. tmp = (0 << 4);
  436. switch (ch->nfft) {
  437. case 0: tmp |= 0x6; break;
  438. case 1: tmp |= 0x8; break;
  439. case 2: tmp |= 0x7; break;
  440. }
  441. dib7000p_write_word(state, 33, tmp);
  442. tmp = dib7000p_read_word(state,509);
  443. if (!((tmp >> 6) & 0x1)) {
  444. /* restart the fec */
  445. tmp = dib7000p_read_word(state,771);
  446. dib7000p_write_word(state, 771, tmp | (1 << 1));
  447. dib7000p_write_word(state, 771, tmp);
  448. msleep(10);
  449. tmp = dib7000p_read_word(state,509);
  450. }
  451. // we achieved a lock - it's time to update the osc freq
  452. if ((tmp >> 6) & 0x1)
  453. dib7000p_update_timf_freq(state);
  454. return 0;
  455. }
  456. static int dib7000p_init(struct dvb_frontend *demod)
  457. {
  458. struct dibx000_agc_config *agc;
  459. struct dib7000p_state *state = demod->demodulator_priv;
  460. int ret = 0;
  461. // Demodulator default configuration
  462. agc = state->cfg.agc;
  463. dib7000p_set_power_mode(state, DIB7000P_POWER_ALL);
  464. dib7000p_set_adc_state(state, DIBX000_SLOW_ADC_ON);
  465. /* AGC */
  466. ret |= dib7000p_write_word(state, 75 , agc->setup );
  467. ret |= dib7000p_write_word(state, 76 , agc->inv_gain );
  468. ret |= dib7000p_write_word(state, 77 , agc->time_stabiliz );
  469. ret |= dib7000p_write_word(state, 100, (agc->alpha_level << 12) | agc->thlock);
  470. // Demod AGC loop configuration
  471. ret |= dib7000p_write_word(state, 101, (agc->alpha_mant << 5) | agc->alpha_exp);
  472. ret |= dib7000p_write_word(state, 102, (agc->beta_mant << 6) | agc->beta_exp);
  473. /* AGC continued */
  474. dprintk("-D- WBD: ref: %d, sel: %d, active: %d, alpha: %d\n",
  475. state->wbd_ref != 0 ? state->wbd_ref : agc->wbd_ref, agc->wbd_sel, !agc->perform_agc_softsplit, agc->wbd_sel);
  476. if (state->wbd_ref != 0)
  477. ret |= dib7000p_write_word(state, 105, (agc->wbd_inv << 12) | state->wbd_ref);
  478. else
  479. ret |= dib7000p_write_word(state, 105, (agc->wbd_inv << 12) | agc->wbd_ref);
  480. ret |= dib7000p_write_word(state, 106, (agc->wbd_sel << 13) | (agc->wbd_alpha << 9) | (agc->perform_agc_softsplit << 8) );
  481. ret |= dib7000p_write_word(state, 107, agc->agc1_max);
  482. ret |= dib7000p_write_word(state, 108, agc->agc1_min);
  483. ret |= dib7000p_write_word(state, 109, agc->agc2_max);
  484. ret |= dib7000p_write_word(state, 110, agc->agc2_min);
  485. ret |= dib7000p_write_word(state, 111, (agc->agc1_pt1 << 8) | agc->agc1_pt2 );
  486. ret |= dib7000p_write_word(state, 112, agc->agc1_pt3);
  487. ret |= dib7000p_write_word(state, 113, (agc->agc1_slope1 << 8) | agc->agc1_slope2);
  488. ret |= dib7000p_write_word(state, 114, (agc->agc2_pt1 << 8) | agc->agc2_pt2);
  489. ret |= dib7000p_write_word(state, 115, (agc->agc2_slope1 << 8) | agc->agc2_slope2);
  490. /* disable power smoothing */
  491. ret |= dib7000p_write_word(state, 145, 0);
  492. ret |= dib7000p_write_word(state, 146, 0);
  493. ret |= dib7000p_write_word(state, 147, 0);
  494. ret |= dib7000p_write_word(state, 148, 0);
  495. ret |= dib7000p_write_word(state, 149, 0);
  496. ret |= dib7000p_write_word(state, 150, 0);
  497. ret |= dib7000p_write_word(state, 151, 0);
  498. ret |= dib7000p_write_word(state, 152, 0);
  499. // P_timf_alpha=6, P_corm_alpha=6, P_corm_thres=128 default: 6,4,26
  500. ret |= dib7000p_write_word(state, 26 ,0x6680);
  501. // P_palf_filter_on=1, P_palf_filter_freeze=0, P_palf_alpha_regul=16
  502. ret |= dib7000p_write_word(state, 142,0x0410);
  503. // P_fft_freq_dir=1, P_fft_nb_to_cut=0
  504. ret |= dib7000p_write_word(state, 154,1 << 13);
  505. // P_pha3_thres, default 0x3000
  506. ret |= dib7000p_write_word(state, 168,0x0ccd);
  507. // P_cti_use_cpe=0, P_cti_use_prog=0, P_cti_win_len=16, default: 0x0010
  508. //ret |= dib7000p_write_word(state, 169,0x0010);
  509. // P_cspu_regul=512, P_cspu_win_cut=15, default: 0x2005
  510. ret |= dib7000p_write_word(state, 183,0x200f);
  511. // P_adp_regul_cnt=573, default: 410
  512. ret |= dib7000p_write_word(state, 187,0x023d);
  513. // P_adp_noise_cnt=
  514. ret |= dib7000p_write_word(state, 188,0x00a4);
  515. // P_adp_regul_ext
  516. ret |= dib7000p_write_word(state, 189,0x00a4);
  517. // P_adp_noise_ext
  518. ret |= dib7000p_write_word(state, 190,0x7ff0);
  519. // P_adp_fil
  520. ret |= dib7000p_write_word(state, 191,0x3ccc);
  521. ret |= dib7000p_write_word(state, 222,0x0010);
  522. // P_smo_mode, P_smo_rs_discard, P_smo_fifo_flush, P_smo_pid_parse, P_smo_error_discard
  523. ret |= dib7000p_write_word(state, 235,0x0062);
  524. // P_iqc_alpha_pha, P_iqc_alpha_amp_dcc_alpha, ...
  525. if(state->cfg.tuner_is_baseband)
  526. ret |= dib7000p_write_word(state, 36,0x0755);
  527. else
  528. ret |= dib7000p_write_word(state, 36,0x1f55);
  529. // auto search configuration
  530. ret |= dib7000p_write_word(state, 2 ,0x0004);
  531. ret |= dib7000p_write_word(state, 3 ,0x1000);
  532. /* Equal Lock */
  533. ret |= dib7000p_write_word(state, 4 ,0x0814);
  534. ret |= dib7000p_write_word(state, 6 ,0x001b);
  535. ret |= dib7000p_write_word(state, 7 ,0x7740);
  536. ret |= dib7000p_write_word(state, 8 ,0x005b);
  537. ret |= dib7000p_write_word(state, 9 ,0x8d80);
  538. ret |= dib7000p_write_word(state, 10 ,0x01c9);
  539. ret |= dib7000p_write_word(state, 11 ,0xc380);
  540. ret |= dib7000p_write_word(state, 12 ,0x0000);
  541. ret |= dib7000p_write_word(state, 13 ,0x0080);
  542. ret |= dib7000p_write_word(state, 14 ,0x0000);
  543. ret |= dib7000p_write_word(state, 15 ,0x0090);
  544. ret |= dib7000p_write_word(state, 16 ,0x0001);
  545. ret |= dib7000p_write_word(state, 17 ,0xd4c0);
  546. // P_clk_cfg1
  547. ret |= dib7000p_write_word(state, 901, 0x0006);
  548. // P_divclksel=3 P_divbitsel=1
  549. ret |= dib7000p_write_word(state, 902, (3 << 10) | (1 << 6));
  550. // Tuner IO bank: max drive (14mA) + divout pads max drive
  551. ret |= dib7000p_write_word(state, 905, 0x2c8e);
  552. ret |= dib7000p_set_bandwidth(&state->demod, BANDWIDTH_8_MHZ);
  553. dib7000p_sad_calib(state);
  554. return ret;
  555. }
  556. static int dib7000p_sleep(struct dvb_frontend *demod)
  557. {
  558. struct dib7000p_state *state = demod->demodulator_priv;
  559. return dib7000p_set_output_mode(state, OUTMODE_HIGH_Z) | dib7000p_set_power_mode(state, DIB7000P_POWER_INTERFACE_ONLY);
  560. }
  561. static int dib7000p_identify(struct dib7000p_state *st)
  562. {
  563. u16 value;
  564. dprintk("-I- DiB7000PC: checking demod on I2C address: %d (%x)\n",
  565. st->i2c_addr, st->i2c_addr);
  566. if ((value = dib7000p_read_word(st, 768)) != 0x01b3) {
  567. dprintk("-E- DiB7000PC: wrong Vendor ID (read=0x%x)\n",value);
  568. return -EREMOTEIO;
  569. }
  570. if ((value = dib7000p_read_word(st, 769)) != 0x4000) {
  571. dprintk("-E- DiB7000PC: wrong Device ID (%x)\n",value);
  572. return -EREMOTEIO;
  573. }
  574. return 0;
  575. }
  576. static int dib7000p_get_frontend(struct dvb_frontend* fe,
  577. struct dvb_frontend_parameters *fep)
  578. {
  579. struct dib7000p_state *state = fe->demodulator_priv;
  580. u16 tps = dib7000p_read_word(state,463);
  581. fep->inversion = INVERSION_AUTO;
  582. fep->u.ofdm.bandwidth = state->current_bandwidth;
  583. switch ((tps >> 8) & 0x3) {
  584. case 0: fep->u.ofdm.transmission_mode = TRANSMISSION_MODE_2K; break;
  585. case 1: fep->u.ofdm.transmission_mode = TRANSMISSION_MODE_8K; break;
  586. /* case 2: fep->u.ofdm.transmission_mode = TRANSMISSION_MODE_4K; break; */
  587. }
  588. switch (tps & 0x3) {
  589. case 0: fep->u.ofdm.guard_interval = GUARD_INTERVAL_1_32; break;
  590. case 1: fep->u.ofdm.guard_interval = GUARD_INTERVAL_1_16; break;
  591. case 2: fep->u.ofdm.guard_interval = GUARD_INTERVAL_1_8; break;
  592. case 3: fep->u.ofdm.guard_interval = GUARD_INTERVAL_1_4; break;
  593. }
  594. switch ((tps >> 14) & 0x3) {
  595. case 0: fep->u.ofdm.constellation = QPSK; break;
  596. case 1: fep->u.ofdm.constellation = QAM_16; break;
  597. case 2:
  598. default: fep->u.ofdm.constellation = QAM_64; break;
  599. }
  600. /* as long as the frontend_param structure is fixed for hierarchical transmission I refuse to use it */
  601. /* (tps >> 13) & 0x1 == hrch is used, (tps >> 10) & 0x7 == alpha */
  602. fep->u.ofdm.hierarchy_information = HIERARCHY_NONE;
  603. switch ((tps >> 5) & 0x7) {
  604. case 1: fep->u.ofdm.code_rate_HP = FEC_1_2; break;
  605. case 2: fep->u.ofdm.code_rate_HP = FEC_2_3; break;
  606. case 3: fep->u.ofdm.code_rate_HP = FEC_3_4; break;
  607. case 5: fep->u.ofdm.code_rate_HP = FEC_5_6; break;
  608. case 7:
  609. default: fep->u.ofdm.code_rate_HP = FEC_7_8; break;
  610. }
  611. switch ((tps >> 2) & 0x7) {
  612. case 1: fep->u.ofdm.code_rate_LP = FEC_1_2; break;
  613. case 2: fep->u.ofdm.code_rate_LP = FEC_2_3; break;
  614. case 3: fep->u.ofdm.code_rate_LP = FEC_3_4; break;
  615. case 5: fep->u.ofdm.code_rate_LP = FEC_5_6; break;
  616. case 7:
  617. default: fep->u.ofdm.code_rate_LP = FEC_7_8; break;
  618. }
  619. /* native interleaver: (dib7000p_read_word(state, 464) >> 5) & 0x1 */
  620. return 0;
  621. }
  622. static int dib7000p_set_frontend(struct dvb_frontend* fe,
  623. struct dvb_frontend_parameters *fep)
  624. {
  625. struct dib7000p_state *state = fe->demodulator_priv;
  626. struct dibx000_ofdm_channel ch;
  627. INIT_OFDM_CHANNEL(&ch);
  628. FEP2DIB(fep,&ch);
  629. state->current_bandwidth = fep->u.ofdm.bandwidth;
  630. dib7000p_set_bandwidth(fe, fep->u.ofdm.bandwidth);
  631. if (fe->ops.tuner_ops.set_params)
  632. fe->ops.tuner_ops.set_params(fe, fep);
  633. if (fep->u.ofdm.transmission_mode == TRANSMISSION_MODE_AUTO ||
  634. fep->u.ofdm.guard_interval == GUARD_INTERVAL_AUTO ||
  635. fep->u.ofdm.constellation == QAM_AUTO ||
  636. fep->u.ofdm.code_rate_HP == FEC_AUTO) {
  637. int i = 800, found;
  638. dib7000p_autosearch_start(fe, &ch);
  639. do {
  640. msleep(1);
  641. found = dib7000p_autosearch_is_irq(fe);
  642. } while (found == 0 && i--);
  643. dprintk("autosearch returns: %d\n",found);
  644. if (found == 0 || found == 1)
  645. return 0; // no channel found
  646. dib7000p_get_frontend(fe, fep);
  647. FEP2DIB(fep, &ch);
  648. }
  649. /* make this a config parameter */
  650. dib7000p_set_output_mode(state, OUTMODE_MPEG2_FIFO);
  651. return dib7000p_tune(fe, &ch);
  652. }
  653. static int dib7000p_read_status(struct dvb_frontend *fe, fe_status_t *stat)
  654. {
  655. struct dib7000p_state *state = fe->demodulator_priv;
  656. u16 lock = dib7000p_read_word(state, 509);
  657. *stat = 0;
  658. if (lock & 0x8000)
  659. *stat |= FE_HAS_SIGNAL;
  660. if (lock & 0x3000)
  661. *stat |= FE_HAS_CARRIER;
  662. if (lock & 0x0100)
  663. *stat |= FE_HAS_VITERBI;
  664. if (lock & 0x0010)
  665. *stat |= FE_HAS_SYNC;
  666. if (lock & 0x0008)
  667. *stat |= FE_HAS_LOCK;
  668. return 0;
  669. }
  670. static int dib7000p_read_ber(struct dvb_frontend *fe, u32 *ber)
  671. {
  672. struct dib7000p_state *state = fe->demodulator_priv;
  673. *ber = (dib7000p_read_word(state, 500) << 16) | dib7000p_read_word(state, 501);
  674. return 0;
  675. }
  676. static int dib7000p_read_unc_blocks(struct dvb_frontend *fe, u32 *unc)
  677. {
  678. struct dib7000p_state *state = fe->demodulator_priv;
  679. *unc = dib7000p_read_word(state, 506);
  680. return 0;
  681. }
  682. static int dib7000p_read_signal_strength(struct dvb_frontend *fe, u16 *strength)
  683. {
  684. struct dib7000p_state *state = fe->demodulator_priv;
  685. u16 val = dib7000p_read_word(state, 394);
  686. *strength = 65535 - val;
  687. return 0;
  688. }
  689. static int dib7000p_read_snr(struct dvb_frontend* fe, u16 *snr)
  690. {
  691. *snr = 0x0000;
  692. return 0;
  693. }
  694. static int dib7000p_fe_get_tune_settings(struct dvb_frontend* fe, struct dvb_frontend_tune_settings *tune)
  695. {
  696. tune->min_delay_ms = 1000;
  697. return 0;
  698. }
  699. static void dib7000p_release(struct dvb_frontend *demod)
  700. {
  701. struct dib7000p_state *st = demod->demodulator_priv;
  702. dibx000_exit_i2c_master(&st->i2c_master);
  703. kfree(st);
  704. }
  705. int dib7000pc_detection(struct i2c_adapter *i2c_adap)
  706. {
  707. u8 tx[2], rx[2];
  708. struct i2c_msg msg[2] = {
  709. { .addr = 18 >> 1, .flags = 0, .buf = tx, .len = 2 },
  710. { .addr = 18 >> 1, .flags = I2C_M_RD, .buf = rx, .len = 2 },
  711. };
  712. tx[0] = 0x03;
  713. tx[1] = 0x00;
  714. if (i2c_transfer(i2c_adap, msg, 2) == 2)
  715. if (rx[0] == 0x01 && rx[1] == 0xb3) {
  716. dprintk("-D- DiB7000PC detected\n");
  717. return 1;
  718. }
  719. msg[0].addr = msg[1].addr = 0x40;
  720. if (i2c_transfer(i2c_adap, msg, 2) == 2)
  721. if (rx[0] == 0x01 && rx[1] == 0xb3) {
  722. dprintk("-D- DiB7000PC detected\n");
  723. return 1;
  724. }
  725. dprintk("-D- DiB7000PC not detected\n");
  726. return 0;
  727. }
  728. EXPORT_SYMBOL(dib7000pc_detection);
  729. struct i2c_adapter * dib7000p_get_i2c_master(struct dvb_frontend *demod, enum dibx000_i2c_interface intf, int gating)
  730. {
  731. struct dib7000p_state *st = demod->demodulator_priv;
  732. return dibx000_get_i2c_adapter(&st->i2c_master, intf, gating);
  733. }
  734. EXPORT_SYMBOL(dib7000p_get_i2c_master);
  735. int dib7000p_i2c_enumeration(struct i2c_adapter *i2c, int no_of_demods, u8 default_addr, struct dib7000p_config cfg[])
  736. {
  737. struct dib7000p_state st = { .i2c_adap = i2c };
  738. int k = 0;
  739. u8 new_addr = 0;
  740. for (k = no_of_demods-1; k >= 0; k--) {
  741. st.cfg = cfg[k];
  742. /* designated i2c address */
  743. new_addr = (0x40 + k) << 1;
  744. st.i2c_addr = new_addr;
  745. if (dib7000p_identify(&st) != 0) {
  746. st.i2c_addr = default_addr;
  747. if (dib7000p_identify(&st) != 0) {
  748. dprintk("DiB7000P #%d: not identified\n", k);
  749. return -EIO;
  750. }
  751. }
  752. /* start diversity to pull_down div_str - just for i2c-enumeration */
  753. dib7000p_set_output_mode(&st, OUTMODE_DIVERSITY);
  754. /* set new i2c address and force divstart */
  755. dib7000p_write_word(&st, 1285, (new_addr << 2) | 0x2);
  756. dprintk("IC %d initialized (to i2c_address 0x%x)\n", k, new_addr);
  757. }
  758. for (k = 0; k < no_of_demods; k++) {
  759. st.cfg = cfg[k];
  760. st.i2c_addr = (0x40 + k) << 1;
  761. // unforce divstr
  762. dib7000p_write_word(&st, 1285, st.i2c_addr << 2);
  763. /* deactivate div - it was just for i2c-enumeration */
  764. dib7000p_set_output_mode(&st, OUTMODE_HIGH_Z);
  765. }
  766. return 0;
  767. }
  768. EXPORT_SYMBOL(dib7000p_i2c_enumeration);
  769. static struct dvb_frontend_ops dib7000p_ops;
  770. struct dvb_frontend * dib7000p_attach(struct i2c_adapter *i2c_adap, u8 i2c_addr, struct dib7000p_config *cfg)
  771. {
  772. struct dvb_frontend *demod;
  773. struct dib7000p_state *st;
  774. st = kzalloc(sizeof(struct dib7000p_state), GFP_KERNEL);
  775. if (st == NULL)
  776. return NULL;
  777. memcpy(&st->cfg, cfg, sizeof(struct dib7000p_config));
  778. st->i2c_adap = i2c_adap;
  779. st->i2c_addr = i2c_addr;
  780. st->gpio_val = cfg->gpio_val;
  781. st->gpio_dir = cfg->gpio_dir;
  782. demod = &st->demod;
  783. demod->demodulator_priv = st;
  784. memcpy(&st->demod.ops, &dib7000p_ops, sizeof(struct dvb_frontend_ops));
  785. if (dib7000p_identify(st) != 0)
  786. goto error;
  787. dibx000_init_i2c_master(&st->i2c_master, DIB7000P, st->i2c_adap, st->i2c_addr);
  788. dib7000p_demod_reset(st);
  789. return demod;
  790. error:
  791. kfree(st);
  792. return NULL;
  793. }
  794. EXPORT_SYMBOL(dib7000p_attach);
  795. static struct dvb_frontend_ops dib7000p_ops = {
  796. .info = {
  797. .name = "DiBcom 7000PC",
  798. .type = FE_OFDM,
  799. .frequency_min = 44250000,
  800. .frequency_max = 867250000,
  801. .frequency_stepsize = 62500,
  802. .caps = FE_CAN_INVERSION_AUTO |
  803. FE_CAN_FEC_1_2 | FE_CAN_FEC_2_3 | FE_CAN_FEC_3_4 |
  804. FE_CAN_FEC_5_6 | FE_CAN_FEC_7_8 | FE_CAN_FEC_AUTO |
  805. FE_CAN_QPSK | FE_CAN_QAM_16 | FE_CAN_QAM_64 | FE_CAN_QAM_AUTO |
  806. FE_CAN_TRANSMISSION_MODE_AUTO |
  807. FE_CAN_GUARD_INTERVAL_AUTO |
  808. FE_CAN_RECOVER |
  809. FE_CAN_HIERARCHY_AUTO,
  810. },
  811. .release = dib7000p_release,
  812. .init = dib7000p_init,
  813. .sleep = dib7000p_sleep,
  814. .set_frontend = dib7000p_set_frontend,
  815. .get_tune_settings = dib7000p_fe_get_tune_settings,
  816. .get_frontend = dib7000p_get_frontend,
  817. .read_status = dib7000p_read_status,
  818. .read_ber = dib7000p_read_ber,
  819. .read_signal_strength = dib7000p_read_signal_strength,
  820. .read_snr = dib7000p_read_snr,
  821. .read_ucblocks = dib7000p_read_unc_blocks,
  822. };
  823. MODULE_AUTHOR("Patrick Boettcher <pboettcher@dibcom.fr>");
  824. MODULE_DESCRIPTION("Driver for the DiBcom 7000PC COFDM demodulator");
  825. MODULE_LICENSE("GPL");