dib7000p.c 39 KB

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