dib0070.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763
  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=%hd; ADC = %hd (ADC) & %dmV\n", state->captrim, adc, (u32) adc*(u32)1800/(u32)1024);
  154. if (adc >= 400) {
  155. adc -= 400;
  156. step_sign = -1;
  157. } else {
  158. adc = 400 - adc;
  159. step_sign = 1;
  160. }
  161. if (adc < state->adc_diff) {
  162. dprintk("CAPTRIM=%hd is closer to target (%hd/%hd)\n", state->captrim, adc, state->adc_diff);
  163. state->adc_diff = adc;
  164. state->fcaptrim = state->captrim;
  165. }
  166. state->captrim += (step_sign * state->step);
  167. if (state->step >= 1)
  168. *tune_state = CT_TUNER_STEP_1;
  169. else
  170. *tune_state = CT_TUNER_STEP_3;
  171. } else if (*tune_state == CT_TUNER_STEP_3) {
  172. dib0070_write_reg(state, 0x14, state->lo4 | state->fcaptrim);
  173. dib0070_write_reg(state, 0x18, 0x07ff);
  174. *tune_state = CT_TUNER_STEP_4;
  175. }
  176. return ret;
  177. }
  178. 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)
  179. {
  180. struct dib0070_state *state = fe->tuner_priv;
  181. u16 lo5 = (third_order_filt << 14) | (0 << 13) | (1 << 12) | (3 << 9) | (cp_current << 6) | (hf_div_trim << 3) | (vco_bias_trim << 0);
  182. dprintk("CTRL_LO5: 0x%x\n", lo5);
  183. return dib0070_write_reg(state, 0x15, lo5);
  184. }
  185. void dib0070_ctrl_agc_filter(struct dvb_frontend *fe, u8 open)
  186. {
  187. struct dib0070_state *state = fe->tuner_priv;
  188. if (open) {
  189. dib0070_write_reg(state, 0x1b, 0xff00);
  190. dib0070_write_reg(state, 0x1a, 0x0000);
  191. } else {
  192. dib0070_write_reg(state, 0x1b, 0x4112);
  193. if (state->cfg->vga_filter != 0) {
  194. dib0070_write_reg(state, 0x1a, state->cfg->vga_filter);
  195. dprintk("vga filter register is set to %x\n", state->cfg->vga_filter);
  196. } else
  197. dib0070_write_reg(state, 0x1a, 0x0009);
  198. }
  199. }
  200. EXPORT_SYMBOL(dib0070_ctrl_agc_filter);
  201. struct dib0070_tuning {
  202. u32 max_freq; /* for every frequency less than or equal to that field: this information is correct */
  203. u8 switch_trim;
  204. u8 vco_band;
  205. u8 hfdiv;
  206. u8 vco_multi;
  207. u8 presc;
  208. u8 wbdmux;
  209. u16 tuner_enable;
  210. };
  211. struct dib0070_lna_match {
  212. u32 max_freq; /* for every frequency less than or equal to that field: this information is correct */
  213. u8 lna_band;
  214. };
  215. static const struct dib0070_tuning dib0070s_tuning_table[] = {
  216. { 570000, 2, 1, 3, 6, 6, 2, 0x4000 | 0x0800 }, /* UHF */
  217. { 700000, 2, 0, 2, 4, 2, 2, 0x4000 | 0x0800 },
  218. { 863999, 2, 1, 2, 4, 2, 2, 0x4000 | 0x0800 },
  219. { 1500000, 0, 1, 1, 2, 2, 4, 0x2000 | 0x0400 }, /* LBAND */
  220. { 1600000, 0, 1, 1, 2, 2, 4, 0x2000 | 0x0400 },
  221. { 2000000, 0, 1, 1, 2, 2, 4, 0x2000 | 0x0400 },
  222. { 0xffffffff, 0, 0, 8, 1, 2, 1, 0x8000 | 0x1000 }, /* SBAND */
  223. };
  224. static const struct dib0070_tuning dib0070_tuning_table[] = {
  225. { 115000, 1, 0, 7, 24, 2, 1, 0x8000 | 0x1000 }, /* FM below 92MHz cannot be tuned */
  226. { 179500, 1, 0, 3, 16, 2, 1, 0x8000 | 0x1000 }, /* VHF */
  227. { 189999, 1, 1, 3, 16, 2, 1, 0x8000 | 0x1000 },
  228. { 250000, 1, 0, 6, 12, 2, 1, 0x8000 | 0x1000 },
  229. { 569999, 2, 1, 5, 6, 2, 2, 0x4000 | 0x0800 }, /* UHF */
  230. { 699999, 2, 0, 1, 4, 2, 2, 0x4000 | 0x0800 },
  231. { 863999, 2, 1, 1, 4, 2, 2, 0x4000 | 0x0800 },
  232. { 0xffffffff, 0, 1, 0, 2, 2, 4, 0x2000 | 0x0400 }, /* LBAND or everything higher than UHF */
  233. };
  234. static const struct dib0070_lna_match dib0070_lna_flip_chip[] = {
  235. { 180000, 0 }, /* VHF */
  236. { 188000, 1 },
  237. { 196400, 2 },
  238. { 250000, 3 },
  239. { 550000, 0 }, /* UHF */
  240. { 590000, 1 },
  241. { 666000, 3 },
  242. { 864000, 5 },
  243. { 1500000, 0 }, /* LBAND or everything higher than UHF */
  244. { 1600000, 1 },
  245. { 2000000, 3 },
  246. { 0xffffffff, 7 },
  247. };
  248. static const struct dib0070_lna_match dib0070_lna[] = {
  249. { 180000, 0 }, /* VHF */
  250. { 188000, 1 },
  251. { 196400, 2 },
  252. { 250000, 3 },
  253. { 550000, 2 }, /* UHF */
  254. { 650000, 3 },
  255. { 750000, 5 },
  256. { 850000, 6 },
  257. { 864000, 7 },
  258. { 1500000, 0 }, /* LBAND or everything higher than UHF */
  259. { 1600000, 1 },
  260. { 2000000, 3 },
  261. { 0xffffffff, 7 },
  262. };
  263. #define LPF 100
  264. static int dib0070_tune_digital(struct dvb_frontend *fe)
  265. {
  266. struct dib0070_state *state = fe->tuner_priv;
  267. const struct dib0070_tuning *tune;
  268. const struct dib0070_lna_match *lna_match;
  269. enum frontend_tune_state *tune_state = &state->tune_state;
  270. int ret = 10; /* 1ms is the default delay most of the time */
  271. u8 band = (u8)BAND_OF_FREQUENCY(fe->dtv_property_cache.frequency/1000);
  272. u32 freq = fe->dtv_property_cache.frequency/1000 + (band == BAND_VHF ? state->cfg->freq_offset_khz_vhf : state->cfg->freq_offset_khz_uhf);
  273. #ifdef CONFIG_SYS_ISDBT
  274. if (state->fe->dtv_property_cache.delivery_system == SYS_ISDBT && state->fe->dtv_property_cache.isdbt_sb_mode == 1)
  275. if (((state->fe->dtv_property_cache.isdbt_sb_segment_count % 2)
  276. && (state->fe->dtv_property_cache.isdbt_sb_segment_idx == ((state->fe->dtv_property_cache.isdbt_sb_segment_count / 2) + 1)))
  277. || (((state->fe->dtv_property_cache.isdbt_sb_segment_count % 2) == 0)
  278. && (state->fe->dtv_property_cache.isdbt_sb_segment_idx == (state->fe->dtv_property_cache.isdbt_sb_segment_count / 2)))
  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) + 1))))
  281. freq += 850;
  282. #endif
  283. if (state->current_rf != freq) {
  284. switch (state->revision) {
  285. case DIB0070S_P1A:
  286. tune = dib0070s_tuning_table;
  287. lna_match = dib0070_lna;
  288. break;
  289. default:
  290. tune = dib0070_tuning_table;
  291. if (state->cfg->flip_chip)
  292. lna_match = dib0070_lna_flip_chip;
  293. else
  294. lna_match = dib0070_lna;
  295. break;
  296. }
  297. while (freq > tune->max_freq) /* find the right one */
  298. tune++;
  299. while (freq > lna_match->max_freq) /* find the right one */
  300. lna_match++;
  301. state->current_tune_table_index = tune;
  302. state->lna_match = lna_match;
  303. }
  304. if (*tune_state == CT_TUNER_START) {
  305. dprintk("Tuning for Band: %hd (%d kHz)\n", band, freq);
  306. if (state->current_rf != freq) {
  307. u8 REFDIV;
  308. u32 FBDiv, Rest, FREF, VCOF_kHz;
  309. u8 Den;
  310. state->current_rf = freq;
  311. state->lo4 = (state->current_tune_table_index->vco_band << 11) | (state->current_tune_table_index->hfdiv << 7);
  312. dib0070_write_reg(state, 0x17, 0x30);
  313. VCOF_kHz = state->current_tune_table_index->vco_multi * freq * 2;
  314. switch (band) {
  315. case BAND_VHF:
  316. REFDIV = (u8) ((state->cfg->clock_khz + 9999) / 10000);
  317. break;
  318. case BAND_FM:
  319. REFDIV = (u8) ((state->cfg->clock_khz) / 1000);
  320. break;
  321. default:
  322. REFDIV = (u8) (state->cfg->clock_khz / 10000);
  323. break;
  324. }
  325. FREF = state->cfg->clock_khz / REFDIV;
  326. switch (state->revision) {
  327. case DIB0070S_P1A:
  328. FBDiv = (VCOF_kHz / state->current_tune_table_index->presc / FREF);
  329. Rest = (VCOF_kHz / state->current_tune_table_index->presc) - FBDiv * FREF;
  330. break;
  331. case DIB0070_P1G:
  332. case DIB0070_P1F:
  333. default:
  334. FBDiv = (freq / (FREF / 2));
  335. Rest = 2 * freq - FBDiv * FREF;
  336. break;
  337. }
  338. if (Rest < LPF)
  339. Rest = 0;
  340. else if (Rest < 2 * LPF)
  341. Rest = 2 * LPF;
  342. else if (Rest > (FREF - LPF)) {
  343. Rest = 0;
  344. FBDiv += 1;
  345. } else if (Rest > (FREF - 2 * LPF))
  346. Rest = FREF - 2 * LPF;
  347. Rest = (Rest * 6528) / (FREF / 10);
  348. Den = 1;
  349. if (Rest > 0) {
  350. state->lo4 |= (1 << 14) | (1 << 12);
  351. Den = 255;
  352. }
  353. dib0070_write_reg(state, 0x11, (u16)FBDiv);
  354. dib0070_write_reg(state, 0x12, (Den << 8) | REFDIV);
  355. dib0070_write_reg(state, 0x13, (u16) Rest);
  356. if (state->revision == DIB0070S_P1A) {
  357. if (band == BAND_SBAND) {
  358. dib0070_set_ctrl_lo5(fe, 2, 4, 3, 0);
  359. dib0070_write_reg(state, 0x1d, 0xFFFF);
  360. } else
  361. dib0070_set_ctrl_lo5(fe, 5, 4, 3, 1);
  362. }
  363. dib0070_write_reg(state, 0x20,
  364. 0x0040 | 0x0020 | 0x0010 | 0x0008 | 0x0002 | 0x0001 | state->current_tune_table_index->tuner_enable);
  365. dprintk("REFDIV: %hd, FREF: %d\n", REFDIV, FREF);
  366. dprintk("FBDIV: %d, Rest: %d\n", FBDiv, Rest);
  367. dprintk("Num: %hd, Den: %hd, SD: %hd\n", (u16) Rest, Den, (state->lo4 >> 12) & 0x1);
  368. dprintk("HFDIV code: %hd\n", state->current_tune_table_index->hfdiv);
  369. dprintk("VCO = %hd\n", state->current_tune_table_index->vco_band);
  370. dprintk("VCOF: ((%hd*%d) << 1))\n", state->current_tune_table_index->vco_multi, freq);
  371. *tune_state = CT_TUNER_STEP_0;
  372. } else { /* we are already tuned to this frequency - the configuration is correct */
  373. ret = 50; /* wakeup time */
  374. *tune_state = CT_TUNER_STEP_5;
  375. }
  376. } else if ((*tune_state > CT_TUNER_START) && (*tune_state < CT_TUNER_STEP_4)) {
  377. ret = dib0070_captrim(state, tune_state);
  378. } else if (*tune_state == CT_TUNER_STEP_4) {
  379. const struct dib0070_wbd_gain_cfg *tmp = state->cfg->wbd_gain;
  380. if (tmp != NULL) {
  381. while (freq/1000 > tmp->freq) /* find the right one */
  382. tmp++;
  383. dib0070_write_reg(state, 0x0f,
  384. (0 << 15) | (1 << 14) | (3 << 12)
  385. | (tmp->wbd_gain_val << 9) | (0 << 8) | (1 << 7)
  386. | (state->current_tune_table_index->wbdmux << 0));
  387. state->wbd_gain_current = tmp->wbd_gain_val;
  388. } else {
  389. dib0070_write_reg(state, 0x0f,
  390. (0 << 15) | (1 << 14) | (3 << 12)
  391. | (6 << 9) | (0 << 8) | (1 << 7)
  392. | (state->current_tune_table_index->wbdmux << 0));
  393. state->wbd_gain_current = 6;
  394. }
  395. dib0070_write_reg(state, 0x06, 0x3fff);
  396. dib0070_write_reg(state, 0x07,
  397. (state->current_tune_table_index->switch_trim << 11) | (7 << 8) | (state->lna_match->lna_band << 3) | (3 << 0));
  398. dib0070_write_reg(state, 0x08, (state->lna_match->lna_band << 10) | (3 << 7) | (127));
  399. dib0070_write_reg(state, 0x0d, 0x0d80);
  400. dib0070_write_reg(state, 0x18, 0x07ff);
  401. dib0070_write_reg(state, 0x17, 0x0033);
  402. *tune_state = CT_TUNER_STEP_5;
  403. } else if (*tune_state == CT_TUNER_STEP_5) {
  404. dib0070_set_bandwidth(fe);
  405. *tune_state = CT_TUNER_STOP;
  406. } else {
  407. ret = FE_CALLBACK_TIME_NEVER; /* tuner finished, time to call again infinite */
  408. }
  409. return ret;
  410. }
  411. static int dib0070_tune(struct dvb_frontend *fe)
  412. {
  413. struct dib0070_state *state = fe->tuner_priv;
  414. uint32_t ret;
  415. state->tune_state = CT_TUNER_START;
  416. do {
  417. ret = dib0070_tune_digital(fe);
  418. if (ret != FE_CALLBACK_TIME_NEVER)
  419. msleep(ret/10);
  420. else
  421. break;
  422. } while (state->tune_state != CT_TUNER_STOP);
  423. return 0;
  424. }
  425. static int dib0070_wakeup(struct dvb_frontend *fe)
  426. {
  427. struct dib0070_state *state = fe->tuner_priv;
  428. if (state->cfg->sleep)
  429. state->cfg->sleep(fe, 0);
  430. return 0;
  431. }
  432. static int dib0070_sleep(struct dvb_frontend *fe)
  433. {
  434. struct dib0070_state *state = fe->tuner_priv;
  435. if (state->cfg->sleep)
  436. state->cfg->sleep(fe, 1);
  437. return 0;
  438. }
  439. u8 dib0070_get_rf_output(struct dvb_frontend *fe)
  440. {
  441. struct dib0070_state *state = fe->tuner_priv;
  442. return (dib0070_read_reg(state, 0x07) >> 11) & 0x3;
  443. }
  444. EXPORT_SYMBOL(dib0070_get_rf_output);
  445. int dib0070_set_rf_output(struct dvb_frontend *fe, u8 no)
  446. {
  447. struct dib0070_state *state = fe->tuner_priv;
  448. u16 rxrf2 = dib0070_read_reg(state, 0x07) & 0xfe7ff;
  449. if (no > 3)
  450. no = 3;
  451. if (no < 1)
  452. no = 1;
  453. return dib0070_write_reg(state, 0x07, rxrf2 | (no << 11));
  454. }
  455. EXPORT_SYMBOL(dib0070_set_rf_output);
  456. static const u16 dib0070_p1f_defaults[] =
  457. {
  458. 7, 0x02,
  459. 0x0008,
  460. 0x0000,
  461. 0x0000,
  462. 0x0000,
  463. 0x0000,
  464. 0x0002,
  465. 0x0100,
  466. 3, 0x0d,
  467. 0x0d80,
  468. 0x0001,
  469. 0x0000,
  470. 4, 0x11,
  471. 0x0000,
  472. 0x0103,
  473. 0x0000,
  474. 0x0000,
  475. 3, 0x16,
  476. 0x0004 | 0x0040,
  477. 0x0030,
  478. 0x07ff,
  479. 6, 0x1b,
  480. 0x4112,
  481. 0xff00,
  482. 0xc07f,
  483. 0x0000,
  484. 0x0180,
  485. 0x4000 | 0x0800 | 0x0040 | 0x0020 | 0x0010 | 0x0008 | 0x0002 | 0x0001,
  486. 0,
  487. };
  488. static u16 dib0070_read_wbd_offset(struct dib0070_state *state, u8 gain)
  489. {
  490. u16 tuner_en = dib0070_read_reg(state, 0x20);
  491. u16 offset;
  492. dib0070_write_reg(state, 0x18, 0x07ff);
  493. dib0070_write_reg(state, 0x20, 0x0800 | 0x4000 | 0x0040 | 0x0020 | 0x0010 | 0x0008 | 0x0002 | 0x0001);
  494. dib0070_write_reg(state, 0x0f, (1 << 14) | (2 << 12) | (gain << 9) | (1 << 8) | (1 << 7) | (0 << 0));
  495. msleep(9);
  496. offset = dib0070_read_reg(state, 0x19);
  497. dib0070_write_reg(state, 0x20, tuner_en);
  498. return offset;
  499. }
  500. static void dib0070_wbd_offset_calibration(struct dib0070_state *state)
  501. {
  502. u8 gain;
  503. for (gain = 6; gain < 8; gain++) {
  504. state->wbd_offset_3_3[gain - 6] = ((dib0070_read_wbd_offset(state, gain) * 8 * 18 / 33 + 1) / 2);
  505. dprintk("Gain: %d, WBDOffset (3.3V) = %hd\n", gain, state->wbd_offset_3_3[gain-6]);
  506. }
  507. }
  508. u16 dib0070_wbd_offset(struct dvb_frontend *fe)
  509. {
  510. struct dib0070_state *state = fe->tuner_priv;
  511. const struct dib0070_wbd_gain_cfg *tmp = state->cfg->wbd_gain;
  512. u32 freq = fe->dtv_property_cache.frequency/1000;
  513. if (tmp != NULL) {
  514. while (freq/1000 > tmp->freq) /* find the right one */
  515. tmp++;
  516. state->wbd_gain_current = tmp->wbd_gain_val;
  517. } else
  518. state->wbd_gain_current = 6;
  519. return state->wbd_offset_3_3[state->wbd_gain_current - 6];
  520. }
  521. EXPORT_SYMBOL(dib0070_wbd_offset);
  522. #define pgm_read_word(w) (*w)
  523. static int dib0070_reset(struct dvb_frontend *fe)
  524. {
  525. struct dib0070_state *state = fe->tuner_priv;
  526. u16 l, r, *n;
  527. HARD_RESET(state);
  528. #ifndef FORCE_SBAND_TUNER
  529. if ((dib0070_read_reg(state, 0x22) >> 9) & 0x1)
  530. state->revision = (dib0070_read_reg(state, 0x1f) >> 8) & 0xff;
  531. else
  532. #else
  533. #warning forcing SBAND
  534. #endif
  535. state->revision = DIB0070S_P1A;
  536. /* P1F or not */
  537. dprintk("Revision: %x\n", state->revision);
  538. if (state->revision == DIB0070_P1D) {
  539. dprintk("Error: this driver is not to be used meant for P1D or earlier\n");
  540. return -EINVAL;
  541. }
  542. n = (u16 *) dib0070_p1f_defaults;
  543. l = pgm_read_word(n++);
  544. while (l) {
  545. r = pgm_read_word(n++);
  546. do {
  547. dib0070_write_reg(state, (u8)r, pgm_read_word(n++));
  548. r++;
  549. } while (--l);
  550. l = pgm_read_word(n++);
  551. }
  552. if (state->cfg->force_crystal_mode != 0)
  553. r = state->cfg->force_crystal_mode;
  554. else if (state->cfg->clock_khz >= 24000)
  555. r = 1;
  556. else
  557. r = 2;
  558. r |= state->cfg->osc_buffer_state << 3;
  559. dib0070_write_reg(state, 0x10, r);
  560. dib0070_write_reg(state, 0x1f, (1 << 8) | ((state->cfg->clock_pad_drive & 0xf) << 5));
  561. if (state->cfg->invert_iq) {
  562. r = dib0070_read_reg(state, 0x02) & 0xffdf;
  563. dib0070_write_reg(state, 0x02, r | (1 << 5));
  564. }
  565. if (state->revision == DIB0070S_P1A)
  566. dib0070_set_ctrl_lo5(fe, 2, 4, 3, 0);
  567. else
  568. dib0070_set_ctrl_lo5(fe, 5, 4, state->cfg->charge_pump,
  569. state->cfg->enable_third_order_filter);
  570. dib0070_write_reg(state, 0x01, (54 << 9) | 0xc8);
  571. dib0070_wbd_offset_calibration(state);
  572. return 0;
  573. }
  574. static int dib0070_get_frequency(struct dvb_frontend *fe, u32 *frequency)
  575. {
  576. struct dib0070_state *state = fe->tuner_priv;
  577. *frequency = 1000 * state->current_rf;
  578. return 0;
  579. }
  580. static void dib0070_release(struct dvb_frontend *fe)
  581. {
  582. kfree(fe->tuner_priv);
  583. fe->tuner_priv = NULL;
  584. }
  585. static const struct dvb_tuner_ops dib0070_ops = {
  586. .info = {
  587. .name = "DiBcom DiB0070",
  588. .frequency_min_hz = 45 * MHz,
  589. .frequency_max_hz = 860 * MHz,
  590. .frequency_step_hz = 1 * kHz,
  591. },
  592. .release = dib0070_release,
  593. .init = dib0070_wakeup,
  594. .sleep = dib0070_sleep,
  595. .set_params = dib0070_tune,
  596. .get_frequency = dib0070_get_frequency,
  597. // .get_bandwidth = dib0070_get_bandwidth
  598. };
  599. struct dvb_frontend *dib0070_attach(struct dvb_frontend *fe, struct i2c_adapter *i2c, struct dib0070_config *cfg)
  600. {
  601. struct dib0070_state *state = kzalloc(sizeof(struct dib0070_state), GFP_KERNEL);
  602. if (state == NULL)
  603. return NULL;
  604. state->cfg = cfg;
  605. state->i2c = i2c;
  606. state->fe = fe;
  607. mutex_init(&state->i2c_buffer_lock);
  608. fe->tuner_priv = state;
  609. if (dib0070_reset(fe) != 0)
  610. goto free_mem;
  611. pr_info("DiB0070: successfully identified\n");
  612. memcpy(&fe->ops.tuner_ops, &dib0070_ops, sizeof(struct dvb_tuner_ops));
  613. fe->tuner_priv = state;
  614. return fe;
  615. free_mem:
  616. kfree(state);
  617. fe->tuner_priv = NULL;
  618. return NULL;
  619. }
  620. EXPORT_SYMBOL(dib0070_attach);
  621. MODULE_AUTHOR("Patrick Boettcher <patrick.boettcher@posteo.de>");
  622. MODULE_DESCRIPTION("Driver for the DiBcom 0070 base-band RF Tuner");
  623. MODULE_LICENSE("GPL");