dib0070.c 20 KB

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