dib0070.c 20 KB

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