dib0070.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698
  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 *st = fe->tuner_priv;
  110. u16 tmp = dib0070_read_reg(st, 0x02) & 0x3fff;
  111. if (fe->dtv_property_cache.bandwidth_hz/1000 > 7000)
  112. tmp |= (0 << 14);
  113. else if (fe->dtv_property_cache.bandwidth_hz/1000 > 6000)
  114. tmp |= (1 << 14);
  115. else if (fe->dtv_property_cache.bandwidth_hz/1000 > 5000)
  116. tmp |= (2 << 14);
  117. else
  118. tmp |= (3 << 14);
  119. dib0070_write_reg(st, 0x02, tmp);
  120. /* sharpen the BB filter in ISDB-T to have higher immunity to adjacent channels */
  121. if (fe->dtv_property_cache.delivery_system == SYS_ISDBT) {
  122. u16 value = dib0070_read_reg(st, 0x17);
  123. dib0070_write_reg(st, 0x17, value & 0xfffc);
  124. tmp = dib0070_read_reg(st, 0x01) & 0x01ff;
  125. dib0070_write_reg(st, 0x01, tmp | (60 << 9));
  126. dib0070_write_reg(st, 0x17, value);
  127. }
  128. return 0;
  129. }
  130. static int dib0070_captrim(struct dib0070_state *st, 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(st, 0x0f, 0xed10);
  137. dib0070_write_reg(st, 0x17, 0x0034);
  138. dib0070_write_reg(st, 0x18, 0x0032);
  139. st->step = st->captrim = st->fcaptrim = 64;
  140. st->adc_diff = 3000;
  141. ret = 20;
  142. *tune_state = CT_TUNER_STEP_1;
  143. } else if (*tune_state == CT_TUNER_STEP_1) {
  144. st->step /= 2;
  145. dib0070_write_reg(st, 0x14, st->lo4 | st->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(st, 0x19);
  150. dprintk( "CAPTRIM=%hd; ADC = %hd (ADC) & %dmV", st->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 < st->adc_diff) {
  159. dprintk( "CAPTRIM=%hd is closer to target (%hd/%hd)", st->captrim, adc, st->adc_diff);
  160. st->adc_diff = adc;
  161. st->fcaptrim = st->captrim;
  162. }
  163. st->captrim += (step_sign * st->step);
  164. if (st->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(st, 0x14, st->lo4 | st->fcaptrim);
  170. dib0070_write_reg(st, 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. struct dib0070_tuning
  183. {
  184. u32 max_freq; /* for every frequency less than or equal to that field: this information is correct */
  185. u8 switch_trim;
  186. u8 vco_band;
  187. u8 hfdiv;
  188. u8 vco_multi;
  189. u8 presc;
  190. u8 wbdmux;
  191. u16 tuner_enable;
  192. };
  193. struct dib0070_lna_match
  194. {
  195. u32 max_freq; /* for every frequency less than or equal to that field: this information is correct */
  196. u8 lna_band;
  197. };
  198. static const struct dib0070_tuning dib0070s_tuning_table[] =
  199. {
  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. {
  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. {
  221. { 180000, 0 }, /* VHF */
  222. { 188000, 1 },
  223. { 196400, 2 },
  224. { 250000, 3 },
  225. { 550000, 0 }, /* UHF */
  226. { 590000, 1 },
  227. { 666000, 3 },
  228. { 864000, 5 },
  229. { 1500000, 0 }, /* LBAND or everything higher than UHF */
  230. { 1600000, 1 },
  231. { 2000000, 3 },
  232. { 0xffffffff, 7 },
  233. };
  234. static const struct dib0070_lna_match dib0070_lna[] =
  235. {
  236. { 180000, 0 }, /* VHF */
  237. { 188000, 1 },
  238. { 196400, 2 },
  239. { 250000, 3 },
  240. { 550000, 2 }, /* UHF */
  241. { 650000, 3 },
  242. { 750000, 5 },
  243. { 850000, 6 },
  244. { 864000, 7 },
  245. { 1500000, 0 }, /* LBAND or everything higher than UHF */
  246. { 1600000, 1 },
  247. { 2000000, 3 },
  248. { 0xffffffff, 7 },
  249. };
  250. #define LPF 100 // define for the loop filter 100kHz by default 16-07-06
  251. static int dib0070_tune_digital(struct dvb_frontend *fe, struct dvb_frontend_parameters *ch)
  252. {
  253. struct dib0070_state *st = fe->tuner_priv;
  254. const struct dib0070_tuning *tune;
  255. const struct dib0070_lna_match *lna_match;
  256. enum frontend_tune_state *tune_state = &st->tune_state;
  257. int ret = 10; /* 1ms is the default delay most of the time */
  258. u8 band = (u8)BAND_OF_FREQUENCY(ch->frequency/1000);
  259. u32 freq = ch->frequency/1000 + (band == BAND_VHF ? st->cfg->freq_offset_khz_vhf : st->cfg->freq_offset_khz_uhf);
  260. #ifdef CONFIG_STANDARD_ISDBT
  261. if (fe->dtv_property_cache.delivery_system == SYS_ISDBT && ch->u.isdbt.sb_mode == 1)
  262. if ( ( (ch->u.isdbt.sb_conn_total_seg % 2) && (ch->u.isdbt.sb_wanted_seg == ((ch->u.isdbt.sb_conn_total_seg/2) + 1) ) ) ||
  263. ( ( (ch->u.isdbt.sb_conn_total_seg % 2) == 0) && (ch->u.isdbt.sb_wanted_seg == (ch->u.isdbt.sb_conn_total_seg/2) ) ) ||
  264. ( ( (ch->u.isdbt.sb_conn_total_seg % 2) == 0) && (ch->u.isdbt.sb_wanted_seg == ((ch->u.isdbt.sb_conn_total_seg/2)+1))) )
  265. freq += 850;
  266. #endif
  267. if (st->current_rf != freq) {
  268. switch (st->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 (st->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. st->current_tune_table_index = tune;
  286. st->lna_match = lna_match;
  287. }
  288. if (*tune_state == CT_TUNER_START) {
  289. dprintk( "Tuning for Band: %hd (%d kHz)", band, freq);
  290. if (st->current_rf != freq) {
  291. u8 REFDIV;
  292. u32 FBDiv, Rest, FREF, VCOF_kHz;
  293. u8 Den;
  294. st->current_rf = freq;
  295. st->lo4 = (st->current_tune_table_index->vco_band << 11) | (st->current_tune_table_index->hfdiv << 7);
  296. dib0070_write_reg(st, 0x17, 0x30);
  297. VCOF_kHz = st->current_tune_table_index->vco_multi * freq * 2;
  298. switch (band) {
  299. case BAND_VHF:
  300. REFDIV = (u8) ((st->cfg->clock_khz + 9999) / 10000);
  301. break;
  302. case BAND_FM:
  303. REFDIV = (u8) ((st->cfg->clock_khz) / 1000);
  304. break;
  305. default:
  306. REFDIV = (u8) ( st->cfg->clock_khz / 10000);
  307. break;
  308. }
  309. FREF = st->cfg->clock_khz / REFDIV;
  310. switch (st->revision) {
  311. case DIB0070S_P1A:
  312. FBDiv = (VCOF_kHz / st->current_tune_table_index->presc / FREF);
  313. Rest = (VCOF_kHz / st->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) Rest = 0;
  323. else if (Rest < 2 * LPF) Rest = 2 * LPF;
  324. else if (Rest > (FREF - LPF)) { Rest = 0 ; FBDiv += 1; }
  325. else if (Rest > (FREF - 2 * LPF)) Rest = FREF - 2 * LPF;
  326. Rest = (Rest * 6528) / (FREF / 10);
  327. Den = 1;
  328. if (Rest > 0) {
  329. st->lo4 |= (1 << 14) | (1 << 12);
  330. Den = 255;
  331. }
  332. dib0070_write_reg(st, 0x11, (u16)FBDiv);
  333. dib0070_write_reg(st, 0x12, (Den << 8) | REFDIV);
  334. dib0070_write_reg(st, 0x13, (u16) Rest);
  335. if (st->revision == DIB0070S_P1A) {
  336. if (band == BAND_SBAND) {
  337. dib0070_set_ctrl_lo5(fe, 2, 4, 3, 0);
  338. dib0070_write_reg(st, 0x1d,0xFFFF);
  339. } else
  340. dib0070_set_ctrl_lo5(fe, 5, 4, 3, 1);
  341. }
  342. dib0070_write_reg(st, 0x20, 0x0040 | 0x0020 | 0x0010 | 0x0008 | 0x0002 | 0x0001 | st->current_tune_table_index->tuner_enable);
  343. dprintk( "REFDIV: %hd, FREF: %d", REFDIV, FREF);
  344. dprintk( "FBDIV: %d, Rest: %d", FBDiv, Rest);
  345. dprintk( "Num: %hd, Den: %hd, SD: %hd",(u16) Rest, Den, (st->lo4 >> 12) & 0x1);
  346. dprintk( "HFDIV code: %hd", st->current_tune_table_index->hfdiv);
  347. dprintk( "VCO = %hd", st->current_tune_table_index->vco_band);
  348. dprintk( "VCOF: ((%hd*%d) << 1))", st->current_tune_table_index->vco_multi, freq);
  349. *tune_state = CT_TUNER_STEP_0;
  350. } else { /* we are already tuned to this frequency - the configuration is correct */
  351. ret = 50; /* wakeup time */
  352. *tune_state = CT_TUNER_STEP_5;
  353. }
  354. } else if ((*tune_state > CT_TUNER_START) && (*tune_state < CT_TUNER_STEP_4)) {
  355. ret = dib0070_captrim(st, tune_state);
  356. } else if (*tune_state == CT_TUNER_STEP_4) {
  357. const struct dib0070_wbd_gain_cfg *tmp = st->cfg->wbd_gain;
  358. if (tmp != NULL) {
  359. while (freq/1000 > tmp->freq) /* find the right one */
  360. tmp++;
  361. dib0070_write_reg(st, 0x0f, (0 << 15) | (1 << 14) | (3 << 12) | (tmp->wbd_gain_val << 9) | (0 << 8) | (1 << 7) | (st->current_tune_table_index->wbdmux << 0));
  362. st->wbd_gain_current = tmp->wbd_gain_val;
  363. } else {
  364. dib0070_write_reg(st, 0x0f, (0 << 15) | (1 << 14) | (3 << 12) | (6 << 9) | (0 << 8) | (1 << 7) | (st->current_tune_table_index->wbdmux << 0));
  365. st->wbd_gain_current = 6;
  366. }
  367. dib0070_write_reg(st, 0x06, 0x3fff);
  368. dib0070_write_reg(st, 0x07, (st->current_tune_table_index->switch_trim << 11) | (7 << 8) | (st->lna_match->lna_band << 3) | (3 << 0));
  369. dib0070_write_reg(st, 0x08, (st->lna_match->lna_band << 10) | (3 << 7) | (127));
  370. dib0070_write_reg(st, 0x0d, 0x0d80);
  371. dib0070_write_reg(st, 0x18, 0x07ff);
  372. dib0070_write_reg(st, 0x17, 0x0033);
  373. *tune_state = CT_TUNER_STEP_5;
  374. } else if (*tune_state == CT_TUNER_STEP_5) {
  375. dib0070_set_bandwidth(fe, ch);
  376. *tune_state = CT_TUNER_STOP;
  377. } else {
  378. ret = FE_CALLBACK_TIME_NEVER; /* tuner finished, time to call again infinite */
  379. }
  380. return ret;
  381. }
  382. static int dib0070_tune(struct dvb_frontend *fe, struct dvb_frontend_parameters *p)
  383. {
  384. struct dib0070_state *state = fe->tuner_priv;
  385. uint32_t ret;
  386. state->tune_state = CT_TUNER_START;
  387. do {
  388. ret = dib0070_tune_digital(fe, p);
  389. if (ret != FE_CALLBACK_TIME_NEVER)
  390. msleep(ret/10);
  391. else
  392. break;
  393. } while (state->tune_state != CT_TUNER_STOP);
  394. return 0;
  395. }
  396. static int dib0070_wakeup(struct dvb_frontend *fe)
  397. {
  398. struct dib0070_state *st = fe->tuner_priv;
  399. if (st->cfg->sleep)
  400. st->cfg->sleep(fe, 0);
  401. return 0;
  402. }
  403. static int dib0070_sleep(struct dvb_frontend *fe)
  404. {
  405. struct dib0070_state *st = fe->tuner_priv;
  406. if (st->cfg->sleep)
  407. st->cfg->sleep(fe, 1);
  408. return 0;
  409. }
  410. static const u16 dib0070_p1f_defaults[] =
  411. {
  412. 7, 0x02,
  413. 0x0008,
  414. 0x0000,
  415. 0x0000,
  416. 0x0000,
  417. 0x0000,
  418. 0x0002,
  419. 0x0100,
  420. 3, 0x0d,
  421. 0x0d80,
  422. 0x0001,
  423. 0x0000,
  424. 4, 0x11,
  425. 0x0000,
  426. 0x0103,
  427. 0x0000,
  428. 0x0000,
  429. 3, 0x16,
  430. 0x0004 | 0x0040,
  431. 0x0030,
  432. 0x07ff,
  433. 6, 0x1b,
  434. 0x4112,
  435. 0xff00,
  436. 0xc07f,
  437. 0x0000,
  438. 0x0180,
  439. 0x4000 | 0x0800 | 0x0040 | 0x0020 | 0x0010 | 0x0008 | 0x0002 | 0x0001,
  440. 0,
  441. };
  442. static u16 dib0070_read_wbd_offset(struct dib0070_state *state, u8 gain)
  443. {
  444. u16 tuner_en = dib0070_read_reg(state, 0x20);
  445. u16 offset;
  446. dib0070_write_reg(state, 0x18, 0x07ff);
  447. dib0070_write_reg(state, 0x20, 0x0800 | 0x4000 | 0x0040 | 0x0020 | 0x0010 | 0x0008 | 0x0002 | 0x0001);
  448. dib0070_write_reg(state, 0x0f, (1 << 14) | (2 << 12) | (gain << 9) | (1 << 8) | (1 << 7) | (0 << 0));
  449. msleep(9);
  450. offset = dib0070_read_reg(state, 0x19);
  451. dib0070_write_reg(state, 0x20, tuner_en);
  452. return offset;
  453. }
  454. static void dib0070_wbd_offset_calibration(struct dib0070_state *state)
  455. {
  456. u8 gain;
  457. for (gain = 6; gain < 8; gain++) {
  458. state->wbd_offset_3_3[gain - 6] = ((dib0070_read_wbd_offset(state, gain) * 8 * 18 / 33 + 1) / 2);
  459. dprintk( "Gain: %d, WBDOffset (3.3V) = %hd", gain, state->wbd_offset_3_3[gain-6]);
  460. }
  461. }
  462. u16 dib0070_wbd_offset(struct dvb_frontend *fe)
  463. {
  464. struct dib0070_state *st = fe->tuner_priv;
  465. return st->wbd_offset_3_3[st->wbd_gain_current - 6];
  466. }
  467. EXPORT_SYMBOL(dib0070_wbd_offset);
  468. #define pgm_read_word(w) (*w)
  469. static int dib0070_reset(struct dvb_frontend *fe)
  470. {
  471. struct dib0070_state *state = fe->tuner_priv;
  472. u16 l, r, *n;
  473. HARD_RESET(state);
  474. #ifndef FORCE_SBAND_TUNER
  475. if ((dib0070_read_reg(state, 0x22) >> 9) & 0x1)
  476. state->revision = (dib0070_read_reg(state, 0x1f) >> 8) & 0xff;
  477. else
  478. #else
  479. #warning forcing SBAND
  480. #endif
  481. state->revision = DIB0070S_P1A;
  482. /* P1F or not */
  483. dprintk( "Revision: %x", state->revision);
  484. if (state->revision == DIB0070_P1D) {
  485. dprintk( "Error: this driver is not to be used meant for P1D or earlier");
  486. return -EINVAL;
  487. }
  488. n = (u16 *) dib0070_p1f_defaults;
  489. l = pgm_read_word(n++);
  490. while (l) {
  491. r = pgm_read_word(n++);
  492. do {
  493. dib0070_write_reg(state, (u8)r, pgm_read_word(n++));
  494. r++;
  495. } while (--l);
  496. l = pgm_read_word(n++);
  497. }
  498. if (state->cfg->force_crystal_mode != 0)
  499. r = state->cfg->force_crystal_mode;
  500. else if (state->cfg->clock_khz >= 24000)
  501. r = 1;
  502. else
  503. r = 2;
  504. r |= state->cfg->osc_buffer_state << 3;
  505. dib0070_write_reg(state, 0x10, r);
  506. dib0070_write_reg(state, 0x1f, (1 << 8) | ((state->cfg->clock_pad_drive & 0xf) << 5));
  507. if (state->cfg->invert_iq) {
  508. r = dib0070_read_reg(state, 0x02) & 0xffdf;
  509. dib0070_write_reg(state, 0x02, r | (1 << 5));
  510. }
  511. if (state->revision == DIB0070S_P1A)
  512. dib0070_set_ctrl_lo5(fe, 2, 4, 3, 0);
  513. else
  514. dib0070_set_ctrl_lo5(fe, 5, 4, state->cfg->charge_pump, state->cfg->enable_third_order_filter);
  515. dib0070_write_reg(state, 0x01, (54 << 9) | 0xc8);
  516. dib0070_wbd_offset_calibration(state);
  517. return 0;
  518. }
  519. static int dib0070_release(struct dvb_frontend *fe)
  520. {
  521. kfree(fe->tuner_priv);
  522. fe->tuner_priv = NULL;
  523. return 0;
  524. }
  525. static const struct dvb_tuner_ops dib0070_ops = {
  526. .info = {
  527. .name = "DiBcom DiB0070",
  528. .frequency_min = 45000000,
  529. .frequency_max = 860000000,
  530. .frequency_step = 1000,
  531. },
  532. .release = dib0070_release,
  533. .init = dib0070_wakeup,
  534. .sleep = dib0070_sleep,
  535. .set_params = dib0070_tune,
  536. // .get_frequency = dib0070_get_frequency,
  537. // .get_bandwidth = dib0070_get_bandwidth
  538. };
  539. struct dvb_frontend * dib0070_attach(struct dvb_frontend *fe, struct i2c_adapter *i2c, struct dib0070_config *cfg)
  540. {
  541. struct dib0070_state *state = kzalloc(sizeof(struct dib0070_state), GFP_KERNEL);
  542. if (state == NULL)
  543. return NULL;
  544. state->cfg = cfg;
  545. state->i2c = i2c;
  546. state->fe = fe;
  547. fe->tuner_priv = state;
  548. if (dib0070_reset(fe) != 0)
  549. goto free_mem;
  550. printk(KERN_INFO "DiB0070: successfully identified\n");
  551. memcpy(&fe->ops.tuner_ops, &dib0070_ops, sizeof(struct dvb_tuner_ops));
  552. fe->tuner_priv = state;
  553. return fe;
  554. free_mem:
  555. kfree(state);
  556. fe->tuner_priv = NULL;
  557. return NULL;
  558. }
  559. EXPORT_SYMBOL(dib0070_attach);
  560. MODULE_AUTHOR("Patrick Boettcher <pboettcher@dibcom.fr>");
  561. MODULE_DESCRIPTION("Driver for the DiBcom 0070 base-band RF Tuner");
  562. MODULE_LICENSE("GPL");