dib0070.c 20 KB

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