dib3000mc.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944
  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. u32 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,
  357. struct dtv_frontend_properties *ch, u16 seq)
  358. {
  359. u16 value;
  360. u32 bw = BANDWIDTH_TO_KHZ(ch->bandwidth_hz);
  361. dib3000mc_set_bandwidth(state, bw);
  362. dib3000mc_set_timing(state, ch->transmission_mode, bw, 0);
  363. #if 1
  364. dib3000mc_write_word(state, 100, (16 << 6) + 9);
  365. #else
  366. if (boost)
  367. dib3000mc_write_word(state, 100, (11 << 6) + 6);
  368. else
  369. dib3000mc_write_word(state, 100, (16 << 6) + 9);
  370. #endif
  371. dib3000mc_write_word(state, 1027, 0x0800);
  372. dib3000mc_write_word(state, 1027, 0x0000);
  373. //Default cfg isi offset adp
  374. dib3000mc_write_word(state, 26, 0x6680);
  375. dib3000mc_write_word(state, 29, 0x1273);
  376. dib3000mc_write_word(state, 33, 5);
  377. dib3000mc_set_adp_cfg(state, QAM_16);
  378. dib3000mc_write_word(state, 133, 15564);
  379. dib3000mc_write_word(state, 12 , 0x0);
  380. dib3000mc_write_word(state, 13 , 0x3e8);
  381. dib3000mc_write_word(state, 14 , 0x0);
  382. dib3000mc_write_word(state, 15 , 0x3f2);
  383. dib3000mc_write_word(state, 93,0);
  384. dib3000mc_write_word(state, 94,0);
  385. dib3000mc_write_word(state, 95,0);
  386. dib3000mc_write_word(state, 96,0);
  387. dib3000mc_write_word(state, 97,0);
  388. dib3000mc_write_word(state, 98,0);
  389. dib3000mc_set_impulse_noise(state, 0, ch->transmission_mode);
  390. value = 0;
  391. switch (ch->transmission_mode) {
  392. case TRANSMISSION_MODE_2K: value |= (0 << 7); break;
  393. default:
  394. case TRANSMISSION_MODE_8K: value |= (1 << 7); break;
  395. }
  396. switch (ch->guard_interval) {
  397. case GUARD_INTERVAL_1_32: value |= (0 << 5); break;
  398. case GUARD_INTERVAL_1_16: value |= (1 << 5); break;
  399. case GUARD_INTERVAL_1_4: value |= (3 << 5); break;
  400. default:
  401. case GUARD_INTERVAL_1_8: value |= (2 << 5); break;
  402. }
  403. switch (ch->modulation) {
  404. case QPSK: value |= (0 << 3); break;
  405. case QAM_16: value |= (1 << 3); break;
  406. default:
  407. case QAM_64: value |= (2 << 3); break;
  408. }
  409. switch (HIERARCHY_1) {
  410. case HIERARCHY_2: value |= 2; break;
  411. case HIERARCHY_4: value |= 4; break;
  412. default:
  413. case HIERARCHY_1: value |= 1; break;
  414. }
  415. dib3000mc_write_word(state, 0, value);
  416. dib3000mc_write_word(state, 5, (1 << 8) | ((seq & 0xf) << 4));
  417. value = 0;
  418. if (ch->hierarchy == 1)
  419. value |= (1 << 4);
  420. if (1 == 1)
  421. value |= 1;
  422. switch ((ch->hierarchy == 0 || 1 == 1) ? ch->code_rate_HP : ch->code_rate_LP) {
  423. case FEC_2_3: value |= (2 << 1); break;
  424. case FEC_3_4: value |= (3 << 1); break;
  425. case FEC_5_6: value |= (5 << 1); break;
  426. case FEC_7_8: value |= (7 << 1); break;
  427. default:
  428. case FEC_1_2: value |= (1 << 1); break;
  429. }
  430. dib3000mc_write_word(state, 181, value);
  431. // diversity synchro delay add 50% SFN margin
  432. switch (ch->transmission_mode) {
  433. case TRANSMISSION_MODE_8K: value = 256; break;
  434. case TRANSMISSION_MODE_2K:
  435. default: value = 64; break;
  436. }
  437. switch (ch->guard_interval) {
  438. case GUARD_INTERVAL_1_16: value *= 2; break;
  439. case GUARD_INTERVAL_1_8: value *= 4; break;
  440. case GUARD_INTERVAL_1_4: value *= 8; break;
  441. default:
  442. case GUARD_INTERVAL_1_32: value *= 1; break;
  443. }
  444. value <<= 4;
  445. value |= dib3000mc_read_word(state, 180) & 0x000f;
  446. dib3000mc_write_word(state, 180, value);
  447. // restart demod
  448. value = dib3000mc_read_word(state, 0);
  449. dib3000mc_write_word(state, 0, value | (1 << 9));
  450. dib3000mc_write_word(state, 0, value);
  451. msleep(30);
  452. dib3000mc_set_impulse_noise(state, state->cfg->impulse_noise_mode, ch->transmission_mode);
  453. }
  454. static int dib3000mc_autosearch_start(struct dvb_frontend *demod)
  455. {
  456. struct dtv_frontend_properties *chan = &demod->dtv_property_cache;
  457. struct dib3000mc_state *state = demod->demodulator_priv;
  458. u16 reg;
  459. // u32 val;
  460. struct dtv_frontend_properties schan;
  461. schan = *chan;
  462. /* TODO what is that ? */
  463. /* a channel for autosearch */
  464. schan.transmission_mode = TRANSMISSION_MODE_8K;
  465. schan.guard_interval = GUARD_INTERVAL_1_32;
  466. schan.modulation = QAM_64;
  467. schan.code_rate_HP = FEC_2_3;
  468. schan.code_rate_LP = FEC_2_3;
  469. schan.hierarchy = 0;
  470. dib3000mc_set_channel_cfg(state, &schan, 11);
  471. reg = dib3000mc_read_word(state, 0);
  472. dib3000mc_write_word(state, 0, reg | (1 << 8));
  473. dib3000mc_read_word(state, 511);
  474. dib3000mc_write_word(state, 0, reg);
  475. return 0;
  476. }
  477. static int dib3000mc_autosearch_is_irq(struct dvb_frontend *demod)
  478. {
  479. struct dib3000mc_state *state = demod->demodulator_priv;
  480. u16 irq_pending = dib3000mc_read_word(state, 511);
  481. if (irq_pending & 0x1) // failed
  482. return 1;
  483. if (irq_pending & 0x2) // succeeded
  484. return 2;
  485. return 0; // still pending
  486. }
  487. static int dib3000mc_tune(struct dvb_frontend *demod)
  488. {
  489. struct dtv_frontend_properties *ch = &demod->dtv_property_cache;
  490. struct dib3000mc_state *state = demod->demodulator_priv;
  491. // ** configure demod **
  492. dib3000mc_set_channel_cfg(state, ch, 0);
  493. // activates isi
  494. if (state->sfn_workaround_active) {
  495. dprintk("SFN workaround is active\n");
  496. dib3000mc_write_word(state, 29, 0x1273);
  497. dib3000mc_write_word(state, 108, 0x4000); // P_pha3_force_pha_shift
  498. } else {
  499. dib3000mc_write_word(state, 29, 0x1073);
  500. dib3000mc_write_word(state, 108, 0x0000); // P_pha3_force_pha_shift
  501. }
  502. dib3000mc_set_adp_cfg(state, (u8)ch->modulation);
  503. if (ch->transmission_mode == TRANSMISSION_MODE_8K) {
  504. dib3000mc_write_word(state, 26, 38528);
  505. dib3000mc_write_word(state, 33, 8);
  506. } else {
  507. dib3000mc_write_word(state, 26, 30336);
  508. dib3000mc_write_word(state, 33, 6);
  509. }
  510. if (dib3000mc_read_word(state, 509) & 0x80)
  511. dib3000mc_set_timing(state, ch->transmission_mode,
  512. BANDWIDTH_TO_KHZ(ch->bandwidth_hz), 1);
  513. return 0;
  514. }
  515. struct i2c_adapter * dib3000mc_get_tuner_i2c_master(struct dvb_frontend *demod, int gating)
  516. {
  517. struct dib3000mc_state *st = demod->demodulator_priv;
  518. return dibx000_get_i2c_adapter(&st->i2c_master, DIBX000_I2C_INTERFACE_TUNER, gating);
  519. }
  520. EXPORT_SYMBOL(dib3000mc_get_tuner_i2c_master);
  521. static int dib3000mc_get_frontend(struct dvb_frontend* fe)
  522. {
  523. struct dtv_frontend_properties *fep = &fe->dtv_property_cache;
  524. struct dib3000mc_state *state = fe->demodulator_priv;
  525. u16 tps = dib3000mc_read_word(state,458);
  526. fep->inversion = INVERSION_AUTO;
  527. fep->bandwidth_hz = state->current_bandwidth;
  528. switch ((tps >> 8) & 0x1) {
  529. case 0: fep->transmission_mode = TRANSMISSION_MODE_2K; break;
  530. case 1: fep->transmission_mode = TRANSMISSION_MODE_8K; break;
  531. }
  532. switch (tps & 0x3) {
  533. case 0: fep->guard_interval = GUARD_INTERVAL_1_32; break;
  534. case 1: fep->guard_interval = GUARD_INTERVAL_1_16; break;
  535. case 2: fep->guard_interval = GUARD_INTERVAL_1_8; break;
  536. case 3: fep->guard_interval = GUARD_INTERVAL_1_4; break;
  537. }
  538. switch ((tps >> 13) & 0x3) {
  539. case 0: fep->modulation = QPSK; break;
  540. case 1: fep->modulation = QAM_16; break;
  541. case 2:
  542. default: fep->modulation = QAM_64; break;
  543. }
  544. /* as long as the frontend_param structure is fixed for hierarchical transmission I refuse to use it */
  545. /* (tps >> 12) & 0x1 == hrch is used, (tps >> 9) & 0x7 == alpha */
  546. fep->hierarchy = HIERARCHY_NONE;
  547. switch ((tps >> 5) & 0x7) {
  548. case 1: fep->code_rate_HP = FEC_1_2; break;
  549. case 2: fep->code_rate_HP = FEC_2_3; break;
  550. case 3: fep->code_rate_HP = FEC_3_4; break;
  551. case 5: fep->code_rate_HP = FEC_5_6; break;
  552. case 7:
  553. default: fep->code_rate_HP = FEC_7_8; break;
  554. }
  555. switch ((tps >> 2) & 0x7) {
  556. case 1: fep->code_rate_LP = FEC_1_2; break;
  557. case 2: fep->code_rate_LP = FEC_2_3; break;
  558. case 3: fep->code_rate_LP = FEC_3_4; break;
  559. case 5: fep->code_rate_LP = FEC_5_6; break;
  560. case 7:
  561. default: fep->code_rate_LP = FEC_7_8; break;
  562. }
  563. return 0;
  564. }
  565. static int dib3000mc_set_frontend(struct dvb_frontend *fe)
  566. {
  567. struct dtv_frontend_properties *fep = &fe->dtv_property_cache;
  568. struct dib3000mc_state *state = fe->demodulator_priv;
  569. int ret;
  570. dib3000mc_set_output_mode(state, OUTMODE_HIGH_Z);
  571. state->current_bandwidth = fep->bandwidth_hz;
  572. dib3000mc_set_bandwidth(state, BANDWIDTH_TO_KHZ(fep->bandwidth_hz));
  573. /* maybe the parameter has been changed */
  574. state->sfn_workaround_active = buggy_sfn_workaround;
  575. if (fe->ops.tuner_ops.set_params) {
  576. fe->ops.tuner_ops.set_params(fe);
  577. msleep(100);
  578. }
  579. if (fep->transmission_mode == TRANSMISSION_MODE_AUTO ||
  580. fep->guard_interval == GUARD_INTERVAL_AUTO ||
  581. fep->modulation == QAM_AUTO ||
  582. fep->code_rate_HP == FEC_AUTO) {
  583. int i = 1000, found;
  584. dib3000mc_autosearch_start(fe);
  585. do {
  586. msleep(1);
  587. found = dib3000mc_autosearch_is_irq(fe);
  588. } while (found == 0 && i--);
  589. dprintk("autosearch returns: %d\n",found);
  590. if (found == 0 || found == 1)
  591. return 0; // no channel found
  592. dib3000mc_get_frontend(fe);
  593. }
  594. ret = dib3000mc_tune(fe);
  595. /* make this a config parameter */
  596. dib3000mc_set_output_mode(state, OUTMODE_MPEG2_FIFO);
  597. return ret;
  598. }
  599. static int dib3000mc_read_status(struct dvb_frontend *fe, enum fe_status *stat)
  600. {
  601. struct dib3000mc_state *state = fe->demodulator_priv;
  602. u16 lock = dib3000mc_read_word(state, 509);
  603. *stat = 0;
  604. if (lock & 0x8000)
  605. *stat |= FE_HAS_SIGNAL;
  606. if (lock & 0x3000)
  607. *stat |= FE_HAS_CARRIER;
  608. if (lock & 0x0100)
  609. *stat |= FE_HAS_VITERBI;
  610. if (lock & 0x0010)
  611. *stat |= FE_HAS_SYNC;
  612. if (lock & 0x0008)
  613. *stat |= FE_HAS_LOCK;
  614. return 0;
  615. }
  616. static int dib3000mc_read_ber(struct dvb_frontend *fe, u32 *ber)
  617. {
  618. struct dib3000mc_state *state = fe->demodulator_priv;
  619. *ber = (dib3000mc_read_word(state, 500) << 16) | dib3000mc_read_word(state, 501);
  620. return 0;
  621. }
  622. static int dib3000mc_read_unc_blocks(struct dvb_frontend *fe, u32 *unc)
  623. {
  624. struct dib3000mc_state *state = fe->demodulator_priv;
  625. *unc = dib3000mc_read_word(state, 508);
  626. return 0;
  627. }
  628. static int dib3000mc_read_signal_strength(struct dvb_frontend *fe, u16 *strength)
  629. {
  630. struct dib3000mc_state *state = fe->demodulator_priv;
  631. u16 val = dib3000mc_read_word(state, 392);
  632. *strength = 65535 - val;
  633. return 0;
  634. }
  635. static int dib3000mc_read_snr(struct dvb_frontend* fe, u16 *snr)
  636. {
  637. *snr = 0x0000;
  638. return 0;
  639. }
  640. static int dib3000mc_fe_get_tune_settings(struct dvb_frontend* fe, struct dvb_frontend_tune_settings *tune)
  641. {
  642. tune->min_delay_ms = 1000;
  643. return 0;
  644. }
  645. static void dib3000mc_release(struct dvb_frontend *fe)
  646. {
  647. struct dib3000mc_state *state = fe->demodulator_priv;
  648. dibx000_exit_i2c_master(&state->i2c_master);
  649. kfree(state);
  650. }
  651. int dib3000mc_pid_control(struct dvb_frontend *fe, int index, int pid,int onoff)
  652. {
  653. struct dib3000mc_state *state = fe->demodulator_priv;
  654. dib3000mc_write_word(state, 212 + index, onoff ? (1 << 13) | pid : 0);
  655. return 0;
  656. }
  657. EXPORT_SYMBOL(dib3000mc_pid_control);
  658. int dib3000mc_pid_parse(struct dvb_frontend *fe, int onoff)
  659. {
  660. struct dib3000mc_state *state = fe->demodulator_priv;
  661. u16 tmp = dib3000mc_read_word(state, 206) & ~(1 << 4);
  662. tmp |= (onoff << 4);
  663. return dib3000mc_write_word(state, 206, tmp);
  664. }
  665. EXPORT_SYMBOL(dib3000mc_pid_parse);
  666. void dib3000mc_set_config(struct dvb_frontend *fe, struct dib3000mc_config *cfg)
  667. {
  668. struct dib3000mc_state *state = fe->demodulator_priv;
  669. state->cfg = cfg;
  670. }
  671. EXPORT_SYMBOL(dib3000mc_set_config);
  672. int dib3000mc_i2c_enumeration(struct i2c_adapter *i2c, int no_of_demods, u8 default_addr, struct dib3000mc_config cfg[])
  673. {
  674. struct dib3000mc_state *dmcst;
  675. int k;
  676. u8 new_addr;
  677. static u8 DIB3000MC_I2C_ADDRESS[] = {20,22,24,26};
  678. dmcst = kzalloc(sizeof(struct dib3000mc_state), GFP_KERNEL);
  679. if (dmcst == NULL)
  680. return -ENOMEM;
  681. dmcst->i2c_adap = i2c;
  682. for (k = no_of_demods-1; k >= 0; k--) {
  683. dmcst->cfg = &cfg[k];
  684. /* designated i2c address */
  685. new_addr = DIB3000MC_I2C_ADDRESS[k];
  686. dmcst->i2c_addr = new_addr;
  687. if (dib3000mc_identify(dmcst) != 0) {
  688. dmcst->i2c_addr = default_addr;
  689. if (dib3000mc_identify(dmcst) != 0) {
  690. dprintk("-E- DiB3000P/MC #%d: not identified\n", k);
  691. kfree(dmcst);
  692. return -ENODEV;
  693. }
  694. }
  695. dib3000mc_set_output_mode(dmcst, OUTMODE_MPEG2_PAR_CONT_CLK);
  696. // set new i2c address and force divstr (Bit 1) to value 0 (Bit 0)
  697. dib3000mc_write_word(dmcst, 1024, (new_addr << 3) | 0x1);
  698. dmcst->i2c_addr = new_addr;
  699. }
  700. for (k = 0; k < no_of_demods; k++) {
  701. dmcst->cfg = &cfg[k];
  702. dmcst->i2c_addr = DIB3000MC_I2C_ADDRESS[k];
  703. dib3000mc_write_word(dmcst, 1024, dmcst->i2c_addr << 3);
  704. /* turn off data output */
  705. dib3000mc_set_output_mode(dmcst, OUTMODE_HIGH_Z);
  706. }
  707. kfree(dmcst);
  708. return 0;
  709. }
  710. EXPORT_SYMBOL(dib3000mc_i2c_enumeration);
  711. static struct dvb_frontend_ops dib3000mc_ops;
  712. struct dvb_frontend * dib3000mc_attach(struct i2c_adapter *i2c_adap, u8 i2c_addr, struct dib3000mc_config *cfg)
  713. {
  714. struct dvb_frontend *demod;
  715. struct dib3000mc_state *st;
  716. st = kzalloc(sizeof(struct dib3000mc_state), GFP_KERNEL);
  717. if (st == NULL)
  718. return NULL;
  719. st->cfg = cfg;
  720. st->i2c_adap = i2c_adap;
  721. st->i2c_addr = i2c_addr;
  722. demod = &st->demod;
  723. demod->demodulator_priv = st;
  724. memcpy(&st->demod.ops, &dib3000mc_ops, sizeof(struct dvb_frontend_ops));
  725. if (dib3000mc_identify(st) != 0)
  726. goto error;
  727. dibx000_init_i2c_master(&st->i2c_master, DIB3000MC, st->i2c_adap, st->i2c_addr);
  728. dib3000mc_write_word(st, 1037, 0x3130);
  729. return demod;
  730. error:
  731. kfree(st);
  732. return NULL;
  733. }
  734. EXPORT_SYMBOL(dib3000mc_attach);
  735. static struct dvb_frontend_ops dib3000mc_ops = {
  736. .delsys = { SYS_DVBT },
  737. .info = {
  738. .name = "DiBcom 3000MC/P",
  739. .frequency_min = 44250000,
  740. .frequency_max = 867250000,
  741. .frequency_stepsize = 62500,
  742. .caps = FE_CAN_INVERSION_AUTO |
  743. FE_CAN_FEC_1_2 | FE_CAN_FEC_2_3 | FE_CAN_FEC_3_4 |
  744. FE_CAN_FEC_5_6 | FE_CAN_FEC_7_8 | FE_CAN_FEC_AUTO |
  745. FE_CAN_QPSK | FE_CAN_QAM_16 | FE_CAN_QAM_64 | FE_CAN_QAM_AUTO |
  746. FE_CAN_TRANSMISSION_MODE_AUTO |
  747. FE_CAN_GUARD_INTERVAL_AUTO |
  748. FE_CAN_RECOVER |
  749. FE_CAN_HIERARCHY_AUTO,
  750. },
  751. .release = dib3000mc_release,
  752. .init = dib3000mc_init,
  753. .sleep = dib3000mc_sleep,
  754. .set_frontend = dib3000mc_set_frontend,
  755. .get_tune_settings = dib3000mc_fe_get_tune_settings,
  756. .get_frontend = dib3000mc_get_frontend,
  757. .read_status = dib3000mc_read_status,
  758. .read_ber = dib3000mc_read_ber,
  759. .read_signal_strength = dib3000mc_read_signal_strength,
  760. .read_snr = dib3000mc_read_snr,
  761. .read_ucblocks = dib3000mc_read_unc_blocks,
  762. };
  763. MODULE_AUTHOR("Patrick Boettcher <pboettcher@dibcom.fr>");
  764. MODULE_DESCRIPTION("Driver for the DiBcom 3000MC/P COFDM demodulator");
  765. MODULE_LICENSE("GPL");