dib0090.c 71 KB

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