dib0070.c 20 KB

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