dib7000m.c 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470
  1. /*
  2. * Linux-DVB Driver for DiBcom's DiB7000M and
  3. * first generation DiB7000P-demodulator-family.
  4. *
  5. * Copyright (C) 2005-7 DiBcom (http://www.dibcom.fr/)
  6. *
  7. * This program is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU General Public License as
  9. * published by the Free Software Foundation, version 2.
  10. */
  11. #include <linux/kernel.h>
  12. #include <linux/slab.h>
  13. #include <linux/i2c.h>
  14. #include <linux/mutex.h>
  15. #include "dvb_frontend.h"
  16. #include "dib7000m.h"
  17. static int debug;
  18. module_param(debug, int, 0644);
  19. MODULE_PARM_DESC(debug, "turn on debugging (default: 0)");
  20. #define dprintk(args...) do { if (debug) { printk(KERN_DEBUG "DiB7000M: "); printk(args); printk("\n"); } } while (0)
  21. struct dib7000m_state {
  22. struct dvb_frontend demod;
  23. struct dib7000m_config cfg;
  24. u8 i2c_addr;
  25. struct i2c_adapter *i2c_adap;
  26. struct dibx000_i2c_master i2c_master;
  27. /* offset is 1 in case of the 7000MC */
  28. u8 reg_offs;
  29. u16 wbd_ref;
  30. u8 current_band;
  31. u32 current_bandwidth;
  32. struct dibx000_agc_config *current_agc;
  33. u32 timf;
  34. u32 timf_default;
  35. u32 internal_clk;
  36. u8 div_force_off : 1;
  37. u8 div_state : 1;
  38. u16 div_sync_wait;
  39. u16 revision;
  40. u8 agc_state;
  41. /* for the I2C transfer */
  42. struct i2c_msg msg[2];
  43. u8 i2c_write_buffer[4];
  44. u8 i2c_read_buffer[2];
  45. struct mutex i2c_buffer_lock;
  46. };
  47. enum dib7000m_power_mode {
  48. DIB7000M_POWER_ALL = 0,
  49. DIB7000M_POWER_NO,
  50. DIB7000M_POWER_INTERF_ANALOG_AGC,
  51. DIB7000M_POWER_COR4_DINTLV_ICIRM_EQUAL_CFROD,
  52. DIB7000M_POWER_COR4_CRY_ESRAM_MOUT_NUD,
  53. DIB7000M_POWER_INTERFACE_ONLY,
  54. };
  55. static u16 dib7000m_read_word(struct dib7000m_state *state, u16 reg)
  56. {
  57. u16 ret;
  58. if (mutex_lock_interruptible(&state->i2c_buffer_lock) < 0) {
  59. dprintk("could not acquire lock");
  60. return 0;
  61. }
  62. state->i2c_write_buffer[0] = (reg >> 8) | 0x80;
  63. state->i2c_write_buffer[1] = reg & 0xff;
  64. memset(state->msg, 0, 2 * sizeof(struct i2c_msg));
  65. state->msg[0].addr = state->i2c_addr >> 1;
  66. state->msg[0].flags = 0;
  67. state->msg[0].buf = state->i2c_write_buffer;
  68. state->msg[0].len = 2;
  69. state->msg[1].addr = state->i2c_addr >> 1;
  70. state->msg[1].flags = I2C_M_RD;
  71. state->msg[1].buf = state->i2c_read_buffer;
  72. state->msg[1].len = 2;
  73. if (i2c_transfer(state->i2c_adap, state->msg, 2) != 2)
  74. dprintk("i2c read error on %d",reg);
  75. ret = (state->i2c_read_buffer[0] << 8) | state->i2c_read_buffer[1];
  76. mutex_unlock(&state->i2c_buffer_lock);
  77. return ret;
  78. }
  79. static int dib7000m_write_word(struct dib7000m_state *state, u16 reg, u16 val)
  80. {
  81. int ret;
  82. if (mutex_lock_interruptible(&state->i2c_buffer_lock) < 0) {
  83. dprintk("could not acquire lock");
  84. return -EINVAL;
  85. }
  86. state->i2c_write_buffer[0] = (reg >> 8) & 0xff;
  87. state->i2c_write_buffer[1] = reg & 0xff;
  88. state->i2c_write_buffer[2] = (val >> 8) & 0xff;
  89. state->i2c_write_buffer[3] = val & 0xff;
  90. memset(&state->msg[0], 0, sizeof(struct i2c_msg));
  91. state->msg[0].addr = state->i2c_addr >> 1;
  92. state->msg[0].flags = 0;
  93. state->msg[0].buf = state->i2c_write_buffer;
  94. state->msg[0].len = 4;
  95. ret = (i2c_transfer(state->i2c_adap, state->msg, 1) != 1 ?
  96. -EREMOTEIO : 0);
  97. mutex_unlock(&state->i2c_buffer_lock);
  98. return ret;
  99. }
  100. static void dib7000m_write_tab(struct dib7000m_state *state, u16 *buf)
  101. {
  102. u16 l = 0, r, *n;
  103. n = buf;
  104. l = *n++;
  105. while (l) {
  106. r = *n++;
  107. if (state->reg_offs && (r >= 112 && r <= 331)) // compensate for 7000MC
  108. r++;
  109. do {
  110. dib7000m_write_word(state, r, *n++);
  111. r++;
  112. } while (--l);
  113. l = *n++;
  114. }
  115. }
  116. static int dib7000m_set_output_mode(struct dib7000m_state *state, int mode)
  117. {
  118. int ret = 0;
  119. u16 outreg, fifo_threshold, smo_mode,
  120. sram = 0x0005; /* by default SRAM output is disabled */
  121. outreg = 0;
  122. fifo_threshold = 1792;
  123. smo_mode = (dib7000m_read_word(state, 294 + state->reg_offs) & 0x0010) | (1 << 1);
  124. dprintk( "setting output mode for demod %p to %d", &state->demod, mode);
  125. switch (mode) {
  126. case OUTMODE_MPEG2_PAR_GATED_CLK: // STBs with parallel gated clock
  127. outreg = (1 << 10); /* 0x0400 */
  128. break;
  129. case OUTMODE_MPEG2_PAR_CONT_CLK: // STBs with parallel continues clock
  130. outreg = (1 << 10) | (1 << 6); /* 0x0440 */
  131. break;
  132. case OUTMODE_MPEG2_SERIAL: // STBs with serial input
  133. outreg = (1 << 10) | (2 << 6) | (0 << 1); /* 0x0482 */
  134. break;
  135. case OUTMODE_DIVERSITY:
  136. if (state->cfg.hostbus_diversity)
  137. outreg = (1 << 10) | (4 << 6); /* 0x0500 */
  138. else
  139. sram |= 0x0c00;
  140. break;
  141. case OUTMODE_MPEG2_FIFO: // e.g. USB feeding
  142. smo_mode |= (3 << 1);
  143. fifo_threshold = 512;
  144. outreg = (1 << 10) | (5 << 6);
  145. break;
  146. case OUTMODE_HIGH_Z: // disable
  147. outreg = 0;
  148. break;
  149. default:
  150. dprintk( "Unhandled output_mode passed to be set for demod %p",&state->demod);
  151. break;
  152. }
  153. if (state->cfg.output_mpeg2_in_188_bytes)
  154. smo_mode |= (1 << 5) ;
  155. ret |= dib7000m_write_word(state, 294 + state->reg_offs, smo_mode);
  156. ret |= dib7000m_write_word(state, 295 + state->reg_offs, fifo_threshold); /* synchronous fread */
  157. ret |= dib7000m_write_word(state, 1795, outreg);
  158. ret |= dib7000m_write_word(state, 1805, sram);
  159. if (state->revision == 0x4003) {
  160. u16 clk_cfg1 = dib7000m_read_word(state, 909) & 0xfffd;
  161. if (mode == OUTMODE_DIVERSITY)
  162. clk_cfg1 |= (1 << 1); // P_O_CLK_en
  163. dib7000m_write_word(state, 909, clk_cfg1);
  164. }
  165. return ret;
  166. }
  167. static void dib7000m_set_power_mode(struct dib7000m_state *state, enum dib7000m_power_mode mode)
  168. {
  169. /* by default everything is going to be powered off */
  170. u16 reg_903 = 0xffff, reg_904 = 0xffff, reg_905 = 0xffff, reg_906 = 0x3fff;
  171. u8 offset = 0;
  172. /* now, depending on the requested mode, we power on */
  173. switch (mode) {
  174. /* power up everything in the demod */
  175. case DIB7000M_POWER_ALL:
  176. reg_903 = 0x0000; reg_904 = 0x0000; reg_905 = 0x0000; reg_906 = 0x0000;
  177. break;
  178. /* just leave power on the control-interfaces: GPIO and (I2C or SDIO or SRAM) */
  179. case DIB7000M_POWER_INTERFACE_ONLY: /* TODO power up either SDIO or I2C or SRAM */
  180. reg_905 &= ~((1 << 7) | (1 << 6) | (1 << 5) | (1 << 2));
  181. break;
  182. case DIB7000M_POWER_INTERF_ANALOG_AGC:
  183. reg_903 &= ~((1 << 15) | (1 << 14) | (1 << 11) | (1 << 10));
  184. reg_905 &= ~((1 << 7) | (1 << 6) | (1 << 5) | (1 << 4) | (1 << 2));
  185. reg_906 &= ~((1 << 0));
  186. break;
  187. case DIB7000M_POWER_COR4_DINTLV_ICIRM_EQUAL_CFROD:
  188. reg_903 = 0x0000; reg_904 = 0x801f; reg_905 = 0x0000; reg_906 = 0x0000;
  189. break;
  190. case DIB7000M_POWER_COR4_CRY_ESRAM_MOUT_NUD:
  191. reg_903 = 0x0000; reg_904 = 0x8000; reg_905 = 0x010b; reg_906 = 0x0000;
  192. break;
  193. case DIB7000M_POWER_NO:
  194. break;
  195. }
  196. /* always power down unused parts */
  197. if (!state->cfg.mobile_mode)
  198. reg_904 |= (1 << 7) | (1 << 6) | (1 << 4) | (1 << 2) | (1 << 1);
  199. /* P_sdio_select_clk = 0 on MC and after*/
  200. if (state->revision != 0x4000)
  201. reg_906 <<= 1;
  202. if (state->revision == 0x4003)
  203. offset = 1;
  204. dib7000m_write_word(state, 903 + offset, reg_903);
  205. dib7000m_write_word(state, 904 + offset, reg_904);
  206. dib7000m_write_word(state, 905 + offset, reg_905);
  207. dib7000m_write_word(state, 906 + offset, reg_906);
  208. }
  209. static int dib7000m_set_adc_state(struct dib7000m_state *state, enum dibx000_adc_states no)
  210. {
  211. int ret = 0;
  212. u16 reg_913 = dib7000m_read_word(state, 913),
  213. reg_914 = dib7000m_read_word(state, 914);
  214. switch (no) {
  215. case DIBX000_SLOW_ADC_ON:
  216. reg_914 |= (1 << 1) | (1 << 0);
  217. ret |= dib7000m_write_word(state, 914, reg_914);
  218. reg_914 &= ~(1 << 1);
  219. break;
  220. case DIBX000_SLOW_ADC_OFF:
  221. reg_914 |= (1 << 1) | (1 << 0);
  222. break;
  223. case DIBX000_ADC_ON:
  224. if (state->revision == 0x4000) { // workaround for PA/MA
  225. // power-up ADC
  226. dib7000m_write_word(state, 913, 0);
  227. dib7000m_write_word(state, 914, reg_914 & 0x3);
  228. // power-down bandgag
  229. dib7000m_write_word(state, 913, (1 << 15));
  230. dib7000m_write_word(state, 914, reg_914 & 0x3);
  231. }
  232. reg_913 &= 0x0fff;
  233. reg_914 &= 0x0003;
  234. break;
  235. case DIBX000_ADC_OFF: // leave the VBG voltage on
  236. reg_913 |= (1 << 14) | (1 << 13) | (1 << 12);
  237. reg_914 |= (1 << 5) | (1 << 4) | (1 << 3) | (1 << 2);
  238. break;
  239. case DIBX000_VBG_ENABLE:
  240. reg_913 &= ~(1 << 15);
  241. break;
  242. case DIBX000_VBG_DISABLE:
  243. reg_913 |= (1 << 15);
  244. break;
  245. default:
  246. break;
  247. }
  248. // dprintk( "913: %x, 914: %x", reg_913, reg_914);
  249. ret |= dib7000m_write_word(state, 913, reg_913);
  250. ret |= dib7000m_write_word(state, 914, reg_914);
  251. return ret;
  252. }
  253. static int dib7000m_set_bandwidth(struct dib7000m_state *state, u32 bw)
  254. {
  255. u32 timf;
  256. if (!bw)
  257. bw = 8000;
  258. // store the current bandwidth for later use
  259. state->current_bandwidth = bw;
  260. if (state->timf == 0) {
  261. dprintk( "using default timf");
  262. timf = state->timf_default;
  263. } else {
  264. dprintk( "using updated timf");
  265. timf = state->timf;
  266. }
  267. timf = timf * (bw / 50) / 160;
  268. dib7000m_write_word(state, 23, (u16) ((timf >> 16) & 0xffff));
  269. dib7000m_write_word(state, 24, (u16) ((timf ) & 0xffff));
  270. return 0;
  271. }
  272. static int dib7000m_set_diversity_in(struct dvb_frontend *demod, int onoff)
  273. {
  274. struct dib7000m_state *state = demod->demodulator_priv;
  275. if (state->div_force_off) {
  276. dprintk( "diversity combination deactivated - forced by COFDM parameters");
  277. onoff = 0;
  278. }
  279. state->div_state = (u8)onoff;
  280. if (onoff) {
  281. dib7000m_write_word(state, 263 + state->reg_offs, 6);
  282. dib7000m_write_word(state, 264 + state->reg_offs, 6);
  283. dib7000m_write_word(state, 266 + state->reg_offs, (state->div_sync_wait << 4) | (1 << 2) | (2 << 0));
  284. } else {
  285. dib7000m_write_word(state, 263 + state->reg_offs, 1);
  286. dib7000m_write_word(state, 264 + state->reg_offs, 0);
  287. dib7000m_write_word(state, 266 + state->reg_offs, 0);
  288. }
  289. return 0;
  290. }
  291. static int dib7000m_sad_calib(struct dib7000m_state *state)
  292. {
  293. /* internal */
  294. // dib7000m_write_word(state, 928, (3 << 14) | (1 << 12) | (524 << 0)); // sampling clock of the SAD is writting in set_bandwidth
  295. dib7000m_write_word(state, 929, (0 << 1) | (0 << 0));
  296. dib7000m_write_word(state, 930, 776); // 0.625*3.3 / 4096
  297. /* do the calibration */
  298. dib7000m_write_word(state, 929, (1 << 0));
  299. dib7000m_write_word(state, 929, (0 << 0));
  300. msleep(1);
  301. return 0;
  302. }
  303. static void dib7000m_reset_pll_common(struct dib7000m_state *state, const struct dibx000_bandwidth_config *bw)
  304. {
  305. dib7000m_write_word(state, 18, (u16) (((bw->internal*1000) >> 16) & 0xffff));
  306. dib7000m_write_word(state, 19, (u16) ( (bw->internal*1000) & 0xffff));
  307. dib7000m_write_word(state, 21, (u16) ( (bw->ifreq >> 16) & 0xffff));
  308. dib7000m_write_word(state, 22, (u16) ( bw->ifreq & 0xffff));
  309. dib7000m_write_word(state, 928, bw->sad_cfg);
  310. }
  311. static void dib7000m_reset_pll(struct dib7000m_state *state)
  312. {
  313. const struct dibx000_bandwidth_config *bw = state->cfg.bw;
  314. u16 reg_907,reg_910;
  315. /* default */
  316. reg_907 = (bw->pll_bypass << 15) | (bw->modulo << 7) |
  317. (bw->ADClkSrc << 6) | (bw->IO_CLK_en_core << 5) | (bw->bypclk_div << 2) |
  318. (bw->enable_refdiv << 1) | (0 << 0);
  319. reg_910 = (((bw->pll_ratio >> 6) & 0x3) << 3) | (bw->pll_range << 1) | bw->pll_reset;
  320. // for this oscillator frequency should be 30 MHz for the Master (default values in the board_parameters give that value)
  321. // this is only working only for 30 MHz crystals
  322. if (!state->cfg.quartz_direct) {
  323. reg_910 |= (1 << 5); // forcing the predivider to 1
  324. // if the previous front-end is baseband, its output frequency is 15 MHz (prev freq divided by 2)
  325. if(state->cfg.input_clk_is_div_2)
  326. reg_907 |= (16 << 9);
  327. else // otherwise the previous front-end puts out its input (default 30MHz) - no extra division necessary
  328. reg_907 |= (8 << 9);
  329. } else {
  330. reg_907 |= (bw->pll_ratio & 0x3f) << 9;
  331. reg_910 |= (bw->pll_prediv << 5);
  332. }
  333. dib7000m_write_word(state, 910, reg_910); // pll cfg
  334. dib7000m_write_word(state, 907, reg_907); // clk cfg0
  335. dib7000m_write_word(state, 908, 0x0006); // clk_cfg1
  336. dib7000m_reset_pll_common(state, bw);
  337. }
  338. static void dib7000mc_reset_pll(struct dib7000m_state *state)
  339. {
  340. const struct dibx000_bandwidth_config *bw = state->cfg.bw;
  341. u16 clk_cfg1;
  342. // clk_cfg0
  343. dib7000m_write_word(state, 907, (bw->pll_prediv << 8) | (bw->pll_ratio << 0));
  344. // clk_cfg1
  345. //dib7000m_write_word(state, 908, (1 << 14) | (3 << 12) |(0 << 11) |
  346. clk_cfg1 = (0 << 14) | (3 << 12) |(0 << 11) |
  347. (bw->IO_CLK_en_core << 10) | (bw->bypclk_div << 5) | (bw->enable_refdiv << 4) |
  348. (1 << 3) | (bw->pll_range << 1) | (bw->pll_reset << 0);
  349. dib7000m_write_word(state, 908, clk_cfg1);
  350. clk_cfg1 = (clk_cfg1 & 0xfff7) | (bw->pll_bypass << 3);
  351. dib7000m_write_word(state, 908, clk_cfg1);
  352. // smpl_cfg
  353. dib7000m_write_word(state, 910, (1 << 12) | (2 << 10) | (bw->modulo << 8) | (bw->ADClkSrc << 7));
  354. dib7000m_reset_pll_common(state, bw);
  355. }
  356. static int dib7000m_reset_gpio(struct dib7000m_state *st)
  357. {
  358. /* reset the GPIOs */
  359. dib7000m_write_word(st, 773, st->cfg.gpio_dir);
  360. dib7000m_write_word(st, 774, st->cfg.gpio_val);
  361. /* TODO 782 is P_gpio_od */
  362. dib7000m_write_word(st, 775, st->cfg.gpio_pwm_pos);
  363. dib7000m_write_word(st, 780, st->cfg.pwm_freq_div);
  364. return 0;
  365. }
  366. static u16 dib7000m_defaults_common[] =
  367. {
  368. // auto search configuration
  369. 3, 2,
  370. 0x0004,
  371. 0x1000,
  372. 0x0814,
  373. 12, 6,
  374. 0x001b,
  375. 0x7740,
  376. 0x005b,
  377. 0x8d80,
  378. 0x01c9,
  379. 0xc380,
  380. 0x0000,
  381. 0x0080,
  382. 0x0000,
  383. 0x0090,
  384. 0x0001,
  385. 0xd4c0,
  386. 1, 26,
  387. 0x6680, // P_corm_thres Lock algorithms configuration
  388. 1, 170,
  389. 0x0410, // P_palf_alpha_regul, P_palf_filter_freeze, P_palf_filter_on
  390. 8, 173,
  391. 0,
  392. 0,
  393. 0,
  394. 0,
  395. 0,
  396. 0,
  397. 0,
  398. 0,
  399. 1, 182,
  400. 8192, // P_fft_nb_to_cut
  401. 2, 195,
  402. 0x0ccd, // P_pha3_thres
  403. 0, // P_cti_use_cpe, P_cti_use_prog
  404. 1, 205,
  405. 0x200f, // P_cspu_regul, P_cspu_win_cut
  406. 5, 214,
  407. 0x023d, // P_adp_regul_cnt
  408. 0x00a4, // P_adp_noise_cnt
  409. 0x00a4, // P_adp_regul_ext
  410. 0x7ff0, // P_adp_noise_ext
  411. 0x3ccc, // P_adp_fil
  412. 1, 226,
  413. 0, // P_2d_byp_ti_num
  414. 1, 255,
  415. 0x800, // P_equal_thres_wgn
  416. 1, 263,
  417. 0x0001,
  418. 1, 281,
  419. 0x0010, // P_fec_*
  420. 1, 294,
  421. 0x0062, // P_smo_mode, P_smo_rs_discard, P_smo_fifo_flush, P_smo_pid_parse, P_smo_error_discard
  422. 0
  423. };
  424. static u16 dib7000m_defaults[] =
  425. {
  426. /* set ADC level to -16 */
  427. 11, 76,
  428. (1 << 13) - 825 - 117,
  429. (1 << 13) - 837 - 117,
  430. (1 << 13) - 811 - 117,
  431. (1 << 13) - 766 - 117,
  432. (1 << 13) - 737 - 117,
  433. (1 << 13) - 693 - 117,
  434. (1 << 13) - 648 - 117,
  435. (1 << 13) - 619 - 117,
  436. (1 << 13) - 575 - 117,
  437. (1 << 13) - 531 - 117,
  438. (1 << 13) - 501 - 117,
  439. // Tuner IO bank: max drive (14mA)
  440. 1, 912,
  441. 0x2c8a,
  442. 1, 1817,
  443. 1,
  444. 0,
  445. };
  446. static int dib7000m_demod_reset(struct dib7000m_state *state)
  447. {
  448. dib7000m_set_power_mode(state, DIB7000M_POWER_ALL);
  449. /* always leave the VBG voltage on - it consumes almost nothing but takes a long time to start */
  450. dib7000m_set_adc_state(state, DIBX000_VBG_ENABLE);
  451. /* restart all parts */
  452. dib7000m_write_word(state, 898, 0xffff);
  453. dib7000m_write_word(state, 899, 0xffff);
  454. dib7000m_write_word(state, 900, 0xff0f);
  455. dib7000m_write_word(state, 901, 0xfffc);
  456. dib7000m_write_word(state, 898, 0);
  457. dib7000m_write_word(state, 899, 0);
  458. dib7000m_write_word(state, 900, 0);
  459. dib7000m_write_word(state, 901, 0);
  460. if (state->revision == 0x4000)
  461. dib7000m_reset_pll(state);
  462. else
  463. dib7000mc_reset_pll(state);
  464. if (dib7000m_reset_gpio(state) != 0)
  465. dprintk( "GPIO reset was not successful.");
  466. if (dib7000m_set_output_mode(state, OUTMODE_HIGH_Z) != 0)
  467. dprintk( "OUTPUT_MODE could not be reset.");
  468. /* unforce divstr regardless whether i2c enumeration was done or not */
  469. dib7000m_write_word(state, 1794, dib7000m_read_word(state, 1794) & ~(1 << 1) );
  470. dib7000m_set_bandwidth(state, 8000);
  471. dib7000m_set_adc_state(state, DIBX000_SLOW_ADC_ON);
  472. dib7000m_sad_calib(state);
  473. dib7000m_set_adc_state(state, DIBX000_SLOW_ADC_OFF);
  474. if (state->cfg.dvbt_mode)
  475. dib7000m_write_word(state, 1796, 0x0); // select DVB-T output
  476. if (state->cfg.mobile_mode)
  477. dib7000m_write_word(state, 261 + state->reg_offs, 2);
  478. else
  479. dib7000m_write_word(state, 224 + state->reg_offs, 1);
  480. // P_iqc_alpha_pha, P_iqc_alpha_amp, P_iqc_dcc_alpha, ...
  481. if(state->cfg.tuner_is_baseband)
  482. dib7000m_write_word(state, 36, 0x0755);
  483. else
  484. dib7000m_write_word(state, 36, 0x1f55);
  485. // P_divclksel=3 P_divbitsel=1
  486. if (state->revision == 0x4000)
  487. dib7000m_write_word(state, 909, (3 << 10) | (1 << 6));
  488. else
  489. dib7000m_write_word(state, 909, (3 << 4) | 1);
  490. dib7000m_write_tab(state, dib7000m_defaults_common);
  491. dib7000m_write_tab(state, dib7000m_defaults);
  492. dib7000m_set_power_mode(state, DIB7000M_POWER_INTERFACE_ONLY);
  493. state->internal_clk = state->cfg.bw->internal;
  494. return 0;
  495. }
  496. static void dib7000m_restart_agc(struct dib7000m_state *state)
  497. {
  498. // P_restart_iqc & P_restart_agc
  499. dib7000m_write_word(state, 898, 0x0c00);
  500. dib7000m_write_word(state, 898, 0x0000);
  501. }
  502. static int dib7000m_agc_soft_split(struct dib7000m_state *state)
  503. {
  504. u16 agc,split_offset;
  505. if(!state->current_agc || !state->current_agc->perform_agc_softsplit || state->current_agc->split.max == 0)
  506. return 0;
  507. // n_agc_global
  508. agc = dib7000m_read_word(state, 390);
  509. if (agc > state->current_agc->split.min_thres)
  510. split_offset = state->current_agc->split.min;
  511. else if (agc < state->current_agc->split.max_thres)
  512. split_offset = state->current_agc->split.max;
  513. else
  514. split_offset = state->current_agc->split.max *
  515. (agc - state->current_agc->split.min_thres) /
  516. (state->current_agc->split.max_thres - state->current_agc->split.min_thres);
  517. dprintk( "AGC split_offset: %d",split_offset);
  518. // P_agc_force_split and P_agc_split_offset
  519. return dib7000m_write_word(state, 103, (dib7000m_read_word(state, 103) & 0xff00) | split_offset);
  520. }
  521. static int dib7000m_update_lna(struct dib7000m_state *state)
  522. {
  523. u16 dyn_gain;
  524. if (state->cfg.update_lna) {
  525. // read dyn_gain here (because it is demod-dependent and not fe)
  526. dyn_gain = dib7000m_read_word(state, 390);
  527. if (state->cfg.update_lna(&state->demod,dyn_gain)) { // LNA has changed
  528. dib7000m_restart_agc(state);
  529. return 1;
  530. }
  531. }
  532. return 0;
  533. }
  534. static int dib7000m_set_agc_config(struct dib7000m_state *state, u8 band)
  535. {
  536. struct dibx000_agc_config *agc = NULL;
  537. int i;
  538. if (state->current_band == band && state->current_agc != NULL)
  539. return 0;
  540. state->current_band = band;
  541. for (i = 0; i < state->cfg.agc_config_count; i++)
  542. if (state->cfg.agc[i].band_caps & band) {
  543. agc = &state->cfg.agc[i];
  544. break;
  545. }
  546. if (agc == NULL) {
  547. dprintk( "no valid AGC configuration found for band 0x%02x",band);
  548. return -EINVAL;
  549. }
  550. state->current_agc = agc;
  551. /* AGC */
  552. dib7000m_write_word(state, 72 , agc->setup);
  553. dib7000m_write_word(state, 73 , agc->inv_gain);
  554. dib7000m_write_word(state, 74 , agc->time_stabiliz);
  555. dib7000m_write_word(state, 97 , (agc->alpha_level << 12) | agc->thlock);
  556. // Demod AGC loop configuration
  557. dib7000m_write_word(state, 98, (agc->alpha_mant << 5) | agc->alpha_exp);
  558. dib7000m_write_word(state, 99, (agc->beta_mant << 6) | agc->beta_exp);
  559. dprintk( "WBD: ref: %d, sel: %d, active: %d, alpha: %d",
  560. state->wbd_ref != 0 ? state->wbd_ref : agc->wbd_ref, agc->wbd_sel, !agc->perform_agc_softsplit, agc->wbd_sel);
  561. /* AGC continued */
  562. if (state->wbd_ref != 0)
  563. dib7000m_write_word(state, 102, state->wbd_ref);
  564. else // use default
  565. dib7000m_write_word(state, 102, agc->wbd_ref);
  566. dib7000m_write_word(state, 103, (agc->wbd_alpha << 9) | (agc->perform_agc_softsplit << 8) );
  567. dib7000m_write_word(state, 104, agc->agc1_max);
  568. dib7000m_write_word(state, 105, agc->agc1_min);
  569. dib7000m_write_word(state, 106, agc->agc2_max);
  570. dib7000m_write_word(state, 107, agc->agc2_min);
  571. dib7000m_write_word(state, 108, (agc->agc1_pt1 << 8) | agc->agc1_pt2 );
  572. dib7000m_write_word(state, 109, (agc->agc1_slope1 << 8) | agc->agc1_slope2);
  573. dib7000m_write_word(state, 110, (agc->agc2_pt1 << 8) | agc->agc2_pt2);
  574. dib7000m_write_word(state, 111, (agc->agc2_slope1 << 8) | agc->agc2_slope2);
  575. if (state->revision > 0x4000) { // settings for the MC
  576. dib7000m_write_word(state, 71, agc->agc1_pt3);
  577. // dprintk( "929: %x %d %d",
  578. // (dib7000m_read_word(state, 929) & 0xffe3) | (agc->wbd_inv << 4) | (agc->wbd_sel << 2), agc->wbd_inv, agc->wbd_sel);
  579. dib7000m_write_word(state, 929, (dib7000m_read_word(state, 929) & 0xffe3) | (agc->wbd_inv << 4) | (agc->wbd_sel << 2));
  580. } else {
  581. // wrong default values
  582. u16 b[9] = { 676, 696, 717, 737, 758, 778, 799, 819, 840 };
  583. for (i = 0; i < 9; i++)
  584. dib7000m_write_word(state, 88 + i, b[i]);
  585. }
  586. return 0;
  587. }
  588. static void dib7000m_update_timf(struct dib7000m_state *state)
  589. {
  590. u32 timf = (dib7000m_read_word(state, 436) << 16) | dib7000m_read_word(state, 437);
  591. state->timf = timf * 160 / (state->current_bandwidth / 50);
  592. dib7000m_write_word(state, 23, (u16) (timf >> 16));
  593. dib7000m_write_word(state, 24, (u16) (timf & 0xffff));
  594. dprintk( "updated timf_frequency: %d (default: %d)",state->timf, state->timf_default);
  595. }
  596. static int dib7000m_agc_startup(struct dvb_frontend *demod)
  597. {
  598. struct dtv_frontend_properties *ch = &demod->dtv_property_cache;
  599. struct dib7000m_state *state = demod->demodulator_priv;
  600. u16 cfg_72 = dib7000m_read_word(state, 72);
  601. int ret = -1;
  602. u8 *agc_state = &state->agc_state;
  603. u8 agc_split;
  604. switch (state->agc_state) {
  605. case 0:
  606. // set power-up level: interf+analog+AGC
  607. dib7000m_set_power_mode(state, DIB7000M_POWER_INTERF_ANALOG_AGC);
  608. dib7000m_set_adc_state(state, DIBX000_ADC_ON);
  609. if (dib7000m_set_agc_config(state, BAND_OF_FREQUENCY(ch->frequency/1000)) != 0)
  610. return -1;
  611. ret = 7; /* ADC power up */
  612. (*agc_state)++;
  613. break;
  614. case 1:
  615. /* AGC initialization */
  616. if (state->cfg.agc_control)
  617. state->cfg.agc_control(&state->demod, 1);
  618. dib7000m_write_word(state, 75, 32768);
  619. if (!state->current_agc->perform_agc_softsplit) {
  620. /* we are using the wbd - so slow AGC startup */
  621. dib7000m_write_word(state, 103, 1 << 8); /* force 0 split on WBD and restart AGC */
  622. (*agc_state)++;
  623. ret = 5;
  624. } else {
  625. /* default AGC startup */
  626. (*agc_state) = 4;
  627. /* wait AGC rough lock time */
  628. ret = 7;
  629. }
  630. dib7000m_restart_agc(state);
  631. break;
  632. case 2: /* fast split search path after 5sec */
  633. dib7000m_write_word(state, 72, cfg_72 | (1 << 4)); /* freeze AGC loop */
  634. dib7000m_write_word(state, 103, 2 << 9); /* fast split search 0.25kHz */
  635. (*agc_state)++;
  636. ret = 14;
  637. break;
  638. case 3: /* split search ended */
  639. agc_split = (u8)dib7000m_read_word(state, 392); /* store the split value for the next time */
  640. dib7000m_write_word(state, 75, dib7000m_read_word(state, 390)); /* set AGC gain start value */
  641. dib7000m_write_word(state, 72, cfg_72 & ~(1 << 4)); /* std AGC loop */
  642. dib7000m_write_word(state, 103, (state->current_agc->wbd_alpha << 9) | agc_split); /* standard split search */
  643. dib7000m_restart_agc(state);
  644. dprintk( "SPLIT %p: %hd", demod, agc_split);
  645. (*agc_state)++;
  646. ret = 5;
  647. break;
  648. case 4: /* LNA startup */
  649. /* wait AGC accurate lock time */
  650. ret = 7;
  651. if (dib7000m_update_lna(state))
  652. // wait only AGC rough lock time
  653. ret = 5;
  654. else
  655. (*agc_state)++;
  656. break;
  657. case 5:
  658. dib7000m_agc_soft_split(state);
  659. if (state->cfg.agc_control)
  660. state->cfg.agc_control(&state->demod, 0);
  661. (*agc_state)++;
  662. break;
  663. default:
  664. break;
  665. }
  666. return ret;
  667. }
  668. static void dib7000m_set_channel(struct dib7000m_state *state, struct dtv_frontend_properties *ch,
  669. u8 seq)
  670. {
  671. u16 value, est[4];
  672. dib7000m_set_bandwidth(state, BANDWIDTH_TO_KHZ(ch->bandwidth_hz));
  673. /* nfft, guard, qam, alpha */
  674. value = 0;
  675. switch (ch->transmission_mode) {
  676. case TRANSMISSION_MODE_2K: value |= (0 << 7); break;
  677. case TRANSMISSION_MODE_4K: value |= (2 << 7); break;
  678. default:
  679. case TRANSMISSION_MODE_8K: value |= (1 << 7); break;
  680. }
  681. switch (ch->guard_interval) {
  682. case GUARD_INTERVAL_1_32: value |= (0 << 5); break;
  683. case GUARD_INTERVAL_1_16: value |= (1 << 5); break;
  684. case GUARD_INTERVAL_1_4: value |= (3 << 5); break;
  685. default:
  686. case GUARD_INTERVAL_1_8: value |= (2 << 5); break;
  687. }
  688. switch (ch->modulation) {
  689. case QPSK: value |= (0 << 3); break;
  690. case QAM_16: value |= (1 << 3); break;
  691. default:
  692. case QAM_64: value |= (2 << 3); break;
  693. }
  694. switch (HIERARCHY_1) {
  695. case HIERARCHY_2: value |= 2; break;
  696. case HIERARCHY_4: value |= 4; break;
  697. default:
  698. case HIERARCHY_1: value |= 1; break;
  699. }
  700. dib7000m_write_word(state, 0, value);
  701. dib7000m_write_word(state, 5, (seq << 4));
  702. /* P_dintl_native, P_dintlv_inv, P_hrch, P_code_rate, P_select_hp */
  703. value = 0;
  704. if (1 != 0)
  705. value |= (1 << 6);
  706. if (ch->hierarchy == 1)
  707. value |= (1 << 4);
  708. if (1 == 1)
  709. value |= 1;
  710. switch ((ch->hierarchy == 0 || 1 == 1) ? ch->code_rate_HP : ch->code_rate_LP) {
  711. case FEC_2_3: value |= (2 << 1); break;
  712. case FEC_3_4: value |= (3 << 1); break;
  713. case FEC_5_6: value |= (5 << 1); break;
  714. case FEC_7_8: value |= (7 << 1); break;
  715. default:
  716. case FEC_1_2: value |= (1 << 1); break;
  717. }
  718. dib7000m_write_word(state, 267 + state->reg_offs, value);
  719. /* offset loop parameters */
  720. /* P_timf_alpha = 6, P_corm_alpha=6, P_corm_thres=0x80 */
  721. dib7000m_write_word(state, 26, (6 << 12) | (6 << 8) | 0x80);
  722. /* P_ctrl_inh_cor=0, P_ctrl_alpha_cor=4, P_ctrl_inh_isi=1, P_ctrl_alpha_isi=3, P_ctrl_inh_cor4=1, P_ctrl_alpha_cor4=3 */
  723. dib7000m_write_word(state, 29, (0 << 14) | (4 << 10) | (1 << 9) | (3 << 5) | (1 << 4) | (0x3));
  724. /* P_ctrl_freeze_pha_shift=0, P_ctrl_pha_off_max=3 */
  725. dib7000m_write_word(state, 32, (0 << 4) | 0x3);
  726. /* P_ctrl_sfreq_inh=0, P_ctrl_sfreq_step=5 */
  727. dib7000m_write_word(state, 33, (0 << 4) | 0x5);
  728. /* P_dvsy_sync_wait */
  729. switch (ch->transmission_mode) {
  730. case TRANSMISSION_MODE_8K: value = 256; break;
  731. case TRANSMISSION_MODE_4K: value = 128; break;
  732. case TRANSMISSION_MODE_2K:
  733. default: value = 64; break;
  734. }
  735. switch (ch->guard_interval) {
  736. case GUARD_INTERVAL_1_16: value *= 2; break;
  737. case GUARD_INTERVAL_1_8: value *= 4; break;
  738. case GUARD_INTERVAL_1_4: value *= 8; break;
  739. default:
  740. case GUARD_INTERVAL_1_32: value *= 1; break;
  741. }
  742. state->div_sync_wait = (value * 3) / 2 + 32; // add 50% SFN margin + compensate for one DVSY-fifo TODO
  743. /* deactive the possibility of diversity reception if extended interleave - not for 7000MC */
  744. /* P_dvsy_sync_mode = 0, P_dvsy_sync_enable=1, P_dvcb_comb_mode=2 */
  745. if (1 == 1 || state->revision > 0x4000)
  746. state->div_force_off = 0;
  747. else
  748. state->div_force_off = 1;
  749. dib7000m_set_diversity_in(&state->demod, state->div_state);
  750. /* channel estimation fine configuration */
  751. switch (ch->modulation) {
  752. case QAM_64:
  753. est[0] = 0x0148; /* P_adp_regul_cnt 0.04 */
  754. est[1] = 0xfff0; /* P_adp_noise_cnt -0.002 */
  755. est[2] = 0x00a4; /* P_adp_regul_ext 0.02 */
  756. est[3] = 0xfff8; /* P_adp_noise_ext -0.001 */
  757. break;
  758. case QAM_16:
  759. est[0] = 0x023d; /* P_adp_regul_cnt 0.07 */
  760. est[1] = 0xffdf; /* P_adp_noise_cnt -0.004 */
  761. est[2] = 0x00a4; /* P_adp_regul_ext 0.02 */
  762. est[3] = 0xfff0; /* P_adp_noise_ext -0.002 */
  763. break;
  764. default:
  765. est[0] = 0x099a; /* P_adp_regul_cnt 0.3 */
  766. est[1] = 0xffae; /* P_adp_noise_cnt -0.01 */
  767. est[2] = 0x0333; /* P_adp_regul_ext 0.1 */
  768. est[3] = 0xfff8; /* P_adp_noise_ext -0.002 */
  769. break;
  770. }
  771. for (value = 0; value < 4; value++)
  772. dib7000m_write_word(state, 214 + value + state->reg_offs, est[value]);
  773. // set power-up level: autosearch
  774. dib7000m_set_power_mode(state, DIB7000M_POWER_COR4_DINTLV_ICIRM_EQUAL_CFROD);
  775. }
  776. static int dib7000m_autosearch_start(struct dvb_frontend *demod)
  777. {
  778. struct dtv_frontend_properties *ch = &demod->dtv_property_cache;
  779. struct dib7000m_state *state = demod->demodulator_priv;
  780. struct dtv_frontend_properties schan;
  781. int ret = 0;
  782. u32 value, factor;
  783. schan = *ch;
  784. schan.modulation = QAM_64;
  785. schan.guard_interval = GUARD_INTERVAL_1_32;
  786. schan.transmission_mode = TRANSMISSION_MODE_8K;
  787. schan.code_rate_HP = FEC_2_3;
  788. schan.code_rate_LP = FEC_3_4;
  789. schan.hierarchy = 0;
  790. dib7000m_set_channel(state, &schan, 7);
  791. factor = BANDWIDTH_TO_KHZ(schan.bandwidth_hz);
  792. if (factor >= 5000)
  793. factor = 1;
  794. else
  795. factor = 6;
  796. // always use the setting for 8MHz here lock_time for 7,6 MHz are longer
  797. value = 30 * state->internal_clk * factor;
  798. ret |= dib7000m_write_word(state, 6, (u16) ((value >> 16) & 0xffff)); // lock0 wait time
  799. ret |= dib7000m_write_word(state, 7, (u16) (value & 0xffff)); // lock0 wait time
  800. value = 100 * state->internal_clk * factor;
  801. ret |= dib7000m_write_word(state, 8, (u16) ((value >> 16) & 0xffff)); // lock1 wait time
  802. ret |= dib7000m_write_word(state, 9, (u16) (value & 0xffff)); // lock1 wait time
  803. value = 500 * state->internal_clk * factor;
  804. ret |= dib7000m_write_word(state, 10, (u16) ((value >> 16) & 0xffff)); // lock2 wait time
  805. ret |= dib7000m_write_word(state, 11, (u16) (value & 0xffff)); // lock2 wait time
  806. // start search
  807. value = dib7000m_read_word(state, 0);
  808. ret |= dib7000m_write_word(state, 0, (u16) (value | (1 << 9)));
  809. /* clear n_irq_pending */
  810. if (state->revision == 0x4000)
  811. dib7000m_write_word(state, 1793, 0);
  812. else
  813. dib7000m_read_word(state, 537);
  814. ret |= dib7000m_write_word(state, 0, (u16) value);
  815. return ret;
  816. }
  817. static int dib7000m_autosearch_irq(struct dib7000m_state *state, u16 reg)
  818. {
  819. u16 irq_pending = dib7000m_read_word(state, reg);
  820. if (irq_pending & 0x1) { // failed
  821. dprintk( "autosearch failed");
  822. return 1;
  823. }
  824. if (irq_pending & 0x2) { // succeeded
  825. dprintk( "autosearch succeeded");
  826. return 2;
  827. }
  828. return 0; // still pending
  829. }
  830. static int dib7000m_autosearch_is_irq(struct dvb_frontend *demod)
  831. {
  832. struct dib7000m_state *state = demod->demodulator_priv;
  833. if (state->revision == 0x4000)
  834. return dib7000m_autosearch_irq(state, 1793);
  835. else
  836. return dib7000m_autosearch_irq(state, 537);
  837. }
  838. static int dib7000m_tune(struct dvb_frontend *demod)
  839. {
  840. struct dtv_frontend_properties *ch = &demod->dtv_property_cache;
  841. struct dib7000m_state *state = demod->demodulator_priv;
  842. int ret = 0;
  843. u16 value;
  844. // we are already tuned - just resuming from suspend
  845. dib7000m_set_channel(state, ch, 0);
  846. // restart demod
  847. ret |= dib7000m_write_word(state, 898, 0x4000);
  848. ret |= dib7000m_write_word(state, 898, 0x0000);
  849. msleep(45);
  850. dib7000m_set_power_mode(state, DIB7000M_POWER_COR4_CRY_ESRAM_MOUT_NUD);
  851. /* P_ctrl_inh_cor=0, P_ctrl_alpha_cor=4, P_ctrl_inh_isi=0, P_ctrl_alpha_isi=3, P_ctrl_inh_cor4=1, P_ctrl_alpha_cor4=3 */
  852. ret |= dib7000m_write_word(state, 29, (0 << 14) | (4 << 10) | (0 << 9) | (3 << 5) | (1 << 4) | (0x3));
  853. // never achieved a lock before - wait for timfreq to update
  854. if (state->timf == 0)
  855. msleep(200);
  856. //dump_reg(state);
  857. /* P_timf_alpha, P_corm_alpha=6, P_corm_thres=0x80 */
  858. value = (6 << 8) | 0x80;
  859. switch (ch->transmission_mode) {
  860. case TRANSMISSION_MODE_2K: value |= (7 << 12); break;
  861. case TRANSMISSION_MODE_4K: value |= (8 << 12); break;
  862. default:
  863. case TRANSMISSION_MODE_8K: value |= (9 << 12); break;
  864. }
  865. ret |= dib7000m_write_word(state, 26, value);
  866. /* P_ctrl_freeze_pha_shift=0, P_ctrl_pha_off_max */
  867. value = (0 << 4);
  868. switch (ch->transmission_mode) {
  869. case TRANSMISSION_MODE_2K: value |= 0x6; break;
  870. case TRANSMISSION_MODE_4K: value |= 0x7; break;
  871. default:
  872. case TRANSMISSION_MODE_8K: value |= 0x8; break;
  873. }
  874. ret |= dib7000m_write_word(state, 32, value);
  875. /* P_ctrl_sfreq_inh=0, P_ctrl_sfreq_step */
  876. value = (0 << 4);
  877. switch (ch->transmission_mode) {
  878. case TRANSMISSION_MODE_2K: value |= 0x6; break;
  879. case TRANSMISSION_MODE_4K: value |= 0x7; break;
  880. default:
  881. case TRANSMISSION_MODE_8K: value |= 0x8; break;
  882. }
  883. ret |= dib7000m_write_word(state, 33, value);
  884. // we achieved a lock - it's time to update the timf freq
  885. if ((dib7000m_read_word(state, 535) >> 6) & 0x1)
  886. dib7000m_update_timf(state);
  887. dib7000m_set_bandwidth(state, BANDWIDTH_TO_KHZ(ch->bandwidth_hz));
  888. return ret;
  889. }
  890. static int dib7000m_wakeup(struct dvb_frontend *demod)
  891. {
  892. struct dib7000m_state *state = demod->demodulator_priv;
  893. dib7000m_set_power_mode(state, DIB7000M_POWER_ALL);
  894. if (dib7000m_set_adc_state(state, DIBX000_SLOW_ADC_ON) != 0)
  895. dprintk( "could not start Slow ADC");
  896. return 0;
  897. }
  898. static int dib7000m_sleep(struct dvb_frontend *demod)
  899. {
  900. struct dib7000m_state *st = demod->demodulator_priv;
  901. dib7000m_set_output_mode(st, OUTMODE_HIGH_Z);
  902. dib7000m_set_power_mode(st, DIB7000M_POWER_INTERFACE_ONLY);
  903. return dib7000m_set_adc_state(st, DIBX000_SLOW_ADC_OFF) |
  904. dib7000m_set_adc_state(st, DIBX000_ADC_OFF);
  905. }
  906. static int dib7000m_identify(struct dib7000m_state *state)
  907. {
  908. u16 value;
  909. if ((value = dib7000m_read_word(state, 896)) != 0x01b3) {
  910. dprintk( "wrong Vendor ID (0x%x)",value);
  911. return -EREMOTEIO;
  912. }
  913. state->revision = dib7000m_read_word(state, 897);
  914. if (state->revision != 0x4000 &&
  915. state->revision != 0x4001 &&
  916. state->revision != 0x4002 &&
  917. state->revision != 0x4003) {
  918. dprintk( "wrong Device ID (0x%x)",value);
  919. return -EREMOTEIO;
  920. }
  921. /* protect this driver to be used with 7000PC */
  922. if (state->revision == 0x4000 && dib7000m_read_word(state, 769) == 0x4000) {
  923. dprintk( "this driver does not work with DiB7000PC");
  924. return -EREMOTEIO;
  925. }
  926. switch (state->revision) {
  927. case 0x4000: dprintk( "found DiB7000MA/PA/MB/PB"); break;
  928. case 0x4001: state->reg_offs = 1; dprintk( "found DiB7000HC"); break;
  929. case 0x4002: state->reg_offs = 1; dprintk( "found DiB7000MC"); break;
  930. case 0x4003: state->reg_offs = 1; dprintk( "found DiB9000"); break;
  931. }
  932. return 0;
  933. }
  934. static int dib7000m_get_frontend(struct dvb_frontend* fe)
  935. {
  936. struct dtv_frontend_properties *fep = &fe->dtv_property_cache;
  937. struct dib7000m_state *state = fe->demodulator_priv;
  938. u16 tps = dib7000m_read_word(state,480);
  939. fep->inversion = INVERSION_AUTO;
  940. fep->bandwidth_hz = BANDWIDTH_TO_HZ(state->current_bandwidth);
  941. switch ((tps >> 8) & 0x3) {
  942. case 0: fep->transmission_mode = TRANSMISSION_MODE_2K; break;
  943. case 1: fep->transmission_mode = TRANSMISSION_MODE_8K; break;
  944. /* case 2: fep->transmission_mode = TRANSMISSION_MODE_4K; break; */
  945. }
  946. switch (tps & 0x3) {
  947. case 0: fep->guard_interval = GUARD_INTERVAL_1_32; break;
  948. case 1: fep->guard_interval = GUARD_INTERVAL_1_16; break;
  949. case 2: fep->guard_interval = GUARD_INTERVAL_1_8; break;
  950. case 3: fep->guard_interval = GUARD_INTERVAL_1_4; break;
  951. }
  952. switch ((tps >> 14) & 0x3) {
  953. case 0: fep->modulation = QPSK; break;
  954. case 1: fep->modulation = QAM_16; break;
  955. case 2:
  956. default: fep->modulation = QAM_64; break;
  957. }
  958. /* as long as the frontend_param structure is fixed for hierarchical transmission I refuse to use it */
  959. /* (tps >> 13) & 0x1 == hrch is used, (tps >> 10) & 0x7 == alpha */
  960. fep->hierarchy = HIERARCHY_NONE;
  961. switch ((tps >> 5) & 0x7) {
  962. case 1: fep->code_rate_HP = FEC_1_2; break;
  963. case 2: fep->code_rate_HP = FEC_2_3; break;
  964. case 3: fep->code_rate_HP = FEC_3_4; break;
  965. case 5: fep->code_rate_HP = FEC_5_6; break;
  966. case 7:
  967. default: fep->code_rate_HP = FEC_7_8; break;
  968. }
  969. switch ((tps >> 2) & 0x7) {
  970. case 1: fep->code_rate_LP = FEC_1_2; break;
  971. case 2: fep->code_rate_LP = FEC_2_3; break;
  972. case 3: fep->code_rate_LP = FEC_3_4; break;
  973. case 5: fep->code_rate_LP = FEC_5_6; break;
  974. case 7:
  975. default: fep->code_rate_LP = FEC_7_8; break;
  976. }
  977. /* native interleaver: (dib7000m_read_word(state, 481) >> 5) & 0x1 */
  978. return 0;
  979. }
  980. static int dib7000m_set_frontend(struct dvb_frontend *fe)
  981. {
  982. struct dtv_frontend_properties *fep = &fe->dtv_property_cache;
  983. struct dib7000m_state *state = fe->demodulator_priv;
  984. int time, ret;
  985. dib7000m_set_output_mode(state, OUTMODE_HIGH_Z);
  986. dib7000m_set_bandwidth(state, BANDWIDTH_TO_KHZ(fep->bandwidth_hz));
  987. if (fe->ops.tuner_ops.set_params)
  988. fe->ops.tuner_ops.set_params(fe);
  989. /* start up the AGC */
  990. state->agc_state = 0;
  991. do {
  992. time = dib7000m_agc_startup(fe);
  993. if (time != -1)
  994. msleep(time);
  995. } while (time != -1);
  996. if (fep->transmission_mode == TRANSMISSION_MODE_AUTO ||
  997. fep->guard_interval == GUARD_INTERVAL_AUTO ||
  998. fep->modulation == QAM_AUTO ||
  999. fep->code_rate_HP == FEC_AUTO) {
  1000. int i = 800, found;
  1001. dib7000m_autosearch_start(fe);
  1002. do {
  1003. msleep(1);
  1004. found = dib7000m_autosearch_is_irq(fe);
  1005. } while (found == 0 && i--);
  1006. dprintk("autosearch returns: %d",found);
  1007. if (found == 0 || found == 1)
  1008. return 0; // no channel found
  1009. dib7000m_get_frontend(fe);
  1010. }
  1011. ret = dib7000m_tune(fe);
  1012. /* make this a config parameter */
  1013. dib7000m_set_output_mode(state, OUTMODE_MPEG2_FIFO);
  1014. return ret;
  1015. }
  1016. static int dib7000m_read_status(struct dvb_frontend *fe, enum fe_status *stat)
  1017. {
  1018. struct dib7000m_state *state = fe->demodulator_priv;
  1019. u16 lock = dib7000m_read_word(state, 535);
  1020. *stat = 0;
  1021. if (lock & 0x8000)
  1022. *stat |= FE_HAS_SIGNAL;
  1023. if (lock & 0x3000)
  1024. *stat |= FE_HAS_CARRIER;
  1025. if (lock & 0x0100)
  1026. *stat |= FE_HAS_VITERBI;
  1027. if (lock & 0x0010)
  1028. *stat |= FE_HAS_SYNC;
  1029. if (lock & 0x0008)
  1030. *stat |= FE_HAS_LOCK;
  1031. return 0;
  1032. }
  1033. static int dib7000m_read_ber(struct dvb_frontend *fe, u32 *ber)
  1034. {
  1035. struct dib7000m_state *state = fe->demodulator_priv;
  1036. *ber = (dib7000m_read_word(state, 526) << 16) | dib7000m_read_word(state, 527);
  1037. return 0;
  1038. }
  1039. static int dib7000m_read_unc_blocks(struct dvb_frontend *fe, u32 *unc)
  1040. {
  1041. struct dib7000m_state *state = fe->demodulator_priv;
  1042. *unc = dib7000m_read_word(state, 534);
  1043. return 0;
  1044. }
  1045. static int dib7000m_read_signal_strength(struct dvb_frontend *fe, u16 *strength)
  1046. {
  1047. struct dib7000m_state *state = fe->demodulator_priv;
  1048. u16 val = dib7000m_read_word(state, 390);
  1049. *strength = 65535 - val;
  1050. return 0;
  1051. }
  1052. static int dib7000m_read_snr(struct dvb_frontend* fe, u16 *snr)
  1053. {
  1054. *snr = 0x0000;
  1055. return 0;
  1056. }
  1057. static int dib7000m_fe_get_tune_settings(struct dvb_frontend* fe, struct dvb_frontend_tune_settings *tune)
  1058. {
  1059. tune->min_delay_ms = 1000;
  1060. return 0;
  1061. }
  1062. static void dib7000m_release(struct dvb_frontend *demod)
  1063. {
  1064. struct dib7000m_state *st = demod->demodulator_priv;
  1065. dibx000_exit_i2c_master(&st->i2c_master);
  1066. kfree(st);
  1067. }
  1068. struct i2c_adapter * dib7000m_get_i2c_master(struct dvb_frontend *demod, enum dibx000_i2c_interface intf, int gating)
  1069. {
  1070. struct dib7000m_state *st = demod->demodulator_priv;
  1071. return dibx000_get_i2c_adapter(&st->i2c_master, intf, gating);
  1072. }
  1073. EXPORT_SYMBOL(dib7000m_get_i2c_master);
  1074. int dib7000m_pid_filter_ctrl(struct dvb_frontend *fe, u8 onoff)
  1075. {
  1076. struct dib7000m_state *state = fe->demodulator_priv;
  1077. u16 val = dib7000m_read_word(state, 294 + state->reg_offs) & 0xffef;
  1078. val |= (onoff & 0x1) << 4;
  1079. dprintk("PID filter enabled %d", onoff);
  1080. return dib7000m_write_word(state, 294 + state->reg_offs, val);
  1081. }
  1082. EXPORT_SYMBOL(dib7000m_pid_filter_ctrl);
  1083. int dib7000m_pid_filter(struct dvb_frontend *fe, u8 id, u16 pid, u8 onoff)
  1084. {
  1085. struct dib7000m_state *state = fe->demodulator_priv;
  1086. dprintk("PID filter: index %x, PID %d, OnOff %d", id, pid, onoff);
  1087. return dib7000m_write_word(state, 300 + state->reg_offs + id,
  1088. onoff ? (1 << 13) | pid : 0);
  1089. }
  1090. EXPORT_SYMBOL(dib7000m_pid_filter);
  1091. #if 0
  1092. /* used with some prototype boards */
  1093. int dib7000m_i2c_enumeration(struct i2c_adapter *i2c, int no_of_demods,
  1094. u8 default_addr, struct dib7000m_config cfg[])
  1095. {
  1096. struct dib7000m_state st = { .i2c_adap = i2c };
  1097. int k = 0;
  1098. u8 new_addr = 0;
  1099. for (k = no_of_demods-1; k >= 0; k--) {
  1100. st.cfg = cfg[k];
  1101. /* designated i2c address */
  1102. new_addr = (0x40 + k) << 1;
  1103. st.i2c_addr = new_addr;
  1104. if (dib7000m_identify(&st) != 0) {
  1105. st.i2c_addr = default_addr;
  1106. if (dib7000m_identify(&st) != 0) {
  1107. dprintk("DiB7000M #%d: not identified", k);
  1108. return -EIO;
  1109. }
  1110. }
  1111. /* start diversity to pull_down div_str - just for i2c-enumeration */
  1112. dib7000m_set_output_mode(&st, OUTMODE_DIVERSITY);
  1113. dib7000m_write_word(&st, 1796, 0x0); // select DVB-T output
  1114. /* set new i2c address and force divstart */
  1115. dib7000m_write_word(&st, 1794, (new_addr << 2) | 0x2);
  1116. dprintk("IC %d initialized (to i2c_address 0x%x)", k, new_addr);
  1117. }
  1118. for (k = 0; k < no_of_demods; k++) {
  1119. st.cfg = cfg[k];
  1120. st.i2c_addr = (0x40 + k) << 1;
  1121. // unforce divstr
  1122. dib7000m_write_word(&st,1794, st.i2c_addr << 2);
  1123. /* deactivate div - it was just for i2c-enumeration */
  1124. dib7000m_set_output_mode(&st, OUTMODE_HIGH_Z);
  1125. }
  1126. return 0;
  1127. }
  1128. EXPORT_SYMBOL(dib7000m_i2c_enumeration);
  1129. #endif
  1130. static struct dvb_frontend_ops dib7000m_ops;
  1131. struct dvb_frontend * dib7000m_attach(struct i2c_adapter *i2c_adap, u8 i2c_addr, struct dib7000m_config *cfg)
  1132. {
  1133. struct dvb_frontend *demod;
  1134. struct dib7000m_state *st;
  1135. st = kzalloc(sizeof(struct dib7000m_state), GFP_KERNEL);
  1136. if (st == NULL)
  1137. return NULL;
  1138. memcpy(&st->cfg, cfg, sizeof(struct dib7000m_config));
  1139. st->i2c_adap = i2c_adap;
  1140. st->i2c_addr = i2c_addr;
  1141. demod = &st->demod;
  1142. demod->demodulator_priv = st;
  1143. memcpy(&st->demod.ops, &dib7000m_ops, sizeof(struct dvb_frontend_ops));
  1144. mutex_init(&st->i2c_buffer_lock);
  1145. st->timf_default = cfg->bw->timf;
  1146. if (dib7000m_identify(st) != 0)
  1147. goto error;
  1148. if (st->revision == 0x4000)
  1149. dibx000_init_i2c_master(&st->i2c_master, DIB7000, st->i2c_adap, st->i2c_addr);
  1150. else
  1151. dibx000_init_i2c_master(&st->i2c_master, DIB7000MC, st->i2c_adap, st->i2c_addr);
  1152. dib7000m_demod_reset(st);
  1153. return demod;
  1154. error:
  1155. kfree(st);
  1156. return NULL;
  1157. }
  1158. EXPORT_SYMBOL(dib7000m_attach);
  1159. static struct dvb_frontend_ops dib7000m_ops = {
  1160. .delsys = { SYS_DVBT },
  1161. .info = {
  1162. .name = "DiBcom 7000MA/MB/PA/PB/MC",
  1163. .frequency_min = 44250000,
  1164. .frequency_max = 867250000,
  1165. .frequency_stepsize = 62500,
  1166. .caps = FE_CAN_INVERSION_AUTO |
  1167. FE_CAN_FEC_1_2 | FE_CAN_FEC_2_3 | FE_CAN_FEC_3_4 |
  1168. FE_CAN_FEC_5_6 | FE_CAN_FEC_7_8 | FE_CAN_FEC_AUTO |
  1169. FE_CAN_QPSK | FE_CAN_QAM_16 | FE_CAN_QAM_64 | FE_CAN_QAM_AUTO |
  1170. FE_CAN_TRANSMISSION_MODE_AUTO |
  1171. FE_CAN_GUARD_INTERVAL_AUTO |
  1172. FE_CAN_RECOVER |
  1173. FE_CAN_HIERARCHY_AUTO,
  1174. },
  1175. .release = dib7000m_release,
  1176. .init = dib7000m_wakeup,
  1177. .sleep = dib7000m_sleep,
  1178. .set_frontend = dib7000m_set_frontend,
  1179. .get_tune_settings = dib7000m_fe_get_tune_settings,
  1180. .get_frontend = dib7000m_get_frontend,
  1181. .read_status = dib7000m_read_status,
  1182. .read_ber = dib7000m_read_ber,
  1183. .read_signal_strength = dib7000m_read_signal_strength,
  1184. .read_snr = dib7000m_read_snr,
  1185. .read_ucblocks = dib7000m_read_unc_blocks,
  1186. };
  1187. MODULE_AUTHOR("Patrick Boettcher <pboettcher@dibcom.fr>");
  1188. MODULE_DESCRIPTION("Driver for the DiBcom 7000MA/MB/PA/PB/MC COFDM demodulator");
  1189. MODULE_LICENSE("GPL");