dib0070.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779
  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)
  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)
  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)
  408. | (6 << 9) | (0 << 8) | (1 << 7)
  409. | (state->current_tune_table_index->wbdmux << 0));
  410. state->wbd_gain_current = 6;
  411. }
  412. dib0070_write_reg(state, 0x06, 0x3fff);
  413. dib0070_write_reg(state, 0x07,
  414. (state->current_tune_table_index->switch_trim << 11) | (7 << 8) | (state->lna_match->lna_band << 3) | (3 << 0));
  415. dib0070_write_reg(state, 0x08, (state->lna_match->lna_band << 10) | (3 << 7) | (127));
  416. dib0070_write_reg(state, 0x0d, 0x0d80);
  417. dib0070_write_reg(state, 0x18, 0x07ff);
  418. dib0070_write_reg(state, 0x17, 0x0033);
  419. *tune_state = CT_TUNER_STEP_5;
  420. } else if (*tune_state == CT_TUNER_STEP_5) {
  421. dib0070_set_bandwidth(fe);
  422. *tune_state = CT_TUNER_STOP;
  423. } else {
  424. ret = FE_CALLBACK_TIME_NEVER; /* tuner finished, time to call again infinite */
  425. }
  426. return ret;
  427. }
  428. static int dib0070_tune(struct dvb_frontend *fe)
  429. {
  430. struct dib0070_state *state = fe->tuner_priv;
  431. uint32_t ret;
  432. state->tune_state = CT_TUNER_START;
  433. do {
  434. ret = dib0070_tune_digital(fe);
  435. if (ret != FE_CALLBACK_TIME_NEVER)
  436. msleep(ret/10);
  437. else
  438. break;
  439. } while (state->tune_state != CT_TUNER_STOP);
  440. return 0;
  441. }
  442. static int dib0070_wakeup(struct dvb_frontend *fe)
  443. {
  444. struct dib0070_state *state = fe->tuner_priv;
  445. if (state->cfg->sleep)
  446. state->cfg->sleep(fe, 0);
  447. return 0;
  448. }
  449. static int dib0070_sleep(struct dvb_frontend *fe)
  450. {
  451. struct dib0070_state *state = fe->tuner_priv;
  452. if (state->cfg->sleep)
  453. state->cfg->sleep(fe, 1);
  454. return 0;
  455. }
  456. u8 dib0070_get_rf_output(struct dvb_frontend *fe)
  457. {
  458. struct dib0070_state *state = fe->tuner_priv;
  459. return (dib0070_read_reg(state, 0x07) >> 11) & 0x3;
  460. }
  461. EXPORT_SYMBOL(dib0070_get_rf_output);
  462. int dib0070_set_rf_output(struct dvb_frontend *fe, u8 no)
  463. {
  464. struct dib0070_state *state = fe->tuner_priv;
  465. u16 rxrf2 = dib0070_read_reg(state, 0x07) & 0xfe7ff;
  466. if (no > 3)
  467. no = 3;
  468. if (no < 1)
  469. no = 1;
  470. return dib0070_write_reg(state, 0x07, rxrf2 | (no << 11));
  471. }
  472. EXPORT_SYMBOL(dib0070_set_rf_output);
  473. static const u16 dib0070_p1f_defaults[] =
  474. {
  475. 7, 0x02,
  476. 0x0008,
  477. 0x0000,
  478. 0x0000,
  479. 0x0000,
  480. 0x0000,
  481. 0x0002,
  482. 0x0100,
  483. 3, 0x0d,
  484. 0x0d80,
  485. 0x0001,
  486. 0x0000,
  487. 4, 0x11,
  488. 0x0000,
  489. 0x0103,
  490. 0x0000,
  491. 0x0000,
  492. 3, 0x16,
  493. 0x0004 | 0x0040,
  494. 0x0030,
  495. 0x07ff,
  496. 6, 0x1b,
  497. 0x4112,
  498. 0xff00,
  499. 0xc07f,
  500. 0x0000,
  501. 0x0180,
  502. 0x4000 | 0x0800 | 0x0040 | 0x0020 | 0x0010 | 0x0008 | 0x0002 | 0x0001,
  503. 0,
  504. };
  505. static u16 dib0070_read_wbd_offset(struct dib0070_state *state, u8 gain)
  506. {
  507. u16 tuner_en = dib0070_read_reg(state, 0x20);
  508. u16 offset;
  509. dib0070_write_reg(state, 0x18, 0x07ff);
  510. dib0070_write_reg(state, 0x20, 0x0800 | 0x4000 | 0x0040 | 0x0020 | 0x0010 | 0x0008 | 0x0002 | 0x0001);
  511. dib0070_write_reg(state, 0x0f, (1 << 14) | (2 << 12) | (gain << 9) | (1 << 8) | (1 << 7) | (0 << 0));
  512. msleep(9);
  513. offset = dib0070_read_reg(state, 0x19);
  514. dib0070_write_reg(state, 0x20, tuner_en);
  515. return offset;
  516. }
  517. static void dib0070_wbd_offset_calibration(struct dib0070_state *state)
  518. {
  519. u8 gain;
  520. for (gain = 6; gain < 8; gain++) {
  521. state->wbd_offset_3_3[gain - 6] = ((dib0070_read_wbd_offset(state, gain) * 8 * 18 / 33 + 1) / 2);
  522. dprintk("Gain: %d, WBDOffset (3.3V) = %hd", gain, state->wbd_offset_3_3[gain-6]);
  523. }
  524. }
  525. u16 dib0070_wbd_offset(struct dvb_frontend *fe)
  526. {
  527. struct dib0070_state *state = fe->tuner_priv;
  528. const struct dib0070_wbd_gain_cfg *tmp = state->cfg->wbd_gain;
  529. u32 freq = fe->dtv_property_cache.frequency/1000;
  530. if (tmp != NULL) {
  531. while (freq/1000 > tmp->freq) /* find the right one */
  532. tmp++;
  533. state->wbd_gain_current = tmp->wbd_gain_val;
  534. } else
  535. state->wbd_gain_current = 6;
  536. return state->wbd_offset_3_3[state->wbd_gain_current - 6];
  537. }
  538. EXPORT_SYMBOL(dib0070_wbd_offset);
  539. #define pgm_read_word(w) (*w)
  540. static int dib0070_reset(struct dvb_frontend *fe)
  541. {
  542. struct dib0070_state *state = fe->tuner_priv;
  543. u16 l, r, *n;
  544. HARD_RESET(state);
  545. #ifndef FORCE_SBAND_TUNER
  546. if ((dib0070_read_reg(state, 0x22) >> 9) & 0x1)
  547. state->revision = (dib0070_read_reg(state, 0x1f) >> 8) & 0xff;
  548. else
  549. #else
  550. #warning forcing SBAND
  551. #endif
  552. state->revision = DIB0070S_P1A;
  553. /* P1F or not */
  554. dprintk("Revision: %x", state->revision);
  555. if (state->revision == DIB0070_P1D) {
  556. dprintk("Error: this driver is not to be used meant for P1D or earlier");
  557. return -EINVAL;
  558. }
  559. n = (u16 *) dib0070_p1f_defaults;
  560. l = pgm_read_word(n++);
  561. while (l) {
  562. r = pgm_read_word(n++);
  563. do {
  564. dib0070_write_reg(state, (u8)r, pgm_read_word(n++));
  565. r++;
  566. } while (--l);
  567. l = pgm_read_word(n++);
  568. }
  569. if (state->cfg->force_crystal_mode != 0)
  570. r = state->cfg->force_crystal_mode;
  571. else if (state->cfg->clock_khz >= 24000)
  572. r = 1;
  573. else
  574. r = 2;
  575. r |= state->cfg->osc_buffer_state << 3;
  576. dib0070_write_reg(state, 0x10, r);
  577. dib0070_write_reg(state, 0x1f, (1 << 8) | ((state->cfg->clock_pad_drive & 0xf) << 5));
  578. if (state->cfg->invert_iq) {
  579. r = dib0070_read_reg(state, 0x02) & 0xffdf;
  580. dib0070_write_reg(state, 0x02, r | (1 << 5));
  581. }
  582. if (state->revision == DIB0070S_P1A)
  583. dib0070_set_ctrl_lo5(fe, 2, 4, 3, 0);
  584. else
  585. dib0070_set_ctrl_lo5(fe, 5, 4, state->cfg->charge_pump,
  586. state->cfg->enable_third_order_filter);
  587. dib0070_write_reg(state, 0x01, (54 << 9) | 0xc8);
  588. dib0070_wbd_offset_calibration(state);
  589. return 0;
  590. }
  591. static int dib0070_get_frequency(struct dvb_frontend *fe, u32 *frequency)
  592. {
  593. struct dib0070_state *state = fe->tuner_priv;
  594. *frequency = 1000 * state->current_rf;
  595. return 0;
  596. }
  597. static int dib0070_release(struct dvb_frontend *fe)
  598. {
  599. kfree(fe->tuner_priv);
  600. fe->tuner_priv = NULL;
  601. return 0;
  602. }
  603. static const struct dvb_tuner_ops dib0070_ops = {
  604. .info = {
  605. .name = "DiBcom DiB0070",
  606. .frequency_min = 45000000,
  607. .frequency_max = 860000000,
  608. .frequency_step = 1000,
  609. },
  610. .release = dib0070_release,
  611. .init = dib0070_wakeup,
  612. .sleep = dib0070_sleep,
  613. .set_params = dib0070_tune,
  614. .get_frequency = dib0070_get_frequency,
  615. // .get_bandwidth = dib0070_get_bandwidth
  616. };
  617. struct dvb_frontend *dib0070_attach(struct dvb_frontend *fe, struct i2c_adapter *i2c, struct dib0070_config *cfg)
  618. {
  619. struct dib0070_state *state = kzalloc(sizeof(struct dib0070_state), GFP_KERNEL);
  620. if (state == NULL)
  621. return NULL;
  622. state->cfg = cfg;
  623. state->i2c = i2c;
  624. state->fe = fe;
  625. mutex_init(&state->i2c_buffer_lock);
  626. fe->tuner_priv = state;
  627. if (dib0070_reset(fe) != 0)
  628. goto free_mem;
  629. printk(KERN_INFO "DiB0070: successfully identified\n");
  630. memcpy(&fe->ops.tuner_ops, &dib0070_ops, sizeof(struct dvb_tuner_ops));
  631. fe->tuner_priv = state;
  632. return fe;
  633. free_mem:
  634. kfree(state);
  635. fe->tuner_priv = NULL;
  636. return NULL;
  637. }
  638. EXPORT_SYMBOL(dib0070_attach);
  639. MODULE_AUTHOR("Patrick Boettcher <pboettcher@dibcom.fr>");
  640. MODULE_DESCRIPTION("Driver for the DiBcom 0070 base-band RF Tuner");
  641. MODULE_LICENSE("GPL");