dib0090.c 41 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522
  1. /*
  2. * Linux-DVB Driver for DiBcom's DiB0090 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/i2c.h>
  28. #include "dvb_frontend.h"
  29. #include "dib0090.h"
  30. #include "dibx000_common.h"
  31. static int debug;
  32. module_param(debug, int, 0644);
  33. MODULE_PARM_DESC(debug, "turn on debugging (default: 0)");
  34. #define dprintk(args...) do { \
  35. if (debug) { \
  36. printk(KERN_DEBUG "DiB0090: "); \
  37. printk(args); \
  38. printk("\n"); \
  39. } \
  40. } while (0)
  41. #define CONFIG_SYS_ISDBT
  42. #define CONFIG_BAND_CBAND
  43. #define CONFIG_BAND_VHF
  44. #define CONFIG_BAND_UHF
  45. #define CONFIG_DIB0090_USE_PWM_AGC
  46. #define EN_LNA0 0x8000
  47. #define EN_LNA1 0x4000
  48. #define EN_LNA2 0x2000
  49. #define EN_LNA3 0x1000
  50. #define EN_MIX0 0x0800
  51. #define EN_MIX1 0x0400
  52. #define EN_MIX2 0x0200
  53. #define EN_MIX3 0x0100
  54. #define EN_IQADC 0x0040
  55. #define EN_PLL 0x0020
  56. #define EN_TX 0x0010
  57. #define EN_BB 0x0008
  58. #define EN_LO 0x0004
  59. #define EN_BIAS 0x0001
  60. #define EN_IQANA 0x0002
  61. #define EN_DIGCLK 0x0080 /* not in the 0x24 reg, only in 0x1b */
  62. #define EN_CRYSTAL 0x0002
  63. #define EN_UHF 0x22E9
  64. #define EN_VHF 0x44E9
  65. #define EN_LBD 0x11E9
  66. #define EN_SBD 0x44E9
  67. #define EN_CAB 0x88E9
  68. #define pgm_read_word(w) (*w)
  69. struct dc_calibration;
  70. struct dib0090_tuning {
  71. u32 max_freq; /* for every frequency less than or equal to that field: this information is correct */
  72. u8 switch_trim;
  73. u8 lna_tune;
  74. u8 lna_bias;
  75. u16 v2i;
  76. u16 mix;
  77. u16 load;
  78. u16 tuner_enable;
  79. };
  80. struct dib0090_pll {
  81. u32 max_freq; /* for every frequency less than or equal to that field: this information is correct */
  82. u8 vco_band;
  83. u8 hfdiv_code;
  84. u8 hfdiv;
  85. u8 topresc;
  86. };
  87. struct dib0090_state {
  88. struct i2c_adapter *i2c;
  89. struct dvb_frontend *fe;
  90. const struct dib0090_config *config;
  91. u8 current_band;
  92. u16 revision;
  93. enum frontend_tune_state tune_state;
  94. u32 current_rf;
  95. u16 wbd_offset;
  96. s16 wbd_target; /* in dB */
  97. s16 rf_gain_limit; /* take-over-point: where to split between bb and rf gain */
  98. s16 current_gain; /* keeps the currently programmed gain */
  99. u8 agc_step; /* new binary search */
  100. u16 gain[2]; /* for channel monitoring */
  101. const u16 *rf_ramp;
  102. const u16 *bb_ramp;
  103. /* for the software AGC ramps */
  104. u16 bb_1_def;
  105. u16 rf_lt_def;
  106. u16 gain_reg[4];
  107. /* for the captrim/dc-offset search */
  108. s8 step;
  109. s16 adc_diff;
  110. s16 min_adc_diff;
  111. s8 captrim;
  112. s8 fcaptrim;
  113. const struct dc_calibration *dc;
  114. u16 bb6, bb7;
  115. const struct dib0090_tuning *current_tune_table_index;
  116. const struct dib0090_pll *current_pll_table_index;
  117. u8 tuner_is_tuned;
  118. u8 agc_freeze;
  119. u8 reset;
  120. };
  121. static u16 dib0090_read_reg(struct dib0090_state *state, u8 reg)
  122. {
  123. u8 b[2];
  124. struct i2c_msg msg[2] = {
  125. {.addr = state->config->i2c_address, .flags = 0, .buf = &reg, .len = 1},
  126. {.addr = state->config->i2c_address, .flags = I2C_M_RD, .buf = b, .len = 2},
  127. };
  128. if (i2c_transfer(state->i2c, msg, 2) != 2) {
  129. printk(KERN_WARNING "DiB0090 I2C read failed\n");
  130. return 0;
  131. }
  132. return (b[0] << 8) | b[1];
  133. }
  134. static int dib0090_write_reg(struct dib0090_state *state, u32 reg, u16 val)
  135. {
  136. u8 b[3] = { reg & 0xff, val >> 8, val & 0xff };
  137. struct i2c_msg msg = {.addr = state->config->i2c_address, .flags = 0, .buf = b, .len = 3 };
  138. if (i2c_transfer(state->i2c, &msg, 1) != 1) {
  139. printk(KERN_WARNING "DiB0090 I2C write failed\n");
  140. return -EREMOTEIO;
  141. }
  142. return 0;
  143. }
  144. #define HARD_RESET(state) do { if (cfg->reset) { if (cfg->sleep) cfg->sleep(fe, 0); msleep(10); cfg->reset(fe, 1); msleep(10); cfg->reset(fe, 0); msleep(10); } } while (0)
  145. #define ADC_TARGET -220
  146. #define GAIN_ALPHA 5
  147. #define WBD_ALPHA 6
  148. #define LPF 100
  149. static void dib0090_write_regs(struct dib0090_state *state, u8 r, const u16 * b, u8 c)
  150. {
  151. do {
  152. dib0090_write_reg(state, r++, *b++);
  153. } while (--c);
  154. }
  155. static u16 dib0090_identify(struct dvb_frontend *fe)
  156. {
  157. struct dib0090_state *state = fe->tuner_priv;
  158. u16 v;
  159. v = dib0090_read_reg(state, 0x1a);
  160. #ifdef FIRMWARE_FIREFLY
  161. /* pll is not locked locked */
  162. if (!(v & 0x800))
  163. dprintk("FE%d : Identification : pll is not yet locked", fe->id);
  164. #endif
  165. /* without PLL lock info */
  166. v &= 0x3ff;
  167. dprintk("P/V: %04x:", v);
  168. if ((v >> 8) & 0xf)
  169. dprintk("FE%d : Product ID = 0x%x : KROSUS", fe->id, (v >> 8) & 0xf);
  170. else
  171. return 0xff;
  172. v &= 0xff;
  173. if (((v >> 5) & 0x7) == 0x1)
  174. dprintk("FE%d : MP001 : 9090/8096", fe->id);
  175. else if (((v >> 5) & 0x7) == 0x4)
  176. dprintk("FE%d : MP005 : Single Sband", fe->id);
  177. else if (((v >> 5) & 0x7) == 0x6)
  178. dprintk("FE%d : MP008 : diversity VHF-UHF-LBAND", fe->id);
  179. else if (((v >> 5) & 0x7) == 0x7)
  180. dprintk("FE%d : MP009 : diversity 29098 CBAND-UHF-LBAND-SBAND", fe->id);
  181. else
  182. return 0xff;
  183. /* revision only */
  184. if ((v & 0x1f) == 0x3)
  185. dprintk("FE%d : P1-D/E/F detected", fe->id);
  186. else if ((v & 0x1f) == 0x1)
  187. dprintk("FE%d : P1C detected", fe->id);
  188. else if ((v & 0x1f) == 0x0) {
  189. #ifdef CONFIG_TUNER_DIB0090_P1B_SUPPORT
  190. dprintk("FE%d : P1-A/B detected: using previous driver - support will be removed soon", fe->id);
  191. dib0090_p1b_register(fe);
  192. #else
  193. dprintk("FE%d : P1-A/B detected: driver is deactivated - not available", fe->id);
  194. return 0xff;
  195. #endif
  196. }
  197. return v;
  198. }
  199. static void dib0090_reset_digital(struct dvb_frontend *fe, const struct dib0090_config *cfg)
  200. {
  201. struct dib0090_state *state = fe->tuner_priv;
  202. HARD_RESET(state);
  203. dib0090_write_reg(state, 0x24, EN_PLL);
  204. dib0090_write_reg(state, 0x1b, EN_DIGCLK | EN_PLL | EN_CRYSTAL); /* PLL, DIG_CLK and CRYSTAL remain */
  205. /* adcClkOutRatio=8->7, release reset */
  206. dib0090_write_reg(state, 0x20, ((cfg->io.adc_clock_ratio - 1) << 11) | (0 << 10) | (1 << 9) | (1 << 8) | (0 << 4) | 0);
  207. if (cfg->clkoutdrive != 0)
  208. dib0090_write_reg(state, 0x23,
  209. (0 << 15) | ((!cfg->analog_output) << 14) | (1 << 10) | (1 << 9) | (0 << 8) | (cfg->clkoutdrive << 5) | (cfg->
  210. clkouttobamse
  211. << 4) | (0
  212. <<
  213. 2)
  214. | (0));
  215. else
  216. dib0090_write_reg(state, 0x23,
  217. (0 << 15) | ((!cfg->analog_output) << 14) | (1 << 10) | (1 << 9) | (0 << 8) | (7 << 5) | (cfg->
  218. clkouttobamse << 4) | (0
  219. <<
  220. 2)
  221. | (0));
  222. /* enable pll, de-activate reset, ratio: 2/1 = 60MHz */
  223. dib0090_write_reg(state, 0x21,
  224. (cfg->io.pll_bypass << 15) | (1 << 13) | (cfg->io.pll_range << 12) | (cfg->io.pll_loopdiv << 6) | (cfg->io.pll_prediv));
  225. }
  226. static int dib0090_wakeup(struct dvb_frontend *fe)
  227. {
  228. struct dib0090_state *state = fe->tuner_priv;
  229. if (state->config->sleep)
  230. state->config->sleep(fe, 0);
  231. return 0;
  232. }
  233. static int dib0090_sleep(struct dvb_frontend *fe)
  234. {
  235. struct dib0090_state *state = fe->tuner_priv;
  236. if (state->config->sleep)
  237. state->config->sleep(fe, 1);
  238. return 0;
  239. }
  240. extern void dib0090_dcc_freq(struct dvb_frontend *fe, u8 fast)
  241. {
  242. struct dib0090_state *state = fe->tuner_priv;
  243. if (fast)
  244. dib0090_write_reg(state, 0x04, 0);
  245. else
  246. dib0090_write_reg(state, 0x04, 1);
  247. }
  248. EXPORT_SYMBOL(dib0090_dcc_freq);
  249. static const u16 rf_ramp_pwm_cband[] = {
  250. 0, /* max RF gain in 10th of dB */
  251. 0, /* ramp_slope = 1dB of gain -> clock_ticks_per_db = clk_khz / ramp_slope -> 0x2b */
  252. 0, /* ramp_max = maximum X used on the ramp */
  253. (0 << 10) | 0, /* 0x2c, LNA 1 = 0dB */
  254. (0 << 10) | 0, /* 0x2d, LNA 1 */
  255. (0 << 10) | 0, /* 0x2e, LNA 2 = 0dB */
  256. (0 << 10) | 0, /* 0x2f, LNA 2 */
  257. (0 << 10) | 0, /* 0x30, LNA 3 = 0dB */
  258. (0 << 10) | 0, /* 0x31, LNA 3 */
  259. (0 << 10) | 0, /* GAIN_4_1, LNA 4 = 0dB */
  260. (0 << 10) | 0, /* GAIN_4_2, LNA 4 */
  261. };
  262. static const u16 rf_ramp_vhf[] = {
  263. 412, /* max RF gain in 10th of dB */
  264. 132, 307, 127, /* LNA1, 13.2dB */
  265. 105, 412, 255, /* LNA2, 10.5dB */
  266. 50, 50, 127, /* LNA3, 5dB */
  267. 125, 175, 127, /* LNA4, 12.5dB */
  268. 0, 0, 127, /* CBAND, 0dB */
  269. };
  270. static const u16 rf_ramp_uhf[] = {
  271. 412, /* max RF gain in 10th of dB */
  272. 132, 307, 127, /* LNA1 : total gain = 13.2dB, point on the ramp where this amp is full gain, value to write to get full gain */
  273. 105, 412, 255, /* LNA2 : 10.5 dB */
  274. 50, 50, 127, /* LNA3 : 5.0 dB */
  275. 125, 175, 127, /* LNA4 : 12.5 dB */
  276. 0, 0, 127, /* CBAND : 0.0 dB */
  277. };
  278. static const u16 rf_ramp_cband[] = {
  279. 332, /* max RF gain in 10th of dB */
  280. 132, 252, 127, /* LNA1, dB */
  281. 80, 332, 255, /* LNA2, dB */
  282. 0, 0, 127, /* LNA3, dB */
  283. 0, 0, 127, /* LNA4, dB */
  284. 120, 120, 127, /* LT1 CBAND */
  285. };
  286. static const u16 rf_ramp_pwm_vhf[] = {
  287. 404, /* max RF gain in 10th of dB */
  288. 25, /* ramp_slope = 1dB of gain -> clock_ticks_per_db = clk_khz / ramp_slope -> 0x2b */
  289. 1011, /* ramp_max = maximum X used on the ramp */
  290. (6 << 10) | 417, /* 0x2c, LNA 1 = 13.2dB */
  291. (0 << 10) | 756, /* 0x2d, LNA 1 */
  292. (16 << 10) | 756, /* 0x2e, LNA 2 = 10.5dB */
  293. (0 << 10) | 1011, /* 0x2f, LNA 2 */
  294. (16 << 10) | 290, /* 0x30, LNA 3 = 5dB */
  295. (0 << 10) | 417, /* 0x31, LNA 3 */
  296. (7 << 10) | 0, /* GAIN_4_1, LNA 4 = 12.5dB */
  297. (0 << 10) | 290, /* GAIN_4_2, LNA 4 */
  298. };
  299. static const u16 rf_ramp_pwm_uhf[] = {
  300. 404, /* max RF gain in 10th of dB */
  301. 25, /* ramp_slope = 1dB of gain -> clock_ticks_per_db = clk_khz / ramp_slope -> 0x2b */
  302. 1011, /* ramp_max = maximum X used on the ramp */
  303. (6 << 10) | 417, /* 0x2c, LNA 1 = 13.2dB */
  304. (0 << 10) | 756, /* 0x2d, LNA 1 */
  305. (16 << 10) | 756, /* 0x2e, LNA 2 = 10.5dB */
  306. (0 << 10) | 1011, /* 0x2f, LNA 2 */
  307. (16 << 10) | 0, /* 0x30, LNA 3 = 5dB */
  308. (0 << 10) | 127, /* 0x31, LNA 3 */
  309. (7 << 10) | 127, /* GAIN_4_1, LNA 4 = 12.5dB */
  310. (0 << 10) | 417, /* GAIN_4_2, LNA 4 */
  311. };
  312. static const u16 bb_ramp_boost[] = {
  313. 550, /* max BB gain in 10th of dB */
  314. 260, 260, 26, /* BB1, 26dB */
  315. 290, 550, 29, /* BB2, 29dB */
  316. };
  317. static const u16 bb_ramp_pwm_normal[] = {
  318. 500, /* max RF gain in 10th of dB */
  319. 8, /* ramp_slope = 1dB of gain -> clock_ticks_per_db = clk_khz / ramp_slope -> 0x34 */
  320. 400,
  321. (2 << 9) | 0, /* 0x35 = 21dB */
  322. (0 << 9) | 168, /* 0x36 */
  323. (2 << 9) | 168, /* 0x37 = 29dB */
  324. (0 << 9) | 400, /* 0x38 */
  325. };
  326. struct slope {
  327. int16_t range;
  328. int16_t slope;
  329. };
  330. static u16 slopes_to_scale(const struct slope *slopes, u8 num, s16 val)
  331. {
  332. u8 i;
  333. u16 rest;
  334. u16 ret = 0;
  335. for (i = 0; i < num; i++) {
  336. if (val > slopes[i].range)
  337. rest = slopes[i].range;
  338. else
  339. rest = val;
  340. ret += (rest * slopes[i].slope) / slopes[i].range;
  341. val -= rest;
  342. }
  343. return ret;
  344. }
  345. static const struct slope dib0090_wbd_slopes[3] = {
  346. {66, 120}, /* -64,-52: offset - 65 */
  347. {600, 170}, /* -52,-35: 65 - 665 */
  348. {170, 250}, /* -45,-10: 665 - 835 */
  349. };
  350. static s16 dib0090_wbd_to_db(struct dib0090_state *state, u16 wbd)
  351. {
  352. wbd &= 0x3ff;
  353. if (wbd < state->wbd_offset)
  354. wbd = 0;
  355. else
  356. wbd -= state->wbd_offset;
  357. /* -64dB is the floor */
  358. return -640 + (s16) slopes_to_scale(dib0090_wbd_slopes, ARRAY_SIZE(dib0090_wbd_slopes), wbd);
  359. }
  360. static void dib0090_wbd_target(struct dib0090_state *state, u32 rf)
  361. {
  362. u16 offset = 250;
  363. /* TODO : DAB digital N+/-1 interferer perfs : offset = 10 */
  364. if (state->current_band == BAND_VHF)
  365. offset = 650;
  366. #ifndef FIRMWARE_FIREFLY
  367. if (state->current_band == BAND_VHF)
  368. offset = state->config->wbd_vhf_offset;
  369. if (state->current_band == BAND_CBAND)
  370. offset = state->config->wbd_cband_offset;
  371. #endif
  372. state->wbd_target = dib0090_wbd_to_db(state, state->wbd_offset + offset);
  373. dprintk("wbd-target: %d dB", (u32) state->wbd_target);
  374. }
  375. static const int gain_reg_addr[4] = {
  376. 0x08, 0x0a, 0x0f, 0x01
  377. };
  378. static void dib0090_gain_apply(struct dib0090_state *state, s16 gain_delta, s16 top_delta, u8 force)
  379. {
  380. u16 rf, bb, ref;
  381. u16 i, v, gain_reg[4] = { 0 }, gain;
  382. const u16 *g;
  383. if (top_delta < -511)
  384. top_delta = -511;
  385. if (top_delta > 511)
  386. top_delta = 511;
  387. if (force) {
  388. top_delta *= (1 << WBD_ALPHA);
  389. gain_delta *= (1 << GAIN_ALPHA);
  390. }
  391. if (top_delta >= ((s16) (state->rf_ramp[0] << WBD_ALPHA) - state->rf_gain_limit)) /* overflow */
  392. state->rf_gain_limit = state->rf_ramp[0] << WBD_ALPHA;
  393. else
  394. state->rf_gain_limit += top_delta;
  395. if (state->rf_gain_limit < 0) /*underflow */
  396. state->rf_gain_limit = 0;
  397. /* use gain as a temporary variable and correct current_gain */
  398. gain = ((state->rf_gain_limit >> WBD_ALPHA) + state->bb_ramp[0]) << GAIN_ALPHA;
  399. if (gain_delta >= ((s16) gain - state->current_gain)) /* overflow */
  400. state->current_gain = gain;
  401. else
  402. state->current_gain += gain_delta;
  403. /* cannot be less than 0 (only if gain_delta is less than 0 we can have current_gain < 0) */
  404. if (state->current_gain < 0)
  405. state->current_gain = 0;
  406. /* now split total gain to rf and bb gain */
  407. gain = state->current_gain >> GAIN_ALPHA;
  408. /* requested gain is bigger than rf gain limit - ACI/WBD adjustment */
  409. if (gain > (state->rf_gain_limit >> WBD_ALPHA)) {
  410. rf = state->rf_gain_limit >> WBD_ALPHA;
  411. bb = gain - rf;
  412. if (bb > state->bb_ramp[0])
  413. bb = state->bb_ramp[0];
  414. } else { /* high signal level -> all gains put on RF */
  415. rf = gain;
  416. bb = 0;
  417. }
  418. state->gain[0] = rf;
  419. state->gain[1] = bb;
  420. /* software ramp */
  421. /* Start with RF gains */
  422. g = state->rf_ramp + 1; /* point on RF LNA1 max gain */
  423. ref = rf;
  424. for (i = 0; i < 7; i++) { /* Go over all amplifiers => 5RF amps + 2 BB amps = 7 amps */
  425. if (g[0] == 0 || ref < (g[1] - g[0])) /* if total gain of the current amp is null or this amp is not concerned because it starts to work from an higher gain value */
  426. v = 0; /* force the gain to write for the current amp to be null */
  427. else if (ref >= g[1]) /* Gain to set is higher than the high working point of this amp */
  428. v = g[2]; /* force this amp to be full gain */
  429. else /* compute the value to set to this amp because we are somewhere in his range */
  430. v = ((ref - (g[1] - g[0])) * g[2]) / g[0];
  431. if (i == 0) /* LNA 1 reg mapping */
  432. gain_reg[0] = v;
  433. else if (i == 1) /* LNA 2 reg mapping */
  434. gain_reg[0] |= v << 7;
  435. else if (i == 2) /* LNA 3 reg mapping */
  436. gain_reg[1] = v;
  437. else if (i == 3) /* LNA 4 reg mapping */
  438. gain_reg[1] |= v << 7;
  439. else if (i == 4) /* CBAND LNA reg mapping */
  440. gain_reg[2] = v | state->rf_lt_def;
  441. else if (i == 5) /* BB gain 1 reg mapping */
  442. gain_reg[3] = v << 3;
  443. else if (i == 6) /* BB gain 2 reg mapping */
  444. gain_reg[3] |= v << 8;
  445. g += 3; /* go to next gain bloc */
  446. /* When RF is finished, start with BB */
  447. if (i == 4) {
  448. g = state->bb_ramp + 1; /* point on BB gain 1 max gain */
  449. ref = bb;
  450. }
  451. }
  452. gain_reg[3] |= state->bb_1_def;
  453. gain_reg[3] |= ((bb % 10) * 100) / 125;
  454. #ifdef DEBUG_AGC
  455. dprintk("GA CALC: DB: %3d(rf) + %3d(bb) = %3d gain_reg[0]=%04x gain_reg[1]=%04x gain_reg[2]=%04x gain_reg[0]=%04x", rf, bb, rf + bb,
  456. gain_reg[0], gain_reg[1], gain_reg[2], gain_reg[3]);
  457. #endif
  458. /* Write the amplifier regs */
  459. for (i = 0; i < 4; i++) {
  460. v = gain_reg[i];
  461. if (force || state->gain_reg[i] != v) {
  462. state->gain_reg[i] = v;
  463. dib0090_write_reg(state, gain_reg_addr[i], v);
  464. }
  465. }
  466. }
  467. static void dib0090_set_boost(struct dib0090_state *state, int onoff)
  468. {
  469. state->bb_1_def &= 0xdfff;
  470. state->bb_1_def |= onoff << 13;
  471. }
  472. static void dib0090_set_rframp(struct dib0090_state *state, const u16 * cfg)
  473. {
  474. state->rf_ramp = cfg;
  475. }
  476. static void dib0090_set_rframp_pwm(struct dib0090_state *state, const u16 * cfg)
  477. {
  478. state->rf_ramp = cfg;
  479. dib0090_write_reg(state, 0x2a, 0xffff);
  480. dprintk("total RF gain: %ddB, step: %d", (u32) cfg[0], dib0090_read_reg(state, 0x2a));
  481. dib0090_write_regs(state, 0x2c, cfg + 3, 6);
  482. dib0090_write_regs(state, 0x3e, cfg + 9, 2);
  483. }
  484. static void dib0090_set_bbramp(struct dib0090_state *state, const u16 * cfg)
  485. {
  486. state->bb_ramp = cfg;
  487. dib0090_set_boost(state, cfg[0] > 500); /* we want the boost if the gain is higher that 50dB */
  488. }
  489. static void dib0090_set_bbramp_pwm(struct dib0090_state *state, const u16 * cfg)
  490. {
  491. state->bb_ramp = cfg;
  492. dib0090_set_boost(state, cfg[0] > 500); /* we want the boost if the gain is higher that 50dB */
  493. dib0090_write_reg(state, 0x33, 0xffff);
  494. dprintk("total BB gain: %ddB, step: %d", (u32) cfg[0], dib0090_read_reg(state, 0x33));
  495. dib0090_write_regs(state, 0x35, cfg + 3, 4);
  496. }
  497. void dib0090_pwm_gain_reset(struct dvb_frontend *fe)
  498. {
  499. struct dib0090_state *state = fe->tuner_priv;
  500. /* reset the AGC */
  501. if (state->config->use_pwm_agc) {
  502. #ifdef CONFIG_BAND_SBAND
  503. if (state->current_band == BAND_SBAND) {
  504. dib0090_set_rframp_pwm(state, rf_ramp_pwm_sband);
  505. dib0090_set_bbramp_pwm(state, bb_ramp_pwm_boost);
  506. } else
  507. #endif
  508. #ifdef CONFIG_BAND_CBAND
  509. if (state->current_band == BAND_CBAND) {
  510. dib0090_set_rframp_pwm(state, rf_ramp_pwm_cband);
  511. dib0090_set_bbramp_pwm(state, bb_ramp_pwm_normal);
  512. } else
  513. #endif
  514. #ifdef CONFIG_BAND_VHF
  515. if (state->current_band == BAND_VHF) {
  516. dib0090_set_rframp_pwm(state, rf_ramp_pwm_vhf);
  517. dib0090_set_bbramp_pwm(state, bb_ramp_pwm_normal);
  518. } else
  519. #endif
  520. {
  521. dib0090_set_rframp_pwm(state, rf_ramp_pwm_uhf);
  522. dib0090_set_bbramp_pwm(state, bb_ramp_pwm_normal);
  523. }
  524. if (state->rf_ramp[0] != 0)
  525. dib0090_write_reg(state, 0x32, (3 << 11));
  526. else
  527. dib0090_write_reg(state, 0x32, (0 << 11));
  528. dib0090_write_reg(state, 0x39, (1 << 10));
  529. }
  530. }
  531. EXPORT_SYMBOL(dib0090_pwm_gain_reset);
  532. int dib0090_gain_control(struct dvb_frontend *fe)
  533. {
  534. struct dib0090_state *state = fe->tuner_priv;
  535. enum frontend_tune_state *tune_state = &state->tune_state;
  536. int ret = 10;
  537. u16 wbd_val = 0;
  538. u8 apply_gain_immediatly = 1;
  539. s16 wbd_error = 0, adc_error = 0;
  540. if (*tune_state == CT_AGC_START) {
  541. state->agc_freeze = 0;
  542. dib0090_write_reg(state, 0x04, 0x0);
  543. #ifdef CONFIG_BAND_SBAND
  544. if (state->current_band == BAND_SBAND) {
  545. dib0090_set_rframp(state, rf_ramp_sband);
  546. dib0090_set_bbramp(state, bb_ramp_boost);
  547. } else
  548. #endif
  549. #ifdef CONFIG_BAND_VHF
  550. if (state->current_band == BAND_VHF) {
  551. dib0090_set_rframp(state, rf_ramp_vhf);
  552. dib0090_set_bbramp(state, bb_ramp_boost);
  553. } else
  554. #endif
  555. #ifdef CONFIG_BAND_CBAND
  556. if (state->current_band == BAND_CBAND) {
  557. dib0090_set_rframp(state, rf_ramp_cband);
  558. dib0090_set_bbramp(state, bb_ramp_boost);
  559. } else
  560. #endif
  561. {
  562. dib0090_set_rframp(state, rf_ramp_uhf);
  563. dib0090_set_bbramp(state, bb_ramp_boost);
  564. }
  565. dib0090_write_reg(state, 0x32, 0);
  566. dib0090_write_reg(state, 0x39, 0);
  567. dib0090_wbd_target(state, state->current_rf);
  568. state->rf_gain_limit = state->rf_ramp[0] << WBD_ALPHA;
  569. state->current_gain = ((state->rf_ramp[0] + state->bb_ramp[0]) / 2) << GAIN_ALPHA;
  570. *tune_state = CT_AGC_STEP_0;
  571. } else if (!state->agc_freeze) {
  572. s16 wbd;
  573. int adc;
  574. wbd_val = dib0090_read_reg(state, 0x1d);
  575. /* read and calc the wbd power */
  576. wbd = dib0090_wbd_to_db(state, wbd_val);
  577. wbd_error = state->wbd_target - wbd;
  578. if (*tune_state == CT_AGC_STEP_0) {
  579. if (wbd_error < 0 && state->rf_gain_limit > 0) {
  580. #ifdef CONFIG_BAND_CBAND
  581. /* in case of CBAND tune reduce first the lt_gain2 before adjusting the RF gain */
  582. u8 ltg2 = (state->rf_lt_def >> 10) & 0x7;
  583. if (state->current_band == BAND_CBAND && ltg2) {
  584. ltg2 >>= 1;
  585. state->rf_lt_def &= ltg2 << 10; /* reduce in 3 steps from 7 to 0 */
  586. }
  587. #endif
  588. } else {
  589. state->agc_step = 0;
  590. *tune_state = CT_AGC_STEP_1;
  591. }
  592. } else {
  593. /* calc the adc power */
  594. adc = state->config->get_adc_power(fe);
  595. adc = (adc * ((s32) 355774) + (((s32) 1) << 20)) >> 21; /* included in [0:-700] */
  596. adc_error = (s16) (((s32) ADC_TARGET) - adc);
  597. #ifdef CONFIG_STANDARD_DAB
  598. if (state->fe->dtv_property_cache.delivery_system == STANDARD_DAB)
  599. adc_error += 130;
  600. #endif
  601. #ifdef CONFIG_STANDARD_DVBT
  602. if (state->fe->dtv_property_cache.delivery_system == STANDARD_DVBT &&
  603. (state->fe->dtv_property_cache.modulation == QAM_64 || state->fe->dtv_property_cache.modulation == QAM_16))
  604. adc_error += 60;
  605. #endif
  606. #ifdef CONFIG_SYS_ISDBT
  607. if ((state->fe->dtv_property_cache.delivery_system == SYS_ISDBT) && (((state->fe->dtv_property_cache.layer[0].segment_count >
  608. 0)
  609. &&
  610. ((state->fe->dtv_property_cache.layer[0].modulation ==
  611. QAM_64)
  612. || (state->fe->dtv_property_cache.layer[0].
  613. modulation == QAM_16)))
  614. ||
  615. ((state->fe->dtv_property_cache.layer[1].segment_count >
  616. 0)
  617. &&
  618. ((state->fe->dtv_property_cache.layer[1].modulation ==
  619. QAM_64)
  620. || (state->fe->dtv_property_cache.layer[1].
  621. modulation == QAM_16)))
  622. ||
  623. ((state->fe->dtv_property_cache.layer[2].segment_count >
  624. 0)
  625. &&
  626. ((state->fe->dtv_property_cache.layer[2].modulation ==
  627. QAM_64)
  628. || (state->fe->dtv_property_cache.layer[2].
  629. modulation == QAM_16)))
  630. )
  631. )
  632. adc_error += 60;
  633. #endif
  634. if (*tune_state == CT_AGC_STEP_1) { /* quickly go to the correct range of the ADC power */
  635. if (ABS(adc_error) < 50 || state->agc_step++ > 5) {
  636. #ifdef CONFIG_STANDARD_DAB
  637. if (state->fe->dtv_property_cache.delivery_system == STANDARD_DAB) {
  638. dib0090_write_reg(state, 0x02, (1 << 15) | (15 << 11) | (31 << 6) | (63)); /* cap value = 63 : narrow BB filter : Fc = 1.8MHz */
  639. dib0090_write_reg(state, 0x04, 0x0);
  640. } else
  641. #endif
  642. {
  643. dib0090_write_reg(state, 0x02, (1 << 15) | (3 << 11) | (6 << 6) | (32));
  644. dib0090_write_reg(state, 0x04, 0x01); /*0 = 1KHz ; 1 = 150Hz ; 2 = 50Hz ; 3 = 50KHz ; 4 = servo fast */
  645. }
  646. *tune_state = CT_AGC_STOP;
  647. }
  648. } else {
  649. /* everything higher than or equal to CT_AGC_STOP means tracking */
  650. ret = 100; /* 10ms interval */
  651. apply_gain_immediatly = 0;
  652. }
  653. }
  654. #ifdef DEBUG_AGC
  655. dprintk
  656. ("FE: %d, tune state %d, ADC = %3ddB (ADC err %3d) WBD %3ddB (WBD err %3d, WBD val SADC: %4d), RFGainLimit (TOP): %3d, signal: %3ddBm",
  657. (u32) fe->id, (u32) *tune_state, (u32) adc, (u32) adc_error, (u32) wbd, (u32) wbd_error, (u32) wbd_val,
  658. (u32) state->rf_gain_limit >> WBD_ALPHA, (s32) 200 + adc - (state->current_gain >> GAIN_ALPHA));
  659. #endif
  660. }
  661. /* apply gain */
  662. if (!state->agc_freeze)
  663. dib0090_gain_apply(state, adc_error, wbd_error, apply_gain_immediatly);
  664. return ret;
  665. }
  666. EXPORT_SYMBOL(dib0090_gain_control);
  667. void dib0090_get_current_gain(struct dvb_frontend *fe, u16 * rf, u16 * bb, u16 * rf_gain_limit, u16 * rflt)
  668. {
  669. struct dib0090_state *state = fe->tuner_priv;
  670. if (rf)
  671. *rf = state->gain[0];
  672. if (bb)
  673. *bb = state->gain[1];
  674. if (rf_gain_limit)
  675. *rf_gain_limit = state->rf_gain_limit;
  676. if (rflt)
  677. *rflt = (state->rf_lt_def >> 10) & 0x7;
  678. }
  679. EXPORT_SYMBOL(dib0090_get_current_gain);
  680. u16 dib0090_get_wbd_offset(struct dvb_frontend *tuner)
  681. {
  682. struct dib0090_state *st = tuner->tuner_priv;
  683. return st->wbd_offset;
  684. }
  685. EXPORT_SYMBOL(dib0090_get_wbd_offset);
  686. static const u16 dib0090_defaults[] = {
  687. 25, 0x01,
  688. 0x0000,
  689. 0x99a0,
  690. 0x6008,
  691. 0x0000,
  692. 0x8acb,
  693. 0x0000,
  694. 0x0405,
  695. 0x0000,
  696. 0x0000,
  697. 0x0000,
  698. 0xb802,
  699. 0x0300,
  700. 0x2d12,
  701. 0xbac0,
  702. 0x7c00,
  703. 0xdbb9,
  704. 0x0954,
  705. 0x0743,
  706. 0x8000,
  707. 0x0001,
  708. 0x0040,
  709. 0x0100,
  710. 0x0000,
  711. 0xe910,
  712. 0x149e,
  713. 1, 0x1c,
  714. 0xff2d,
  715. 1, 0x39,
  716. 0x0000,
  717. 1, 0x1b,
  718. EN_IQADC | EN_BB | EN_BIAS | EN_DIGCLK | EN_PLL | EN_CRYSTAL,
  719. 2, 0x1e,
  720. 0x07FF,
  721. 0x0007,
  722. 1, 0x24,
  723. EN_UHF | EN_CRYSTAL,
  724. 2, 0x3c,
  725. 0x3ff,
  726. 0x111,
  727. 0
  728. };
  729. static int dib0090_reset(struct dvb_frontend *fe)
  730. {
  731. struct dib0090_state *state = fe->tuner_priv;
  732. u16 l, r, *n;
  733. dib0090_reset_digital(fe, state->config);
  734. state->revision = dib0090_identify(fe);
  735. /* Revision definition */
  736. if (state->revision == 0xff)
  737. return -EINVAL;
  738. #ifdef EFUSE
  739. else if ((state->revision & 0x1f) >= 3) /* Update the efuse : Only available for KROSUS > P1C */
  740. dib0090_set_EFUSE(state);
  741. #endif
  742. #ifdef CONFIG_TUNER_DIB0090_P1B_SUPPORT
  743. if (!(state->revision & 0x1)) /* it is P1B - reset is already done */
  744. return 0;
  745. #endif
  746. /* Upload the default values */
  747. n = (u16 *) dib0090_defaults;
  748. l = pgm_read_word(n++);
  749. while (l) {
  750. r = pgm_read_word(n++);
  751. do {
  752. /* DEBUG_TUNER */
  753. /* dprintk("%d, %d, %d", l, r, pgm_read_word(n)); */
  754. dib0090_write_reg(state, r, pgm_read_word(n++));
  755. r++;
  756. } while (--l);
  757. l = pgm_read_word(n++);
  758. }
  759. /* Congigure in function of the crystal */
  760. if (state->config->io.clock_khz >= 24000)
  761. l = 1;
  762. else
  763. l = 2;
  764. dib0090_write_reg(state, 0x14, l);
  765. dprintk("Pll lock : %d", (dib0090_read_reg(state, 0x1a) >> 11) & 0x1);
  766. state->reset = 3; /* enable iq-offset-calibration and wbd-calibration when tuning next time */
  767. return 0;
  768. }
  769. #define steps(u) (((u) > 15) ? ((u)-16) : (u))
  770. #define INTERN_WAIT 10
  771. static int dib0090_get_offset(struct dib0090_state *state, enum frontend_tune_state *tune_state)
  772. {
  773. int ret = INTERN_WAIT * 10;
  774. switch (*tune_state) {
  775. case CT_TUNER_STEP_2:
  776. /* Turns to positive */
  777. dib0090_write_reg(state, 0x1f, 0x7);
  778. *tune_state = CT_TUNER_STEP_3;
  779. break;
  780. case CT_TUNER_STEP_3:
  781. state->adc_diff = dib0090_read_reg(state, 0x1d);
  782. /* Turns to negative */
  783. dib0090_write_reg(state, 0x1f, 0x4);
  784. *tune_state = CT_TUNER_STEP_4;
  785. break;
  786. case CT_TUNER_STEP_4:
  787. state->adc_diff -= dib0090_read_reg(state, 0x1d);
  788. *tune_state = CT_TUNER_STEP_5;
  789. ret = 0;
  790. break;
  791. default:
  792. break;
  793. }
  794. return ret;
  795. }
  796. struct dc_calibration {
  797. uint8_t addr;
  798. uint8_t offset;
  799. uint8_t pga:1;
  800. uint16_t bb1;
  801. uint8_t i:1;
  802. };
  803. static const struct dc_calibration dc_table[] = {
  804. /* Step1 BB gain1= 26 with boost 1, gain 2 = 0 */
  805. {0x06, 5, 1, (1 << 13) | (0 << 8) | (26 << 3), 1},
  806. {0x07, 11, 1, (1 << 13) | (0 << 8) | (26 << 3), 0},
  807. /* Step 2 BB gain 1 = 26 with boost = 1 & gain 2 = 29 */
  808. {0x06, 0, 0, (1 << 13) | (29 << 8) | (26 << 3), 1},
  809. {0x06, 10, 0, (1 << 13) | (29 << 8) | (26 << 3), 0},
  810. {0},
  811. };
  812. static void dib0090_set_trim(struct dib0090_state *state)
  813. {
  814. u16 *val;
  815. if (state->dc->addr == 0x07)
  816. val = &state->bb7;
  817. else
  818. val = &state->bb6;
  819. *val &= ~(0x1f << state->dc->offset);
  820. *val |= state->step << state->dc->offset;
  821. dib0090_write_reg(state, state->dc->addr, *val);
  822. }
  823. static int dib0090_dc_offset_calibration(struct dib0090_state *state, enum frontend_tune_state *tune_state)
  824. {
  825. int ret = 0;
  826. switch (*tune_state) {
  827. case CT_TUNER_START:
  828. /* init */
  829. dprintk("Internal DC calibration");
  830. /* the LNA is off */
  831. dib0090_write_reg(state, 0x24, 0x02ed);
  832. /* force vcm2 = 0.8V */
  833. state->bb6 = 0;
  834. state->bb7 = 0x040d;
  835. state->dc = dc_table;
  836. *tune_state = CT_TUNER_STEP_0;
  837. /* fall through */
  838. case CT_TUNER_STEP_0:
  839. dib0090_write_reg(state, 0x01, state->dc->bb1);
  840. dib0090_write_reg(state, 0x07, state->bb7 | (state->dc->i << 7));
  841. state->step = 0;
  842. state->min_adc_diff = 1023;
  843. *tune_state = CT_TUNER_STEP_1;
  844. ret = 50;
  845. break;
  846. case CT_TUNER_STEP_1:
  847. dib0090_set_trim(state);
  848. *tune_state = CT_TUNER_STEP_2;
  849. break;
  850. case CT_TUNER_STEP_2:
  851. case CT_TUNER_STEP_3:
  852. case CT_TUNER_STEP_4:
  853. ret = dib0090_get_offset(state, tune_state);
  854. break;
  855. case CT_TUNER_STEP_5: /* found an offset */
  856. dprintk("FE%d: IQC read=%d, current=%x", state->fe->id, (u32) state->adc_diff, state->step);
  857. /* first turn for this frequency */
  858. if (state->step == 0) {
  859. if (state->dc->pga && state->adc_diff < 0)
  860. state->step = 0x10;
  861. if (state->dc->pga == 0 && state->adc_diff > 0)
  862. state->step = 0x10;
  863. }
  864. state->adc_diff = ABS(state->adc_diff);
  865. if (state->adc_diff < state->min_adc_diff && steps(state->step) < 15) { /* stop search when the delta to 0 is increasing */
  866. state->step++;
  867. state->min_adc_diff = state->adc_diff;
  868. *tune_state = CT_TUNER_STEP_1;
  869. } else {
  870. /* the minimum was what we have seen in the step before */
  871. state->step--;
  872. dib0090_set_trim(state);
  873. dprintk("FE%d: BB Offset Cal, BBreg=%hd,Offset=%hd,Value Set=%hd", state->fe->id, state->dc->addr, state->adc_diff,
  874. state->step);
  875. state->dc++;
  876. if (state->dc->addr == 0) /* done */
  877. *tune_state = CT_TUNER_STEP_6;
  878. else
  879. *tune_state = CT_TUNER_STEP_0;
  880. }
  881. break;
  882. case CT_TUNER_STEP_6:
  883. dib0090_write_reg(state, 0x07, state->bb7 & ~0x0008);
  884. dib0090_write_reg(state, 0x1f, 0x7);
  885. *tune_state = CT_TUNER_START; /* reset done -> real tuning can now begin */
  886. state->reset &= ~0x1;
  887. default:
  888. break;
  889. }
  890. return ret;
  891. }
  892. static int dib0090_wbd_calibration(struct dib0090_state *state, enum frontend_tune_state *tune_state)
  893. {
  894. switch (*tune_state) {
  895. case CT_TUNER_START:
  896. /* WBD-mode=log, Bias=2, Gain=6, Testmode=1, en=1, WBDMUX=1 */
  897. dib0090_write_reg(state, 0x10, 0xdb09 | (1 << 10));
  898. dib0090_write_reg(state, 0x24, EN_UHF & 0x0fff);
  899. *tune_state = CT_TUNER_STEP_0;
  900. return 90; /* wait for the WBDMUX to switch and for the ADC to sample */
  901. case CT_TUNER_STEP_0:
  902. state->wbd_offset = dib0090_read_reg(state, 0x1d);
  903. dprintk("WBD calibration offset = %d", state->wbd_offset);
  904. *tune_state = CT_TUNER_START; /* reset done -> real tuning can now begin */
  905. state->reset &= ~0x2;
  906. break;
  907. default:
  908. break;
  909. }
  910. return 0;
  911. }
  912. static void dib0090_set_bandwidth(struct dib0090_state *state)
  913. {
  914. u16 tmp;
  915. if (state->fe->dtv_property_cache.bandwidth_hz / 1000 <= 5000)
  916. tmp = (3 << 14);
  917. else if (state->fe->dtv_property_cache.bandwidth_hz / 1000 <= 6000)
  918. tmp = (2 << 14);
  919. else if (state->fe->dtv_property_cache.bandwidth_hz / 1000 <= 7000)
  920. tmp = (1 << 14);
  921. else
  922. tmp = (0 << 14);
  923. state->bb_1_def &= 0x3fff;
  924. state->bb_1_def |= tmp;
  925. dib0090_write_reg(state, 0x01, state->bb_1_def); /* be sure that we have the right bb-filter */
  926. }
  927. static const struct dib0090_pll dib0090_pll_table[] = {
  928. #ifdef CONFIG_BAND_CBAND
  929. {56000, 0, 9, 48, 6},
  930. {70000, 1, 9, 48, 6},
  931. {87000, 0, 8, 32, 4},
  932. {105000, 1, 8, 32, 4},
  933. {115000, 0, 7, 24, 6},
  934. {140000, 1, 7, 24, 6},
  935. {170000, 0, 6, 16, 4},
  936. #endif
  937. #ifdef CONFIG_BAND_VHF
  938. {200000, 1, 6, 16, 4},
  939. {230000, 0, 5, 12, 6},
  940. {280000, 1, 5, 12, 6},
  941. {340000, 0, 4, 8, 4},
  942. {380000, 1, 4, 8, 4},
  943. {450000, 0, 3, 6, 6},
  944. #endif
  945. #ifdef CONFIG_BAND_UHF
  946. {580000, 1, 3, 6, 6},
  947. {700000, 0, 2, 4, 4},
  948. {860000, 1, 2, 4, 4},
  949. #endif
  950. #ifdef CONFIG_BAND_LBAND
  951. {1800000, 1, 0, 2, 4},
  952. #endif
  953. #ifdef CONFIG_BAND_SBAND
  954. {2900000, 0, 14, 1, 4},
  955. #endif
  956. };
  957. static const struct dib0090_tuning dib0090_tuning_table_fm_vhf_on_cband[] = {
  958. #ifdef CONFIG_BAND_CBAND
  959. {184000, 4, 1, 15, 0x280, 0x2912, 0xb94e, EN_CAB},
  960. {227000, 4, 3, 15, 0x280, 0x2912, 0xb94e, EN_CAB},
  961. {380000, 4, 7, 15, 0x280, 0x2912, 0xb94e, EN_CAB},
  962. #endif
  963. #ifdef CONFIG_BAND_UHF
  964. {520000, 2, 0, 15, 0x300, 0x1d12, 0xb9ce, EN_UHF},
  965. {550000, 2, 2, 15, 0x300, 0x1d12, 0xb9ce, EN_UHF},
  966. {650000, 2, 3, 15, 0x300, 0x1d12, 0xb9ce, EN_UHF},
  967. {750000, 2, 5, 15, 0x300, 0x1d12, 0xb9ce, EN_UHF},
  968. {850000, 2, 6, 15, 0x300, 0x1d12, 0xb9ce, EN_UHF},
  969. {900000, 2, 7, 15, 0x300, 0x1d12, 0xb9ce, EN_UHF},
  970. #endif
  971. #ifdef CONFIG_BAND_LBAND
  972. {1500000, 4, 0, 20, 0x300, 0x1912, 0x82c9, EN_LBD},
  973. {1600000, 4, 1, 20, 0x300, 0x1912, 0x82c9, EN_LBD},
  974. {1800000, 4, 3, 20, 0x300, 0x1912, 0x82c9, EN_LBD},
  975. #endif
  976. #ifdef CONFIG_BAND_SBAND
  977. {2300000, 1, 4, 20, 0x300, 0x2d2A, 0x82c7, EN_SBD},
  978. {2900000, 1, 7, 20, 0x280, 0x2deb, 0x8347, EN_SBD},
  979. #endif
  980. };
  981. static const struct dib0090_tuning dib0090_tuning_table[] = {
  982. #ifdef CONFIG_BAND_CBAND
  983. {170000, 4, 1, 15, 0x280, 0x2912, 0xb94e, EN_CAB},
  984. #endif
  985. #ifdef CONFIG_BAND_VHF
  986. {184000, 1, 1, 15, 0x300, 0x4d12, 0xb94e, EN_VHF},
  987. {227000, 1, 3, 15, 0x300, 0x4d12, 0xb94e, EN_VHF},
  988. {380000, 1, 7, 15, 0x300, 0x4d12, 0xb94e, EN_VHF},
  989. #endif
  990. #ifdef CONFIG_BAND_UHF
  991. {520000, 2, 0, 15, 0x300, 0x1d12, 0xb9ce, EN_UHF},
  992. {550000, 2, 2, 15, 0x300, 0x1d12, 0xb9ce, EN_UHF},
  993. {650000, 2, 3, 15, 0x300, 0x1d12, 0xb9ce, EN_UHF},
  994. {750000, 2, 5, 15, 0x300, 0x1d12, 0xb9ce, EN_UHF},
  995. {850000, 2, 6, 15, 0x300, 0x1d12, 0xb9ce, EN_UHF},
  996. {900000, 2, 7, 15, 0x300, 0x1d12, 0xb9ce, EN_UHF},
  997. #endif
  998. #ifdef CONFIG_BAND_LBAND
  999. {1500000, 4, 0, 20, 0x300, 0x1912, 0x82c9, EN_LBD},
  1000. {1600000, 4, 1, 20, 0x300, 0x1912, 0x82c9, EN_LBD},
  1001. {1800000, 4, 3, 20, 0x300, 0x1912, 0x82c9, EN_LBD},
  1002. #endif
  1003. #ifdef CONFIG_BAND_SBAND
  1004. {2300000, 1, 4, 20, 0x300, 0x2d2A, 0x82c7, EN_SBD},
  1005. {2900000, 1, 7, 20, 0x280, 0x2deb, 0x8347, EN_SBD},
  1006. #endif
  1007. };
  1008. #define WBD 0x781 /* 1 1 1 1 0000 0 0 1 */
  1009. static int dib0090_tune(struct dvb_frontend *fe)
  1010. {
  1011. struct dib0090_state *state = fe->tuner_priv;
  1012. const struct dib0090_tuning *tune = state->current_tune_table_index;
  1013. const struct dib0090_pll *pll = state->current_pll_table_index;
  1014. enum frontend_tune_state *tune_state = &state->tune_state;
  1015. u32 rf;
  1016. u16 lo4 = 0xe900, lo5, lo6, Den;
  1017. u32 FBDiv, Rest, FREF, VCOF_kHz = 0;
  1018. u16 tmp, adc;
  1019. int8_t step_sign;
  1020. int ret = 10; /* 1ms is the default delay most of the time */
  1021. u8 c, i;
  1022. state->current_band = (u8) BAND_OF_FREQUENCY(fe->dtv_property_cache.frequency / 1000);
  1023. rf = fe->dtv_property_cache.frequency / 1000 + (state->current_band ==
  1024. BAND_UHF ? state->config->freq_offset_khz_uhf : state->config->freq_offset_khz_vhf);
  1025. /* in any case we first need to do a reset if needed */
  1026. if (state->reset & 0x1)
  1027. return dib0090_dc_offset_calibration(state, tune_state);
  1028. else if (state->reset & 0x2)
  1029. return dib0090_wbd_calibration(state, tune_state);
  1030. /************************* VCO ***************************/
  1031. /* Default values for FG */
  1032. /* from these are needed : */
  1033. /* Cp,HFdiv,VCOband,SD,Num,Den,FB and REFDiv */
  1034. #ifdef CONFIG_SYS_ISDBT
  1035. if (state->fe->dtv_property_cache.delivery_system == SYS_ISDBT && state->fe->dtv_property_cache.isdbt_sb_mode == 1)
  1036. rf += 850;
  1037. #endif
  1038. if (state->current_rf != rf) {
  1039. state->tuner_is_tuned = 0;
  1040. tune = dib0090_tuning_table;
  1041. tmp = (state->revision >> 5) & 0x7;
  1042. if (tmp == 0x4 || tmp == 0x7) {
  1043. /* CBAND tuner version for VHF */
  1044. if (state->current_band == BAND_FM || state->current_band == BAND_VHF) {
  1045. /* Force CBAND */
  1046. state->current_band = BAND_CBAND;
  1047. tune = dib0090_tuning_table_fm_vhf_on_cband;
  1048. }
  1049. }
  1050. pll = dib0090_pll_table;
  1051. /* Look for the interval */
  1052. while (rf > tune->max_freq)
  1053. tune++;
  1054. while (rf > pll->max_freq)
  1055. pll++;
  1056. state->current_tune_table_index = tune;
  1057. state->current_pll_table_index = pll;
  1058. }
  1059. if (*tune_state == CT_TUNER_START) {
  1060. if (state->tuner_is_tuned == 0)
  1061. state->current_rf = 0;
  1062. if (state->current_rf != rf) {
  1063. dib0090_write_reg(state, 0x0b, 0xb800 | (tune->switch_trim));
  1064. /* external loop filter, otherwise:
  1065. * lo5 = (0 << 15) | (0 << 12) | (0 << 11) | (3 << 9) | (4 << 6) | (3 << 4) | 4;
  1066. * lo6 = 0x0e34 */
  1067. if (pll->vco_band)
  1068. lo5 = 0x049e;
  1069. else if (state->config->analog_output)
  1070. lo5 = 0x041d;
  1071. else
  1072. lo5 = 0x041c;
  1073. lo5 |= (pll->hfdiv_code << 11) | (pll->vco_band << 7); /* bit 15 is the split to the slave, we do not do it here */
  1074. if (!state->config->io.pll_int_loop_filt)
  1075. lo6 = 0xff28;
  1076. else
  1077. lo6 = (state->config->io.pll_int_loop_filt << 3);
  1078. VCOF_kHz = (pll->hfdiv * rf) * 2;
  1079. FREF = state->config->io.clock_khz;
  1080. FBDiv = (VCOF_kHz / pll->topresc / FREF);
  1081. Rest = (VCOF_kHz / pll->topresc) - FBDiv * FREF;
  1082. if (Rest < LPF)
  1083. Rest = 0;
  1084. else if (Rest < 2 * LPF)
  1085. Rest = 2 * LPF;
  1086. else if (Rest > (FREF - LPF)) {
  1087. Rest = 0;
  1088. FBDiv += 1;
  1089. } else if (Rest > (FREF - 2 * LPF))
  1090. Rest = FREF - 2 * LPF;
  1091. Rest = (Rest * 6528) / (FREF / 10);
  1092. Den = 1;
  1093. dprintk(" ***** ******* Rest value = %d", Rest);
  1094. if (Rest > 0) {
  1095. if (state->config->analog_output)
  1096. lo6 |= (1 << 2) | 2;
  1097. else
  1098. lo6 |= (1 << 2) | 1;
  1099. Den = 255;
  1100. }
  1101. #ifdef CONFIG_BAND_SBAND
  1102. if (state->current_band == BAND_SBAND)
  1103. lo6 &= 0xfffb;
  1104. #endif
  1105. dib0090_write_reg(state, 0x15, (u16) FBDiv);
  1106. dib0090_write_reg(state, 0x16, (Den << 8) | 1);
  1107. dib0090_write_reg(state, 0x17, (u16) Rest);
  1108. dib0090_write_reg(state, 0x19, lo5);
  1109. dib0090_write_reg(state, 0x1c, lo6);
  1110. lo6 = tune->tuner_enable;
  1111. if (state->config->analog_output)
  1112. lo6 = (lo6 & 0xff9f) | 0x2;
  1113. dib0090_write_reg(state, 0x24, lo6 | EN_LO
  1114. #ifdef CONFIG_DIB0090_USE_PWM_AGC
  1115. | state->config->use_pwm_agc * EN_CRYSTAL
  1116. #endif
  1117. );
  1118. state->current_rf = rf;
  1119. /* prepare a complete captrim */
  1120. state->step = state->captrim = state->fcaptrim = 64;
  1121. } else { /* we are already tuned to this frequency - the configuration is correct */
  1122. /* do a minimal captrim even if the frequency has not changed */
  1123. state->step = 4;
  1124. state->captrim = state->fcaptrim = dib0090_read_reg(state, 0x18) & 0x7f;
  1125. }
  1126. state->adc_diff = 3000;
  1127. dib0090_write_reg(state, 0x10, 0x2B1);
  1128. dib0090_write_reg(state, 0x1e, 0x0032);
  1129. ret = 20;
  1130. *tune_state = CT_TUNER_STEP_1;
  1131. } else if (*tune_state == CT_TUNER_STEP_0) {
  1132. /* nothing */
  1133. } else if (*tune_state == CT_TUNER_STEP_1) {
  1134. state->step /= 2;
  1135. dib0090_write_reg(state, 0x18, lo4 | state->captrim);
  1136. *tune_state = CT_TUNER_STEP_2;
  1137. } else if (*tune_state == CT_TUNER_STEP_2) {
  1138. adc = dib0090_read_reg(state, 0x1d);
  1139. dprintk("FE %d CAPTRIM=%d; ADC = %d (ADC) & %dmV", (u32) fe->id, (u32) state->captrim, (u32) adc,
  1140. (u32) (adc) * (u32) 1800 / (u32) 1024);
  1141. if (adc >= 400) {
  1142. adc -= 400;
  1143. step_sign = -1;
  1144. } else {
  1145. adc = 400 - adc;
  1146. step_sign = 1;
  1147. }
  1148. if (adc < state->adc_diff) {
  1149. dprintk("FE %d CAPTRIM=%d is closer to target (%d/%d)", (u32) fe->id, (u32) state->captrim, (u32) adc, (u32) state->adc_diff);
  1150. state->adc_diff = adc;
  1151. state->fcaptrim = state->captrim;
  1152. }
  1153. state->captrim += step_sign * state->step;
  1154. if (state->step >= 1)
  1155. *tune_state = CT_TUNER_STEP_1;
  1156. else
  1157. *tune_state = CT_TUNER_STEP_3;
  1158. ret = 15;
  1159. } else if (*tune_state == CT_TUNER_STEP_3) {
  1160. /*write the final cptrim config */
  1161. dib0090_write_reg(state, 0x18, lo4 | state->fcaptrim);
  1162. #ifdef CONFIG_TUNER_DIB0090_CAPTRIM_MEMORY
  1163. state->memory[state->memory_index].cap = state->fcaptrim;
  1164. #endif
  1165. *tune_state = CT_TUNER_STEP_4;
  1166. } else if (*tune_state == CT_TUNER_STEP_4) {
  1167. dib0090_write_reg(state, 0x1e, 0x07ff);
  1168. dprintk("FE %d Final Captrim: %d", (u32) fe->id, (u32) state->fcaptrim);
  1169. dprintk("FE %d HFDIV code: %d", (u32) fe->id, (u32) pll->hfdiv_code);
  1170. dprintk("FE %d VCO = %d", (u32) fe->id, (u32) pll->vco_band);
  1171. dprintk("FE %d VCOF in kHz: %d ((%d*%d) << 1))", (u32) fe->id, (u32) ((pll->hfdiv * rf) * 2), (u32) pll->hfdiv, (u32) rf);
  1172. dprintk("FE %d REFDIV: %d, FREF: %d", (u32) fe->id, (u32) 1, (u32) state->config->io.clock_khz);
  1173. dprintk("FE %d FBDIV: %d, Rest: %d", (u32) fe->id, (u32) dib0090_read_reg(state, 0x15), (u32) dib0090_read_reg(state, 0x17));
  1174. dprintk("FE %d Num: %d, Den: %d, SD: %d", (u32) fe->id, (u32) dib0090_read_reg(state, 0x17),
  1175. (u32) (dib0090_read_reg(state, 0x16) >> 8), (u32) dib0090_read_reg(state, 0x1c) & 0x3);
  1176. c = 4;
  1177. i = 3;
  1178. #if defined(CONFIG_BAND_LBAND) || defined(CONFIG_BAND_SBAND)
  1179. if ((state->current_band == BAND_LBAND) || (state->current_band == BAND_SBAND)) {
  1180. c = 2;
  1181. i = 2;
  1182. }
  1183. #endif
  1184. dib0090_write_reg(state, 0x10, (c << 13) | (i << 11) | (WBD
  1185. #ifdef CONFIG_DIB0090_USE_PWM_AGC
  1186. | (state->config->use_pwm_agc << 1)
  1187. #endif
  1188. ));
  1189. dib0090_write_reg(state, 0x09, (tune->lna_tune << 5) | (tune->lna_bias << 0));
  1190. dib0090_write_reg(state, 0x0c, tune->v2i);
  1191. dib0090_write_reg(state, 0x0d, tune->mix);
  1192. dib0090_write_reg(state, 0x0e, tune->load);
  1193. *tune_state = CT_TUNER_STEP_5;
  1194. } else if (*tune_state == CT_TUNER_STEP_5) {
  1195. /* initialize the lt gain register */
  1196. state->rf_lt_def = 0x7c00;
  1197. dib0090_write_reg(state, 0x0f, state->rf_lt_def);
  1198. dib0090_set_bandwidth(state);
  1199. state->tuner_is_tuned = 1;
  1200. *tune_state = CT_TUNER_STOP;
  1201. } else
  1202. ret = FE_CALLBACK_TIME_NEVER;
  1203. return ret;
  1204. }
  1205. static int dib0090_release(struct dvb_frontend *fe)
  1206. {
  1207. kfree(fe->tuner_priv);
  1208. fe->tuner_priv = NULL;
  1209. return 0;
  1210. }
  1211. enum frontend_tune_state dib0090_get_tune_state(struct dvb_frontend *fe)
  1212. {
  1213. struct dib0090_state *state = fe->tuner_priv;
  1214. return state->tune_state;
  1215. }
  1216. EXPORT_SYMBOL(dib0090_get_tune_state);
  1217. int dib0090_set_tune_state(struct dvb_frontend *fe, enum frontend_tune_state tune_state)
  1218. {
  1219. struct dib0090_state *state = fe->tuner_priv;
  1220. state->tune_state = tune_state;
  1221. return 0;
  1222. }
  1223. EXPORT_SYMBOL(dib0090_set_tune_state);
  1224. static int dib0090_get_frequency(struct dvb_frontend *fe, u32 * frequency)
  1225. {
  1226. struct dib0090_state *state = fe->tuner_priv;
  1227. *frequency = 1000 * state->current_rf;
  1228. return 0;
  1229. }
  1230. static int dib0090_set_params(struct dvb_frontend *fe, struct dvb_frontend_parameters *p)
  1231. {
  1232. struct dib0090_state *state = fe->tuner_priv;
  1233. uint32_t ret;
  1234. state->tune_state = CT_TUNER_START;
  1235. do {
  1236. ret = dib0090_tune(fe);
  1237. if (ret != FE_CALLBACK_TIME_NEVER)
  1238. msleep(ret / 10);
  1239. else
  1240. break;
  1241. } while (state->tune_state != CT_TUNER_STOP);
  1242. return 0;
  1243. }
  1244. static const struct dvb_tuner_ops dib0090_ops = {
  1245. .info = {
  1246. .name = "DiBcom DiB0090",
  1247. .frequency_min = 45000000,
  1248. .frequency_max = 860000000,
  1249. .frequency_step = 1000,
  1250. },
  1251. .release = dib0090_release,
  1252. .init = dib0090_wakeup,
  1253. .sleep = dib0090_sleep,
  1254. .set_params = dib0090_set_params,
  1255. .get_frequency = dib0090_get_frequency,
  1256. };
  1257. struct dvb_frontend *dib0090_register(struct dvb_frontend *fe, struct i2c_adapter *i2c, const struct dib0090_config *config)
  1258. {
  1259. struct dib0090_state *st = kzalloc(sizeof(struct dib0090_state), GFP_KERNEL);
  1260. if (st == NULL)
  1261. return NULL;
  1262. st->config = config;
  1263. st->i2c = i2c;
  1264. st->fe = fe;
  1265. fe->tuner_priv = st;
  1266. if (dib0090_reset(fe) != 0)
  1267. goto free_mem;
  1268. printk(KERN_INFO "DiB0090: successfully identified\n");
  1269. memcpy(&fe->ops.tuner_ops, &dib0090_ops, sizeof(struct dvb_tuner_ops));
  1270. return fe;
  1271. free_mem:
  1272. kfree(st);
  1273. fe->tuner_priv = NULL;
  1274. return NULL;
  1275. }
  1276. EXPORT_SYMBOL(dib0090_register);
  1277. MODULE_AUTHOR("Patrick Boettcher <pboettcher@dibcom.fr>");
  1278. MODULE_AUTHOR("Olivier Grenie <olivier.grenie@dibcom.fr>");
  1279. MODULE_DESCRIPTION("Driver for the DiBcom 0090 base-band RF Tuner");
  1280. MODULE_LICENSE("GPL");