dib0070.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712
  1. /*
  2. * Linux-DVB Driver for DiBcom's DiB0070 base-band RF Tuner.
  3. *
  4. * Copyright (C) 2005-9 DiBcom (http://www.dibcom.fr/)
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License as
  8. * published by the Free Software Foundation; either version 2 of the
  9. * License, or (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful, but
  12. * WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. *
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  20. *
  21. *
  22. * This code is more or less generated from another driver, please
  23. * excuse some codingstyle oddities.
  24. *
  25. */
  26. #include <linux/kernel.h>
  27. #include <linux/i2c.h>
  28. #include "dvb_frontend.h"
  29. #include "dib0070.h"
  30. #include "dibx000_common.h"
  31. static int debug;
  32. module_param(debug, int, 0644);
  33. MODULE_PARM_DESC(debug, "turn on debugging (default: 0)");
  34. #define dprintk(args...) do { \
  35. if (debug) { \
  36. printk(KERN_DEBUG "DiB0070: "); \
  37. printk(args); \
  38. printk("\n"); \
  39. } \
  40. } while (0)
  41. #define DIB0070_P1D 0x00
  42. #define DIB0070_P1F 0x01
  43. #define DIB0070_P1G 0x03
  44. #define DIB0070S_P1A 0x02
  45. enum frontend_tune_state {
  46. CT_TUNER_START = 10,
  47. CT_TUNER_STEP_0,
  48. CT_TUNER_STEP_1,
  49. CT_TUNER_STEP_2,
  50. CT_TUNER_STEP_3,
  51. CT_TUNER_STEP_4,
  52. CT_TUNER_STEP_5,
  53. CT_TUNER_STEP_6,
  54. CT_TUNER_STEP_7,
  55. CT_TUNER_STOP,
  56. };
  57. #define FE_CALLBACK_TIME_NEVER 0xffffffff
  58. struct dib0070_state {
  59. struct i2c_adapter *i2c;
  60. struct dvb_frontend *fe;
  61. const struct dib0070_config *cfg;
  62. u16 wbd_ff_offset;
  63. u8 revision;
  64. enum frontend_tune_state tune_state;
  65. u32 current_rf;
  66. /* for the captrim binary search */
  67. s8 step;
  68. u16 adc_diff;
  69. s8 captrim;
  70. s8 fcaptrim;
  71. u16 lo4;
  72. const struct dib0070_tuning *current_tune_table_index;
  73. const struct dib0070_lna_match *lna_match;
  74. u8 wbd_gain_current;
  75. u16 wbd_offset_3_3[2];
  76. };
  77. static uint16_t dib0070_read_reg(struct dib0070_state *state, u8 reg)
  78. {
  79. u8 b[2];
  80. struct i2c_msg msg[2] = {
  81. {.addr = state->cfg->i2c_address,.flags = 0,.buf = &reg,.len = 1},
  82. {.addr = state->cfg->i2c_address,.flags = I2C_M_RD,.buf = b,.len = 2},
  83. };
  84. if (i2c_transfer(state->i2c, msg, 2) != 2) {
  85. printk(KERN_WARNING "DiB0070 I2C read failed\n");
  86. return 0;
  87. }
  88. return (b[0] << 8) | b[1];
  89. }
  90. static int dib0070_write_reg(struct dib0070_state *state, u8 reg, u16 val)
  91. {
  92. u8 b[3] = { reg, val >> 8, val & 0xff };
  93. struct i2c_msg msg = {.addr = state->cfg->i2c_address,.flags = 0,.buf = b,.len = 3 };
  94. if (i2c_transfer(state->i2c, &msg, 1) != 1) {
  95. printk(KERN_WARNING "DiB0070 I2C write failed\n");
  96. return -EREMOTEIO;
  97. }
  98. return 0;
  99. }
  100. #define HARD_RESET(state) do { \
  101. state->cfg->sleep(state->fe, 0); \
  102. if (state->cfg->reset) { \
  103. state->cfg->reset(state->fe,1); msleep(10); \
  104. state->cfg->reset(state->fe,0); msleep(10); \
  105. } \
  106. } while (0)
  107. static int dib0070_set_bandwidth(struct dvb_frontend *fe, struct dvb_frontend_parameters *ch)
  108. {
  109. struct dib0070_state *state = fe->tuner_priv;
  110. u16 tmp = dib0070_read_reg(state, 0x02) & 0x3fff;
  111. if (state->fe->dtv_property_cache.bandwidth_hz / 1000 > 7000)
  112. tmp |= (0 << 14);
  113. else if (state->fe->dtv_property_cache.bandwidth_hz / 1000 > 6000)
  114. tmp |= (1 << 14);
  115. else if (state->fe->dtv_property_cache.bandwidth_hz / 1000 > 5000)
  116. tmp |= (2 << 14);
  117. else
  118. tmp |= (3 << 14);
  119. dib0070_write_reg(state, 0x02, tmp);
  120. /* sharpen the BB filter in ISDB-T to have higher immunity to adjacent channels */
  121. if (state->fe->dtv_property_cache.delivery_system == SYS_ISDBT) {
  122. u16 value = dib0070_read_reg(state, 0x17);
  123. dib0070_write_reg(state, 0x17, value & 0xfffc);
  124. tmp = dib0070_read_reg(state, 0x01) & 0x01ff;
  125. dib0070_write_reg(state, 0x01, tmp | (60 << 9));
  126. dib0070_write_reg(state, 0x17, value);
  127. }
  128. return 0;
  129. }
  130. static int dib0070_captrim(struct dib0070_state *state, enum frontend_tune_state *tune_state)
  131. {
  132. int8_t step_sign;
  133. u16 adc;
  134. int ret = 0;
  135. if (*tune_state == CT_TUNER_STEP_0) {
  136. dib0070_write_reg(state, 0x0f, 0xed10);
  137. dib0070_write_reg(state, 0x17, 0x0034);
  138. dib0070_write_reg(state, 0x18, 0x0032);
  139. state->step = state->captrim = state->fcaptrim = 64;
  140. state->adc_diff = 3000;
  141. ret = 20;
  142. *tune_state = CT_TUNER_STEP_1;
  143. } else if (*tune_state == CT_TUNER_STEP_1) {
  144. state->step /= 2;
  145. dib0070_write_reg(state, 0x14, state->lo4 | state->captrim);
  146. ret = 15;
  147. *tune_state = CT_TUNER_STEP_2;
  148. } else if (*tune_state == CT_TUNER_STEP_2) {
  149. adc = dib0070_read_reg(state, 0x19);
  150. dprintk("CAPTRIM=%hd; ADC = %hd (ADC) & %dmV", state->captrim, adc, (u32) adc * (u32) 1800 / (u32) 1024);
  151. if (adc >= 400) {
  152. adc -= 400;
  153. step_sign = -1;
  154. } else {
  155. adc = 400 - adc;
  156. step_sign = 1;
  157. }
  158. if (adc < state->adc_diff) {
  159. dprintk("CAPTRIM=%hd is closer to target (%hd/%hd)", state->captrim, adc, state->adc_diff);
  160. state->adc_diff = adc;
  161. state->fcaptrim = state->captrim;
  162. }
  163. state->captrim += (step_sign * state->step);
  164. if (state->step >= 1)
  165. *tune_state = CT_TUNER_STEP_1;
  166. else
  167. *tune_state = CT_TUNER_STEP_3;
  168. } else if (*tune_state == CT_TUNER_STEP_3) {
  169. dib0070_write_reg(state, 0x14, state->lo4 | state->fcaptrim);
  170. dib0070_write_reg(state, 0x18, 0x07ff);
  171. *tune_state = CT_TUNER_STEP_4;
  172. }
  173. return ret;
  174. }
  175. static int dib0070_set_ctrl_lo5(struct dvb_frontend *fe, u8 vco_bias_trim, u8 hf_div_trim, u8 cp_current, u8 third_order_filt)
  176. {
  177. struct dib0070_state *state = fe->tuner_priv;
  178. u16 lo5 = (third_order_filt << 14) | (0 << 13) | (1 << 12) | (3 << 9) | (cp_current << 6) | (hf_div_trim << 3) | (vco_bias_trim << 0);
  179. dprintk("CTRL_LO5: 0x%x", lo5);
  180. return dib0070_write_reg(state, 0x15, lo5);
  181. }
  182. void dib0070_ctrl_agc_filter(struct dvb_frontend *fe, u8 open)
  183. {
  184. struct dib0070_state *state = fe->tuner_priv;
  185. if (open) {
  186. dib0070_write_reg(state, 0x1b, 0xff00);
  187. dib0070_write_reg(state, 0x1a, 0x0000);
  188. } else {
  189. dib0070_write_reg(state, 0x1b, 0x4112);
  190. if (state->cfg->vga_filter != 0) {
  191. dib0070_write_reg(state, 0x1a, state->cfg->vga_filter);
  192. dprintk("vga filter register is set to %x", state->cfg->vga_filter);
  193. } else
  194. dib0070_write_reg(state, 0x1a, 0x0009);
  195. }
  196. }
  197. EXPORT_SYMBOL(dib0070_ctrl_agc_filter);
  198. struct dib0070_tuning {
  199. u32 max_freq; /* for every frequency less than or equal to that field: this information is correct */
  200. u8 switch_trim;
  201. u8 vco_band;
  202. u8 hfdiv;
  203. u8 vco_multi;
  204. u8 presc;
  205. u8 wbdmux;
  206. u16 tuner_enable;
  207. };
  208. struct dib0070_lna_match {
  209. u32 max_freq; /* for every frequency less than or equal to that field: this information is correct */
  210. u8 lna_band;
  211. };
  212. static const struct dib0070_tuning dib0070s_tuning_table[] = {
  213. {570000, 2, 1, 3, 6, 6, 2, 0x4000 | 0x0800}, /* UHF */
  214. {700000, 2, 0, 2, 4, 2, 2, 0x4000 | 0x0800},
  215. {863999, 2, 1, 2, 4, 2, 2, 0x4000 | 0x0800},
  216. {1500000, 0, 1, 1, 2, 2, 4, 0x2000 | 0x0400}, /* LBAND */
  217. {1600000, 0, 1, 1, 2, 2, 4, 0x2000 | 0x0400},
  218. {2000000, 0, 1, 1, 2, 2, 4, 0x2000 | 0x0400},
  219. {0xffffffff, 0, 0, 8, 1, 2, 1, 0x8000 | 0x1000}, /* SBAND */
  220. };
  221. static const struct dib0070_tuning dib0070_tuning_table[] = {
  222. {115000, 1, 0, 7, 24, 2, 1, 0x8000 | 0x1000}, /* FM below 92MHz cannot be tuned */
  223. {179500, 1, 0, 3, 16, 2, 1, 0x8000 | 0x1000}, /* VHF */
  224. {189999, 1, 1, 3, 16, 2, 1, 0x8000 | 0x1000},
  225. {250000, 1, 0, 6, 12, 2, 1, 0x8000 | 0x1000},
  226. {569999, 2, 1, 5, 6, 2, 2, 0x4000 | 0x0800}, /* UHF */
  227. {699999, 2, 0, 1, 4, 2, 2, 0x4000 | 0x0800},
  228. {863999, 2, 1, 1, 4, 2, 2, 0x4000 | 0x0800},
  229. {0xffffffff, 0, 1, 0, 2, 2, 4, 0x2000 | 0x0400}, /* LBAND or everything higher than UHF */
  230. };
  231. static const struct dib0070_lna_match dib0070_lna_flip_chip[] = {
  232. {180000, 0}, /* VHF */
  233. {188000, 1},
  234. {196400, 2},
  235. {250000, 3},
  236. {550000, 0}, /* UHF */
  237. {590000, 1},
  238. {666000, 3},
  239. {864000, 5},
  240. {1500000, 0}, /* LBAND or everything higher than UHF */
  241. {1600000, 1},
  242. {2000000, 3},
  243. {0xffffffff, 7},
  244. };
  245. static const struct dib0070_lna_match dib0070_lna[] = {
  246. {180000, 0}, /* VHF */
  247. {188000, 1},
  248. {196400, 2},
  249. {250000, 3},
  250. {550000, 2}, /* UHF */
  251. {650000, 3},
  252. {750000, 5},
  253. {850000, 6},
  254. {864000, 7},
  255. {1500000, 0}, /* LBAND or everything higher than UHF */
  256. {1600000, 1},
  257. {2000000, 3},
  258. {0xffffffff, 7},
  259. };
  260. #define LPF 100 // define for the loop filter 100kHz by default 16-07-06
  261. static int dib0070_tune_digital(struct dvb_frontend *fe, struct dvb_frontend_parameters *ch)
  262. {
  263. struct dib0070_state *state = fe->tuner_priv;
  264. const struct dib0070_tuning *tune;
  265. const struct dib0070_lna_match *lna_match;
  266. enum frontend_tune_state *tune_state = &state->tune_state;
  267. int ret = 10; /* 1ms is the default delay most of the time */
  268. u8 band = (u8) BAND_OF_FREQUENCY(fe->dtv_property_cache.frequency / 1000);
  269. u32 freq = fe->dtv_property_cache.frequency / 1000 + (band == BAND_VHF ? state->cfg->freq_offset_khz_vhf : state->cfg->freq_offset_khz_uhf);
  270. #ifdef CONFIG_SYS_ISDBT
  271. if (state->fe->dtv_property_cache.delivery_system == SYS_ISDBT && state->fe->dtv_property_cache.isdbt_sb_mode == 1)
  272. if (((state->fe->dtv_property_cache.isdbt_sb_segment_count % 2)
  273. && (state->fe->dtv_property_cache.isdbt_sb_segment_idx == ((state->fe->dtv_property_cache.isdbt_sb_segment_count / 2) + 1)))
  274. || (((state->fe->dtv_property_cache.isdbt_sb_segment_count % 2) == 0)
  275. && (state->fe->dtv_property_cache.isdbt_sb_segment_idx == (state->fe->dtv_property_cache.isdbt_sb_segment_count / 2)))
  276. || (((state->fe->dtv_property_cache.isdbt_sb_segment_count % 2) == 0)
  277. && (state->fe->dtv_property_cache.isdbt_sb_segment_idx == ((state->fe->dtv_property_cache.isdbt_sb_segment_count / 2) + 1))))
  278. freq += 850;
  279. #endif
  280. if (state->current_rf != freq) {
  281. switch (state->revision) {
  282. case DIB0070S_P1A:
  283. tune = dib0070s_tuning_table;
  284. lna_match = dib0070_lna;
  285. break;
  286. default:
  287. tune = dib0070_tuning_table;
  288. if (state->cfg->flip_chip)
  289. lna_match = dib0070_lna_flip_chip;
  290. else
  291. lna_match = dib0070_lna;
  292. break;
  293. }
  294. while (freq > tune->max_freq) /* find the right one */
  295. tune++;
  296. while (freq > lna_match->max_freq) /* find the right one */
  297. lna_match++;
  298. state->current_tune_table_index = tune;
  299. state->lna_match = lna_match;
  300. }
  301. if (*tune_state == CT_TUNER_START) {
  302. dprintk("Tuning for Band: %hd (%d kHz)", band, freq);
  303. if (state->current_rf != freq) {
  304. u8 REFDIV;
  305. u32 FBDiv, Rest, FREF, VCOF_kHz;
  306. u8 Den;
  307. state->current_rf = freq;
  308. state->lo4 = (state->current_tune_table_index->vco_band << 11) | (state->current_tune_table_index->hfdiv << 7);
  309. dib0070_write_reg(state, 0x17, 0x30);
  310. VCOF_kHz = state->current_tune_table_index->vco_multi * freq * 2;
  311. switch (band) {
  312. case BAND_VHF:
  313. REFDIV = (u8) ((state->cfg->clock_khz + 9999) / 10000);
  314. break;
  315. case BAND_FM:
  316. REFDIV = (u8) ((state->cfg->clock_khz) / 1000);
  317. break;
  318. default:
  319. REFDIV = (u8) (state->cfg->clock_khz / 10000);
  320. break;
  321. }
  322. FREF = state->cfg->clock_khz / REFDIV;
  323. switch (state->revision) {
  324. case DIB0070S_P1A:
  325. FBDiv = (VCOF_kHz / state->current_tune_table_index->presc / FREF);
  326. Rest = (VCOF_kHz / state->current_tune_table_index->presc) - FBDiv * FREF;
  327. break;
  328. case DIB0070_P1G:
  329. case DIB0070_P1F:
  330. default:
  331. FBDiv = (freq / (FREF / 2));
  332. Rest = 2 * freq - FBDiv * FREF;
  333. break;
  334. }
  335. if (Rest < LPF)
  336. Rest = 0;
  337. else if (Rest < 2 * LPF)
  338. Rest = 2 * LPF;
  339. else if (Rest > (FREF - LPF)) {
  340. Rest = 0;
  341. FBDiv += 1;
  342. } else if (Rest > (FREF - 2 * LPF))
  343. Rest = FREF - 2 * LPF;
  344. Rest = (Rest * 6528) / (FREF / 10);
  345. Den = 1;
  346. if (Rest > 0) {
  347. state->lo4 |= (1 << 14) | (1 << 12);
  348. Den = 255;
  349. }
  350. dib0070_write_reg(state, 0x11, (u16) FBDiv);
  351. dib0070_write_reg(state, 0x12, (Den << 8) | REFDIV);
  352. dib0070_write_reg(state, 0x13, (u16) Rest);
  353. if (state->revision == DIB0070S_P1A) {
  354. if (band == BAND_SBAND) {
  355. dib0070_set_ctrl_lo5(fe, 2, 4, 3, 0);
  356. dib0070_write_reg(state, 0x1d, 0xFFFF);
  357. } else
  358. dib0070_set_ctrl_lo5(fe, 5, 4, 3, 1);
  359. }
  360. dib0070_write_reg(state, 0x20,
  361. 0x0040 | 0x0020 | 0x0010 | 0x0008 | 0x0002 | 0x0001 | state->current_tune_table_index->tuner_enable);
  362. dprintk("REFDIV: %hd, FREF: %d", REFDIV, FREF);
  363. dprintk("FBDIV: %d, Rest: %d", FBDiv, Rest);
  364. dprintk("Num: %hd, Den: %hd, SD: %hd", (u16) Rest, Den, (state->lo4 >> 12) & 0x1);
  365. dprintk("HFDIV code: %hd", state->current_tune_table_index->hfdiv);
  366. dprintk("VCO = %hd", state->current_tune_table_index->vco_band);
  367. dprintk("VCOF: ((%hd*%d) << 1))", state->current_tune_table_index->vco_multi, freq);
  368. *tune_state = CT_TUNER_STEP_0;
  369. } else { /* we are already tuned to this frequency - the configuration is correct */
  370. ret = 50; /* wakeup time */
  371. *tune_state = CT_TUNER_STEP_5;
  372. }
  373. } else if ((*tune_state > CT_TUNER_START) && (*tune_state < CT_TUNER_STEP_4)) {
  374. ret = dib0070_captrim(state, tune_state);
  375. } else if (*tune_state == CT_TUNER_STEP_4) {
  376. const struct dib0070_wbd_gain_cfg *tmp = state->cfg->wbd_gain;
  377. if (tmp != NULL) {
  378. while (freq / 1000 > tmp->freq) /* find the right one */
  379. tmp++;
  380. dib0070_write_reg(state, 0x0f,
  381. (0 << 15) | (1 << 14) | (3 << 12) | (tmp->wbd_gain_val << 9) | (0 << 8) | (1 << 7) | (state->
  382. current_tune_table_index->
  383. wbdmux << 0));
  384. state->wbd_gain_current = tmp->wbd_gain_val;
  385. } else {
  386. dib0070_write_reg(state, 0x0f,
  387. (0 << 15) | (1 << 14) | (3 << 12) | (6 << 9) | (0 << 8) | (1 << 7) | (state->current_tune_table_index->
  388. wbdmux << 0));
  389. state->wbd_gain_current = 6;
  390. }
  391. dib0070_write_reg(state, 0x06, 0x3fff);
  392. dib0070_write_reg(state, 0x07,
  393. (state->current_tune_table_index->switch_trim << 11) | (7 << 8) | (state->lna_match->lna_band << 3) | (3 << 0));
  394. dib0070_write_reg(state, 0x08, (state->lna_match->lna_band << 10) | (3 << 7) | (127));
  395. dib0070_write_reg(state, 0x0d, 0x0d80);
  396. dib0070_write_reg(state, 0x18, 0x07ff);
  397. dib0070_write_reg(state, 0x17, 0x0033);
  398. *tune_state = CT_TUNER_STEP_5;
  399. } else if (*tune_state == CT_TUNER_STEP_5) {
  400. dib0070_set_bandwidth(fe, ch);
  401. *tune_state = CT_TUNER_STOP;
  402. } else {
  403. ret = FE_CALLBACK_TIME_NEVER; /* tuner finished, time to call again infinite */
  404. }
  405. return ret;
  406. }
  407. static int dib0070_tune(struct dvb_frontend *fe, struct dvb_frontend_parameters *p)
  408. {
  409. struct dib0070_state *state = fe->tuner_priv;
  410. uint32_t ret;
  411. state->tune_state = CT_TUNER_START;
  412. do {
  413. ret = dib0070_tune_digital(fe, p);
  414. if (ret != FE_CALLBACK_TIME_NEVER)
  415. msleep(ret / 10);
  416. else
  417. break;
  418. } while (state->tune_state != CT_TUNER_STOP);
  419. return 0;
  420. }
  421. static int dib0070_wakeup(struct dvb_frontend *fe)
  422. {
  423. struct dib0070_state *state = fe->tuner_priv;
  424. if (state->cfg->sleep)
  425. state->cfg->sleep(fe, 0);
  426. return 0;
  427. }
  428. static int dib0070_sleep(struct dvb_frontend *fe)
  429. {
  430. struct dib0070_state *state = fe->tuner_priv;
  431. if (state->cfg->sleep)
  432. state->cfg->sleep(fe, 1);
  433. return 0;
  434. }
  435. static const u16 dib0070_p1f_defaults[] = {
  436. 7, 0x02,
  437. 0x0008,
  438. 0x0000,
  439. 0x0000,
  440. 0x0000,
  441. 0x0000,
  442. 0x0002,
  443. 0x0100,
  444. 3, 0x0d,
  445. 0x0d80,
  446. 0x0001,
  447. 0x0000,
  448. 4, 0x11,
  449. 0x0000,
  450. 0x0103,
  451. 0x0000,
  452. 0x0000,
  453. 3, 0x16,
  454. 0x0004 | 0x0040,
  455. 0x0030,
  456. 0x07ff,
  457. 6, 0x1b,
  458. 0x4112,
  459. 0xff00,
  460. 0xc07f,
  461. 0x0000,
  462. 0x0180,
  463. 0x4000 | 0x0800 | 0x0040 | 0x0020 | 0x0010 | 0x0008 | 0x0002 | 0x0001,
  464. 0,
  465. };
  466. static u16 dib0070_read_wbd_offset(struct dib0070_state *state, u8 gain)
  467. {
  468. u16 tuner_en = dib0070_read_reg(state, 0x20);
  469. u16 offset;
  470. dib0070_write_reg(state, 0x18, 0x07ff);
  471. dib0070_write_reg(state, 0x20, 0x0800 | 0x4000 | 0x0040 | 0x0020 | 0x0010 | 0x0008 | 0x0002 | 0x0001);
  472. dib0070_write_reg(state, 0x0f, (1 << 14) | (2 << 12) | (gain << 9) | (1 << 8) | (1 << 7) | (0 << 0));
  473. msleep(9);
  474. offset = dib0070_read_reg(state, 0x19);
  475. dib0070_write_reg(state, 0x20, tuner_en);
  476. return offset;
  477. }
  478. static void dib0070_wbd_offset_calibration(struct dib0070_state *state)
  479. {
  480. u8 gain;
  481. for (gain = 6; gain < 8; gain++) {
  482. state->wbd_offset_3_3[gain - 6] = ((dib0070_read_wbd_offset(state, gain) * 8 * 18 / 33 + 1) / 2);
  483. dprintk("Gain: %d, WBDOffset (3.3V) = %hd", gain, state->wbd_offset_3_3[gain - 6]);
  484. }
  485. }
  486. u16 dib0070_wbd_offset(struct dvb_frontend *fe)
  487. {
  488. struct dib0070_state *state = fe->tuner_priv;
  489. const struct dib0070_wbd_gain_cfg *tmp = state->cfg->wbd_gain;
  490. u32 freq = fe->dtv_property_cache.frequency / 1000;
  491. if (tmp != NULL) {
  492. while (freq / 1000 > tmp->freq) /* find the right one */
  493. tmp++;
  494. state->wbd_gain_current = tmp->wbd_gain_val;
  495. } else
  496. state->wbd_gain_current = 6;
  497. return state->wbd_offset_3_3[state->wbd_gain_current - 6];
  498. }
  499. EXPORT_SYMBOL(dib0070_wbd_offset);
  500. #define pgm_read_word(w) (*w)
  501. static int dib0070_reset(struct dvb_frontend *fe)
  502. {
  503. struct dib0070_state *state = fe->tuner_priv;
  504. u16 l, r, *n;
  505. HARD_RESET(state);
  506. #ifndef FORCE_SBAND_TUNER
  507. if ((dib0070_read_reg(state, 0x22) >> 9) & 0x1)
  508. state->revision = (dib0070_read_reg(state, 0x1f) >> 8) & 0xff;
  509. else
  510. #else
  511. #warning forcing SBAND
  512. #endif
  513. state->revision = DIB0070S_P1A;
  514. /* P1F or not */
  515. dprintk("Revision: %x", state->revision);
  516. if (state->revision == DIB0070_P1D) {
  517. dprintk("Error: this driver is not to be used meant for P1D or earlier");
  518. return -EINVAL;
  519. }
  520. n = (u16 *) dib0070_p1f_defaults;
  521. l = pgm_read_word(n++);
  522. while (l) {
  523. r = pgm_read_word(n++);
  524. do {
  525. dib0070_write_reg(state, (u8) r, pgm_read_word(n++));
  526. r++;
  527. } while (--l);
  528. l = pgm_read_word(n++);
  529. }
  530. if (state->cfg->force_crystal_mode != 0)
  531. r = state->cfg->force_crystal_mode;
  532. else if (state->cfg->clock_khz >= 24000)
  533. r = 1;
  534. else
  535. r = 2;
  536. r |= state->cfg->osc_buffer_state << 3;
  537. dib0070_write_reg(state, 0x10, r);
  538. dib0070_write_reg(state, 0x1f, (1 << 8) | ((state->cfg->clock_pad_drive & 0xf) << 5));
  539. if (state->cfg->invert_iq) {
  540. r = dib0070_read_reg(state, 0x02) & 0xffdf;
  541. dib0070_write_reg(state, 0x02, r | (1 << 5));
  542. }
  543. if (state->revision == DIB0070S_P1A)
  544. dib0070_set_ctrl_lo5(fe, 2, 4, 3, 0);
  545. else
  546. dib0070_set_ctrl_lo5(fe, 5, 4, state->cfg->charge_pump, state->cfg->enable_third_order_filter);
  547. dib0070_write_reg(state, 0x01, (54 << 9) | 0xc8);
  548. dib0070_wbd_offset_calibration(state);
  549. return 0;
  550. }
  551. static int dib0070_release(struct dvb_frontend *fe)
  552. {
  553. kfree(fe->tuner_priv);
  554. fe->tuner_priv = NULL;
  555. return 0;
  556. }
  557. static const struct dvb_tuner_ops dib0070_ops = {
  558. .info = {
  559. .name = "DiBcom DiB0070",
  560. .frequency_min = 45000000,
  561. .frequency_max = 860000000,
  562. .frequency_step = 1000,
  563. },
  564. .release = dib0070_release,
  565. .init = dib0070_wakeup,
  566. .sleep = dib0070_sleep,
  567. .set_params = dib0070_tune,
  568. // .get_frequency = dib0070_get_frequency,
  569. // .get_bandwidth = dib0070_get_bandwidth
  570. };
  571. struct dvb_frontend *dib0070_attach(struct dvb_frontend *fe, struct i2c_adapter *i2c, struct dib0070_config *cfg)
  572. {
  573. struct dib0070_state *state = kzalloc(sizeof(struct dib0070_state), GFP_KERNEL);
  574. if (state == NULL)
  575. return NULL;
  576. state->cfg = cfg;
  577. state->i2c = i2c;
  578. state->fe = fe;
  579. fe->tuner_priv = state;
  580. if (dib0070_reset(fe) != 0)
  581. goto free_mem;
  582. printk(KERN_INFO "DiB0070: successfully identified\n");
  583. memcpy(&fe->ops.tuner_ops, &dib0070_ops, sizeof(struct dvb_tuner_ops));
  584. fe->tuner_priv = state;
  585. return fe;
  586. free_mem:
  587. kfree(state);
  588. fe->tuner_priv = NULL;
  589. return NULL;
  590. }
  591. EXPORT_SYMBOL(dib0070_attach);
  592. MODULE_AUTHOR("Patrick Boettcher <pboettcher@dibcom.fr>");
  593. MODULE_DESCRIPTION("Driver for the DiBcom 0070 base-band RF Tuner");
  594. MODULE_LICENSE("GPL");