dib3000mc.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935
  1. /*
  2. * Driver for DiBcom DiB3000MC/P-demodulator.
  3. *
  4. * Copyright (C) 2004-7 DiBcom (http://www.dibcom.fr/)
  5. * Copyright (C) 2004-5 Patrick Boettcher (patrick.boettcher@desy.de)
  6. *
  7. * This code is partially based on the previous dib3000mc.c .
  8. *
  9. * This program is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU General Public License as
  11. * published by the Free Software Foundation, version 2.
  12. */
  13. #include <linux/kernel.h>
  14. #include <linux/slab.h>
  15. #include <linux/i2c.h>
  16. #include "dvb_frontend.h"
  17. #include "dib3000mc.h"
  18. static int debug;
  19. module_param(debug, int, 0644);
  20. MODULE_PARM_DESC(debug, "turn on debugging (default: 0)");
  21. static int buggy_sfn_workaround;
  22. module_param(buggy_sfn_workaround, int, 0644);
  23. MODULE_PARM_DESC(buggy_sfn_workaround, "Enable work-around for buggy SFNs (default: 0)");
  24. #define dprintk(args...) do { if (debug) { printk(KERN_DEBUG "DiB3000MC/P:"); printk(args); printk("\n"); } } while (0)
  25. struct dib3000mc_state {
  26. struct dvb_frontend demod;
  27. struct dib3000mc_config *cfg;
  28. u8 i2c_addr;
  29. struct i2c_adapter *i2c_adap;
  30. struct dibx000_i2c_master i2c_master;
  31. u32 timf;
  32. fe_bandwidth_t current_bandwidth;
  33. u16 dev_id;
  34. u8 sfn_workaround_active :1;
  35. };
  36. static u16 dib3000mc_read_word(struct dib3000mc_state *state, u16 reg)
  37. {
  38. u8 wb[2] = { (reg >> 8) | 0x80, reg & 0xff };
  39. u8 rb[2];
  40. struct i2c_msg msg[2] = {
  41. { .addr = state->i2c_addr >> 1, .flags = 0, .buf = wb, .len = 2 },
  42. { .addr = state->i2c_addr >> 1, .flags = I2C_M_RD, .buf = rb, .len = 2 },
  43. };
  44. if (i2c_transfer(state->i2c_adap, msg, 2) != 2)
  45. dprintk("i2c read error on %d\n",reg);
  46. return (rb[0] << 8) | rb[1];
  47. }
  48. static int dib3000mc_write_word(struct dib3000mc_state *state, u16 reg, u16 val)
  49. {
  50. u8 b[4] = {
  51. (reg >> 8) & 0xff, reg & 0xff,
  52. (val >> 8) & 0xff, val & 0xff,
  53. };
  54. struct i2c_msg msg = {
  55. .addr = state->i2c_addr >> 1, .flags = 0, .buf = b, .len = 4
  56. };
  57. return i2c_transfer(state->i2c_adap, &msg, 1) != 1 ? -EREMOTEIO : 0;
  58. }
  59. static int dib3000mc_identify(struct dib3000mc_state *state)
  60. {
  61. u16 value;
  62. if ((value = dib3000mc_read_word(state, 1025)) != 0x01b3) {
  63. dprintk("-E- DiB3000MC/P: wrong Vendor ID (read=0x%x)\n",value);
  64. return -EREMOTEIO;
  65. }
  66. value = dib3000mc_read_word(state, 1026);
  67. if (value != 0x3001 && value != 0x3002) {
  68. dprintk("-E- DiB3000MC/P: wrong Device ID (%x)\n",value);
  69. return -EREMOTEIO;
  70. }
  71. state->dev_id = value;
  72. dprintk("-I- found DiB3000MC/P: %x\n",state->dev_id);
  73. return 0;
  74. }
  75. static int dib3000mc_set_timing(struct dib3000mc_state *state, s16 nfft, u32 bw, u8 update_offset)
  76. {
  77. u32 timf;
  78. if (state->timf == 0) {
  79. timf = 1384402; // default value for 8MHz
  80. if (update_offset)
  81. msleep(200); // first time we do an update
  82. } else
  83. timf = state->timf;
  84. timf *= (bw / 1000);
  85. if (update_offset) {
  86. s16 tim_offs = dib3000mc_read_word(state, 416);
  87. if (tim_offs & 0x2000)
  88. tim_offs -= 0x4000;
  89. if (nfft == TRANSMISSION_MODE_2K)
  90. tim_offs *= 4;
  91. timf += tim_offs;
  92. state->timf = timf / (bw / 1000);
  93. }
  94. dprintk("timf: %d\n", timf);
  95. dib3000mc_write_word(state, 23, (u16) (timf >> 16));
  96. dib3000mc_write_word(state, 24, (u16) (timf ) & 0xffff);
  97. return 0;
  98. }
  99. static int dib3000mc_setup_pwm_state(struct dib3000mc_state *state)
  100. {
  101. u16 reg_51, reg_52 = state->cfg->agc->setup & 0xfefb;
  102. if (state->cfg->pwm3_inversion) {
  103. reg_51 = (2 << 14) | (0 << 10) | (7 << 6) | (2 << 2) | (2 << 0);
  104. reg_52 |= (1 << 2);
  105. } else {
  106. reg_51 = (2 << 14) | (4 << 10) | (7 << 6) | (2 << 2) | (2 << 0);
  107. reg_52 |= (1 << 8);
  108. }
  109. dib3000mc_write_word(state, 51, reg_51);
  110. dib3000mc_write_word(state, 52, reg_52);
  111. if (state->cfg->use_pwm3)
  112. dib3000mc_write_word(state, 245, (1 << 3) | (1 << 0));
  113. else
  114. dib3000mc_write_word(state, 245, 0);
  115. dib3000mc_write_word(state, 1040, 0x3);
  116. return 0;
  117. }
  118. static int dib3000mc_set_output_mode(struct dib3000mc_state *state, int mode)
  119. {
  120. int ret = 0;
  121. u16 fifo_threshold = 1792;
  122. u16 outreg = 0;
  123. u16 outmode = 0;
  124. u16 elecout = 1;
  125. u16 smo_reg = dib3000mc_read_word(state, 206) & 0x0010; /* keep the pid_parse bit */
  126. dprintk("-I- Setting output mode for demod %p to %d\n",
  127. &state->demod, mode);
  128. switch (mode) {
  129. case OUTMODE_HIGH_Z: // disable
  130. elecout = 0;
  131. break;
  132. case OUTMODE_MPEG2_PAR_GATED_CLK: // STBs with parallel gated clock
  133. outmode = 0;
  134. break;
  135. case OUTMODE_MPEG2_PAR_CONT_CLK: // STBs with parallel continues clock
  136. outmode = 1;
  137. break;
  138. case OUTMODE_MPEG2_SERIAL: // STBs with serial input
  139. outmode = 2;
  140. break;
  141. case OUTMODE_MPEG2_FIFO: // e.g. USB feeding
  142. elecout = 3;
  143. /*ADDR @ 206 :
  144. P_smo_error_discard [1;6:6] = 0
  145. P_smo_rs_discard [1;5:5] = 0
  146. P_smo_pid_parse [1;4:4] = 0
  147. P_smo_fifo_flush [1;3:3] = 0
  148. P_smo_mode [2;2:1] = 11
  149. P_smo_ovf_prot [1;0:0] = 0
  150. */
  151. smo_reg |= 3 << 1;
  152. fifo_threshold = 512;
  153. outmode = 5;
  154. break;
  155. case OUTMODE_DIVERSITY:
  156. outmode = 4;
  157. elecout = 1;
  158. break;
  159. default:
  160. dprintk("Unhandled output_mode passed to be set for demod %p\n",&state->demod);
  161. outmode = 0;
  162. break;
  163. }
  164. if ((state->cfg->output_mpeg2_in_188_bytes))
  165. smo_reg |= (1 << 5); // P_smo_rs_discard [1;5:5] = 1
  166. outreg = dib3000mc_read_word(state, 244) & 0x07FF;
  167. outreg |= (outmode << 11);
  168. ret |= dib3000mc_write_word(state, 244, outreg);
  169. ret |= dib3000mc_write_word(state, 206, smo_reg); /*smo_ mode*/
  170. ret |= dib3000mc_write_word(state, 207, fifo_threshold); /* synchronous fread */
  171. ret |= dib3000mc_write_word(state, 1040, elecout); /* P_out_cfg */
  172. return ret;
  173. }
  174. static int dib3000mc_set_bandwidth(struct dib3000mc_state *state, u32 bw)
  175. {
  176. u16 bw_cfg[6] = { 0 };
  177. u16 imp_bw_cfg[3] = { 0 };
  178. u16 reg;
  179. /* settings here are for 27.7MHz */
  180. switch (bw) {
  181. case 8000:
  182. bw_cfg[0] = 0x0019; bw_cfg[1] = 0x5c30; bw_cfg[2] = 0x0054; bw_cfg[3] = 0x88a0; bw_cfg[4] = 0x01a6; bw_cfg[5] = 0xab20;
  183. imp_bw_cfg[0] = 0x04db; imp_bw_cfg[1] = 0x00db; imp_bw_cfg[2] = 0x00b7;
  184. break;
  185. case 7000:
  186. bw_cfg[0] = 0x001c; bw_cfg[1] = 0xfba5; bw_cfg[2] = 0x0060; bw_cfg[3] = 0x9c25; bw_cfg[4] = 0x01e3; bw_cfg[5] = 0x0cb7;
  187. imp_bw_cfg[0] = 0x04c0; imp_bw_cfg[1] = 0x00c0; imp_bw_cfg[2] = 0x00a0;
  188. break;
  189. case 6000:
  190. bw_cfg[0] = 0x0021; bw_cfg[1] = 0xd040; bw_cfg[2] = 0x0070; bw_cfg[3] = 0xb62b; bw_cfg[4] = 0x0233; bw_cfg[5] = 0x8ed5;
  191. imp_bw_cfg[0] = 0x04a5; imp_bw_cfg[1] = 0x00a5; imp_bw_cfg[2] = 0x0089;
  192. break;
  193. case 5000:
  194. bw_cfg[0] = 0x0028; bw_cfg[1] = 0x9380; bw_cfg[2] = 0x0087; bw_cfg[3] = 0x4100; bw_cfg[4] = 0x02a4; bw_cfg[5] = 0x4500;
  195. imp_bw_cfg[0] = 0x0489; imp_bw_cfg[1] = 0x0089; imp_bw_cfg[2] = 0x0072;
  196. break;
  197. default: return -EINVAL;
  198. }
  199. for (reg = 6; reg < 12; reg++)
  200. dib3000mc_write_word(state, reg, bw_cfg[reg - 6]);
  201. dib3000mc_write_word(state, 12, 0x0000);
  202. dib3000mc_write_word(state, 13, 0x03e8);
  203. dib3000mc_write_word(state, 14, 0x0000);
  204. dib3000mc_write_word(state, 15, 0x03f2);
  205. dib3000mc_write_word(state, 16, 0x0001);
  206. dib3000mc_write_word(state, 17, 0xb0d0);
  207. // P_sec_len
  208. dib3000mc_write_word(state, 18, 0x0393);
  209. dib3000mc_write_word(state, 19, 0x8700);
  210. for (reg = 55; reg < 58; reg++)
  211. dib3000mc_write_word(state, reg, imp_bw_cfg[reg - 55]);
  212. // Timing configuration
  213. dib3000mc_set_timing(state, TRANSMISSION_MODE_2K, bw, 0);
  214. return 0;
  215. }
  216. static u16 impulse_noise_val[29] =
  217. {
  218. 0x38, 0x6d9, 0x3f28, 0x7a7, 0x3a74, 0x196, 0x32a, 0x48c, 0x3ffe, 0x7f3,
  219. 0x2d94, 0x76, 0x53d, 0x3ff8, 0x7e3, 0x3320, 0x76, 0x5b3, 0x3feb, 0x7d2,
  220. 0x365e, 0x76, 0x48c, 0x3ffe, 0x5b3, 0x3feb, 0x76, 0x0000, 0xd
  221. };
  222. static void dib3000mc_set_impulse_noise(struct dib3000mc_state *state, u8 mode, s16 nfft)
  223. {
  224. u16 i;
  225. for (i = 58; i < 87; i++)
  226. dib3000mc_write_word(state, i, impulse_noise_val[i-58]);
  227. if (nfft == TRANSMISSION_MODE_8K) {
  228. dib3000mc_write_word(state, 58, 0x3b);
  229. dib3000mc_write_word(state, 84, 0x00);
  230. dib3000mc_write_word(state, 85, 0x8200);
  231. }
  232. dib3000mc_write_word(state, 34, 0x1294);
  233. dib3000mc_write_word(state, 35, 0x1ff8);
  234. if (mode == 1)
  235. dib3000mc_write_word(state, 55, dib3000mc_read_word(state, 55) | (1 << 10));
  236. }
  237. static int dib3000mc_init(struct dvb_frontend *demod)
  238. {
  239. struct dib3000mc_state *state = demod->demodulator_priv;
  240. struct dibx000_agc_config *agc = state->cfg->agc;
  241. // Restart Configuration
  242. dib3000mc_write_word(state, 1027, 0x8000);
  243. dib3000mc_write_word(state, 1027, 0x0000);
  244. // power up the demod + mobility configuration
  245. dib3000mc_write_word(state, 140, 0x0000);
  246. dib3000mc_write_word(state, 1031, 0);
  247. if (state->cfg->mobile_mode) {
  248. dib3000mc_write_word(state, 139, 0x0000);
  249. dib3000mc_write_word(state, 141, 0x0000);
  250. dib3000mc_write_word(state, 175, 0x0002);
  251. dib3000mc_write_word(state, 1032, 0x0000);
  252. } else {
  253. dib3000mc_write_word(state, 139, 0x0001);
  254. dib3000mc_write_word(state, 141, 0x0000);
  255. dib3000mc_write_word(state, 175, 0x0000);
  256. dib3000mc_write_word(state, 1032, 0x012C);
  257. }
  258. dib3000mc_write_word(state, 1033, 0x0000);
  259. // P_clk_cfg
  260. dib3000mc_write_word(state, 1037, 0x3130);
  261. // other configurations
  262. // P_ctrl_sfreq
  263. dib3000mc_write_word(state, 33, (5 << 0));
  264. dib3000mc_write_word(state, 88, (1 << 10) | (0x10 << 0));
  265. // Phase noise control
  266. // P_fft_phacor_inh, P_fft_phacor_cpe, P_fft_powrange
  267. dib3000mc_write_word(state, 99, (1 << 9) | (0x20 << 0));
  268. if (state->cfg->phase_noise_mode == 0)
  269. dib3000mc_write_word(state, 111, 0x00);
  270. else
  271. dib3000mc_write_word(state, 111, 0x02);
  272. // P_agc_global
  273. dib3000mc_write_word(state, 50, 0x8000);
  274. // agc setup misc
  275. dib3000mc_setup_pwm_state(state);
  276. // P_agc_counter_lock
  277. dib3000mc_write_word(state, 53, 0x87);
  278. // P_agc_counter_unlock
  279. dib3000mc_write_word(state, 54, 0x87);
  280. /* agc */
  281. dib3000mc_write_word(state, 36, state->cfg->max_time);
  282. dib3000mc_write_word(state, 37, (state->cfg->agc_command1 << 13) | (state->cfg->agc_command2 << 12) | (0x1d << 0));
  283. dib3000mc_write_word(state, 38, state->cfg->pwm3_value);
  284. dib3000mc_write_word(state, 39, state->cfg->ln_adc_level);
  285. // set_agc_loop_Bw
  286. dib3000mc_write_word(state, 40, 0x0179);
  287. dib3000mc_write_word(state, 41, 0x03f0);
  288. dib3000mc_write_word(state, 42, agc->agc1_max);
  289. dib3000mc_write_word(state, 43, agc->agc1_min);
  290. dib3000mc_write_word(state, 44, agc->agc2_max);
  291. dib3000mc_write_word(state, 45, agc->agc2_min);
  292. dib3000mc_write_word(state, 46, (agc->agc1_pt1 << 8) | agc->agc1_pt2);
  293. dib3000mc_write_word(state, 47, (agc->agc1_slope1 << 8) | agc->agc1_slope2);
  294. dib3000mc_write_word(state, 48, (agc->agc2_pt1 << 8) | agc->agc2_pt2);
  295. dib3000mc_write_word(state, 49, (agc->agc2_slope1 << 8) | agc->agc2_slope2);
  296. // Begin: TimeOut registers
  297. // P_pha3_thres
  298. dib3000mc_write_word(state, 110, 3277);
  299. // P_timf_alpha = 6, P_corm_alpha = 6, P_corm_thres = 0x80
  300. dib3000mc_write_word(state, 26, 0x6680);
  301. // lock_mask0
  302. dib3000mc_write_word(state, 1, 4);
  303. // lock_mask1
  304. dib3000mc_write_word(state, 2, 4);
  305. // lock_mask2
  306. dib3000mc_write_word(state, 3, 0x1000);
  307. // P_search_maxtrial=1
  308. dib3000mc_write_word(state, 5, 1);
  309. dib3000mc_set_bandwidth(state, 8000);
  310. // div_lock_mask
  311. dib3000mc_write_word(state, 4, 0x814);
  312. dib3000mc_write_word(state, 21, (1 << 9) | 0x164);
  313. dib3000mc_write_word(state, 22, 0x463d);
  314. // Spurious rm cfg
  315. // P_cspu_regul, P_cspu_win_cut
  316. dib3000mc_write_word(state, 120, 0x200f);
  317. // P_adp_selec_monit
  318. dib3000mc_write_word(state, 134, 0);
  319. // Fec cfg
  320. dib3000mc_write_word(state, 195, 0x10);
  321. // diversity register: P_dvsy_sync_wait..
  322. dib3000mc_write_word(state, 180, 0x2FF0);
  323. // Impulse noise configuration
  324. dib3000mc_set_impulse_noise(state, 0, TRANSMISSION_MODE_8K);
  325. // output mode set-up
  326. dib3000mc_set_output_mode(state, OUTMODE_HIGH_Z);
  327. /* close the i2c-gate */
  328. dib3000mc_write_word(state, 769, (1 << 7) );
  329. return 0;
  330. }
  331. static int dib3000mc_sleep(struct dvb_frontend *demod)
  332. {
  333. struct dib3000mc_state *state = demod->demodulator_priv;
  334. dib3000mc_write_word(state, 1031, 0xFFFF);
  335. dib3000mc_write_word(state, 1032, 0xFFFF);
  336. dib3000mc_write_word(state, 1033, 0xFFF0);
  337. return 0;
  338. }
  339. static void dib3000mc_set_adp_cfg(struct dib3000mc_state *state, s16 qam)
  340. {
  341. u16 cfg[4] = { 0 },reg;
  342. switch (qam) {
  343. case QPSK:
  344. cfg[0] = 0x099a; cfg[1] = 0x7fae; cfg[2] = 0x0333; cfg[3] = 0x7ff0;
  345. break;
  346. case QAM_16:
  347. cfg[0] = 0x023d; cfg[1] = 0x7fdf; cfg[2] = 0x00a4; cfg[3] = 0x7ff0;
  348. break;
  349. case QAM_64:
  350. cfg[0] = 0x0148; cfg[1] = 0x7ff0; cfg[2] = 0x00a4; cfg[3] = 0x7ff8;
  351. break;
  352. }
  353. for (reg = 129; reg < 133; reg++)
  354. dib3000mc_write_word(state, reg, cfg[reg - 129]);
  355. }
  356. static void dib3000mc_set_channel_cfg(struct dib3000mc_state *state, struct dvb_frontend_parameters *ch, u16 seq)
  357. {
  358. u16 value;
  359. dib3000mc_set_bandwidth(state, BANDWIDTH_TO_KHZ(ch->u.ofdm.bandwidth));
  360. dib3000mc_set_timing(state, ch->u.ofdm.transmission_mode, BANDWIDTH_TO_KHZ(ch->u.ofdm.bandwidth), 0);
  361. // if (boost)
  362. // dib3000mc_write_word(state, 100, (11 << 6) + 6);
  363. // else
  364. dib3000mc_write_word(state, 100, (16 << 6) + 9);
  365. dib3000mc_write_word(state, 1027, 0x0800);
  366. dib3000mc_write_word(state, 1027, 0x0000);
  367. //Default cfg isi offset adp
  368. dib3000mc_write_word(state, 26, 0x6680);
  369. dib3000mc_write_word(state, 29, 0x1273);
  370. dib3000mc_write_word(state, 33, 5);
  371. dib3000mc_set_adp_cfg(state, QAM_16);
  372. dib3000mc_write_word(state, 133, 15564);
  373. dib3000mc_write_word(state, 12 , 0x0);
  374. dib3000mc_write_word(state, 13 , 0x3e8);
  375. dib3000mc_write_word(state, 14 , 0x0);
  376. dib3000mc_write_word(state, 15 , 0x3f2);
  377. dib3000mc_write_word(state, 93,0);
  378. dib3000mc_write_word(state, 94,0);
  379. dib3000mc_write_word(state, 95,0);
  380. dib3000mc_write_word(state, 96,0);
  381. dib3000mc_write_word(state, 97,0);
  382. dib3000mc_write_word(state, 98,0);
  383. dib3000mc_set_impulse_noise(state, 0, ch->u.ofdm.transmission_mode);
  384. value = 0;
  385. switch (ch->u.ofdm.transmission_mode) {
  386. case TRANSMISSION_MODE_2K: value |= (0 << 7); break;
  387. default:
  388. case TRANSMISSION_MODE_8K: value |= (1 << 7); break;
  389. }
  390. switch (ch->u.ofdm.guard_interval) {
  391. case GUARD_INTERVAL_1_32: value |= (0 << 5); break;
  392. case GUARD_INTERVAL_1_16: value |= (1 << 5); break;
  393. case GUARD_INTERVAL_1_4: value |= (3 << 5); break;
  394. default:
  395. case GUARD_INTERVAL_1_8: value |= (2 << 5); break;
  396. }
  397. switch (ch->u.ofdm.constellation) {
  398. case QPSK: value |= (0 << 3); break;
  399. case QAM_16: value |= (1 << 3); break;
  400. default:
  401. case QAM_64: value |= (2 << 3); break;
  402. }
  403. switch (HIERARCHY_1) {
  404. case HIERARCHY_2: value |= 2; break;
  405. case HIERARCHY_4: value |= 4; break;
  406. default:
  407. case HIERARCHY_1: value |= 1; break;
  408. }
  409. dib3000mc_write_word(state, 0, value);
  410. dib3000mc_write_word(state, 5, (1 << 8) | ((seq & 0xf) << 4));
  411. value = 0;
  412. if (ch->u.ofdm.hierarchy_information == 1)
  413. value |= (1 << 4);
  414. if (1 == 1)
  415. value |= 1;
  416. switch ((ch->u.ofdm.hierarchy_information == 0 || 1 == 1) ? ch->u.ofdm.code_rate_HP : ch->u.ofdm.code_rate_LP) {
  417. case FEC_2_3: value |= (2 << 1); break;
  418. case FEC_3_4: value |= (3 << 1); break;
  419. case FEC_5_6: value |= (5 << 1); break;
  420. case FEC_7_8: value |= (7 << 1); break;
  421. default:
  422. case FEC_1_2: value |= (1 << 1); break;
  423. }
  424. dib3000mc_write_word(state, 181, value);
  425. // diversity synchro delay add 50% SFN margin
  426. switch (ch->u.ofdm.transmission_mode) {
  427. case TRANSMISSION_MODE_8K: value = 256; break;
  428. case TRANSMISSION_MODE_2K:
  429. default: value = 64; break;
  430. }
  431. switch (ch->u.ofdm.guard_interval) {
  432. case GUARD_INTERVAL_1_16: value *= 2; break;
  433. case GUARD_INTERVAL_1_8: value *= 4; break;
  434. case GUARD_INTERVAL_1_4: value *= 8; break;
  435. default:
  436. case GUARD_INTERVAL_1_32: value *= 1; break;
  437. }
  438. value <<= 4;
  439. value |= dib3000mc_read_word(state, 180) & 0x000f;
  440. dib3000mc_write_word(state, 180, value);
  441. // restart demod
  442. value = dib3000mc_read_word(state, 0);
  443. dib3000mc_write_word(state, 0, value | (1 << 9));
  444. dib3000mc_write_word(state, 0, value);
  445. msleep(30);
  446. dib3000mc_set_impulse_noise(state, state->cfg->impulse_noise_mode, ch->u.ofdm.transmission_mode);
  447. }
  448. static int dib3000mc_autosearch_start(struct dvb_frontend *demod, struct dvb_frontend_parameters *chan)
  449. {
  450. struct dib3000mc_state *state = demod->demodulator_priv;
  451. u16 reg;
  452. // u32 val;
  453. struct dvb_frontend_parameters schan;
  454. schan = *chan;
  455. /* TODO what is that ? */
  456. /* a channel for autosearch */
  457. schan.u.ofdm.transmission_mode = TRANSMISSION_MODE_8K;
  458. schan.u.ofdm.guard_interval = GUARD_INTERVAL_1_32;
  459. schan.u.ofdm.constellation = QAM_64;
  460. schan.u.ofdm.code_rate_HP = FEC_2_3;
  461. schan.u.ofdm.code_rate_LP = FEC_2_3;
  462. schan.u.ofdm.hierarchy_information = 0;
  463. dib3000mc_set_channel_cfg(state, &schan, 11);
  464. reg = dib3000mc_read_word(state, 0);
  465. dib3000mc_write_word(state, 0, reg | (1 << 8));
  466. dib3000mc_read_word(state, 511);
  467. dib3000mc_write_word(state, 0, reg);
  468. return 0;
  469. }
  470. static int dib3000mc_autosearch_is_irq(struct dvb_frontend *demod)
  471. {
  472. struct dib3000mc_state *state = demod->demodulator_priv;
  473. u16 irq_pending = dib3000mc_read_word(state, 511);
  474. if (irq_pending & 0x1) // failed
  475. return 1;
  476. if (irq_pending & 0x2) // succeeded
  477. return 2;
  478. return 0; // still pending
  479. }
  480. static int dib3000mc_tune(struct dvb_frontend *demod, struct dvb_frontend_parameters *ch)
  481. {
  482. struct dib3000mc_state *state = demod->demodulator_priv;
  483. // ** configure demod **
  484. dib3000mc_set_channel_cfg(state, ch, 0);
  485. // activates isi
  486. if (state->sfn_workaround_active) {
  487. dprintk("SFN workaround is active\n");
  488. dib3000mc_write_word(state, 29, 0x1273);
  489. dib3000mc_write_word(state, 108, 0x4000); // P_pha3_force_pha_shift
  490. } else {
  491. dib3000mc_write_word(state, 29, 0x1073);
  492. dib3000mc_write_word(state, 108, 0x0000); // P_pha3_force_pha_shift
  493. }
  494. dib3000mc_set_adp_cfg(state, (u8)ch->u.ofdm.constellation);
  495. if (ch->u.ofdm.transmission_mode == TRANSMISSION_MODE_8K) {
  496. dib3000mc_write_word(state, 26, 38528);
  497. dib3000mc_write_word(state, 33, 8);
  498. } else {
  499. dib3000mc_write_word(state, 26, 30336);
  500. dib3000mc_write_word(state, 33, 6);
  501. }
  502. if (dib3000mc_read_word(state, 509) & 0x80)
  503. dib3000mc_set_timing(state, ch->u.ofdm.transmission_mode, BANDWIDTH_TO_KHZ(ch->u.ofdm.bandwidth), 1);
  504. return 0;
  505. }
  506. struct i2c_adapter * dib3000mc_get_tuner_i2c_master(struct dvb_frontend *demod, int gating)
  507. {
  508. struct dib3000mc_state *st = demod->demodulator_priv;
  509. return dibx000_get_i2c_adapter(&st->i2c_master, DIBX000_I2C_INTERFACE_TUNER, gating);
  510. }
  511. EXPORT_SYMBOL(dib3000mc_get_tuner_i2c_master);
  512. static int dib3000mc_get_frontend(struct dvb_frontend* fe,
  513. struct dvb_frontend_parameters *fep)
  514. {
  515. struct dib3000mc_state *state = fe->demodulator_priv;
  516. u16 tps = dib3000mc_read_word(state,458);
  517. fep->inversion = INVERSION_AUTO;
  518. fep->u.ofdm.bandwidth = state->current_bandwidth;
  519. switch ((tps >> 8) & 0x1) {
  520. case 0: fep->u.ofdm.transmission_mode = TRANSMISSION_MODE_2K; break;
  521. case 1: fep->u.ofdm.transmission_mode = TRANSMISSION_MODE_8K; break;
  522. }
  523. switch (tps & 0x3) {
  524. case 0: fep->u.ofdm.guard_interval = GUARD_INTERVAL_1_32; break;
  525. case 1: fep->u.ofdm.guard_interval = GUARD_INTERVAL_1_16; break;
  526. case 2: fep->u.ofdm.guard_interval = GUARD_INTERVAL_1_8; break;
  527. case 3: fep->u.ofdm.guard_interval = GUARD_INTERVAL_1_4; break;
  528. }
  529. switch ((tps >> 13) & 0x3) {
  530. case 0: fep->u.ofdm.constellation = QPSK; break;
  531. case 1: fep->u.ofdm.constellation = QAM_16; break;
  532. case 2:
  533. default: fep->u.ofdm.constellation = QAM_64; break;
  534. }
  535. /* as long as the frontend_param structure is fixed for hierarchical transmission I refuse to use it */
  536. /* (tps >> 12) & 0x1 == hrch is used, (tps >> 9) & 0x7 == alpha */
  537. fep->u.ofdm.hierarchy_information = HIERARCHY_NONE;
  538. switch ((tps >> 5) & 0x7) {
  539. case 1: fep->u.ofdm.code_rate_HP = FEC_1_2; break;
  540. case 2: fep->u.ofdm.code_rate_HP = FEC_2_3; break;
  541. case 3: fep->u.ofdm.code_rate_HP = FEC_3_4; break;
  542. case 5: fep->u.ofdm.code_rate_HP = FEC_5_6; break;
  543. case 7:
  544. default: fep->u.ofdm.code_rate_HP = FEC_7_8; break;
  545. }
  546. switch ((tps >> 2) & 0x7) {
  547. case 1: fep->u.ofdm.code_rate_LP = FEC_1_2; break;
  548. case 2: fep->u.ofdm.code_rate_LP = FEC_2_3; break;
  549. case 3: fep->u.ofdm.code_rate_LP = FEC_3_4; break;
  550. case 5: fep->u.ofdm.code_rate_LP = FEC_5_6; break;
  551. case 7:
  552. default: fep->u.ofdm.code_rate_LP = FEC_7_8; break;
  553. }
  554. return 0;
  555. }
  556. static int dib3000mc_set_frontend(struct dvb_frontend* fe,
  557. struct dvb_frontend_parameters *fep)
  558. {
  559. struct dib3000mc_state *state = fe->demodulator_priv;
  560. int ret;
  561. dib3000mc_set_output_mode(state, OUTMODE_HIGH_Z);
  562. state->current_bandwidth = fep->u.ofdm.bandwidth;
  563. dib3000mc_set_bandwidth(state, BANDWIDTH_TO_KHZ(fep->u.ofdm.bandwidth));
  564. /* maybe the parameter has been changed */
  565. state->sfn_workaround_active = buggy_sfn_workaround;
  566. if (fe->ops.tuner_ops.set_params) {
  567. fe->ops.tuner_ops.set_params(fe, fep);
  568. msleep(100);
  569. }
  570. if (fep->u.ofdm.transmission_mode == TRANSMISSION_MODE_AUTO ||
  571. fep->u.ofdm.guard_interval == GUARD_INTERVAL_AUTO ||
  572. fep->u.ofdm.constellation == QAM_AUTO ||
  573. fep->u.ofdm.code_rate_HP == FEC_AUTO) {
  574. int i = 1000, found;
  575. dib3000mc_autosearch_start(fe, fep);
  576. do {
  577. msleep(1);
  578. found = dib3000mc_autosearch_is_irq(fe);
  579. } while (found == 0 && i--);
  580. dprintk("autosearch returns: %d\n",found);
  581. if (found == 0 || found == 1)
  582. return 0; // no channel found
  583. dib3000mc_get_frontend(fe, fep);
  584. }
  585. ret = dib3000mc_tune(fe, fep);
  586. /* make this a config parameter */
  587. dib3000mc_set_output_mode(state, OUTMODE_MPEG2_FIFO);
  588. return ret;
  589. }
  590. static int dib3000mc_read_status(struct dvb_frontend *fe, fe_status_t *stat)
  591. {
  592. struct dib3000mc_state *state = fe->demodulator_priv;
  593. u16 lock = dib3000mc_read_word(state, 509);
  594. *stat = 0;
  595. if (lock & 0x8000)
  596. *stat |= FE_HAS_SIGNAL;
  597. if (lock & 0x3000)
  598. *stat |= FE_HAS_CARRIER;
  599. if (lock & 0x0100)
  600. *stat |= FE_HAS_VITERBI;
  601. if (lock & 0x0010)
  602. *stat |= FE_HAS_SYNC;
  603. if (lock & 0x0008)
  604. *stat |= FE_HAS_LOCK;
  605. return 0;
  606. }
  607. static int dib3000mc_read_ber(struct dvb_frontend *fe, u32 *ber)
  608. {
  609. struct dib3000mc_state *state = fe->demodulator_priv;
  610. *ber = (dib3000mc_read_word(state, 500) << 16) | dib3000mc_read_word(state, 501);
  611. return 0;
  612. }
  613. static int dib3000mc_read_unc_blocks(struct dvb_frontend *fe, u32 *unc)
  614. {
  615. struct dib3000mc_state *state = fe->demodulator_priv;
  616. *unc = dib3000mc_read_word(state, 508);
  617. return 0;
  618. }
  619. static int dib3000mc_read_signal_strength(struct dvb_frontend *fe, u16 *strength)
  620. {
  621. struct dib3000mc_state *state = fe->demodulator_priv;
  622. u16 val = dib3000mc_read_word(state, 392);
  623. *strength = 65535 - val;
  624. return 0;
  625. }
  626. static int dib3000mc_read_snr(struct dvb_frontend* fe, u16 *snr)
  627. {
  628. *snr = 0x0000;
  629. return 0;
  630. }
  631. static int dib3000mc_fe_get_tune_settings(struct dvb_frontend* fe, struct dvb_frontend_tune_settings *tune)
  632. {
  633. tune->min_delay_ms = 1000;
  634. return 0;
  635. }
  636. static void dib3000mc_release(struct dvb_frontend *fe)
  637. {
  638. struct dib3000mc_state *state = fe->demodulator_priv;
  639. dibx000_exit_i2c_master(&state->i2c_master);
  640. kfree(state);
  641. }
  642. int dib3000mc_pid_control(struct dvb_frontend *fe, int index, int pid,int onoff)
  643. {
  644. struct dib3000mc_state *state = fe->demodulator_priv;
  645. dib3000mc_write_word(state, 212 + index, onoff ? (1 << 13) | pid : 0);
  646. return 0;
  647. }
  648. EXPORT_SYMBOL(dib3000mc_pid_control);
  649. int dib3000mc_pid_parse(struct dvb_frontend *fe, int onoff)
  650. {
  651. struct dib3000mc_state *state = fe->demodulator_priv;
  652. u16 tmp = dib3000mc_read_word(state, 206) & ~(1 << 4);
  653. tmp |= (onoff << 4);
  654. return dib3000mc_write_word(state, 206, tmp);
  655. }
  656. EXPORT_SYMBOL(dib3000mc_pid_parse);
  657. void dib3000mc_set_config(struct dvb_frontend *fe, struct dib3000mc_config *cfg)
  658. {
  659. struct dib3000mc_state *state = fe->demodulator_priv;
  660. state->cfg = cfg;
  661. }
  662. EXPORT_SYMBOL(dib3000mc_set_config);
  663. int dib3000mc_i2c_enumeration(struct i2c_adapter *i2c, int no_of_demods, u8 default_addr, struct dib3000mc_config cfg[])
  664. {
  665. struct dib3000mc_state *dmcst;
  666. int k;
  667. u8 new_addr;
  668. static u8 DIB3000MC_I2C_ADDRESS[] = {20,22,24,26};
  669. dmcst = kzalloc(sizeof(struct dib3000mc_state), GFP_KERNEL);
  670. if (dmcst == NULL)
  671. return -ENOMEM;
  672. dmcst->i2c_adap = i2c;
  673. for (k = no_of_demods-1; k >= 0; k--) {
  674. dmcst->cfg = &cfg[k];
  675. /* designated i2c address */
  676. new_addr = DIB3000MC_I2C_ADDRESS[k];
  677. dmcst->i2c_addr = new_addr;
  678. if (dib3000mc_identify(dmcst) != 0) {
  679. dmcst->i2c_addr = default_addr;
  680. if (dib3000mc_identify(dmcst) != 0) {
  681. dprintk("-E- DiB3000P/MC #%d: not identified\n", k);
  682. kfree(dmcst);
  683. return -ENODEV;
  684. }
  685. }
  686. dib3000mc_set_output_mode(dmcst, OUTMODE_MPEG2_PAR_CONT_CLK);
  687. // set new i2c address and force divstr (Bit 1) to value 0 (Bit 0)
  688. dib3000mc_write_word(dmcst, 1024, (new_addr << 3) | 0x1);
  689. dmcst->i2c_addr = new_addr;
  690. }
  691. for (k = 0; k < no_of_demods; k++) {
  692. dmcst->cfg = &cfg[k];
  693. dmcst->i2c_addr = DIB3000MC_I2C_ADDRESS[k];
  694. dib3000mc_write_word(dmcst, 1024, dmcst->i2c_addr << 3);
  695. /* turn off data output */
  696. dib3000mc_set_output_mode(dmcst, OUTMODE_HIGH_Z);
  697. }
  698. kfree(dmcst);
  699. return 0;
  700. }
  701. EXPORT_SYMBOL(dib3000mc_i2c_enumeration);
  702. static struct dvb_frontend_ops dib3000mc_ops;
  703. struct dvb_frontend * dib3000mc_attach(struct i2c_adapter *i2c_adap, u8 i2c_addr, struct dib3000mc_config *cfg)
  704. {
  705. struct dvb_frontend *demod;
  706. struct dib3000mc_state *st;
  707. st = kzalloc(sizeof(struct dib3000mc_state), GFP_KERNEL);
  708. if (st == NULL)
  709. return NULL;
  710. st->cfg = cfg;
  711. st->i2c_adap = i2c_adap;
  712. st->i2c_addr = i2c_addr;
  713. demod = &st->demod;
  714. demod->demodulator_priv = st;
  715. memcpy(&st->demod.ops, &dib3000mc_ops, sizeof(struct dvb_frontend_ops));
  716. if (dib3000mc_identify(st) != 0)
  717. goto error;
  718. dibx000_init_i2c_master(&st->i2c_master, DIB3000MC, st->i2c_adap, st->i2c_addr);
  719. dib3000mc_write_word(st, 1037, 0x3130);
  720. return demod;
  721. error:
  722. kfree(st);
  723. return NULL;
  724. }
  725. EXPORT_SYMBOL(dib3000mc_attach);
  726. static struct dvb_frontend_ops dib3000mc_ops = {
  727. .info = {
  728. .name = "DiBcom 3000MC/P",
  729. .type = FE_OFDM,
  730. .frequency_min = 44250000,
  731. .frequency_max = 867250000,
  732. .frequency_stepsize = 62500,
  733. .caps = FE_CAN_INVERSION_AUTO |
  734. FE_CAN_FEC_1_2 | FE_CAN_FEC_2_3 | FE_CAN_FEC_3_4 |
  735. FE_CAN_FEC_5_6 | FE_CAN_FEC_7_8 | FE_CAN_FEC_AUTO |
  736. FE_CAN_QPSK | FE_CAN_QAM_16 | FE_CAN_QAM_64 | FE_CAN_QAM_AUTO |
  737. FE_CAN_TRANSMISSION_MODE_AUTO |
  738. FE_CAN_GUARD_INTERVAL_AUTO |
  739. FE_CAN_RECOVER |
  740. FE_CAN_HIERARCHY_AUTO,
  741. },
  742. .release = dib3000mc_release,
  743. .init = dib3000mc_init,
  744. .sleep = dib3000mc_sleep,
  745. .set_frontend = dib3000mc_set_frontend,
  746. .get_tune_settings = dib3000mc_fe_get_tune_settings,
  747. .get_frontend = dib3000mc_get_frontend,
  748. .read_status = dib3000mc_read_status,
  749. .read_ber = dib3000mc_read_ber,
  750. .read_signal_strength = dib3000mc_read_signal_strength,
  751. .read_snr = dib3000mc_read_snr,
  752. .read_ucblocks = dib3000mc_read_unc_blocks,
  753. };
  754. MODULE_AUTHOR("Patrick Boettcher <pboettcher@dibcom.fr>");
  755. MODULE_DESCRIPTION("Driver for the DiBcom 3000MC/P COFDM demodulator");
  756. MODULE_LICENSE("GPL");