dib0070.c 20 KB

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