dib0090.c 69 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456
  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_DVBT
  43. #define CONFIG_SYS_ISDBT
  44. #define CONFIG_BAND_CBAND
  45. #define CONFIG_BAND_VHF
  46. #define CONFIG_BAND_UHF
  47. #define CONFIG_DIB0090_USE_PWM_AGC
  48. #define EN_LNA0 0x8000
  49. #define EN_LNA1 0x4000
  50. #define EN_LNA2 0x2000
  51. #define EN_LNA3 0x1000
  52. #define EN_MIX0 0x0800
  53. #define EN_MIX1 0x0400
  54. #define EN_MIX2 0x0200
  55. #define EN_MIX3 0x0100
  56. #define EN_IQADC 0x0040
  57. #define EN_PLL 0x0020
  58. #define EN_TX 0x0010
  59. #define EN_BB 0x0008
  60. #define EN_LO 0x0004
  61. #define EN_BIAS 0x0001
  62. #define EN_IQANA 0x0002
  63. #define EN_DIGCLK 0x0080 /* not in the 0x24 reg, only in 0x1b */
  64. #define EN_CRYSTAL 0x0002
  65. #define EN_UHF 0x22E9
  66. #define EN_VHF 0x44E9
  67. #define EN_LBD 0x11E9
  68. #define EN_SBD 0x44E9
  69. #define EN_CAB 0x88E9
  70. /* Calibration defines */
  71. #define DC_CAL 0x1
  72. #define WBD_CAL 0x2
  73. #define TEMP_CAL 0x4
  74. #define CAPTRIM_CAL 0x8
  75. #define KROSUS_PLL_LOCKED 0x800
  76. #define KROSUS 0x2
  77. /* Use those defines to identify SOC version */
  78. #define SOC 0x02
  79. #define SOC_7090_P1G_11R1 0x82
  80. #define SOC_7090_P1G_21R1 0x8a
  81. #define SOC_8090_P1G_11R1 0x86
  82. #define SOC_8090_P1G_21R1 0x8e
  83. /* else use thos ones to check */
  84. #define P1A_B 0x0
  85. #define P1C 0x1
  86. #define P1D_E_F 0x3
  87. #define P1G 0x7
  88. #define P1G_21R2 0xf
  89. #define MP001 0x1 /* Single 9090/8096 */
  90. #define MP005 0x4 /* Single Sband */
  91. #define MP008 0x6 /* Dual diversity VHF-UHF-LBAND */
  92. #define MP009 0x7 /* Dual diversity 29098 CBAND-UHF-LBAND-SBAND */
  93. #define pgm_read_word(w) (*w)
  94. struct dc_calibration;
  95. struct dib0090_tuning {
  96. u32 max_freq; /* for every frequency less than or equal to that field: this information is correct */
  97. u8 switch_trim;
  98. u8 lna_tune;
  99. u16 lna_bias;
  100. u16 v2i;
  101. u16 mix;
  102. u16 load;
  103. u16 tuner_enable;
  104. };
  105. struct dib0090_pll {
  106. u32 max_freq; /* for every frequency less than or equal to that field: this information is correct */
  107. u8 vco_band;
  108. u8 hfdiv_code;
  109. u8 hfdiv;
  110. u8 topresc;
  111. };
  112. struct dib0090_identity {
  113. u8 version;
  114. u8 product;
  115. u8 p1g;
  116. u8 in_soc;
  117. };
  118. struct dib0090_state {
  119. struct i2c_adapter *i2c;
  120. struct dvb_frontend *fe;
  121. const struct dib0090_config *config;
  122. u8 current_band;
  123. enum frontend_tune_state tune_state;
  124. u32 current_rf;
  125. u16 wbd_offset;
  126. s16 wbd_target; /* in dB */
  127. s16 rf_gain_limit; /* take-over-point: where to split between bb and rf gain */
  128. s16 current_gain; /* keeps the currently programmed gain */
  129. u8 agc_step; /* new binary search */
  130. u16 gain[2]; /* for channel monitoring */
  131. const u16 *rf_ramp;
  132. const u16 *bb_ramp;
  133. /* for the software AGC ramps */
  134. u16 bb_1_def;
  135. u16 rf_lt_def;
  136. u16 gain_reg[4];
  137. /* for the captrim/dc-offset search */
  138. s8 step;
  139. s16 adc_diff;
  140. s16 min_adc_diff;
  141. s8 captrim;
  142. s8 fcaptrim;
  143. const struct dc_calibration *dc;
  144. u16 bb6, bb7;
  145. const struct dib0090_tuning *current_tune_table_index;
  146. const struct dib0090_pll *current_pll_table_index;
  147. u8 tuner_is_tuned;
  148. u8 agc_freeze;
  149. struct dib0090_identity identity;
  150. u32 rf_request;
  151. u8 current_standard;
  152. u8 calibrate;
  153. u32 rest;
  154. u16 bias;
  155. s16 temperature;
  156. u8 wbd_calibration_gain;
  157. const struct dib0090_wbd_slope *current_wbd_table;
  158. u16 wbdmux;
  159. };
  160. struct dib0090_fw_state {
  161. struct i2c_adapter *i2c;
  162. struct dvb_frontend *fe;
  163. struct dib0090_identity identity;
  164. const struct dib0090_config *config;
  165. };
  166. static u16 dib0090_read_reg(struct dib0090_state *state, u8 reg)
  167. {
  168. u8 b[2];
  169. struct i2c_msg msg[2] = {
  170. {.addr = state->config->i2c_address, .flags = 0, .buf = &reg, .len = 1},
  171. {.addr = state->config->i2c_address, .flags = I2C_M_RD, .buf = b, .len = 2},
  172. };
  173. if (i2c_transfer(state->i2c, msg, 2) != 2) {
  174. printk(KERN_WARNING "DiB0090 I2C read failed\n");
  175. return 0;
  176. }
  177. return (b[0] << 8) | b[1];
  178. }
  179. static int dib0090_write_reg(struct dib0090_state *state, u32 reg, u16 val)
  180. {
  181. u8 b[3] = { reg & 0xff, val >> 8, val & 0xff };
  182. struct i2c_msg msg = {.addr = state->config->i2c_address, .flags = 0, .buf = b, .len = 3 };
  183. if (i2c_transfer(state->i2c, &msg, 1) != 1) {
  184. printk(KERN_WARNING "DiB0090 I2C write failed\n");
  185. return -EREMOTEIO;
  186. }
  187. return 0;
  188. }
  189. static u16 dib0090_fw_read_reg(struct dib0090_fw_state *state, u8 reg)
  190. {
  191. u8 b[2];
  192. struct i2c_msg msg = {.addr = reg, .flags = I2C_M_RD, .buf = b, .len = 2 };
  193. if (i2c_transfer(state->i2c, &msg, 1) != 1) {
  194. printk(KERN_WARNING "DiB0090 I2C read failed\n");
  195. return 0;
  196. }
  197. return (b[0] << 8) | b[1];
  198. }
  199. static int dib0090_fw_write_reg(struct dib0090_fw_state *state, u8 reg, u16 val)
  200. {
  201. u8 b[2] = { val >> 8, val & 0xff };
  202. struct i2c_msg msg = {.addr = reg, .flags = 0, .buf = b, .len = 2 };
  203. if (i2c_transfer(state->i2c, &msg, 1) != 1) {
  204. printk(KERN_WARNING "DiB0090 I2C write failed\n");
  205. return -EREMOTEIO;
  206. }
  207. return 0;
  208. }
  209. #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)
  210. #define ADC_TARGET -220
  211. #define GAIN_ALPHA 5
  212. #define WBD_ALPHA 6
  213. #define LPF 100
  214. static void dib0090_write_regs(struct dib0090_state *state, u8 r, const u16 * b, u8 c)
  215. {
  216. do {
  217. dib0090_write_reg(state, r++, *b++);
  218. } while (--c);
  219. }
  220. static int dib0090_identify(struct dvb_frontend *fe)
  221. {
  222. struct dib0090_state *state = fe->tuner_priv;
  223. u16 v;
  224. struct dib0090_identity *identity = &state->identity;
  225. v = dib0090_read_reg(state, 0x1a);
  226. identity->p1g = 0;
  227. identity->in_soc = 0;
  228. dprintk("Tuner identification (Version = 0x%04x)", v);
  229. /* without PLL lock info */
  230. v &= ~KROSUS_PLL_LOCKED;
  231. identity->version = v & 0xff;
  232. identity->product = (v >> 8) & 0xf;
  233. if (identity->product != KROSUS)
  234. goto identification_error;
  235. if ((identity->version & 0x3) == SOC) {
  236. identity->in_soc = 1;
  237. switch (identity->version) {
  238. case SOC_8090_P1G_11R1:
  239. dprintk("SOC 8090 P1-G11R1 Has been detected");
  240. identity->p1g = 1;
  241. break;
  242. case SOC_8090_P1G_21R1:
  243. dprintk("SOC 8090 P1-G21R1 Has been detected");
  244. identity->p1g = 1;
  245. break;
  246. case SOC_7090_P1G_11R1:
  247. dprintk("SOC 7090 P1-G11R1 Has been detected");
  248. identity->p1g = 1;
  249. break;
  250. case SOC_7090_P1G_21R1:
  251. dprintk("SOC 7090 P1-G21R1 Has been detected");
  252. identity->p1g = 1;
  253. break;
  254. default:
  255. goto identification_error;
  256. }
  257. } else {
  258. switch ((identity->version >> 5) & 0x7) {
  259. case MP001:
  260. dprintk("MP001 : 9090/8096");
  261. break;
  262. case MP005:
  263. dprintk("MP005 : Single Sband");
  264. break;
  265. case MP008:
  266. dprintk("MP008 : diversity VHF-UHF-LBAND");
  267. break;
  268. case MP009:
  269. dprintk("MP009 : diversity 29098 CBAND-UHF-LBAND-SBAND");
  270. break;
  271. default:
  272. goto identification_error;
  273. }
  274. switch (identity->version & 0x1f) {
  275. case P1G_21R2:
  276. dprintk("P1G_21R2 detected");
  277. identity->p1g = 1;
  278. break;
  279. case P1G:
  280. dprintk("P1G detected");
  281. identity->p1g = 1;
  282. break;
  283. case P1D_E_F:
  284. dprintk("P1D/E/F detected");
  285. break;
  286. case P1C:
  287. dprintk("P1C detected");
  288. break;
  289. case P1A_B:
  290. dprintk("P1-A/B detected: driver is deactivated - not available");
  291. goto identification_error;
  292. break;
  293. default:
  294. goto identification_error;
  295. }
  296. }
  297. return 0;
  298. identification_error:
  299. return -EIO;
  300. }
  301. static int dib0090_fw_identify(struct dvb_frontend *fe)
  302. {
  303. struct dib0090_fw_state *state = fe->tuner_priv;
  304. struct dib0090_identity *identity = &state->identity;
  305. u16 v = dib0090_fw_read_reg(state, 0x1a);
  306. identity->p1g = 0;
  307. identity->in_soc = 0;
  308. dprintk("FE: Tuner identification (Version = 0x%04x)", v);
  309. /* without PLL lock info */
  310. v &= ~KROSUS_PLL_LOCKED;
  311. identity->version = v & 0xff;
  312. identity->product = (v >> 8) & 0xf;
  313. if (identity->product != KROSUS)
  314. goto identification_error;
  315. if ((identity->version & 0x3) == SOC) {
  316. identity->in_soc = 1;
  317. switch (identity->version) {
  318. case SOC_8090_P1G_11R1:
  319. dprintk("SOC 8090 P1-G11R1 Has been detected");
  320. identity->p1g = 1;
  321. break;
  322. case SOC_8090_P1G_21R1:
  323. dprintk("SOC 8090 P1-G21R1 Has been detected");
  324. identity->p1g = 1;
  325. break;
  326. case SOC_7090_P1G_11R1:
  327. dprintk("SOC 7090 P1-G11R1 Has been detected");
  328. identity->p1g = 1;
  329. break;
  330. case SOC_7090_P1G_21R1:
  331. dprintk("SOC 7090 P1-G21R1 Has been detected");
  332. identity->p1g = 1;
  333. break;
  334. default:
  335. goto identification_error;
  336. }
  337. } else {
  338. switch ((identity->version >> 5) & 0x7) {
  339. case MP001:
  340. dprintk("MP001 : 9090/8096");
  341. break;
  342. case MP005:
  343. dprintk("MP005 : Single Sband");
  344. break;
  345. case MP008:
  346. dprintk("MP008 : diversity VHF-UHF-LBAND");
  347. break;
  348. case MP009:
  349. dprintk("MP009 : diversity 29098 CBAND-UHF-LBAND-SBAND");
  350. break;
  351. default:
  352. goto identification_error;
  353. }
  354. switch (identity->version & 0x1f) {
  355. case P1G_21R2:
  356. dprintk("P1G_21R2 detected");
  357. identity->p1g = 1;
  358. break;
  359. case P1G:
  360. dprintk("P1G detected");
  361. identity->p1g = 1;
  362. break;
  363. case P1D_E_F:
  364. dprintk("P1D/E/F detected");
  365. break;
  366. case P1C:
  367. dprintk("P1C detected");
  368. break;
  369. case P1A_B:
  370. dprintk("P1-A/B detected: driver is deactivated - not available");
  371. goto identification_error;
  372. break;
  373. default:
  374. goto identification_error;
  375. }
  376. }
  377. return 0;
  378. identification_error:
  379. return -EIO;;
  380. }
  381. static void dib0090_reset_digital(struct dvb_frontend *fe, const struct dib0090_config *cfg)
  382. {
  383. struct dib0090_state *state = fe->tuner_priv;
  384. u16 PllCfg, i, v;
  385. HARD_RESET(state);
  386. dib0090_write_reg(state, 0x24, EN_PLL | EN_CRYSTAL);
  387. dib0090_write_reg(state, 0x1b, EN_DIGCLK | EN_PLL | EN_CRYSTAL); /* PLL, DIG_CLK and CRYSTAL remain */
  388. if (!cfg->in_soc) {
  389. /* adcClkOutRatio=8->7, release reset */
  390. dib0090_write_reg(state, 0x20, ((cfg->io.adc_clock_ratio - 1) << 11) | (0 << 10) | (1 << 9) | (1 << 8) | (0 << 4) | 0);
  391. if (cfg->clkoutdrive != 0)
  392. dib0090_write_reg(state, 0x23, (0 << 15) | ((!cfg->analog_output) << 14) | (2 << 10) | (1 << 9) | (0 << 8)
  393. | (cfg->clkoutdrive << 5) | (cfg->clkouttobamse << 4) | (0 << 2) | (0));
  394. else
  395. dib0090_write_reg(state, 0x23, (0 << 15) | ((!cfg->analog_output) << 14) | (2 << 10) | (1 << 9) | (0 << 8)
  396. | (7 << 5) | (cfg->clkouttobamse << 4) | (0 << 2) | (0));
  397. }
  398. /* Read Pll current config * */
  399. PllCfg = dib0090_read_reg(state, 0x21);
  400. /** Reconfigure PLL if current setting is different from default setting **/
  401. if ((PllCfg & 0x1FFF) != ((cfg->io.pll_range << 12) | (cfg->io.pll_loopdiv << 6) | (cfg->io.pll_prediv)) && (!cfg->in_soc)
  402. && !cfg->io.pll_bypass) {
  403. /* Set Bypass mode */
  404. PllCfg |= (1 << 15);
  405. dib0090_write_reg(state, 0x21, PllCfg);
  406. /* Set Reset Pll */
  407. PllCfg &= ~(1 << 13);
  408. dib0090_write_reg(state, 0x21, PllCfg);
  409. /*** Set new Pll configuration in bypass and reset state ***/
  410. PllCfg = (1 << 15) | (0 << 13) | (cfg->io.pll_range << 12) | (cfg->io.pll_loopdiv << 6) | (cfg->io.pll_prediv);
  411. dib0090_write_reg(state, 0x21, PllCfg);
  412. /* Remove Reset Pll */
  413. PllCfg |= (1 << 13);
  414. dib0090_write_reg(state, 0x21, PllCfg);
  415. /*** Wait for PLL lock ***/
  416. i = 100;
  417. do {
  418. v = !!(dib0090_read_reg(state, 0x1a) & 0x800);
  419. if (v)
  420. break;
  421. } while (--i);
  422. if (i == 0) {
  423. dprintk("Pll: Unable to lock Pll");
  424. return;
  425. }
  426. /* Finally Remove Bypass mode */
  427. PllCfg &= ~(1 << 15);
  428. dib0090_write_reg(state, 0x21, PllCfg);
  429. }
  430. if (cfg->io.pll_bypass) {
  431. PllCfg |= (cfg->io.pll_bypass << 15);
  432. dib0090_write_reg(state, 0x21, PllCfg);
  433. }
  434. }
  435. static int dib0090_fw_reset_digital(struct dvb_frontend *fe, const struct dib0090_config *cfg)
  436. {
  437. struct dib0090_fw_state *state = fe->tuner_priv;
  438. u16 PllCfg;
  439. u16 v;
  440. int i;
  441. dprintk("fw reset digital");
  442. HARD_RESET(state);
  443. dib0090_fw_write_reg(state, 0x24, EN_PLL | EN_CRYSTAL);
  444. dib0090_fw_write_reg(state, 0x1b, EN_DIGCLK | EN_PLL | EN_CRYSTAL); /* PLL, DIG_CLK and CRYSTAL remain */
  445. dib0090_fw_write_reg(state, 0x20,
  446. ((cfg->io.adc_clock_ratio - 1) << 11) | (0 << 10) | (1 << 9) | (1 << 8) | (cfg->data_tx_drv << 4) | cfg->ls_cfg_pad_drv);
  447. v = (0 << 15) | ((!cfg->analog_output) << 14) | (1 << 9) | (0 << 8) | (cfg->clkouttobamse << 4) | (0 << 2) | (0);
  448. if (cfg->clkoutdrive != 0)
  449. v |= cfg->clkoutdrive << 5;
  450. else
  451. v |= 7 << 5;
  452. v |= 2 << 10;
  453. dib0090_fw_write_reg(state, 0x23, v);
  454. /* Read Pll current config * */
  455. PllCfg = dib0090_fw_read_reg(state, 0x21);
  456. /** Reconfigure PLL if current setting is different from default setting **/
  457. if ((PllCfg & 0x1FFF) != ((cfg->io.pll_range << 12) | (cfg->io.pll_loopdiv << 6) | (cfg->io.pll_prediv)) && !cfg->io.pll_bypass) {
  458. /* Set Bypass mode */
  459. PllCfg |= (1 << 15);
  460. dib0090_fw_write_reg(state, 0x21, PllCfg);
  461. /* Set Reset Pll */
  462. PllCfg &= ~(1 << 13);
  463. dib0090_fw_write_reg(state, 0x21, PllCfg);
  464. /*** Set new Pll configuration in bypass and reset state ***/
  465. PllCfg = (1 << 15) | (0 << 13) | (cfg->io.pll_range << 12) | (cfg->io.pll_loopdiv << 6) | (cfg->io.pll_prediv);
  466. dib0090_fw_write_reg(state, 0x21, PllCfg);
  467. /* Remove Reset Pll */
  468. PllCfg |= (1 << 13);
  469. dib0090_fw_write_reg(state, 0x21, PllCfg);
  470. /*** Wait for PLL lock ***/
  471. i = 100;
  472. do {
  473. v = !!(dib0090_fw_read_reg(state, 0x1a) & 0x800);
  474. if (v)
  475. break;
  476. } while (--i);
  477. if (i == 0) {
  478. dprintk("Pll: Unable to lock Pll");
  479. return -EIO;
  480. }
  481. /* Finally Remove Bypass mode */
  482. PllCfg &= ~(1 << 15);
  483. dib0090_fw_write_reg(state, 0x21, PllCfg);
  484. }
  485. if (cfg->io.pll_bypass) {
  486. PllCfg |= (cfg->io.pll_bypass << 15);
  487. dib0090_fw_write_reg(state, 0x21, PllCfg);
  488. }
  489. return dib0090_fw_identify(fe);
  490. }
  491. static int dib0090_wakeup(struct dvb_frontend *fe)
  492. {
  493. struct dib0090_state *state = fe->tuner_priv;
  494. if (state->config->sleep)
  495. state->config->sleep(fe, 0);
  496. /* enable dataTX in case we have been restarted in the wrong moment */
  497. dib0090_write_reg(state, 0x23, dib0090_read_reg(state, 0x23) | (1 << 14));
  498. return 0;
  499. }
  500. static int dib0090_sleep(struct dvb_frontend *fe)
  501. {
  502. struct dib0090_state *state = fe->tuner_priv;
  503. if (state->config->sleep)
  504. state->config->sleep(fe, 1);
  505. return 0;
  506. }
  507. void dib0090_dcc_freq(struct dvb_frontend *fe, u8 fast)
  508. {
  509. struct dib0090_state *state = fe->tuner_priv;
  510. if (fast)
  511. dib0090_write_reg(state, 0x04, 0);
  512. else
  513. dib0090_write_reg(state, 0x04, 1);
  514. }
  515. EXPORT_SYMBOL(dib0090_dcc_freq);
  516. static const u16 bb_ramp_pwm_normal_socs[] = {
  517. 550, /* max BB gain in 10th of dB */
  518. (1 << 9) | 8, /* ramp_slope = 1dB of gain -> clock_ticks_per_db = clk_khz / ramp_slope -> BB_RAMP2 */
  519. 440,
  520. (4 << 9) | 0, /* BB_RAMP3 = 26dB */
  521. (0 << 9) | 208, /* BB_RAMP4 */
  522. (4 << 9) | 208, /* BB_RAMP5 = 29dB */
  523. (0 << 9) | 440, /* BB_RAMP6 */
  524. };
  525. static const u16 rf_ramp_pwm_cband_7090[] = {
  526. 280, /* max RF gain in 10th of dB */
  527. 18, /* ramp_slope = 1dB of gain -> clock_ticks_per_db = clk_khz / ramp_slope -> RF_RAMP2 */
  528. 504, /* ramp_max = maximum X used on the ramp */
  529. (29 << 10) | 364, /* RF_RAMP5, LNA 1 = 8dB */
  530. (0 << 10) | 504, /* RF_RAMP6, LNA 1 */
  531. (60 << 10) | 228, /* RF_RAMP7, LNA 2 = 7.7dB */
  532. (0 << 10) | 364, /* RF_RAMP8, LNA 2 */
  533. (34 << 10) | 109, /* GAIN_4_1, LNA 3 = 6.8dB */
  534. (0 << 10) | 228, /* GAIN_4_2, LNA 3 */
  535. (37 << 10) | 0, /* RF_RAMP3, LNA 4 = 6.2dB */
  536. (0 << 10) | 109, /* RF_RAMP4, LNA 4 */
  537. };
  538. static const u16 rf_ramp_pwm_cband_8090[] = {
  539. 345, /* max RF gain in 10th of dB */
  540. 29, /* ramp_slope = 1dB of gain -> clock_ticks_per_db = clk_khz / ramp_slope -> RF_RAMP2 */
  541. 1000, /* ramp_max = maximum X used on the ramp */
  542. (35 << 10) | 772, /* RF_RAMP3, LNA 1 = 8dB */
  543. (0 << 10) | 1000, /* RF_RAMP4, LNA 1 */
  544. (58 << 10) | 496, /* RF_RAMP5, LNA 2 = 9.5dB */
  545. (0 << 10) | 772, /* RF_RAMP6, LNA 2 */
  546. (27 << 10) | 200, /* RF_RAMP7, LNA 3 = 10.5dB */
  547. (0 << 10) | 496, /* RF_RAMP8, LNA 3 */
  548. (40 << 10) | 0, /* GAIN_4_1, LNA 4 = 7dB */
  549. (0 << 10) | 200, /* GAIN_4_2, LNA 4 */
  550. };
  551. static const u16 rf_ramp_pwm_uhf_7090[] = {
  552. 407, /* max RF gain in 10th of dB */
  553. 13, /* ramp_slope = 1dB of gain -> clock_ticks_per_db = clk_khz / ramp_slope -> RF_RAMP2 */
  554. 529, /* ramp_max = maximum X used on the ramp */
  555. (23 << 10) | 0, /* RF_RAMP3, LNA 1 = 14.7dB */
  556. (0 << 10) | 176, /* RF_RAMP4, LNA 1 */
  557. (63 << 10) | 400, /* RF_RAMP5, LNA 2 = 8dB */
  558. (0 << 10) | 529, /* RF_RAMP6, LNA 2 */
  559. (48 << 10) | 316, /* RF_RAMP7, LNA 3 = 6.8dB */
  560. (0 << 10) | 400, /* RF_RAMP8, LNA 3 */
  561. (29 << 10) | 176, /* GAIN_4_1, LNA 4 = 11.5dB */
  562. (0 << 10) | 316, /* GAIN_4_2, LNA 4 */
  563. };
  564. static const u16 rf_ramp_pwm_uhf_8090[] = {
  565. 388, /* max RF gain in 10th of dB */
  566. 26, /* ramp_slope = 1dB of gain -> clock_ticks_per_db = clk_khz / ramp_slope -> RF_RAMP2 */
  567. 1008, /* ramp_max = maximum X used on the ramp */
  568. (11 << 10) | 0, /* RF_RAMP3, LNA 1 = 14.7dB */
  569. (0 << 10) | 369, /* RF_RAMP4, LNA 1 */
  570. (41 << 10) | 809, /* RF_RAMP5, LNA 2 = 8dB */
  571. (0 << 10) | 1008, /* RF_RAMP6, LNA 2 */
  572. (27 << 10) | 659, /* RF_RAMP7, LNA 3 = 6dB */
  573. (0 << 10) | 809, /* RF_RAMP8, LNA 3 */
  574. (14 << 10) | 369, /* GAIN_4_1, LNA 4 = 11.5dB */
  575. (0 << 10) | 659, /* GAIN_4_2, LNA 4 */
  576. };
  577. static const u16 rf_ramp_pwm_cband[] = {
  578. 0, /* max RF gain in 10th of dB */
  579. 0, /* ramp_slope = 1dB of gain -> clock_ticks_per_db = clk_khz / ramp_slope -> 0x2b */
  580. 0, /* ramp_max = maximum X used on the ramp */
  581. (0 << 10) | 0, /* 0x2c, LNA 1 = 0dB */
  582. (0 << 10) | 0, /* 0x2d, LNA 1 */
  583. (0 << 10) | 0, /* 0x2e, LNA 2 = 0dB */
  584. (0 << 10) | 0, /* 0x2f, LNA 2 */
  585. (0 << 10) | 0, /* 0x30, LNA 3 = 0dB */
  586. (0 << 10) | 0, /* 0x31, LNA 3 */
  587. (0 << 10) | 0, /* GAIN_4_1, LNA 4 = 0dB */
  588. (0 << 10) | 0, /* GAIN_4_2, LNA 4 */
  589. };
  590. static const u16 rf_ramp_vhf[] = {
  591. 412, /* max RF gain in 10th of dB */
  592. 132, 307, 127, /* LNA1, 13.2dB */
  593. 105, 412, 255, /* LNA2, 10.5dB */
  594. 50, 50, 127, /* LNA3, 5dB */
  595. 125, 175, 127, /* LNA4, 12.5dB */
  596. 0, 0, 127, /* CBAND, 0dB */
  597. };
  598. static const u16 rf_ramp_uhf[] = {
  599. 412, /* max RF gain in 10th of dB */
  600. 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 */
  601. 105, 412, 255, /* LNA2 : 10.5 dB */
  602. 50, 50, 127, /* LNA3 : 5.0 dB */
  603. 125, 175, 127, /* LNA4 : 12.5 dB */
  604. 0, 0, 127, /* CBAND : 0.0 dB */
  605. };
  606. static const u16 rf_ramp_cband_broadmatching[] = /* for p1G only */
  607. {
  608. 314, /* Calibrated at 200MHz order has been changed g4-g3-g2-g1 */
  609. 84, 314, 127, /* LNA1 */
  610. 80, 230, 255, /* LNA2 */
  611. 80, 150, 127, /* LNA3 It was measured 12dB, do not lock if 120 */
  612. 70, 70, 127, /* LNA4 */
  613. 0, 0, 127, /* CBAND */
  614. };
  615. static const u16 rf_ramp_cband[] = {
  616. 332, /* max RF gain in 10th of dB */
  617. 132, 252, 127, /* LNA1, dB */
  618. 80, 332, 255, /* LNA2, dB */
  619. 0, 0, 127, /* LNA3, dB */
  620. 0, 0, 127, /* LNA4, dB */
  621. 120, 120, 127, /* LT1 CBAND */
  622. };
  623. static const u16 rf_ramp_pwm_vhf[] = {
  624. 404, /* max RF gain in 10th of dB */
  625. 25, /* ramp_slope = 1dB of gain -> clock_ticks_per_db = clk_khz / ramp_slope -> 0x2b */
  626. 1011, /* ramp_max = maximum X used on the ramp */
  627. (6 << 10) | 417, /* 0x2c, LNA 1 = 13.2dB */
  628. (0 << 10) | 756, /* 0x2d, LNA 1 */
  629. (16 << 10) | 756, /* 0x2e, LNA 2 = 10.5dB */
  630. (0 << 10) | 1011, /* 0x2f, LNA 2 */
  631. (16 << 10) | 290, /* 0x30, LNA 3 = 5dB */
  632. (0 << 10) | 417, /* 0x31, LNA 3 */
  633. (7 << 10) | 0, /* GAIN_4_1, LNA 4 = 12.5dB */
  634. (0 << 10) | 290, /* GAIN_4_2, LNA 4 */
  635. };
  636. static const u16 rf_ramp_pwm_uhf[] = {
  637. 404, /* max RF gain in 10th of dB */
  638. 25, /* ramp_slope = 1dB of gain -> clock_ticks_per_db = clk_khz / ramp_slope -> 0x2b */
  639. 1011, /* ramp_max = maximum X used on the ramp */
  640. (6 << 10) | 417, /* 0x2c, LNA 1 = 13.2dB */
  641. (0 << 10) | 756, /* 0x2d, LNA 1 */
  642. (16 << 10) | 756, /* 0x2e, LNA 2 = 10.5dB */
  643. (0 << 10) | 1011, /* 0x2f, LNA 2 */
  644. (16 << 10) | 0, /* 0x30, LNA 3 = 5dB */
  645. (0 << 10) | 127, /* 0x31, LNA 3 */
  646. (7 << 10) | 127, /* GAIN_4_1, LNA 4 = 12.5dB */
  647. (0 << 10) | 417, /* GAIN_4_2, LNA 4 */
  648. };
  649. static const u16 bb_ramp_boost[] = {
  650. 550, /* max BB gain in 10th of dB */
  651. 260, 260, 26, /* BB1, 26dB */
  652. 290, 550, 29, /* BB2, 29dB */
  653. };
  654. static const u16 bb_ramp_pwm_normal[] = {
  655. 500, /* max RF gain in 10th of dB */
  656. 8, /* ramp_slope = 1dB of gain -> clock_ticks_per_db = clk_khz / ramp_slope -> 0x34 */
  657. 400,
  658. (2 << 9) | 0, /* 0x35 = 21dB */
  659. (0 << 9) | 168, /* 0x36 */
  660. (2 << 9) | 168, /* 0x37 = 29dB */
  661. (0 << 9) | 400, /* 0x38 */
  662. };
  663. struct slope {
  664. s16 range;
  665. s16 slope;
  666. };
  667. static u16 slopes_to_scale(const struct slope *slopes, u8 num, s16 val)
  668. {
  669. u8 i;
  670. u16 rest;
  671. u16 ret = 0;
  672. for (i = 0; i < num; i++) {
  673. if (val > slopes[i].range)
  674. rest = slopes[i].range;
  675. else
  676. rest = val;
  677. ret += (rest * slopes[i].slope) / slopes[i].range;
  678. val -= rest;
  679. }
  680. return ret;
  681. }
  682. static const struct slope dib0090_wbd_slopes[3] = {
  683. {66, 120}, /* -64,-52: offset - 65 */
  684. {600, 170}, /* -52,-35: 65 - 665 */
  685. {170, 250}, /* -45,-10: 665 - 835 */
  686. };
  687. static s16 dib0090_wbd_to_db(struct dib0090_state *state, u16 wbd)
  688. {
  689. wbd &= 0x3ff;
  690. if (wbd < state->wbd_offset)
  691. wbd = 0;
  692. else
  693. wbd -= state->wbd_offset;
  694. /* -64dB is the floor */
  695. return -640 + (s16) slopes_to_scale(dib0090_wbd_slopes, ARRAY_SIZE(dib0090_wbd_slopes), wbd);
  696. }
  697. static void dib0090_wbd_target(struct dib0090_state *state, u32 rf)
  698. {
  699. u16 offset = 250;
  700. /* TODO : DAB digital N+/-1 interferer perfs : offset = 10 */
  701. if (state->current_band == BAND_VHF)
  702. offset = 650;
  703. #ifndef FIRMWARE_FIREFLY
  704. if (state->current_band == BAND_VHF)
  705. offset = state->config->wbd_vhf_offset;
  706. if (state->current_band == BAND_CBAND)
  707. offset = state->config->wbd_cband_offset;
  708. #endif
  709. state->wbd_target = dib0090_wbd_to_db(state, state->wbd_offset + offset);
  710. dprintk("wbd-target: %d dB", (u32) state->wbd_target);
  711. }
  712. static const int gain_reg_addr[4] = {
  713. 0x08, 0x0a, 0x0f, 0x01
  714. };
  715. static void dib0090_gain_apply(struct dib0090_state *state, s16 gain_delta, s16 top_delta, u8 force)
  716. {
  717. u16 rf, bb, ref;
  718. u16 i, v, gain_reg[4] = { 0 }, gain;
  719. const u16 *g;
  720. if (top_delta < -511)
  721. top_delta = -511;
  722. if (top_delta > 511)
  723. top_delta = 511;
  724. if (force) {
  725. top_delta *= (1 << WBD_ALPHA);
  726. gain_delta *= (1 << GAIN_ALPHA);
  727. }
  728. if (top_delta >= ((s16) (state->rf_ramp[0] << WBD_ALPHA) - state->rf_gain_limit)) /* overflow */
  729. state->rf_gain_limit = state->rf_ramp[0] << WBD_ALPHA;
  730. else
  731. state->rf_gain_limit += top_delta;
  732. if (state->rf_gain_limit < 0) /*underflow */
  733. state->rf_gain_limit = 0;
  734. /* use gain as a temporary variable and correct current_gain */
  735. gain = ((state->rf_gain_limit >> WBD_ALPHA) + state->bb_ramp[0]) << GAIN_ALPHA;
  736. if (gain_delta >= ((s16) gain - state->current_gain)) /* overflow */
  737. state->current_gain = gain;
  738. else
  739. state->current_gain += gain_delta;
  740. /* cannot be less than 0 (only if gain_delta is less than 0 we can have current_gain < 0) */
  741. if (state->current_gain < 0)
  742. state->current_gain = 0;
  743. /* now split total gain to rf and bb gain */
  744. gain = state->current_gain >> GAIN_ALPHA;
  745. /* requested gain is bigger than rf gain limit - ACI/WBD adjustment */
  746. if (gain > (state->rf_gain_limit >> WBD_ALPHA)) {
  747. rf = state->rf_gain_limit >> WBD_ALPHA;
  748. bb = gain - rf;
  749. if (bb > state->bb_ramp[0])
  750. bb = state->bb_ramp[0];
  751. } else { /* high signal level -> all gains put on RF */
  752. rf = gain;
  753. bb = 0;
  754. }
  755. state->gain[0] = rf;
  756. state->gain[1] = bb;
  757. /* software ramp */
  758. /* Start with RF gains */
  759. g = state->rf_ramp + 1; /* point on RF LNA1 max gain */
  760. ref = rf;
  761. for (i = 0; i < 7; i++) { /* Go over all amplifiers => 5RF amps + 2 BB amps = 7 amps */
  762. 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 */
  763. v = 0; /* force the gain to write for the current amp to be null */
  764. else if (ref >= g[1]) /* Gain to set is higher than the high working point of this amp */
  765. v = g[2]; /* force this amp to be full gain */
  766. else /* compute the value to set to this amp because we are somewhere in his range */
  767. v = ((ref - (g[1] - g[0])) * g[2]) / g[0];
  768. if (i == 0) /* LNA 1 reg mapping */
  769. gain_reg[0] = v;
  770. else if (i == 1) /* LNA 2 reg mapping */
  771. gain_reg[0] |= v << 7;
  772. else if (i == 2) /* LNA 3 reg mapping */
  773. gain_reg[1] = v;
  774. else if (i == 3) /* LNA 4 reg mapping */
  775. gain_reg[1] |= v << 7;
  776. else if (i == 4) /* CBAND LNA reg mapping */
  777. gain_reg[2] = v | state->rf_lt_def;
  778. else if (i == 5) /* BB gain 1 reg mapping */
  779. gain_reg[3] = v << 3;
  780. else if (i == 6) /* BB gain 2 reg mapping */
  781. gain_reg[3] |= v << 8;
  782. g += 3; /* go to next gain bloc */
  783. /* When RF is finished, start with BB */
  784. if (i == 4) {
  785. g = state->bb_ramp + 1; /* point on BB gain 1 max gain */
  786. ref = bb;
  787. }
  788. }
  789. gain_reg[3] |= state->bb_1_def;
  790. gain_reg[3] |= ((bb % 10) * 100) / 125;
  791. #ifdef DEBUG_AGC
  792. 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,
  793. gain_reg[0], gain_reg[1], gain_reg[2], gain_reg[3]);
  794. #endif
  795. /* Write the amplifier regs */
  796. for (i = 0; i < 4; i++) {
  797. v = gain_reg[i];
  798. if (force || state->gain_reg[i] != v) {
  799. state->gain_reg[i] = v;
  800. dib0090_write_reg(state, gain_reg_addr[i], v);
  801. }
  802. }
  803. }
  804. static void dib0090_set_boost(struct dib0090_state *state, int onoff)
  805. {
  806. state->bb_1_def &= 0xdfff;
  807. state->bb_1_def |= onoff << 13;
  808. }
  809. static void dib0090_set_rframp(struct dib0090_state *state, const u16 * cfg)
  810. {
  811. state->rf_ramp = cfg;
  812. }
  813. static void dib0090_set_rframp_pwm(struct dib0090_state *state, const u16 * cfg)
  814. {
  815. state->rf_ramp = cfg;
  816. dib0090_write_reg(state, 0x2a, 0xffff);
  817. dprintk("total RF gain: %ddB, step: %d", (u32) cfg[0], dib0090_read_reg(state, 0x2a));
  818. dib0090_write_regs(state, 0x2c, cfg + 3, 6);
  819. dib0090_write_regs(state, 0x3e, cfg + 9, 2);
  820. }
  821. static void dib0090_set_bbramp(struct dib0090_state *state, const u16 * cfg)
  822. {
  823. state->bb_ramp = cfg;
  824. dib0090_set_boost(state, cfg[0] > 500); /* we want the boost if the gain is higher that 50dB */
  825. }
  826. static void dib0090_set_bbramp_pwm(struct dib0090_state *state, const u16 * cfg)
  827. {
  828. state->bb_ramp = cfg;
  829. dib0090_set_boost(state, cfg[0] > 500); /* we want the boost if the gain is higher that 50dB */
  830. dib0090_write_reg(state, 0x33, 0xffff);
  831. dprintk("total BB gain: %ddB, step: %d", (u32) cfg[0], dib0090_read_reg(state, 0x33));
  832. dib0090_write_regs(state, 0x35, cfg + 3, 4);
  833. }
  834. void dib0090_pwm_gain_reset(struct dvb_frontend *fe)
  835. {
  836. struct dib0090_state *state = fe->tuner_priv;
  837. /* reset the AGC */
  838. if (state->config->use_pwm_agc) {
  839. #ifdef CONFIG_BAND_SBAND
  840. if (state->current_band == BAND_SBAND) {
  841. dib0090_set_rframp_pwm(state, rf_ramp_pwm_sband);
  842. dib0090_set_bbramp_pwm(state, bb_ramp_pwm_boost);
  843. } else
  844. #endif
  845. #ifdef CONFIG_BAND_CBAND
  846. if (state->current_band == BAND_CBAND) {
  847. if (state->identity.in_soc) {
  848. dib0090_set_bbramp_pwm(state, bb_ramp_pwm_normal_socs);
  849. if (state->identity.version == SOC_8090_P1G_11R1 || state->identity.version == SOC_8090_P1G_21R1)
  850. dib0090_set_rframp_pwm(state, rf_ramp_pwm_cband_8090);
  851. else if (state->identity.version == SOC_7090_P1G_11R1 || state->identity.version == SOC_7090_P1G_21R1)
  852. dib0090_set_rframp_pwm(state, rf_ramp_pwm_cband_7090);
  853. } else {
  854. dib0090_set_rframp_pwm(state, rf_ramp_pwm_cband);
  855. dib0090_set_bbramp_pwm(state, bb_ramp_pwm_normal);
  856. }
  857. } else
  858. #endif
  859. #ifdef CONFIG_BAND_VHF
  860. if (state->current_band == BAND_VHF) {
  861. if (state->identity.in_soc) {
  862. dib0090_set_bbramp_pwm(state, bb_ramp_pwm_normal_socs);
  863. } else {
  864. dib0090_set_rframp_pwm(state, rf_ramp_pwm_vhf);
  865. dib0090_set_bbramp_pwm(state, bb_ramp_pwm_normal);
  866. }
  867. } else
  868. #endif
  869. {
  870. if (state->identity.in_soc) {
  871. if (state->identity.version == SOC_8090_P1G_11R1 || state->identity.version == SOC_8090_P1G_21R1)
  872. dib0090_set_rframp_pwm(state, rf_ramp_pwm_uhf_8090);
  873. else if (state->identity.version == SOC_7090_P1G_11R1 || state->identity.version == SOC_7090_P1G_21R1)
  874. dib0090_set_rframp_pwm(state, rf_ramp_pwm_uhf_7090);
  875. dib0090_set_bbramp_pwm(state, bb_ramp_pwm_normal_socs);
  876. } else {
  877. dib0090_set_rframp_pwm(state, rf_ramp_pwm_uhf);
  878. dib0090_set_bbramp_pwm(state, bb_ramp_pwm_normal);
  879. }
  880. }
  881. if (state->rf_ramp[0] != 0)
  882. dib0090_write_reg(state, 0x32, (3 << 11));
  883. else
  884. dib0090_write_reg(state, 0x32, (0 << 11));
  885. dib0090_write_reg(state, 0x04, 0x01);
  886. dib0090_write_reg(state, 0x39, (1 << 10));
  887. }
  888. }
  889. EXPORT_SYMBOL(dib0090_pwm_gain_reset);
  890. static u32 dib0090_get_slow_adc_val(struct dib0090_state *state)
  891. {
  892. u16 adc_val = dib0090_read_reg(state, 0x1d);
  893. if (state->identity.in_soc)
  894. adc_val >>= 2;
  895. return adc_val;
  896. }
  897. int dib0090_gain_control(struct dvb_frontend *fe)
  898. {
  899. struct dib0090_state *state = fe->tuner_priv;
  900. enum frontend_tune_state *tune_state = &state->tune_state;
  901. int ret = 10;
  902. u16 wbd_val = 0;
  903. u8 apply_gain_immediatly = 1;
  904. s16 wbd_error = 0, adc_error = 0;
  905. if (*tune_state == CT_AGC_START) {
  906. state->agc_freeze = 0;
  907. dib0090_write_reg(state, 0x04, 0x0);
  908. #ifdef CONFIG_BAND_SBAND
  909. if (state->current_band == BAND_SBAND) {
  910. dib0090_set_rframp(state, rf_ramp_sband);
  911. dib0090_set_bbramp(state, bb_ramp_boost);
  912. } else
  913. #endif
  914. #ifdef CONFIG_BAND_VHF
  915. if (state->current_band == BAND_VHF && !state->identity.p1g) {
  916. dib0090_set_rframp(state, rf_ramp_vhf);
  917. dib0090_set_bbramp(state, bb_ramp_boost);
  918. } else
  919. #endif
  920. #ifdef CONFIG_BAND_CBAND
  921. if (state->current_band == BAND_CBAND && !state->identity.p1g) {
  922. dib0090_set_rframp(state, rf_ramp_cband);
  923. dib0090_set_bbramp(state, bb_ramp_boost);
  924. } else
  925. #endif
  926. if ((state->current_band == BAND_CBAND || state->current_band == BAND_VHF) && state->identity.p1g) {
  927. dib0090_set_rframp(state, rf_ramp_cband_broadmatching);
  928. dib0090_set_bbramp(state, bb_ramp_boost);
  929. } else {
  930. dib0090_set_rframp(state, rf_ramp_uhf);
  931. dib0090_set_bbramp(state, bb_ramp_boost);
  932. }
  933. dib0090_write_reg(state, 0x32, 0);
  934. dib0090_write_reg(state, 0x39, 0);
  935. dib0090_wbd_target(state, state->current_rf);
  936. state->rf_gain_limit = state->rf_ramp[0] << WBD_ALPHA;
  937. state->current_gain = ((state->rf_ramp[0] + state->bb_ramp[0]) / 2) << GAIN_ALPHA;
  938. *tune_state = CT_AGC_STEP_0;
  939. } else if (!state->agc_freeze) {
  940. s16 wbd = 0, i, cnt;
  941. int adc;
  942. wbd_val = dib0090_get_slow_adc_val(state);
  943. if (*tune_state == CT_AGC_STEP_0)
  944. cnt = 5;
  945. else
  946. cnt = 1;
  947. for (i = 0; i < cnt; i++) {
  948. wbd_val = dib0090_get_slow_adc_val(state);
  949. wbd += dib0090_wbd_to_db(state, wbd_val);
  950. }
  951. wbd /= cnt;
  952. wbd_error = state->wbd_target - wbd;
  953. if (*tune_state == CT_AGC_STEP_0) {
  954. if (wbd_error < 0 && state->rf_gain_limit > 0 && !state->identity.p1g) {
  955. #ifdef CONFIG_BAND_CBAND
  956. /* in case of CBAND tune reduce first the lt_gain2 before adjusting the RF gain */
  957. u8 ltg2 = (state->rf_lt_def >> 10) & 0x7;
  958. if (state->current_band == BAND_CBAND && ltg2) {
  959. ltg2 >>= 1;
  960. state->rf_lt_def &= ltg2 << 10; /* reduce in 3 steps from 7 to 0 */
  961. }
  962. #endif
  963. } else {
  964. state->agc_step = 0;
  965. *tune_state = CT_AGC_STEP_1;
  966. }
  967. } else {
  968. /* calc the adc power */
  969. adc = state->config->get_adc_power(fe);
  970. adc = (adc * ((s32) 355774) + (((s32) 1) << 20)) >> 21; /* included in [0:-700] */
  971. adc_error = (s16) (((s32) ADC_TARGET) - adc);
  972. #ifdef CONFIG_STANDARD_DAB
  973. if (state->fe->dtv_property_cache.delivery_system == STANDARD_DAB)
  974. adc_error -= 10;
  975. #endif
  976. #ifdef CONFIG_STANDARD_DVBT
  977. if (state->fe->dtv_property_cache.delivery_system == STANDARD_DVBT &&
  978. (state->fe->dtv_property_cache.modulation == QAM_64 || state->fe->dtv_property_cache.modulation == QAM_16))
  979. adc_error += 60;
  980. #endif
  981. #ifdef CONFIG_SYS_ISDBT
  982. if ((state->fe->dtv_property_cache.delivery_system == SYS_ISDBT) && (((state->fe->dtv_property_cache.layer[0].segment_count >
  983. 0)
  984. &&
  985. ((state->fe->dtv_property_cache.layer[0].modulation ==
  986. QAM_64)
  987. || (state->fe->dtv_property_cache.
  988. layer[0].modulation == QAM_16)))
  989. ||
  990. ((state->fe->dtv_property_cache.layer[1].segment_count >
  991. 0)
  992. &&
  993. ((state->fe->dtv_property_cache.layer[1].modulation ==
  994. QAM_64)
  995. || (state->fe->dtv_property_cache.
  996. layer[1].modulation == QAM_16)))
  997. ||
  998. ((state->fe->dtv_property_cache.layer[2].segment_count >
  999. 0)
  1000. &&
  1001. ((state->fe->dtv_property_cache.layer[2].modulation ==
  1002. QAM_64)
  1003. || (state->fe->dtv_property_cache.
  1004. layer[2].modulation == QAM_16)))
  1005. )
  1006. )
  1007. adc_error += 60;
  1008. #endif
  1009. if (*tune_state == CT_AGC_STEP_1) { /* quickly go to the correct range of the ADC power */
  1010. if (ABS(adc_error) < 50 || state->agc_step++ > 5) {
  1011. #ifdef CONFIG_STANDARD_DAB
  1012. if (state->fe->dtv_property_cache.delivery_system == STANDARD_DAB) {
  1013. dib0090_write_reg(state, 0x02, (1 << 15) | (15 << 11) | (31 << 6) | (63)); /* cap value = 63 : narrow BB filter : Fc = 1.8MHz */
  1014. dib0090_write_reg(state, 0x04, 0x0);
  1015. } else
  1016. #endif
  1017. {
  1018. dib0090_write_reg(state, 0x02, (1 << 15) | (3 << 11) | (6 << 6) | (32));
  1019. dib0090_write_reg(state, 0x04, 0x01); /*0 = 1KHz ; 1 = 150Hz ; 2 = 50Hz ; 3 = 50KHz ; 4 = servo fast */
  1020. }
  1021. *tune_state = CT_AGC_STOP;
  1022. }
  1023. } else {
  1024. /* everything higher than or equal to CT_AGC_STOP means tracking */
  1025. ret = 100; /* 10ms interval */
  1026. apply_gain_immediatly = 0;
  1027. }
  1028. }
  1029. #ifdef DEBUG_AGC
  1030. dprintk
  1031. ("tune state %d, ADC = %3ddB (ADC err %3d) WBD %3ddB (WBD err %3d, WBD val SADC: %4d), RFGainLimit (TOP): %3d, signal: %3ddBm",
  1032. (u32) *tune_state, (u32) adc, (u32) adc_error, (u32) wbd, (u32) wbd_error, (u32) wbd_val,
  1033. (u32) state->rf_gain_limit >> WBD_ALPHA, (s32) 200 + adc - (state->current_gain >> GAIN_ALPHA));
  1034. #endif
  1035. }
  1036. /* apply gain */
  1037. if (!state->agc_freeze)
  1038. dib0090_gain_apply(state, adc_error, wbd_error, apply_gain_immediatly);
  1039. return ret;
  1040. }
  1041. EXPORT_SYMBOL(dib0090_gain_control);
  1042. void dib0090_get_current_gain(struct dvb_frontend *fe, u16 * rf, u16 * bb, u16 * rf_gain_limit, u16 * rflt)
  1043. {
  1044. struct dib0090_state *state = fe->tuner_priv;
  1045. if (rf)
  1046. *rf = state->gain[0];
  1047. if (bb)
  1048. *bb = state->gain[1];
  1049. if (rf_gain_limit)
  1050. *rf_gain_limit = state->rf_gain_limit;
  1051. if (rflt)
  1052. *rflt = (state->rf_lt_def >> 10) & 0x7;
  1053. }
  1054. EXPORT_SYMBOL(dib0090_get_current_gain);
  1055. u16 dib0090_get_wbd_offset(struct dvb_frontend *fe)
  1056. {
  1057. struct dib0090_state *state = fe->tuner_priv;
  1058. u32 f_MHz = state->fe->dtv_property_cache.frequency / 1000000;
  1059. s32 current_temp = state->temperature;
  1060. s32 wbd_thot, wbd_tcold;
  1061. const struct dib0090_wbd_slope *wbd = state->current_wbd_table;
  1062. while (f_MHz > wbd->max_freq)
  1063. wbd++;
  1064. dprintk("using wbd-table-entry with max freq %d", wbd->max_freq);
  1065. if (current_temp < 0)
  1066. current_temp = 0;
  1067. if (current_temp > 128)
  1068. current_temp = 128;
  1069. state->wbdmux &= ~(7 << 13);
  1070. if (wbd->wbd_gain != 0)
  1071. state->wbdmux |= (wbd->wbd_gain << 13);
  1072. else
  1073. state->wbdmux |= (4 << 13);
  1074. dib0090_write_reg(state, 0x10, state->wbdmux);
  1075. wbd_thot = wbd->offset_hot - (((u32) wbd->slope_hot * f_MHz) >> 6);
  1076. wbd_tcold = wbd->offset_cold - (((u32) wbd->slope_cold * f_MHz) >> 6);
  1077. wbd_tcold += ((wbd_thot - wbd_tcold) * current_temp) >> 7;
  1078. state->wbd_target = dib0090_wbd_to_db(state, state->wbd_offset + wbd_tcold);
  1079. dprintk("wbd-target: %d dB", (u32) state->wbd_target);
  1080. dprintk("wbd offset applied is %d", wbd_tcold);
  1081. return state->wbd_offset + wbd_tcold;
  1082. }
  1083. EXPORT_SYMBOL(dib0090_get_wbd_offset);
  1084. static const u16 dib0090_defaults[] = {
  1085. 25, 0x01,
  1086. 0x0000,
  1087. 0x99a0,
  1088. 0x6008,
  1089. 0x0000,
  1090. 0x8bcb,
  1091. 0x0000,
  1092. 0x0405,
  1093. 0x0000,
  1094. 0x0000,
  1095. 0x0000,
  1096. 0xb802,
  1097. 0x0300,
  1098. 0x2d12,
  1099. 0xbac0,
  1100. 0x7c00,
  1101. 0xdbb9,
  1102. 0x0954,
  1103. 0x0743,
  1104. 0x8000,
  1105. 0x0001,
  1106. 0x0040,
  1107. 0x0100,
  1108. 0x0000,
  1109. 0xe910,
  1110. 0x149e,
  1111. 1, 0x1c,
  1112. 0xff2d,
  1113. 1, 0x39,
  1114. 0x0000,
  1115. 2, 0x1e,
  1116. 0x07FF,
  1117. 0x0007,
  1118. 1, 0x24,
  1119. EN_UHF | EN_CRYSTAL,
  1120. 2, 0x3c,
  1121. 0x3ff,
  1122. 0x111,
  1123. 0
  1124. };
  1125. static const u16 dib0090_p1g_additionnal_defaults[] = {
  1126. 1, 0x05,
  1127. 0xabcd,
  1128. 1, 0x11,
  1129. 0x00b4,
  1130. 1, 0x1c,
  1131. 0xfffd,
  1132. 1, 0x40,
  1133. 0x108,
  1134. 0
  1135. };
  1136. static void dib0090_set_default_config(struct dib0090_state *state, const u16 * n)
  1137. {
  1138. u16 l, r;
  1139. l = pgm_read_word(n++);
  1140. while (l) {
  1141. r = pgm_read_word(n++);
  1142. do {
  1143. dib0090_write_reg(state, r, pgm_read_word(n++));
  1144. r++;
  1145. } while (--l);
  1146. l = pgm_read_word(n++);
  1147. }
  1148. }
  1149. #define CAP_VALUE_MIN (u8) 9
  1150. #define CAP_VALUE_MAX (u8) 40
  1151. #define HR_MIN (u8) 25
  1152. #define HR_MAX (u8) 40
  1153. #define POLY_MIN (u8) 0
  1154. #define POLY_MAX (u8) 8
  1155. void dib0090_set_EFUSE(struct dib0090_state *state)
  1156. {
  1157. u8 c, h, n;
  1158. u16 e2, e4;
  1159. u16 cal;
  1160. e2 = dib0090_read_reg(state, 0x26);
  1161. e4 = dib0090_read_reg(state, 0x28);
  1162. if ((state->identity.version == P1D_E_F) ||
  1163. (state->identity.version == P1G) || (e2 == 0xffff)) {
  1164. dib0090_write_reg(state, 0x22, 0x10);
  1165. cal = (dib0090_read_reg(state, 0x22) >> 6) & 0x3ff;
  1166. if ((cal < 670) || (cal == 1023))
  1167. cal = 850;
  1168. n = 165 - ((cal * 10)>>6) ;
  1169. e2 = e4 = (3<<12) | (34<<6) | (n);
  1170. }
  1171. if (e2 != e4)
  1172. e2 &= e4; /* Remove the redundancy */
  1173. if (e2 != 0xffff) {
  1174. c = e2 & 0x3f;
  1175. n = (e2 >> 12) & 0xf;
  1176. h = (e2 >> 6) & 0x3f;
  1177. if ((c >= CAP_VALUE_MAX) || (c <= CAP_VALUE_MIN))
  1178. c = 32;
  1179. if ((h >= HR_MAX) || (h <= HR_MIN))
  1180. h = 34;
  1181. if ((n >= POLY_MAX) || (n <= POLY_MIN))
  1182. n = 3;
  1183. dib0090_write_reg(state, 0x13, (h << 10)) ;
  1184. e2 = (n<<11) | ((h>>2)<<6) | (c);
  1185. dib0090_write_reg(state, 0x2, e2) ; /* Load the BB_2 */
  1186. }
  1187. }
  1188. static int dib0090_reset(struct dvb_frontend *fe)
  1189. {
  1190. struct dib0090_state *state = fe->tuner_priv;
  1191. dib0090_reset_digital(fe, state->config);
  1192. if (dib0090_identify(fe) < 0)
  1193. return -EIO;
  1194. #ifdef CONFIG_TUNER_DIB0090_P1B_SUPPORT
  1195. if (!(state->identity.version & 0x1)) /* it is P1B - reset is already done */
  1196. return 0;
  1197. #endif
  1198. if (!state->identity.in_soc) {
  1199. if ((dib0090_read_reg(state, 0x1a) >> 5) & 0x2)
  1200. dib0090_write_reg(state, 0x1b, (EN_IQADC | EN_BB | EN_BIAS | EN_DIGCLK | EN_PLL | EN_CRYSTAL));
  1201. else
  1202. dib0090_write_reg(state, 0x1b, (EN_DIGCLK | EN_PLL | EN_CRYSTAL));
  1203. }
  1204. dib0090_set_default_config(state, dib0090_defaults);
  1205. if (state->identity.in_soc)
  1206. dib0090_write_reg(state, 0x18, 0x2910); /* charge pump current = 0 */
  1207. if (state->identity.p1g)
  1208. dib0090_set_default_config(state, dib0090_p1g_additionnal_defaults);
  1209. /* Update the efuse : Only available for KROSUS > P1C and SOC as well*/
  1210. if (((state->identity.version & 0x1f) >= P1D_E_F) || (state->identity.in_soc))
  1211. dib0090_set_EFUSE(state);
  1212. /* Congigure in function of the crystal */
  1213. if (state->config->io.clock_khz >= 24000)
  1214. dib0090_write_reg(state, 0x14, 1);
  1215. else
  1216. dib0090_write_reg(state, 0x14, 2);
  1217. dprintk("Pll lock : %d", (dib0090_read_reg(state, 0x1a) >> 11) & 0x1);
  1218. state->calibrate = DC_CAL | WBD_CAL | TEMP_CAL; /* enable iq-offset-calibration and wbd-calibration when tuning next time */
  1219. return 0;
  1220. }
  1221. #define steps(u) (((u) > 15) ? ((u)-16) : (u))
  1222. #define INTERN_WAIT 10
  1223. static int dib0090_get_offset(struct dib0090_state *state, enum frontend_tune_state *tune_state)
  1224. {
  1225. int ret = INTERN_WAIT * 10;
  1226. switch (*tune_state) {
  1227. case CT_TUNER_STEP_2:
  1228. /* Turns to positive */
  1229. dib0090_write_reg(state, 0x1f, 0x7);
  1230. *tune_state = CT_TUNER_STEP_3;
  1231. break;
  1232. case CT_TUNER_STEP_3:
  1233. state->adc_diff = dib0090_read_reg(state, 0x1d);
  1234. /* Turns to negative */
  1235. dib0090_write_reg(state, 0x1f, 0x4);
  1236. *tune_state = CT_TUNER_STEP_4;
  1237. break;
  1238. case CT_TUNER_STEP_4:
  1239. state->adc_diff -= dib0090_read_reg(state, 0x1d);
  1240. *tune_state = CT_TUNER_STEP_5;
  1241. ret = 0;
  1242. break;
  1243. default:
  1244. break;
  1245. }
  1246. return ret;
  1247. }
  1248. struct dc_calibration {
  1249. u8 addr;
  1250. u8 offset;
  1251. u8 pga:1;
  1252. u16 bb1;
  1253. u8 i:1;
  1254. };
  1255. static const struct dc_calibration dc_table[] = {
  1256. /* Step1 BB gain1= 26 with boost 1, gain 2 = 0 */
  1257. {0x06, 5, 1, (1 << 13) | (0 << 8) | (26 << 3), 1},
  1258. {0x07, 11, 1, (1 << 13) | (0 << 8) | (26 << 3), 0},
  1259. /* Step 2 BB gain 1 = 26 with boost = 1 & gain 2 = 29 */
  1260. {0x06, 0, 0, (1 << 13) | (29 << 8) | (26 << 3), 1},
  1261. {0x06, 10, 0, (1 << 13) | (29 << 8) | (26 << 3), 0},
  1262. {0},
  1263. };
  1264. static const struct dc_calibration dc_p1g_table[] = {
  1265. /* Step1 BB gain1= 26 with boost 1, gain 2 = 0 */
  1266. /* addr ; trim reg offset ; pga ; CTRL_BB1 value ; i or q */
  1267. {0x06, 5, 1, (1 << 13) | (0 << 8) | (15 << 3), 1},
  1268. {0x07, 11, 1, (1 << 13) | (0 << 8) | (15 << 3), 0},
  1269. /* Step 2 BB gain 1 = 26 with boost = 1 & gain 2 = 29 */
  1270. {0x06, 0, 0, (1 << 13) | (29 << 8) | (15 << 3), 1},
  1271. {0x06, 10, 0, (1 << 13) | (29 << 8) | (15 << 3), 0},
  1272. {0},
  1273. };
  1274. static void dib0090_set_trim(struct dib0090_state *state)
  1275. {
  1276. u16 *val;
  1277. if (state->dc->addr == 0x07)
  1278. val = &state->bb7;
  1279. else
  1280. val = &state->bb6;
  1281. *val &= ~(0x1f << state->dc->offset);
  1282. *val |= state->step << state->dc->offset;
  1283. dib0090_write_reg(state, state->dc->addr, *val);
  1284. }
  1285. static int dib0090_dc_offset_calibration(struct dib0090_state *state, enum frontend_tune_state *tune_state)
  1286. {
  1287. int ret = 0;
  1288. u16 reg;
  1289. switch (*tune_state) {
  1290. case CT_TUNER_START:
  1291. dprintk("Start DC offset calibration");
  1292. /* force vcm2 = 0.8V */
  1293. state->bb6 = 0;
  1294. state->bb7 = 0x040d;
  1295. /* the LNA AND LO are off */
  1296. reg = dib0090_read_reg(state, 0x24) & 0x0ffb; /* shutdown lna and lo */
  1297. dib0090_write_reg(state, 0x24, reg);
  1298. state->wbdmux = dib0090_read_reg(state, 0x10);
  1299. dib0090_write_reg(state, 0x10, (state->wbdmux & ~(0xff << 3)) | (0x7 << 3) | 0x3);
  1300. dib0090_write_reg(state, 0x23, dib0090_read_reg(state, 0x23) & ~(1 << 14));
  1301. state->dc = dc_table;
  1302. if (state->identity.p1g)
  1303. state->dc = dc_p1g_table;
  1304. *tune_state = CT_TUNER_STEP_0;
  1305. /* fall through */
  1306. case CT_TUNER_STEP_0:
  1307. dprintk("Sart/continue DC calibration for %s path", (state->dc->i == 1) ? "I" : "Q");
  1308. dib0090_write_reg(state, 0x01, state->dc->bb1);
  1309. dib0090_write_reg(state, 0x07, state->bb7 | (state->dc->i << 7));
  1310. state->step = 0;
  1311. state->min_adc_diff = 1023;
  1312. *tune_state = CT_TUNER_STEP_1;
  1313. ret = 50;
  1314. break;
  1315. case CT_TUNER_STEP_1:
  1316. dib0090_set_trim(state);
  1317. *tune_state = CT_TUNER_STEP_2;
  1318. break;
  1319. case CT_TUNER_STEP_2:
  1320. case CT_TUNER_STEP_3:
  1321. case CT_TUNER_STEP_4:
  1322. ret = dib0090_get_offset(state, tune_state);
  1323. break;
  1324. case CT_TUNER_STEP_5: /* found an offset */
  1325. dprintk("adc_diff = %d, current step= %d", (u32) state->adc_diff, state->step);
  1326. if (state->step == 0 && state->adc_diff < 0) {
  1327. state->min_adc_diff = -1023;
  1328. dprintk("Change of sign of the minimum adc diff");
  1329. }
  1330. dprintk("adc_diff = %d, min_adc_diff = %d current_step = %d", state->adc_diff, state->min_adc_diff, state->step);
  1331. /* first turn for this frequency */
  1332. if (state->step == 0) {
  1333. if (state->dc->pga && state->adc_diff < 0)
  1334. state->step = 0x10;
  1335. if (state->dc->pga == 0 && state->adc_diff > 0)
  1336. state->step = 0x10;
  1337. }
  1338. /* Look for a change of Sign in the Adc_diff.min_adc_diff is used to STORE the setp N-1 */
  1339. if ((state->adc_diff & 0x8000) == (state->min_adc_diff & 0x8000) && steps(state->step) < 15) {
  1340. /* stop search when the delta the sign is changing and Steps =15 and Step=0 is force for continuance */
  1341. state->step++;
  1342. state->min_adc_diff = state->adc_diff;
  1343. *tune_state = CT_TUNER_STEP_1;
  1344. } else {
  1345. /* the minimum was what we have seen in the step before */
  1346. if (ABS(state->adc_diff) > ABS(state->min_adc_diff)) {
  1347. dprintk("Since adc_diff N = %d > adc_diff step N-1 = %d, Come back one step", state->adc_diff, state->min_adc_diff);
  1348. state->step--;
  1349. }
  1350. dib0090_set_trim(state);
  1351. dprintk("BB Offset Cal, BBreg=%hd,Offset=%hd,Value Set=%hd", state->dc->addr, state->adc_diff, state->step);
  1352. state->dc++;
  1353. if (state->dc->addr == 0) /* done */
  1354. *tune_state = CT_TUNER_STEP_6;
  1355. else
  1356. *tune_state = CT_TUNER_STEP_0;
  1357. }
  1358. break;
  1359. case CT_TUNER_STEP_6:
  1360. dib0090_write_reg(state, 0x07, state->bb7 & ~0x0008);
  1361. dib0090_write_reg(state, 0x1f, 0x7);
  1362. *tune_state = CT_TUNER_START; /* reset done -> real tuning can now begin */
  1363. state->calibrate &= ~DC_CAL;
  1364. default:
  1365. break;
  1366. }
  1367. return ret;
  1368. }
  1369. static int dib0090_wbd_calibration(struct dib0090_state *state, enum frontend_tune_state *tune_state)
  1370. {
  1371. u8 wbd_gain;
  1372. const struct dib0090_wbd_slope *wbd = state->current_wbd_table;
  1373. switch (*tune_state) {
  1374. case CT_TUNER_START:
  1375. while (state->current_rf / 1000 > wbd->max_freq)
  1376. wbd++;
  1377. if (wbd->wbd_gain != 0)
  1378. wbd_gain = wbd->wbd_gain;
  1379. else {
  1380. wbd_gain = 4;
  1381. #if defined(CONFIG_BAND_LBAND) || defined(CONFIG_BAND_SBAND)
  1382. if ((state->current_band == BAND_LBAND) || (state->current_band == BAND_SBAND))
  1383. wbd_gain = 2;
  1384. #endif
  1385. }
  1386. if (wbd_gain == state->wbd_calibration_gain) { /* the WBD calibration has already been done */
  1387. *tune_state = CT_TUNER_START;
  1388. state->calibrate &= ~WBD_CAL;
  1389. return 0;
  1390. }
  1391. dib0090_write_reg(state, 0x10, 0x1b81 | (1 << 10) | (wbd_gain << 13) | (1 << 3));
  1392. dib0090_write_reg(state, 0x24, ((EN_UHF & 0x0fff) | (1 << 1)));
  1393. *tune_state = CT_TUNER_STEP_0;
  1394. state->wbd_calibration_gain = wbd_gain;
  1395. return 90; /* wait for the WBDMUX to switch and for the ADC to sample */
  1396. case CT_TUNER_STEP_0:
  1397. state->wbd_offset = dib0090_get_slow_adc_val(state);
  1398. dprintk("WBD calibration offset = %d", state->wbd_offset);
  1399. *tune_state = CT_TUNER_START; /* reset done -> real tuning can now begin */
  1400. state->calibrate &= ~WBD_CAL;
  1401. break;
  1402. default:
  1403. break;
  1404. }
  1405. return 0;
  1406. }
  1407. static void dib0090_set_bandwidth(struct dib0090_state *state)
  1408. {
  1409. u16 tmp;
  1410. if (state->fe->dtv_property_cache.bandwidth_hz / 1000 <= 5000)
  1411. tmp = (3 << 14);
  1412. else if (state->fe->dtv_property_cache.bandwidth_hz / 1000 <= 6000)
  1413. tmp = (2 << 14);
  1414. else if (state->fe->dtv_property_cache.bandwidth_hz / 1000 <= 7000)
  1415. tmp = (1 << 14);
  1416. else
  1417. tmp = (0 << 14);
  1418. state->bb_1_def &= 0x3fff;
  1419. state->bb_1_def |= tmp;
  1420. dib0090_write_reg(state, 0x01, state->bb_1_def); /* be sure that we have the right bb-filter */
  1421. dib0090_write_reg(state, 0x03, 0x6008); /* = 0x6008 : vcm3_trim = 1 ; filter2_gm1_trim = 8 ; filter2_cutoff_freq = 0 */
  1422. dib0090_write_reg(state, 0x04, 0x1); /* 0 = 1KHz ; 1 = 50Hz ; 2 = 150Hz ; 3 = 50KHz ; 4 = servo fast */
  1423. if (state->identity.in_soc) {
  1424. dib0090_write_reg(state, 0x05, 0x9bcf); /* attenuator_ibias_tri = 2 ; input_stage_ibias_tr = 1 ; nc = 11 ; ext_gm_trim = 1 ; obuf_ibias_trim = 4 ; filter13_gm2_ibias_t = 15 */
  1425. } else {
  1426. dib0090_write_reg(state, 0x02, (5 << 11) | (8 << 6) | (22 & 0x3f)); /* 22 = cap_value */
  1427. dib0090_write_reg(state, 0x05, 0xabcd); /* = 0xabcd : attenuator_ibias_tri = 2 ; input_stage_ibias_tr = 2 ; nc = 11 ; ext_gm_trim = 1 ; obuf_ibias_trim = 4 ; filter13_gm2_ibias_t = 13 */
  1428. }
  1429. }
  1430. static const struct dib0090_pll dib0090_pll_table[] = {
  1431. #ifdef CONFIG_BAND_CBAND
  1432. {56000, 0, 9, 48, 6},
  1433. {70000, 1, 9, 48, 6},
  1434. {87000, 0, 8, 32, 4},
  1435. {105000, 1, 8, 32, 4},
  1436. {115000, 0, 7, 24, 6},
  1437. {140000, 1, 7, 24, 6},
  1438. {170000, 0, 6, 16, 4},
  1439. #endif
  1440. #ifdef CONFIG_BAND_VHF
  1441. {200000, 1, 6, 16, 4},
  1442. {230000, 0, 5, 12, 6},
  1443. {280000, 1, 5, 12, 6},
  1444. {340000, 0, 4, 8, 4},
  1445. {380000, 1, 4, 8, 4},
  1446. {450000, 0, 3, 6, 6},
  1447. #endif
  1448. #ifdef CONFIG_BAND_UHF
  1449. {580000, 1, 3, 6, 6},
  1450. {700000, 0, 2, 4, 4},
  1451. {860000, 1, 2, 4, 4},
  1452. #endif
  1453. #ifdef CONFIG_BAND_LBAND
  1454. {1800000, 1, 0, 2, 4},
  1455. #endif
  1456. #ifdef CONFIG_BAND_SBAND
  1457. {2900000, 0, 14, 1, 4},
  1458. #endif
  1459. };
  1460. static const struct dib0090_tuning dib0090_tuning_table_fm_vhf_on_cband[] = {
  1461. #ifdef CONFIG_BAND_CBAND
  1462. {184000, 4, 1, 15, 0x280, 0x2912, 0xb94e, EN_CAB},
  1463. {227000, 4, 3, 15, 0x280, 0x2912, 0xb94e, EN_CAB},
  1464. {380000, 4, 7, 15, 0x280, 0x2912, 0xb94e, EN_CAB},
  1465. #endif
  1466. #ifdef CONFIG_BAND_UHF
  1467. {520000, 2, 0, 15, 0x300, 0x1d12, 0xb9ce, EN_UHF},
  1468. {550000, 2, 2, 15, 0x300, 0x1d12, 0xb9ce, EN_UHF},
  1469. {650000, 2, 3, 15, 0x300, 0x1d12, 0xb9ce, EN_UHF},
  1470. {750000, 2, 5, 15, 0x300, 0x1d12, 0xb9ce, EN_UHF},
  1471. {850000, 2, 6, 15, 0x300, 0x1d12, 0xb9ce, EN_UHF},
  1472. {900000, 2, 7, 15, 0x300, 0x1d12, 0xb9ce, EN_UHF},
  1473. #endif
  1474. #ifdef CONFIG_BAND_LBAND
  1475. {1500000, 4, 0, 20, 0x300, 0x1912, 0x82c9, EN_LBD},
  1476. {1600000, 4, 1, 20, 0x300, 0x1912, 0x82c9, EN_LBD},
  1477. {1800000, 4, 3, 20, 0x300, 0x1912, 0x82c9, EN_LBD},
  1478. #endif
  1479. #ifdef CONFIG_BAND_SBAND
  1480. {2300000, 1, 4, 20, 0x300, 0x2d2A, 0x82c7, EN_SBD},
  1481. {2900000, 1, 7, 20, 0x280, 0x2deb, 0x8347, EN_SBD},
  1482. #endif
  1483. };
  1484. static const struct dib0090_tuning dib0090_tuning_table[] = {
  1485. #ifdef CONFIG_BAND_CBAND
  1486. {170000, 4, 1, 15, 0x280, 0x2912, 0xb94e, EN_CAB},
  1487. #endif
  1488. #ifdef CONFIG_BAND_VHF
  1489. {184000, 1, 1, 15, 0x300, 0x4d12, 0xb94e, EN_VHF},
  1490. {227000, 1, 3, 15, 0x300, 0x4d12, 0xb94e, EN_VHF},
  1491. {380000, 1, 7, 15, 0x300, 0x4d12, 0xb94e, EN_VHF},
  1492. #endif
  1493. #ifdef CONFIG_BAND_UHF
  1494. {520000, 2, 0, 15, 0x300, 0x1d12, 0xb9ce, EN_UHF},
  1495. {550000, 2, 2, 15, 0x300, 0x1d12, 0xb9ce, EN_UHF},
  1496. {650000, 2, 3, 15, 0x300, 0x1d12, 0xb9ce, EN_UHF},
  1497. {750000, 2, 5, 15, 0x300, 0x1d12, 0xb9ce, EN_UHF},
  1498. {850000, 2, 6, 15, 0x300, 0x1d12, 0xb9ce, EN_UHF},
  1499. {900000, 2, 7, 15, 0x300, 0x1d12, 0xb9ce, EN_UHF},
  1500. #endif
  1501. #ifdef CONFIG_BAND_LBAND
  1502. {1500000, 4, 0, 20, 0x300, 0x1912, 0x82c9, EN_LBD},
  1503. {1600000, 4, 1, 20, 0x300, 0x1912, 0x82c9, EN_LBD},
  1504. {1800000, 4, 3, 20, 0x300, 0x1912, 0x82c9, EN_LBD},
  1505. #endif
  1506. #ifdef CONFIG_BAND_SBAND
  1507. {2300000, 1, 4, 20, 0x300, 0x2d2A, 0x82c7, EN_SBD},
  1508. {2900000, 1, 7, 20, 0x280, 0x2deb, 0x8347, EN_SBD},
  1509. #endif
  1510. };
  1511. static const struct dib0090_tuning dib0090_p1g_tuning_table[] = {
  1512. #ifdef CONFIG_BAND_CBAND
  1513. {170000, 4, 1, 0x820f, 0x300, 0x2d22, 0x82cb, EN_CAB},
  1514. #endif
  1515. #ifdef CONFIG_BAND_VHF
  1516. {184000, 1, 1, 15, 0x300, 0x4d12, 0xb94e, EN_VHF},
  1517. {227000, 1, 3, 15, 0x300, 0x4d12, 0xb94e, EN_VHF},
  1518. {380000, 1, 7, 15, 0x300, 0x4d12, 0xb94e, EN_VHF},
  1519. #endif
  1520. #ifdef CONFIG_BAND_UHF
  1521. {510000, 2, 0, 15, 0x300, 0x1d12, 0xb9ce, EN_UHF},
  1522. {540000, 2, 1, 15, 0x300, 0x1d12, 0xb9ce, EN_UHF},
  1523. {600000, 2, 3, 15, 0x300, 0x1d12, 0xb9ce, EN_UHF},
  1524. {630000, 2, 4, 15, 0x300, 0x1d12, 0xb9ce, EN_UHF},
  1525. {680000, 2, 5, 15, 0x300, 0x1d12, 0xb9ce, EN_UHF},
  1526. {720000, 2, 6, 15, 0x300, 0x1d12, 0xb9ce, EN_UHF},
  1527. {900000, 2, 7, 15, 0x300, 0x1d12, 0xb9ce, EN_UHF},
  1528. #endif
  1529. #ifdef CONFIG_BAND_LBAND
  1530. {1500000, 4, 0, 20, 0x300, 0x1912, 0x82c9, EN_LBD},
  1531. {1600000, 4, 1, 20, 0x300, 0x1912, 0x82c9, EN_LBD},
  1532. {1800000, 4, 3, 20, 0x300, 0x1912, 0x82c9, EN_LBD},
  1533. #endif
  1534. #ifdef CONFIG_BAND_SBAND
  1535. {2300000, 1, 4, 20, 0x300, 0x2d2A, 0x82c7, EN_SBD},
  1536. {2900000, 1, 7, 20, 0x280, 0x2deb, 0x8347, EN_SBD},
  1537. #endif
  1538. };
  1539. static const struct dib0090_pll dib0090_p1g_pll_table[] = {
  1540. #ifdef CONFIG_BAND_CBAND
  1541. {57000, 0, 11, 48, 6},
  1542. {70000, 1, 11, 48, 6},
  1543. {86000, 0, 10, 32, 4},
  1544. {105000, 1, 10, 32, 4},
  1545. {115000, 0, 9, 24, 6},
  1546. {140000, 1, 9, 24, 6},
  1547. {170000, 0, 8, 16, 4},
  1548. #endif
  1549. #ifdef CONFIG_BAND_VHF
  1550. {200000, 1, 8, 16, 4},
  1551. {230000, 0, 7, 12, 6},
  1552. {280000, 1, 7, 12, 6},
  1553. {340000, 0, 6, 8, 4},
  1554. {380000, 1, 6, 8, 4},
  1555. {455000, 0, 5, 6, 6},
  1556. #endif
  1557. #ifdef CONFIG_BAND_UHF
  1558. {580000, 1, 5, 6, 6},
  1559. {680000, 0, 4, 4, 4},
  1560. {860000, 1, 4, 4, 4},
  1561. #endif
  1562. #ifdef CONFIG_BAND_LBAND
  1563. {1800000, 1, 2, 2, 4},
  1564. #endif
  1565. #ifdef CONFIG_BAND_SBAND
  1566. {2900000, 0, 1, 1, 6},
  1567. #endif
  1568. };
  1569. static const struct dib0090_tuning dib0090_p1g_tuning_table_fm_vhf_on_cband[] = {
  1570. #ifdef CONFIG_BAND_CBAND
  1571. {184000, 4, 3, 0x4187, 0x2c0, 0x2d22, 0x81cb, EN_CAB},
  1572. {227000, 4, 3, 0x4187, 0x2c0, 0x2d22, 0x81cb, EN_CAB},
  1573. {380000, 4, 3, 0x4187, 0x2c0, 0x2d22, 0x81cb, EN_CAB},
  1574. #endif
  1575. #ifdef CONFIG_BAND_UHF
  1576. {520000, 2, 0, 15, 0x300, 0x1d12, 0xb9ce, EN_UHF},
  1577. {550000, 2, 2, 15, 0x300, 0x1d12, 0xb9ce, EN_UHF},
  1578. {650000, 2, 3, 15, 0x300, 0x1d12, 0xb9ce, EN_UHF},
  1579. {750000, 2, 5, 15, 0x300, 0x1d12, 0xb9ce, EN_UHF},
  1580. {850000, 2, 6, 15, 0x300, 0x1d12, 0xb9ce, EN_UHF},
  1581. {900000, 2, 7, 15, 0x300, 0x1d12, 0xb9ce, EN_UHF},
  1582. #endif
  1583. #ifdef CONFIG_BAND_LBAND
  1584. {1500000, 4, 0, 20, 0x300, 0x1912, 0x82c9, EN_LBD},
  1585. {1600000, 4, 1, 20, 0x300, 0x1912, 0x82c9, EN_LBD},
  1586. {1800000, 4, 3, 20, 0x300, 0x1912, 0x82c9, EN_LBD},
  1587. #endif
  1588. #ifdef CONFIG_BAND_SBAND
  1589. {2300000, 1, 4, 20, 0x300, 0x2d2A, 0x82c7, EN_SBD},
  1590. {2900000, 1, 7, 20, 0x280, 0x2deb, 0x8347, EN_SBD},
  1591. #endif
  1592. };
  1593. static const struct dib0090_tuning dib0090_tuning_table_cband_7090[] = {
  1594. #ifdef CONFIG_BAND_CBAND
  1595. {300000, 4, 3, 0x018F, 0x2c0, 0x2d22, 0xb9ce, EN_CAB},
  1596. {380000, 4, 10, 0x018F, 0x2c0, 0x2d22, 0xb9ce, EN_CAB},
  1597. {570000, 4, 10, 0x8190, 0x2c0, 0x2d22, 0xb9ce, EN_CAB},
  1598. {858000, 4, 5, 0x8190, 0x2c0, 0x2d22, 0xb9ce, EN_CAB},
  1599. #endif
  1600. };
  1601. static int dib0090_captrim_search(struct dib0090_state *state, enum frontend_tune_state *tune_state)
  1602. {
  1603. int ret = 0;
  1604. u16 lo4 = 0xe900;
  1605. s16 adc_target;
  1606. u16 adc;
  1607. s8 step_sign;
  1608. u8 force_soft_search = 0;
  1609. if (state->identity.version == SOC_8090_P1G_11R1 || state->identity.version == SOC_8090_P1G_21R1)
  1610. force_soft_search = 1;
  1611. if (*tune_state == CT_TUNER_START) {
  1612. dprintk("Start Captrim search : %s", (force_soft_search == 1) ? "FORCE SOFT SEARCH" : "AUTO");
  1613. dib0090_write_reg(state, 0x10, 0x2B1);
  1614. dib0090_write_reg(state, 0x1e, 0x0032);
  1615. if (!state->tuner_is_tuned) {
  1616. /* prepare a complete captrim */
  1617. if (!state->identity.p1g || force_soft_search)
  1618. state->step = state->captrim = state->fcaptrim = 64;
  1619. state->current_rf = state->rf_request;
  1620. } else { /* we are already tuned to this frequency - the configuration is correct */
  1621. if (!state->identity.p1g || force_soft_search) {
  1622. /* do a minimal captrim even if the frequency has not changed */
  1623. state->step = 4;
  1624. state->captrim = state->fcaptrim = dib0090_read_reg(state, 0x18) & 0x7f;
  1625. }
  1626. }
  1627. state->adc_diff = 3000;
  1628. *tune_state = CT_TUNER_STEP_0;
  1629. } else if (*tune_state == CT_TUNER_STEP_0) {
  1630. if (state->identity.p1g && !force_soft_search) {
  1631. u8 ratio = 31;
  1632. dib0090_write_reg(state, 0x40, (3 << 7) | (ratio << 2) | (1 << 1) | 1);
  1633. dib0090_read_reg(state, 0x40);
  1634. ret = 50;
  1635. } else {
  1636. state->step /= 2;
  1637. dib0090_write_reg(state, 0x18, lo4 | state->captrim);
  1638. if (state->identity.in_soc)
  1639. ret = 25;
  1640. }
  1641. *tune_state = CT_TUNER_STEP_1;
  1642. } else if (*tune_state == CT_TUNER_STEP_1) {
  1643. if (state->identity.p1g && !force_soft_search) {
  1644. dib0090_write_reg(state, 0x40, 0x18c | (0 << 1) | 0);
  1645. dib0090_read_reg(state, 0x40);
  1646. state->fcaptrim = dib0090_read_reg(state, 0x18) & 0x7F;
  1647. dprintk("***Final Captrim= 0x%x", state->fcaptrim);
  1648. *tune_state = CT_TUNER_STEP_3;
  1649. } else {
  1650. /* MERGE for all krosus before P1G */
  1651. adc = dib0090_get_slow_adc_val(state);
  1652. dprintk("CAPTRIM=%d; ADC = %d (ADC) & %dmV", (u32) state->captrim, (u32) adc, (u32) (adc) * (u32) 1800 / (u32) 1024);
  1653. if (state->rest == 0 || state->identity.in_soc) { /* Just for 8090P SOCS where auto captrim HW bug : TO CHECK IN ACI for SOCS !!! if 400 for 8090p SOC => tune issue !!! */
  1654. adc_target = 200;
  1655. } else
  1656. adc_target = 400;
  1657. if (adc >= adc_target) {
  1658. adc -= adc_target;
  1659. step_sign = -1;
  1660. } else {
  1661. adc = adc_target - adc;
  1662. step_sign = 1;
  1663. }
  1664. if (adc < state->adc_diff) {
  1665. dprintk("CAPTRIM=%d is closer to target (%d/%d)", (u32) state->captrim, (u32) adc, (u32) state->adc_diff);
  1666. state->adc_diff = adc;
  1667. state->fcaptrim = state->captrim;
  1668. }
  1669. state->captrim += step_sign * state->step;
  1670. if (state->step >= 1)
  1671. *tune_state = CT_TUNER_STEP_0;
  1672. else
  1673. *tune_state = CT_TUNER_STEP_2;
  1674. ret = 25;
  1675. }
  1676. } else if (*tune_state == CT_TUNER_STEP_2) { /* this step is only used by krosus < P1G */
  1677. /*write the final cptrim config */
  1678. dib0090_write_reg(state, 0x18, lo4 | state->fcaptrim);
  1679. *tune_state = CT_TUNER_STEP_3;
  1680. } else if (*tune_state == CT_TUNER_STEP_3) {
  1681. state->calibrate &= ~CAPTRIM_CAL;
  1682. *tune_state = CT_TUNER_STEP_0;
  1683. }
  1684. return ret;
  1685. }
  1686. static int dib0090_get_temperature(struct dib0090_state *state, enum frontend_tune_state *tune_state)
  1687. {
  1688. int ret = 15;
  1689. s16 val;
  1690. switch (*tune_state) {
  1691. case CT_TUNER_START:
  1692. state->wbdmux = dib0090_read_reg(state, 0x10);
  1693. dib0090_write_reg(state, 0x10, (state->wbdmux & ~(0xff << 3)) | (0x8 << 3));
  1694. state->bias = dib0090_read_reg(state, 0x13);
  1695. dib0090_write_reg(state, 0x13, state->bias | (0x3 << 8));
  1696. *tune_state = CT_TUNER_STEP_0;
  1697. /* wait for the WBDMUX to switch and for the ADC to sample */
  1698. break;
  1699. case CT_TUNER_STEP_0:
  1700. state->adc_diff = dib0090_get_slow_adc_val(state);
  1701. dib0090_write_reg(state, 0x13, (state->bias & ~(0x3 << 8)) | (0x2 << 8));
  1702. *tune_state = CT_TUNER_STEP_1;
  1703. break;
  1704. case CT_TUNER_STEP_1:
  1705. val = dib0090_get_slow_adc_val(state);
  1706. state->temperature = ((s16) ((val - state->adc_diff) * 180) >> 8) + 55;
  1707. dprintk("temperature: %d C", state->temperature - 30);
  1708. *tune_state = CT_TUNER_STEP_2;
  1709. break;
  1710. case CT_TUNER_STEP_2:
  1711. dib0090_write_reg(state, 0x13, state->bias);
  1712. dib0090_write_reg(state, 0x10, state->wbdmux); /* write back original WBDMUX */
  1713. *tune_state = CT_TUNER_START;
  1714. state->calibrate &= ~TEMP_CAL;
  1715. if (state->config->analog_output == 0)
  1716. dib0090_write_reg(state, 0x23, dib0090_read_reg(state, 0x23) | (1 << 14));
  1717. break;
  1718. default:
  1719. ret = 0;
  1720. break;
  1721. }
  1722. return ret;
  1723. }
  1724. #define WBD 0x781 /* 1 1 1 1 0000 0 0 1 */
  1725. static int dib0090_tune(struct dvb_frontend *fe)
  1726. {
  1727. struct dib0090_state *state = fe->tuner_priv;
  1728. const struct dib0090_tuning *tune = state->current_tune_table_index;
  1729. const struct dib0090_pll *pll = state->current_pll_table_index;
  1730. enum frontend_tune_state *tune_state = &state->tune_state;
  1731. u16 lo5, lo6, Den, tmp;
  1732. u32 FBDiv, Rest, FREF, VCOF_kHz = 0;
  1733. int ret = 10; /* 1ms is the default delay most of the time */
  1734. u8 c, i;
  1735. /************************* VCO ***************************/
  1736. /* Default values for FG */
  1737. /* from these are needed : */
  1738. /* Cp,HFdiv,VCOband,SD,Num,Den,FB and REFDiv */
  1739. /* in any case we first need to do a calibration if needed */
  1740. if (*tune_state == CT_TUNER_START) {
  1741. /* deactivate DataTX before some calibrations */
  1742. if (state->calibrate & (DC_CAL | TEMP_CAL | WBD_CAL))
  1743. dib0090_write_reg(state, 0x23, dib0090_read_reg(state, 0x23) & ~(1 << 14));
  1744. else
  1745. /* Activate DataTX in case a calibration has been done before */
  1746. if (state->config->analog_output == 0)
  1747. dib0090_write_reg(state, 0x23, dib0090_read_reg(state, 0x23) | (1 << 14));
  1748. }
  1749. if (state->calibrate & DC_CAL)
  1750. return dib0090_dc_offset_calibration(state, tune_state);
  1751. else if (state->calibrate & WBD_CAL) {
  1752. if (state->current_rf == 0)
  1753. state->current_rf = state->fe->dtv_property_cache.frequency / 1000;
  1754. return dib0090_wbd_calibration(state, tune_state);
  1755. } else if (state->calibrate & TEMP_CAL)
  1756. return dib0090_get_temperature(state, tune_state);
  1757. else if (state->calibrate & CAPTRIM_CAL)
  1758. return dib0090_captrim_search(state, tune_state);
  1759. if (*tune_state == CT_TUNER_START) {
  1760. /* if soc and AGC pwm control, disengage mux to be able to R/W access to 0x01 register to set the right filter (cutoff_freq_select) during the tune sequence, otherwise, SOC SERPAR error when accessing to 0x01 */
  1761. if (state->config->use_pwm_agc && state->identity.in_soc) {
  1762. tmp = dib0090_read_reg(state, 0x39);
  1763. if ((tmp >> 10) & 0x1)
  1764. dib0090_write_reg(state, 0x39, tmp & ~(1 << 10));
  1765. }
  1766. state->current_band = (u8) BAND_OF_FREQUENCY(state->fe->dtv_property_cache.frequency / 1000);
  1767. state->rf_request =
  1768. state->fe->dtv_property_cache.frequency / 1000 + (state->current_band ==
  1769. BAND_UHF ? state->config->freq_offset_khz_uhf : state->config->
  1770. freq_offset_khz_vhf);
  1771. /* in ISDB-T 1seg we shift tuning frequency */
  1772. if ((state->fe->dtv_property_cache.delivery_system == SYS_ISDBT && state->fe->dtv_property_cache.isdbt_sb_mode == 1
  1773. && state->fe->dtv_property_cache.isdbt_partial_reception == 0)) {
  1774. const struct dib0090_low_if_offset_table *LUT_offset = state->config->low_if;
  1775. u8 found_offset = 0;
  1776. u32 margin_khz = 100;
  1777. if (LUT_offset != NULL) {
  1778. while (LUT_offset->RF_freq != 0xffff) {
  1779. if (((state->rf_request > (LUT_offset->RF_freq - margin_khz))
  1780. && (state->rf_request < (LUT_offset->RF_freq + margin_khz)))
  1781. && LUT_offset->std == state->fe->dtv_property_cache.delivery_system) {
  1782. state->rf_request += LUT_offset->offset_khz;
  1783. found_offset = 1;
  1784. break;
  1785. }
  1786. LUT_offset++;
  1787. }
  1788. }
  1789. if (found_offset == 0)
  1790. state->rf_request += 400;
  1791. }
  1792. if (state->current_rf != state->rf_request || (state->current_standard != state->fe->dtv_property_cache.delivery_system)) {
  1793. state->tuner_is_tuned = 0;
  1794. state->current_rf = 0;
  1795. state->current_standard = 0;
  1796. tune = dib0090_tuning_table;
  1797. if (state->identity.p1g)
  1798. tune = dib0090_p1g_tuning_table;
  1799. tmp = (state->identity.version >> 5) & 0x7;
  1800. if (state->identity.in_soc) {
  1801. if (state->config->force_cband_input) { /* Use the CBAND input for all band */
  1802. if (state->current_band & BAND_CBAND || state->current_band & BAND_FM || state->current_band & BAND_VHF
  1803. || state->current_band & BAND_UHF) {
  1804. state->current_band = BAND_CBAND;
  1805. tune = dib0090_tuning_table_cband_7090;
  1806. }
  1807. } else { /* Use the CBAND input for all band under UHF */
  1808. if (state->current_band & BAND_CBAND || state->current_band & BAND_FM || state->current_band & BAND_VHF) {
  1809. state->current_band = BAND_CBAND;
  1810. tune = dib0090_tuning_table_cband_7090;
  1811. }
  1812. }
  1813. } else
  1814. if (tmp == 0x4 || tmp == 0x7) {
  1815. /* CBAND tuner version for VHF */
  1816. if (state->current_band == BAND_FM || state->current_band == BAND_CBAND || state->current_band == BAND_VHF) {
  1817. state->current_band = BAND_CBAND; /* Force CBAND */
  1818. tune = dib0090_tuning_table_fm_vhf_on_cband;
  1819. if (state->identity.p1g)
  1820. tune = dib0090_p1g_tuning_table_fm_vhf_on_cband;
  1821. }
  1822. }
  1823. pll = dib0090_pll_table;
  1824. if (state->identity.p1g)
  1825. pll = dib0090_p1g_pll_table;
  1826. /* Look for the interval */
  1827. while (state->rf_request > tune->max_freq)
  1828. tune++;
  1829. while (state->rf_request > pll->max_freq)
  1830. pll++;
  1831. state->current_tune_table_index = tune;
  1832. state->current_pll_table_index = pll;
  1833. dib0090_write_reg(state, 0x0b, 0xb800 | (tune->switch_trim));
  1834. VCOF_kHz = (pll->hfdiv * state->rf_request) * 2;
  1835. FREF = state->config->io.clock_khz;
  1836. if (state->config->fref_clock_ratio != 0)
  1837. FREF /= state->config->fref_clock_ratio;
  1838. FBDiv = (VCOF_kHz / pll->topresc / FREF);
  1839. Rest = (VCOF_kHz / pll->topresc) - FBDiv * FREF;
  1840. if (Rest < LPF)
  1841. Rest = 0;
  1842. else if (Rest < 2 * LPF)
  1843. Rest = 2 * LPF;
  1844. else if (Rest > (FREF - LPF)) {
  1845. Rest = 0;
  1846. FBDiv += 1;
  1847. } else if (Rest > (FREF - 2 * LPF))
  1848. Rest = FREF - 2 * LPF;
  1849. Rest = (Rest * 6528) / (FREF / 10);
  1850. state->rest = Rest;
  1851. /* external loop filter, otherwise:
  1852. * lo5 = (0 << 15) | (0 << 12) | (0 << 11) | (3 << 9) | (4 << 6) | (3 << 4) | 4;
  1853. * lo6 = 0x0e34 */
  1854. if (Rest == 0) {
  1855. if (pll->vco_band)
  1856. lo5 = 0x049f;
  1857. else
  1858. lo5 = 0x041f;
  1859. } else {
  1860. if (pll->vco_band)
  1861. lo5 = 0x049e;
  1862. else if (state->config->analog_output)
  1863. lo5 = 0x041d;
  1864. else
  1865. lo5 = 0x041c;
  1866. }
  1867. if (state->identity.p1g) { /* Bias is done automatically in P1G */
  1868. if (state->identity.in_soc) {
  1869. if (state->identity.version == SOC_8090_P1G_11R1)
  1870. lo5 = 0x46f;
  1871. else
  1872. lo5 = 0x42f;
  1873. } else
  1874. lo5 = 0x42c;
  1875. }
  1876. lo5 |= (pll->hfdiv_code << 11) | (pll->vco_band << 7); /* bit 15 is the split to the slave, we do not do it here */
  1877. if (!state->config->io.pll_int_loop_filt) {
  1878. if (state->identity.in_soc)
  1879. lo6 = 0xff98;
  1880. else if (state->identity.p1g || (Rest == 0))
  1881. lo6 = 0xfff8;
  1882. else
  1883. lo6 = 0xff28;
  1884. } else
  1885. lo6 = (state->config->io.pll_int_loop_filt << 3);
  1886. Den = 1;
  1887. if (Rest > 0) {
  1888. if (state->config->analog_output)
  1889. lo6 |= (1 << 2) | 2;
  1890. else {
  1891. if (state->identity.in_soc)
  1892. lo6 |= (1 << 2) | 2;
  1893. else
  1894. lo6 |= (1 << 2) | 2;
  1895. }
  1896. Den = 255;
  1897. }
  1898. dib0090_write_reg(state, 0x15, (u16) FBDiv);
  1899. if (state->config->fref_clock_ratio != 0)
  1900. dib0090_write_reg(state, 0x16, (Den << 8) | state->config->fref_clock_ratio);
  1901. else
  1902. dib0090_write_reg(state, 0x16, (Den << 8) | 1);
  1903. dib0090_write_reg(state, 0x17, (u16) Rest);
  1904. dib0090_write_reg(state, 0x19, lo5);
  1905. dib0090_write_reg(state, 0x1c, lo6);
  1906. lo6 = tune->tuner_enable;
  1907. if (state->config->analog_output)
  1908. lo6 = (lo6 & 0xff9f) | 0x2;
  1909. dib0090_write_reg(state, 0x24, lo6 | EN_LO | state->config->use_pwm_agc * EN_CRYSTAL);
  1910. }
  1911. state->current_rf = state->rf_request;
  1912. state->current_standard = state->fe->dtv_property_cache.delivery_system;
  1913. ret = 20;
  1914. state->calibrate = CAPTRIM_CAL; /* captrim serach now */
  1915. }
  1916. else if (*tune_state == CT_TUNER_STEP_0) { /* Warning : because of captrim cal, if you change this step, change it also in _cal.c file because it is the step following captrim cal state machine */
  1917. const struct dib0090_wbd_slope *wbd = state->current_wbd_table;
  1918. while (state->current_rf / 1000 > wbd->max_freq)
  1919. wbd++;
  1920. dib0090_write_reg(state, 0x1e, 0x07ff);
  1921. dprintk("Final Captrim: %d", (u32) state->fcaptrim);
  1922. dprintk("HFDIV code: %d", (u32) pll->hfdiv_code);
  1923. dprintk("VCO = %d", (u32) pll->vco_band);
  1924. dprintk("VCOF in kHz: %d ((%d*%d) << 1))", (u32) ((pll->hfdiv * state->rf_request) * 2), (u32) pll->hfdiv, (u32) state->rf_request);
  1925. dprintk("REFDIV: %d, FREF: %d", (u32) 1, (u32) state->config->io.clock_khz);
  1926. dprintk("FBDIV: %d, Rest: %d", (u32) dib0090_read_reg(state, 0x15), (u32) dib0090_read_reg(state, 0x17));
  1927. dprintk("Num: %d, Den: %d, SD: %d", (u32) dib0090_read_reg(state, 0x17), (u32) (dib0090_read_reg(state, 0x16) >> 8),
  1928. (u32) dib0090_read_reg(state, 0x1c) & 0x3);
  1929. #define WBD 0x781 /* 1 1 1 1 0000 0 0 1 */
  1930. c = 4;
  1931. i = 3;
  1932. if (wbd->wbd_gain != 0)
  1933. c = wbd->wbd_gain;
  1934. state->wbdmux = (c << 13) | (i << 11) | (WBD | (state->config->use_pwm_agc << 1));
  1935. dib0090_write_reg(state, 0x10, state->wbdmux);
  1936. if ((tune->tuner_enable == EN_CAB) && state->identity.p1g) {
  1937. dprintk("P1G : The cable band is selected and lna_tune = %d", tune->lna_tune);
  1938. dib0090_write_reg(state, 0x09, tune->lna_bias);
  1939. dib0090_write_reg(state, 0x0b, 0xb800 | (tune->lna_tune << 6) | (tune->switch_trim));
  1940. } else
  1941. dib0090_write_reg(state, 0x09, (tune->lna_tune << 5) | tune->lna_bias);
  1942. dib0090_write_reg(state, 0x0c, tune->v2i);
  1943. dib0090_write_reg(state, 0x0d, tune->mix);
  1944. dib0090_write_reg(state, 0x0e, tune->load);
  1945. *tune_state = CT_TUNER_STEP_1;
  1946. } else if (*tune_state == CT_TUNER_STEP_1) {
  1947. /* initialize the lt gain register */
  1948. state->rf_lt_def = 0x7c00;
  1949. dib0090_set_bandwidth(state);
  1950. state->tuner_is_tuned = 1;
  1951. state->calibrate |= WBD_CAL;
  1952. state->calibrate |= TEMP_CAL;
  1953. *tune_state = CT_TUNER_STOP;
  1954. } else
  1955. ret = FE_CALLBACK_TIME_NEVER;
  1956. return ret;
  1957. }
  1958. static int dib0090_release(struct dvb_frontend *fe)
  1959. {
  1960. kfree(fe->tuner_priv);
  1961. fe->tuner_priv = NULL;
  1962. return 0;
  1963. }
  1964. enum frontend_tune_state dib0090_get_tune_state(struct dvb_frontend *fe)
  1965. {
  1966. struct dib0090_state *state = fe->tuner_priv;
  1967. return state->tune_state;
  1968. }
  1969. EXPORT_SYMBOL(dib0090_get_tune_state);
  1970. int dib0090_set_tune_state(struct dvb_frontend *fe, enum frontend_tune_state tune_state)
  1971. {
  1972. struct dib0090_state *state = fe->tuner_priv;
  1973. state->tune_state = tune_state;
  1974. return 0;
  1975. }
  1976. EXPORT_SYMBOL(dib0090_set_tune_state);
  1977. static int dib0090_get_frequency(struct dvb_frontend *fe, u32 * frequency)
  1978. {
  1979. struct dib0090_state *state = fe->tuner_priv;
  1980. *frequency = 1000 * state->current_rf;
  1981. return 0;
  1982. }
  1983. static int dib0090_set_params(struct dvb_frontend *fe, struct dvb_frontend_parameters *p)
  1984. {
  1985. struct dib0090_state *state = fe->tuner_priv;
  1986. u32 ret;
  1987. state->tune_state = CT_TUNER_START;
  1988. do {
  1989. ret = dib0090_tune(fe);
  1990. if (ret != FE_CALLBACK_TIME_NEVER)
  1991. msleep(ret / 10);
  1992. else
  1993. break;
  1994. } while (state->tune_state != CT_TUNER_STOP);
  1995. return 0;
  1996. }
  1997. static const struct dvb_tuner_ops dib0090_ops = {
  1998. .info = {
  1999. .name = "DiBcom DiB0090",
  2000. .frequency_min = 45000000,
  2001. .frequency_max = 860000000,
  2002. .frequency_step = 1000,
  2003. },
  2004. .release = dib0090_release,
  2005. .init = dib0090_wakeup,
  2006. .sleep = dib0090_sleep,
  2007. .set_params = dib0090_set_params,
  2008. .get_frequency = dib0090_get_frequency,
  2009. };
  2010. static const struct dvb_tuner_ops dib0090_fw_ops = {
  2011. .info = {
  2012. .name = "DiBcom DiB0090",
  2013. .frequency_min = 45000000,
  2014. .frequency_max = 860000000,
  2015. .frequency_step = 1000,
  2016. },
  2017. .release = dib0090_release,
  2018. .init = NULL,
  2019. .sleep = NULL,
  2020. .set_params = NULL,
  2021. .get_frequency = NULL,
  2022. };
  2023. static const struct dib0090_wbd_slope dib0090_wbd_table_default[] = {
  2024. {470, 0, 250, 0, 100, 4},
  2025. {860, 51, 866, 21, 375, 4},
  2026. {1700, 0, 800, 0, 850, 4},
  2027. {2900, 0, 250, 0, 100, 6},
  2028. {0xFFFF, 0, 0, 0, 0, 0},
  2029. };
  2030. struct dvb_frontend *dib0090_register(struct dvb_frontend *fe, struct i2c_adapter *i2c, const struct dib0090_config *config)
  2031. {
  2032. struct dib0090_state *st = kzalloc(sizeof(struct dib0090_state), GFP_KERNEL);
  2033. if (st == NULL)
  2034. return NULL;
  2035. st->config = config;
  2036. st->i2c = i2c;
  2037. st->fe = fe;
  2038. fe->tuner_priv = st;
  2039. if (config->wbd == NULL)
  2040. st->current_wbd_table = dib0090_wbd_table_default;
  2041. else
  2042. st->current_wbd_table = config->wbd;
  2043. if (dib0090_reset(fe) != 0)
  2044. goto free_mem;
  2045. printk(KERN_INFO "DiB0090: successfully identified\n");
  2046. memcpy(&fe->ops.tuner_ops, &dib0090_ops, sizeof(struct dvb_tuner_ops));
  2047. return fe;
  2048. free_mem:
  2049. kfree(st);
  2050. fe->tuner_priv = NULL;
  2051. return NULL;
  2052. }
  2053. EXPORT_SYMBOL(dib0090_register);
  2054. struct dvb_frontend *dib0090_fw_register(struct dvb_frontend *fe, struct i2c_adapter *i2c, const struct dib0090_config *config)
  2055. {
  2056. struct dib0090_fw_state *st = kzalloc(sizeof(struct dib0090_fw_state), GFP_KERNEL);
  2057. if (st == NULL)
  2058. return NULL;
  2059. st->config = config;
  2060. st->i2c = i2c;
  2061. st->fe = fe;
  2062. fe->tuner_priv = st;
  2063. if (dib0090_fw_reset_digital(fe, st->config) != 0)
  2064. goto free_mem;
  2065. dprintk("DiB0090 FW: successfully identified");
  2066. memcpy(&fe->ops.tuner_ops, &dib0090_fw_ops, sizeof(struct dvb_tuner_ops));
  2067. return fe;
  2068. free_mem:
  2069. kfree(st);
  2070. fe->tuner_priv = NULL;
  2071. return NULL;
  2072. }
  2073. EXPORT_SYMBOL(dib0090_fw_register);
  2074. MODULE_AUTHOR("Patrick Boettcher <pboettcher@dibcom.fr>");
  2075. MODULE_AUTHOR("Olivier Grenie <olivier.grenie@dibcom.fr>");
  2076. MODULE_DESCRIPTION("Driver for the DiBcom 0090 base-band RF Tuner");
  2077. MODULE_LICENSE("GPL");