dib0090.c 70 KB

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