dib7000p.c 30 KB

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