dib0070.c 21 KB

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