dib7000p.c 40 KB

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