dib0090.c 41 KB

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