dib3000mb.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835
  1. /*
  2. * Frontend driver for mobile DVB-T demodulator DiBcom 3000M-B
  3. * DiBcom (http://www.dibcom.fr/)
  4. *
  5. * Copyright (C) 2004-5 Patrick Boettcher (patrick.boettcher@desy.de)
  6. *
  7. * based on GPL code from DibCom, which has
  8. *
  9. * Copyright (C) 2004 Amaury Demol for DiBcom (ademol@dibcom.fr)
  10. *
  11. * This program is free software; you can redistribute it and/or
  12. * modify it under the terms of the GNU General Public License as
  13. * published by the Free Software Foundation, version 2.
  14. *
  15. * Acknowledgements
  16. *
  17. * Amaury Demol (ademol@dibcom.fr) from DiBcom for providing specs and driver
  18. * sources, on which this driver (and the dvb-dibusb) are based.
  19. *
  20. * see Documentation/dvb/README.dibusb for more information
  21. *
  22. */
  23. #include <linux/kernel.h>
  24. #include <linux/module.h>
  25. #include <linux/init.h>
  26. #include <linux/delay.h>
  27. #include <linux/string.h>
  28. #include <linux/slab.h>
  29. #include "dvb_frontend.h"
  30. #include "dib3000.h"
  31. #include "dib3000mb_priv.h"
  32. /* Version information */
  33. #define DRIVER_VERSION "0.1"
  34. #define DRIVER_DESC "DiBcom 3000M-B DVB-T demodulator"
  35. #define DRIVER_AUTHOR "Patrick Boettcher, patrick.boettcher@desy.de"
  36. static int debug;
  37. module_param(debug, int, 0644);
  38. MODULE_PARM_DESC(debug, "set debugging level (1=info,2=xfer,4=setfe,8=getfe (|-able)).");
  39. #define deb_info(args...) dprintk(0x01,args)
  40. #define deb_i2c(args...) dprintk(0x02,args)
  41. #define deb_srch(args...) dprintk(0x04,args)
  42. #define deb_info(args...) dprintk(0x01,args)
  43. #define deb_xfer(args...) dprintk(0x02,args)
  44. #define deb_setf(args...) dprintk(0x04,args)
  45. #define deb_getf(args...) dprintk(0x08,args)
  46. static int dib3000_read_reg(struct dib3000_state *state, u16 reg)
  47. {
  48. u8 wb[] = { ((reg >> 8) | 0x80) & 0xff, reg & 0xff };
  49. u8 rb[2];
  50. struct i2c_msg msg[] = {
  51. { .addr = state->config.demod_address, .flags = 0, .buf = wb, .len = 2 },
  52. { .addr = state->config.demod_address, .flags = I2C_M_RD, .buf = rb, .len = 2 },
  53. };
  54. if (i2c_transfer(state->i2c, msg, 2) != 2)
  55. deb_i2c("i2c read error\n");
  56. deb_i2c("reading i2c bus (reg: %5d 0x%04x, val: %5d 0x%04x)\n",reg,reg,
  57. (rb[0] << 8) | rb[1],(rb[0] << 8) | rb[1]);
  58. return (rb[0] << 8) | rb[1];
  59. }
  60. static int dib3000_write_reg(struct dib3000_state *state, u16 reg, u16 val)
  61. {
  62. u8 b[] = {
  63. (reg >> 8) & 0xff, reg & 0xff,
  64. (val >> 8) & 0xff, val & 0xff,
  65. };
  66. struct i2c_msg msg[] = {
  67. { .addr = state->config.demod_address, .flags = 0, .buf = b, .len = 4 }
  68. };
  69. deb_i2c("writing i2c bus (reg: %5d 0x%04x, val: %5d 0x%04x)\n",reg,reg,val,val);
  70. return i2c_transfer(state->i2c,msg, 1) != 1 ? -EREMOTEIO : 0;
  71. }
  72. static int dib3000_search_status(u16 irq,u16 lock)
  73. {
  74. if (irq & 0x02) {
  75. if (lock & 0x01) {
  76. deb_srch("auto search succeeded\n");
  77. return 1; // auto search succeeded
  78. } else {
  79. deb_srch("auto search not successful\n");
  80. return 0; // auto search failed
  81. }
  82. } else if (irq & 0x01) {
  83. deb_srch("auto search failed\n");
  84. return 0; // auto search failed
  85. }
  86. return -1; // try again
  87. }
  88. /* for auto search */
  89. static u16 dib3000_seq[2][2][2] = /* fft,gua, inv */
  90. { /* fft */
  91. { /* gua */
  92. { 0, 1 }, /* 0 0 { 0,1 } */
  93. { 3, 9 }, /* 0 1 { 0,1 } */
  94. },
  95. {
  96. { 2, 5 }, /* 1 0 { 0,1 } */
  97. { 6, 11 }, /* 1 1 { 0,1 } */
  98. }
  99. };
  100. static int dib3000mb_get_frontend(struct dvb_frontend* fe,
  101. struct dvb_frontend_parameters *fep);
  102. static int dib3000mb_set_frontend(struct dvb_frontend* fe,
  103. struct dvb_frontend_parameters *fep, int tuner)
  104. {
  105. struct dib3000_state* state = fe->demodulator_priv;
  106. struct dvb_ofdm_parameters *ofdm = &fep->u.ofdm;
  107. fe_code_rate_t fe_cr = FEC_NONE;
  108. int search_state, seq;
  109. if (tuner && fe->ops.tuner_ops.set_params) {
  110. fe->ops.tuner_ops.set_params(fe, fep);
  111. if (fe->ops.i2c_gate_ctrl) fe->ops.i2c_gate_ctrl(fe, 0);
  112. deb_setf("bandwidth: ");
  113. switch (ofdm->bandwidth) {
  114. case BANDWIDTH_8_MHZ:
  115. deb_setf("8 MHz\n");
  116. wr_foreach(dib3000mb_reg_timing_freq, dib3000mb_timing_freq[2]);
  117. wr_foreach(dib3000mb_reg_bandwidth, dib3000mb_bandwidth_8mhz);
  118. break;
  119. case BANDWIDTH_7_MHZ:
  120. deb_setf("7 MHz\n");
  121. wr_foreach(dib3000mb_reg_timing_freq, dib3000mb_timing_freq[1]);
  122. wr_foreach(dib3000mb_reg_bandwidth, dib3000mb_bandwidth_7mhz);
  123. break;
  124. case BANDWIDTH_6_MHZ:
  125. deb_setf("6 MHz\n");
  126. wr_foreach(dib3000mb_reg_timing_freq, dib3000mb_timing_freq[0]);
  127. wr_foreach(dib3000mb_reg_bandwidth, dib3000mb_bandwidth_6mhz);
  128. break;
  129. case BANDWIDTH_AUTO:
  130. return -EOPNOTSUPP;
  131. default:
  132. err("unknown bandwidth value.");
  133. return -EINVAL;
  134. }
  135. }
  136. wr(DIB3000MB_REG_LOCK1_MASK, DIB3000MB_LOCK1_SEARCH_4);
  137. deb_setf("transmission mode: ");
  138. switch (ofdm->transmission_mode) {
  139. case TRANSMISSION_MODE_2K:
  140. deb_setf("2k\n");
  141. wr(DIB3000MB_REG_FFT, DIB3000_TRANSMISSION_MODE_2K);
  142. break;
  143. case TRANSMISSION_MODE_8K:
  144. deb_setf("8k\n");
  145. wr(DIB3000MB_REG_FFT, DIB3000_TRANSMISSION_MODE_8K);
  146. break;
  147. case TRANSMISSION_MODE_AUTO:
  148. deb_setf("auto\n");
  149. break;
  150. default:
  151. return -EINVAL;
  152. }
  153. deb_setf("guard: ");
  154. switch (ofdm->guard_interval) {
  155. case GUARD_INTERVAL_1_32:
  156. deb_setf("1_32\n");
  157. wr(DIB3000MB_REG_GUARD_TIME, DIB3000_GUARD_TIME_1_32);
  158. break;
  159. case GUARD_INTERVAL_1_16:
  160. deb_setf("1_16\n");
  161. wr(DIB3000MB_REG_GUARD_TIME, DIB3000_GUARD_TIME_1_16);
  162. break;
  163. case GUARD_INTERVAL_1_8:
  164. deb_setf("1_8\n");
  165. wr(DIB3000MB_REG_GUARD_TIME, DIB3000_GUARD_TIME_1_8);
  166. break;
  167. case GUARD_INTERVAL_1_4:
  168. deb_setf("1_4\n");
  169. wr(DIB3000MB_REG_GUARD_TIME, DIB3000_GUARD_TIME_1_4);
  170. break;
  171. case GUARD_INTERVAL_AUTO:
  172. deb_setf("auto\n");
  173. break;
  174. default:
  175. return -EINVAL;
  176. }
  177. deb_setf("inversion: ");
  178. switch (fep->inversion) {
  179. case INVERSION_OFF:
  180. deb_setf("off\n");
  181. wr(DIB3000MB_REG_DDS_INV, DIB3000_DDS_INVERSION_OFF);
  182. break;
  183. case INVERSION_AUTO:
  184. deb_setf("auto ");
  185. break;
  186. case INVERSION_ON:
  187. deb_setf("on\n");
  188. wr(DIB3000MB_REG_DDS_INV, DIB3000_DDS_INVERSION_ON);
  189. break;
  190. default:
  191. return -EINVAL;
  192. }
  193. deb_setf("constellation: ");
  194. switch (ofdm->constellation) {
  195. case QPSK:
  196. deb_setf("qpsk\n");
  197. wr(DIB3000MB_REG_QAM, DIB3000_CONSTELLATION_QPSK);
  198. break;
  199. case QAM_16:
  200. deb_setf("qam16\n");
  201. wr(DIB3000MB_REG_QAM, DIB3000_CONSTELLATION_16QAM);
  202. break;
  203. case QAM_64:
  204. deb_setf("qam64\n");
  205. wr(DIB3000MB_REG_QAM, DIB3000_CONSTELLATION_64QAM);
  206. break;
  207. case QAM_AUTO:
  208. break;
  209. default:
  210. return -EINVAL;
  211. }
  212. deb_setf("hierarchy: ");
  213. switch (ofdm->hierarchy_information) {
  214. case HIERARCHY_NONE:
  215. deb_setf("none ");
  216. /* fall through */
  217. case HIERARCHY_1:
  218. deb_setf("alpha=1\n");
  219. wr(DIB3000MB_REG_VIT_ALPHA, DIB3000_ALPHA_1);
  220. break;
  221. case HIERARCHY_2:
  222. deb_setf("alpha=2\n");
  223. wr(DIB3000MB_REG_VIT_ALPHA, DIB3000_ALPHA_2);
  224. break;
  225. case HIERARCHY_4:
  226. deb_setf("alpha=4\n");
  227. wr(DIB3000MB_REG_VIT_ALPHA, DIB3000_ALPHA_4);
  228. break;
  229. case HIERARCHY_AUTO:
  230. deb_setf("alpha=auto\n");
  231. break;
  232. default:
  233. return -EINVAL;
  234. }
  235. deb_setf("hierarchy: ");
  236. if (ofdm->hierarchy_information == HIERARCHY_NONE) {
  237. deb_setf("none\n");
  238. wr(DIB3000MB_REG_VIT_HRCH, DIB3000_HRCH_OFF);
  239. wr(DIB3000MB_REG_VIT_HP, DIB3000_SELECT_HP);
  240. fe_cr = ofdm->code_rate_HP;
  241. } else if (ofdm->hierarchy_information != HIERARCHY_AUTO) {
  242. deb_setf("on\n");
  243. wr(DIB3000MB_REG_VIT_HRCH, DIB3000_HRCH_ON);
  244. wr(DIB3000MB_REG_VIT_HP, DIB3000_SELECT_LP);
  245. fe_cr = ofdm->code_rate_LP;
  246. }
  247. deb_setf("fec: ");
  248. switch (fe_cr) {
  249. case FEC_1_2:
  250. deb_setf("1_2\n");
  251. wr(DIB3000MB_REG_VIT_CODE_RATE, DIB3000_FEC_1_2);
  252. break;
  253. case FEC_2_3:
  254. deb_setf("2_3\n");
  255. wr(DIB3000MB_REG_VIT_CODE_RATE, DIB3000_FEC_2_3);
  256. break;
  257. case FEC_3_4:
  258. deb_setf("3_4\n");
  259. wr(DIB3000MB_REG_VIT_CODE_RATE, DIB3000_FEC_3_4);
  260. break;
  261. case FEC_5_6:
  262. deb_setf("5_6\n");
  263. wr(DIB3000MB_REG_VIT_CODE_RATE, DIB3000_FEC_5_6);
  264. break;
  265. case FEC_7_8:
  266. deb_setf("7_8\n");
  267. wr(DIB3000MB_REG_VIT_CODE_RATE, DIB3000_FEC_7_8);
  268. break;
  269. case FEC_NONE:
  270. deb_setf("none ");
  271. break;
  272. case FEC_AUTO:
  273. deb_setf("auto\n");
  274. break;
  275. default:
  276. return -EINVAL;
  277. }
  278. seq = dib3000_seq
  279. [ofdm->transmission_mode == TRANSMISSION_MODE_AUTO]
  280. [ofdm->guard_interval == GUARD_INTERVAL_AUTO]
  281. [fep->inversion == INVERSION_AUTO];
  282. deb_setf("seq? %d\n", seq);
  283. wr(DIB3000MB_REG_SEQ, seq);
  284. wr(DIB3000MB_REG_ISI, seq ? DIB3000MB_ISI_INHIBIT : DIB3000MB_ISI_ACTIVATE);
  285. if (ofdm->transmission_mode == TRANSMISSION_MODE_2K) {
  286. if (ofdm->guard_interval == GUARD_INTERVAL_1_8) {
  287. wr(DIB3000MB_REG_SYNC_IMPROVEMENT, DIB3000MB_SYNC_IMPROVE_2K_1_8);
  288. } else {
  289. wr(DIB3000MB_REG_SYNC_IMPROVEMENT, DIB3000MB_SYNC_IMPROVE_DEFAULT);
  290. }
  291. wr(DIB3000MB_REG_UNK_121, DIB3000MB_UNK_121_2K);
  292. } else {
  293. wr(DIB3000MB_REG_UNK_121, DIB3000MB_UNK_121_DEFAULT);
  294. }
  295. wr(DIB3000MB_REG_MOBILE_ALGO, DIB3000MB_MOBILE_ALGO_OFF);
  296. wr(DIB3000MB_REG_MOBILE_MODE_QAM, DIB3000MB_MOBILE_MODE_QAM_OFF);
  297. wr(DIB3000MB_REG_MOBILE_MODE, DIB3000MB_MOBILE_MODE_OFF);
  298. wr_foreach(dib3000mb_reg_agc_bandwidth, dib3000mb_agc_bandwidth_high);
  299. wr(DIB3000MB_REG_ISI, DIB3000MB_ISI_ACTIVATE);
  300. wr(DIB3000MB_REG_RESTART, DIB3000MB_RESTART_AGC + DIB3000MB_RESTART_CTRL);
  301. wr(DIB3000MB_REG_RESTART, DIB3000MB_RESTART_OFF);
  302. /* wait for AGC lock */
  303. msleep(70);
  304. wr_foreach(dib3000mb_reg_agc_bandwidth, dib3000mb_agc_bandwidth_low);
  305. /* something has to be auto searched */
  306. if (ofdm->constellation == QAM_AUTO ||
  307. ofdm->hierarchy_information == HIERARCHY_AUTO ||
  308. fe_cr == FEC_AUTO ||
  309. fep->inversion == INVERSION_AUTO) {
  310. int as_count=0;
  311. deb_setf("autosearch enabled.\n");
  312. wr(DIB3000MB_REG_ISI, DIB3000MB_ISI_INHIBIT);
  313. wr(DIB3000MB_REG_RESTART, DIB3000MB_RESTART_AUTO_SEARCH);
  314. wr(DIB3000MB_REG_RESTART, DIB3000MB_RESTART_OFF);
  315. while ((search_state =
  316. dib3000_search_status(
  317. rd(DIB3000MB_REG_AS_IRQ_PENDING),
  318. rd(DIB3000MB_REG_LOCK2_VALUE))) < 0 && as_count++ < 100)
  319. msleep(1);
  320. deb_setf("search_state after autosearch %d after %d checks\n",search_state,as_count);
  321. if (search_state == 1) {
  322. struct dvb_frontend_parameters feps;
  323. if (dib3000mb_get_frontend(fe, &feps) == 0) {
  324. deb_setf("reading tuning data from frontend succeeded.\n");
  325. return dib3000mb_set_frontend(fe, &feps, 0);
  326. }
  327. }
  328. } else {
  329. wr(DIB3000MB_REG_RESTART, DIB3000MB_RESTART_CTRL);
  330. wr(DIB3000MB_REG_RESTART, DIB3000MB_RESTART_OFF);
  331. }
  332. return 0;
  333. }
  334. static int dib3000mb_fe_init(struct dvb_frontend* fe, int mobile_mode)
  335. {
  336. struct dib3000_state* state = fe->demodulator_priv;
  337. deb_info("dib3000mb is getting up.\n");
  338. wr(DIB3000MB_REG_POWER_CONTROL, DIB3000MB_POWER_UP);
  339. wr(DIB3000MB_REG_RESTART, DIB3000MB_RESTART_AGC);
  340. wr(DIB3000MB_REG_RESET_DEVICE, DIB3000MB_RESET_DEVICE);
  341. wr(DIB3000MB_REG_RESET_DEVICE, DIB3000MB_RESET_DEVICE_RST);
  342. wr(DIB3000MB_REG_CLOCK, DIB3000MB_CLOCK_DEFAULT);
  343. wr(DIB3000MB_REG_ELECT_OUT_MODE, DIB3000MB_ELECT_OUT_MODE_ON);
  344. wr(DIB3000MB_REG_DDS_FREQ_MSB, DIB3000MB_DDS_FREQ_MSB);
  345. wr(DIB3000MB_REG_DDS_FREQ_LSB, DIB3000MB_DDS_FREQ_LSB);
  346. wr_foreach(dib3000mb_reg_timing_freq, dib3000mb_timing_freq[2]);
  347. wr_foreach(dib3000mb_reg_impulse_noise,
  348. dib3000mb_impulse_noise_values[DIB3000MB_IMPNOISE_OFF]);
  349. wr_foreach(dib3000mb_reg_agc_gain, dib3000mb_default_agc_gain);
  350. wr(DIB3000MB_REG_PHASE_NOISE, DIB3000MB_PHASE_NOISE_DEFAULT);
  351. wr_foreach(dib3000mb_reg_phase_noise, dib3000mb_default_noise_phase);
  352. wr_foreach(dib3000mb_reg_lock_duration, dib3000mb_default_lock_duration);
  353. wr_foreach(dib3000mb_reg_agc_bandwidth, dib3000mb_agc_bandwidth_low);
  354. wr(DIB3000MB_REG_LOCK0_MASK, DIB3000MB_LOCK0_DEFAULT);
  355. wr(DIB3000MB_REG_LOCK1_MASK, DIB3000MB_LOCK1_SEARCH_4);
  356. wr(DIB3000MB_REG_LOCK2_MASK, DIB3000MB_LOCK2_DEFAULT);
  357. wr(DIB3000MB_REG_SEQ, dib3000_seq[1][1][1]);
  358. wr_foreach(dib3000mb_reg_bandwidth, dib3000mb_bandwidth_8mhz);
  359. wr(DIB3000MB_REG_UNK_68, DIB3000MB_UNK_68);
  360. wr(DIB3000MB_REG_UNK_69, DIB3000MB_UNK_69);
  361. wr(DIB3000MB_REG_UNK_71, DIB3000MB_UNK_71);
  362. wr(DIB3000MB_REG_UNK_77, DIB3000MB_UNK_77);
  363. wr(DIB3000MB_REG_UNK_78, DIB3000MB_UNK_78);
  364. wr(DIB3000MB_REG_ISI, DIB3000MB_ISI_INHIBIT);
  365. wr(DIB3000MB_REG_UNK_92, DIB3000MB_UNK_92);
  366. wr(DIB3000MB_REG_UNK_96, DIB3000MB_UNK_96);
  367. wr(DIB3000MB_REG_UNK_97, DIB3000MB_UNK_97);
  368. wr(DIB3000MB_REG_UNK_106, DIB3000MB_UNK_106);
  369. wr(DIB3000MB_REG_UNK_107, DIB3000MB_UNK_107);
  370. wr(DIB3000MB_REG_UNK_108, DIB3000MB_UNK_108);
  371. wr(DIB3000MB_REG_UNK_122, DIB3000MB_UNK_122);
  372. wr(DIB3000MB_REG_MOBILE_MODE_QAM, DIB3000MB_MOBILE_MODE_QAM_OFF);
  373. wr(DIB3000MB_REG_BERLEN, DIB3000MB_BERLEN_DEFAULT);
  374. wr_foreach(dib3000mb_reg_filter_coeffs, dib3000mb_filter_coeffs);
  375. wr(DIB3000MB_REG_MOBILE_ALGO, DIB3000MB_MOBILE_ALGO_ON);
  376. wr(DIB3000MB_REG_MULTI_DEMOD_MSB, DIB3000MB_MULTI_DEMOD_MSB);
  377. wr(DIB3000MB_REG_MULTI_DEMOD_LSB, DIB3000MB_MULTI_DEMOD_LSB);
  378. wr(DIB3000MB_REG_OUTPUT_MODE, DIB3000MB_OUTPUT_MODE_SLAVE);
  379. wr(DIB3000MB_REG_FIFO_142, DIB3000MB_FIFO_142);
  380. wr(DIB3000MB_REG_MPEG2_OUT_MODE, DIB3000MB_MPEG2_OUT_MODE_188);
  381. wr(DIB3000MB_REG_PID_PARSE, DIB3000MB_PID_PARSE_ACTIVATE);
  382. wr(DIB3000MB_REG_FIFO, DIB3000MB_FIFO_INHIBIT);
  383. wr(DIB3000MB_REG_FIFO_146, DIB3000MB_FIFO_146);
  384. wr(DIB3000MB_REG_FIFO_147, DIB3000MB_FIFO_147);
  385. wr(DIB3000MB_REG_DATA_IN_DIVERSITY, DIB3000MB_DATA_DIVERSITY_IN_OFF);
  386. return 0;
  387. }
  388. static int dib3000mb_get_frontend(struct dvb_frontend* fe,
  389. struct dvb_frontend_parameters *fep)
  390. {
  391. struct dib3000_state* state = fe->demodulator_priv;
  392. struct dvb_ofdm_parameters *ofdm = &fep->u.ofdm;
  393. fe_code_rate_t *cr;
  394. u16 tps_val;
  395. int inv_test1,inv_test2;
  396. u32 dds_val, threshold = 0x800000;
  397. if (!rd(DIB3000MB_REG_TPS_LOCK))
  398. return 0;
  399. dds_val = ((rd(DIB3000MB_REG_DDS_VALUE_MSB) & 0xff) << 16) + rd(DIB3000MB_REG_DDS_VALUE_LSB);
  400. deb_getf("DDS_VAL: %x %x %x",dds_val, rd(DIB3000MB_REG_DDS_VALUE_MSB), rd(DIB3000MB_REG_DDS_VALUE_LSB));
  401. if (dds_val < threshold)
  402. inv_test1 = 0;
  403. else if (dds_val == threshold)
  404. inv_test1 = 1;
  405. else
  406. inv_test1 = 2;
  407. dds_val = ((rd(DIB3000MB_REG_DDS_FREQ_MSB) & 0xff) << 16) + rd(DIB3000MB_REG_DDS_FREQ_LSB);
  408. deb_getf("DDS_FREQ: %x %x %x",dds_val, rd(DIB3000MB_REG_DDS_FREQ_MSB), rd(DIB3000MB_REG_DDS_FREQ_LSB));
  409. if (dds_val < threshold)
  410. inv_test2 = 0;
  411. else if (dds_val == threshold)
  412. inv_test2 = 1;
  413. else
  414. inv_test2 = 2;
  415. fep->inversion =
  416. ((inv_test2 == 2) && (inv_test1==1 || inv_test1==0)) ||
  417. ((inv_test2 == 0) && (inv_test1==1 || inv_test1==2)) ?
  418. INVERSION_ON : INVERSION_OFF;
  419. deb_getf("inversion %d %d, %d\n", inv_test2, inv_test1, fep->inversion);
  420. switch ((tps_val = rd(DIB3000MB_REG_TPS_QAM))) {
  421. case DIB3000_CONSTELLATION_QPSK:
  422. deb_getf("QPSK ");
  423. ofdm->constellation = QPSK;
  424. break;
  425. case DIB3000_CONSTELLATION_16QAM:
  426. deb_getf("QAM16 ");
  427. ofdm->constellation = QAM_16;
  428. break;
  429. case DIB3000_CONSTELLATION_64QAM:
  430. deb_getf("QAM64 ");
  431. ofdm->constellation = QAM_64;
  432. break;
  433. default:
  434. err("Unexpected constellation returned by TPS (%d)", tps_val);
  435. break;
  436. }
  437. deb_getf("TPS: %d\n", tps_val);
  438. if (rd(DIB3000MB_REG_TPS_HRCH)) {
  439. deb_getf("HRCH ON\n");
  440. cr = &ofdm->code_rate_LP;
  441. ofdm->code_rate_HP = FEC_NONE;
  442. switch ((tps_val = rd(DIB3000MB_REG_TPS_VIT_ALPHA))) {
  443. case DIB3000_ALPHA_0:
  444. deb_getf("HIERARCHY_NONE ");
  445. ofdm->hierarchy_information = HIERARCHY_NONE;
  446. break;
  447. case DIB3000_ALPHA_1:
  448. deb_getf("HIERARCHY_1 ");
  449. ofdm->hierarchy_information = HIERARCHY_1;
  450. break;
  451. case DIB3000_ALPHA_2:
  452. deb_getf("HIERARCHY_2 ");
  453. ofdm->hierarchy_information = HIERARCHY_2;
  454. break;
  455. case DIB3000_ALPHA_4:
  456. deb_getf("HIERARCHY_4 ");
  457. ofdm->hierarchy_information = HIERARCHY_4;
  458. break;
  459. default:
  460. err("Unexpected ALPHA value returned by TPS (%d)", tps_val);
  461. break;
  462. }
  463. deb_getf("TPS: %d\n", tps_val);
  464. tps_val = rd(DIB3000MB_REG_TPS_CODE_RATE_LP);
  465. } else {
  466. deb_getf("HRCH OFF\n");
  467. cr = &ofdm->code_rate_HP;
  468. ofdm->code_rate_LP = FEC_NONE;
  469. ofdm->hierarchy_information = HIERARCHY_NONE;
  470. tps_val = rd(DIB3000MB_REG_TPS_CODE_RATE_HP);
  471. }
  472. switch (tps_val) {
  473. case DIB3000_FEC_1_2:
  474. deb_getf("FEC_1_2 ");
  475. *cr = FEC_1_2;
  476. break;
  477. case DIB3000_FEC_2_3:
  478. deb_getf("FEC_2_3 ");
  479. *cr = FEC_2_3;
  480. break;
  481. case DIB3000_FEC_3_4:
  482. deb_getf("FEC_3_4 ");
  483. *cr = FEC_3_4;
  484. break;
  485. case DIB3000_FEC_5_6:
  486. deb_getf("FEC_5_6 ");
  487. *cr = FEC_4_5;
  488. break;
  489. case DIB3000_FEC_7_8:
  490. deb_getf("FEC_7_8 ");
  491. *cr = FEC_7_8;
  492. break;
  493. default:
  494. err("Unexpected FEC returned by TPS (%d)", tps_val);
  495. break;
  496. }
  497. deb_getf("TPS: %d\n",tps_val);
  498. switch ((tps_val = rd(DIB3000MB_REG_TPS_GUARD_TIME))) {
  499. case DIB3000_GUARD_TIME_1_32:
  500. deb_getf("GUARD_INTERVAL_1_32 ");
  501. ofdm->guard_interval = GUARD_INTERVAL_1_32;
  502. break;
  503. case DIB3000_GUARD_TIME_1_16:
  504. deb_getf("GUARD_INTERVAL_1_16 ");
  505. ofdm->guard_interval = GUARD_INTERVAL_1_16;
  506. break;
  507. case DIB3000_GUARD_TIME_1_8:
  508. deb_getf("GUARD_INTERVAL_1_8 ");
  509. ofdm->guard_interval = GUARD_INTERVAL_1_8;
  510. break;
  511. case DIB3000_GUARD_TIME_1_4:
  512. deb_getf("GUARD_INTERVAL_1_4 ");
  513. ofdm->guard_interval = GUARD_INTERVAL_1_4;
  514. break;
  515. default:
  516. err("Unexpected Guard Time returned by TPS (%d)", tps_val);
  517. break;
  518. }
  519. deb_getf("TPS: %d\n", tps_val);
  520. switch ((tps_val = rd(DIB3000MB_REG_TPS_FFT))) {
  521. case DIB3000_TRANSMISSION_MODE_2K:
  522. deb_getf("TRANSMISSION_MODE_2K ");
  523. ofdm->transmission_mode = TRANSMISSION_MODE_2K;
  524. break;
  525. case DIB3000_TRANSMISSION_MODE_8K:
  526. deb_getf("TRANSMISSION_MODE_8K ");
  527. ofdm->transmission_mode = TRANSMISSION_MODE_8K;
  528. break;
  529. default:
  530. err("unexpected transmission mode return by TPS (%d)", tps_val);
  531. break;
  532. }
  533. deb_getf("TPS: %d\n", tps_val);
  534. return 0;
  535. }
  536. static int dib3000mb_read_status(struct dvb_frontend* fe, fe_status_t *stat)
  537. {
  538. struct dib3000_state* state = fe->demodulator_priv;
  539. *stat = 0;
  540. if (rd(DIB3000MB_REG_AGC_LOCK))
  541. *stat |= FE_HAS_SIGNAL;
  542. if (rd(DIB3000MB_REG_CARRIER_LOCK))
  543. *stat |= FE_HAS_CARRIER;
  544. if (rd(DIB3000MB_REG_VIT_LCK))
  545. *stat |= FE_HAS_VITERBI;
  546. if (rd(DIB3000MB_REG_TS_SYNC_LOCK))
  547. *stat |= (FE_HAS_SYNC | FE_HAS_LOCK);
  548. deb_getf("actual status is %2x\n",*stat);
  549. deb_getf("autoval: tps: %d, qam: %d, hrch: %d, alpha: %d, hp: %d, lp: %d, guard: %d, fft: %d cell: %d\n",
  550. rd(DIB3000MB_REG_TPS_LOCK),
  551. rd(DIB3000MB_REG_TPS_QAM),
  552. rd(DIB3000MB_REG_TPS_HRCH),
  553. rd(DIB3000MB_REG_TPS_VIT_ALPHA),
  554. rd(DIB3000MB_REG_TPS_CODE_RATE_HP),
  555. rd(DIB3000MB_REG_TPS_CODE_RATE_LP),
  556. rd(DIB3000MB_REG_TPS_GUARD_TIME),
  557. rd(DIB3000MB_REG_TPS_FFT),
  558. rd(DIB3000MB_REG_TPS_CELL_ID));
  559. //*stat = FE_HAS_SIGNAL | FE_HAS_CARRIER | FE_HAS_VITERBI | FE_HAS_SYNC | FE_HAS_LOCK;
  560. return 0;
  561. }
  562. static int dib3000mb_read_ber(struct dvb_frontend* fe, u32 *ber)
  563. {
  564. struct dib3000_state* state = fe->demodulator_priv;
  565. *ber = ((rd(DIB3000MB_REG_BER_MSB) << 16) | rd(DIB3000MB_REG_BER_LSB));
  566. return 0;
  567. }
  568. /* see dib3000-watch dvb-apps for exact calcuations of signal_strength and snr */
  569. static int dib3000mb_read_signal_strength(struct dvb_frontend* fe, u16 *strength)
  570. {
  571. struct dib3000_state* state = fe->demodulator_priv;
  572. *strength = rd(DIB3000MB_REG_SIGNAL_POWER) * 0xffff / 0x170;
  573. return 0;
  574. }
  575. static int dib3000mb_read_snr(struct dvb_frontend* fe, u16 *snr)
  576. {
  577. struct dib3000_state* state = fe->demodulator_priv;
  578. short sigpow = rd(DIB3000MB_REG_SIGNAL_POWER);
  579. int icipow = ((rd(DIB3000MB_REG_NOISE_POWER_MSB) & 0xff) << 16) |
  580. rd(DIB3000MB_REG_NOISE_POWER_LSB);
  581. *snr = (sigpow << 8) / ((icipow > 0) ? icipow : 1);
  582. return 0;
  583. }
  584. static int dib3000mb_read_unc_blocks(struct dvb_frontend* fe, u32 *unc)
  585. {
  586. struct dib3000_state* state = fe->demodulator_priv;
  587. *unc = rd(DIB3000MB_REG_PACKET_ERROR_RATE);
  588. return 0;
  589. }
  590. static int dib3000mb_sleep(struct dvb_frontend* fe)
  591. {
  592. struct dib3000_state* state = fe->demodulator_priv;
  593. deb_info("dib3000mb is going to bed.\n");
  594. wr(DIB3000MB_REG_POWER_CONTROL, DIB3000MB_POWER_DOWN);
  595. return 0;
  596. }
  597. static int dib3000mb_fe_get_tune_settings(struct dvb_frontend* fe, struct dvb_frontend_tune_settings *tune)
  598. {
  599. tune->min_delay_ms = 800;
  600. return 0;
  601. }
  602. static int dib3000mb_fe_init_nonmobile(struct dvb_frontend* fe)
  603. {
  604. return dib3000mb_fe_init(fe, 0);
  605. }
  606. static int dib3000mb_set_frontend_and_tuner(struct dvb_frontend* fe, struct dvb_frontend_parameters *fep)
  607. {
  608. return dib3000mb_set_frontend(fe, fep, 1);
  609. }
  610. static void dib3000mb_release(struct dvb_frontend* fe)
  611. {
  612. struct dib3000_state *state = fe->demodulator_priv;
  613. kfree(state);
  614. }
  615. /* pid filter and transfer stuff */
  616. static int dib3000mb_pid_control(struct dvb_frontend *fe,int index, int pid,int onoff)
  617. {
  618. struct dib3000_state *state = fe->demodulator_priv;
  619. pid = (onoff ? pid | DIB3000_ACTIVATE_PID_FILTERING : 0);
  620. wr(index+DIB3000MB_REG_FIRST_PID,pid);
  621. return 0;
  622. }
  623. static int dib3000mb_fifo_control(struct dvb_frontend *fe, int onoff)
  624. {
  625. struct dib3000_state *state = fe->demodulator_priv;
  626. deb_xfer("%s fifo\n",onoff ? "enabling" : "disabling");
  627. if (onoff) {
  628. wr(DIB3000MB_REG_FIFO, DIB3000MB_FIFO_ACTIVATE);
  629. } else {
  630. wr(DIB3000MB_REG_FIFO, DIB3000MB_FIFO_INHIBIT);
  631. }
  632. return 0;
  633. }
  634. static int dib3000mb_pid_parse(struct dvb_frontend *fe, int onoff)
  635. {
  636. struct dib3000_state *state = fe->demodulator_priv;
  637. deb_xfer("%s pid parsing\n",onoff ? "enabling" : "disabling");
  638. wr(DIB3000MB_REG_PID_PARSE,onoff);
  639. return 0;
  640. }
  641. static int dib3000mb_tuner_pass_ctrl(struct dvb_frontend *fe, int onoff, u8 pll_addr)
  642. {
  643. struct dib3000_state *state = fe->demodulator_priv;
  644. if (onoff) {
  645. wr(DIB3000MB_REG_TUNER, DIB3000_TUNER_WRITE_ENABLE(pll_addr));
  646. } else {
  647. wr(DIB3000MB_REG_TUNER, DIB3000_TUNER_WRITE_DISABLE(pll_addr));
  648. }
  649. return 0;
  650. }
  651. static struct dvb_frontend_ops dib3000mb_ops;
  652. struct dvb_frontend* dib3000mb_attach(const struct dib3000_config* config,
  653. struct i2c_adapter* i2c, struct dib_fe_xfer_ops *xfer_ops)
  654. {
  655. struct dib3000_state* state = NULL;
  656. /* allocate memory for the internal state */
  657. state = kzalloc(sizeof(struct dib3000_state), GFP_KERNEL);
  658. if (state == NULL)
  659. goto error;
  660. /* setup the state */
  661. state->i2c = i2c;
  662. memcpy(&state->config,config,sizeof(struct dib3000_config));
  663. /* check for the correct demod */
  664. if (rd(DIB3000_REG_MANUFACTOR_ID) != DIB3000_I2C_ID_DIBCOM)
  665. goto error;
  666. if (rd(DIB3000_REG_DEVICE_ID) != DIB3000MB_DEVICE_ID)
  667. goto error;
  668. /* create dvb_frontend */
  669. memcpy(&state->frontend.ops, &dib3000mb_ops, sizeof(struct dvb_frontend_ops));
  670. state->frontend.demodulator_priv = state;
  671. /* set the xfer operations */
  672. xfer_ops->pid_parse = dib3000mb_pid_parse;
  673. xfer_ops->fifo_ctrl = dib3000mb_fifo_control;
  674. xfer_ops->pid_ctrl = dib3000mb_pid_control;
  675. xfer_ops->tuner_pass_ctrl = dib3000mb_tuner_pass_ctrl;
  676. return &state->frontend;
  677. error:
  678. kfree(state);
  679. return NULL;
  680. }
  681. static struct dvb_frontend_ops dib3000mb_ops = {
  682. .info = {
  683. .name = "DiBcom 3000M-B DVB-T",
  684. .type = FE_OFDM,
  685. .frequency_min = 44250000,
  686. .frequency_max = 867250000,
  687. .frequency_stepsize = 62500,
  688. .caps = FE_CAN_INVERSION_AUTO |
  689. FE_CAN_FEC_1_2 | FE_CAN_FEC_2_3 | FE_CAN_FEC_3_4 |
  690. FE_CAN_FEC_5_6 | FE_CAN_FEC_7_8 | FE_CAN_FEC_AUTO |
  691. FE_CAN_QPSK | FE_CAN_QAM_16 | FE_CAN_QAM_64 | FE_CAN_QAM_AUTO |
  692. FE_CAN_TRANSMISSION_MODE_AUTO |
  693. FE_CAN_GUARD_INTERVAL_AUTO |
  694. FE_CAN_RECOVER |
  695. FE_CAN_HIERARCHY_AUTO,
  696. },
  697. .release = dib3000mb_release,
  698. .init = dib3000mb_fe_init_nonmobile,
  699. .sleep = dib3000mb_sleep,
  700. .set_frontend = dib3000mb_set_frontend_and_tuner,
  701. .get_frontend = dib3000mb_get_frontend,
  702. .get_tune_settings = dib3000mb_fe_get_tune_settings,
  703. .read_status = dib3000mb_read_status,
  704. .read_ber = dib3000mb_read_ber,
  705. .read_signal_strength = dib3000mb_read_signal_strength,
  706. .read_snr = dib3000mb_read_snr,
  707. .read_ucblocks = dib3000mb_read_unc_blocks,
  708. };
  709. MODULE_AUTHOR(DRIVER_AUTHOR);
  710. MODULE_DESCRIPTION(DRIVER_DESC);
  711. MODULE_LICENSE("GPL");
  712. EXPORT_SYMBOL(dib3000mb_attach);