dib0090.c 76 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675
  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. if (cfg->in_soc)
  449. return;
  450. dib0090_write_reg(state, 0x1b, EN_DIGCLK | EN_PLL | EN_CRYSTAL); /* PLL, DIG_CLK and CRYSTAL remain */
  451. /* adcClkOutRatio=8->7, release reset */
  452. dib0090_write_reg(state, 0x20, ((cfg->io.adc_clock_ratio - 1) << 11) | (0 << 10) | (1 << 9) | (1 << 8) | (0 << 4) | 0);
  453. if (cfg->clkoutdrive != 0)
  454. dib0090_write_reg(state, 0x23, (0 << 15) | ((!cfg->analog_output) << 14) | (2 << 10) | (1 << 9) | (0 << 8)
  455. | (cfg->clkoutdrive << 5) | (cfg->clkouttobamse << 4) | (0 << 2) | (0));
  456. else
  457. dib0090_write_reg(state, 0x23, (0 << 15) | ((!cfg->analog_output) << 14) | (2 << 10) | (1 << 9) | (0 << 8)
  458. | (7 << 5) | (cfg->clkouttobamse << 4) | (0 << 2) | (0));
  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_7090p[] = {
  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_7090e_sensitivity[] = {
  600. 186, /* max RF gain in 10th of dB */
  601. 40, /* ramp_slope = 1dB of gain -> clock_ticks_per_db = clk_khz / ramp_slope -> RF_RAMP2 */
  602. 746, /* ramp_max = maximum X used on the ramp */
  603. (10 << 10) | 345, /* RF_RAMP5, LNA 1 = 10dB */
  604. (0 << 10) | 746, /* RF_RAMP6, LNA 1 */
  605. (0 << 10) | 0, /* RF_RAMP7, LNA 2 = 0 dB */
  606. (0 << 10) | 0, /* RF_RAMP8, LNA 2 */
  607. (28 << 10) | 200, /* GAIN_4_1, LNA 3 = 6.8dB */ /* 3.61 dB */
  608. (0 << 10) | 345, /* GAIN_4_2, LNA 3 */
  609. (20 << 10) | 0, /* RF_RAMP3, LNA 4 = 6.2dB */ /* 4.96 dB */
  610. (0 << 10) | 200, /* RF_RAMP4, LNA 4 */
  611. };
  612. static const u16 rf_ramp_pwm_cband_7090e_aci[] = {
  613. 86, /* max RF gain in 10th of dB */
  614. 40, /* ramp_slope = 1dB of gain -> clock_ticks_per_db = clk_khz / ramp_slope -> RF_RAMP2 */
  615. 345, /* ramp_max = maximum X used on the ramp */
  616. (0 << 10) | 0, /* RF_RAMP5, LNA 1 = 8dB */ /* 7.47 dB */
  617. (0 << 10) | 0, /* RF_RAMP6, LNA 1 */
  618. (0 << 10) | 0, /* RF_RAMP7, LNA 2 = 0 dB */
  619. (0 << 10) | 0, /* RF_RAMP8, LNA 2 */
  620. (28 << 10) | 200, /* GAIN_4_1, LNA 3 = 6.8dB */ /* 3.61 dB */
  621. (0 << 10) | 345, /* GAIN_4_2, LNA 3 */
  622. (20 << 10) | 0, /* RF_RAMP3, LNA 4 = 6.2dB */ /* 4.96 dB */
  623. (0 << 10) | 200, /* RF_RAMP4, LNA 4 */
  624. };
  625. static const u16 rf_ramp_pwm_cband_8090[] = {
  626. 345, /* max RF gain in 10th of dB */
  627. 29, /* ramp_slope = 1dB of gain -> clock_ticks_per_db = clk_khz / ramp_slope -> RF_RAMP2 */
  628. 1000, /* ramp_max = maximum X used on the ramp */
  629. (35 << 10) | 772, /* RF_RAMP3, LNA 1 = 8dB */
  630. (0 << 10) | 1000, /* RF_RAMP4, LNA 1 */
  631. (58 << 10) | 496, /* RF_RAMP5, LNA 2 = 9.5dB */
  632. (0 << 10) | 772, /* RF_RAMP6, LNA 2 */
  633. (27 << 10) | 200, /* RF_RAMP7, LNA 3 = 10.5dB */
  634. (0 << 10) | 496, /* RF_RAMP8, LNA 3 */
  635. (40 << 10) | 0, /* GAIN_4_1, LNA 4 = 7dB */
  636. (0 << 10) | 200, /* GAIN_4_2, LNA 4 */
  637. };
  638. static const u16 rf_ramp_pwm_uhf_7090[] = {
  639. 407, /* max RF gain in 10th of dB */
  640. 13, /* ramp_slope = 1dB of gain -> clock_ticks_per_db = clk_khz / ramp_slope -> RF_RAMP2 */
  641. 529, /* ramp_max = maximum X used on the ramp */
  642. (23 << 10) | 0, /* RF_RAMP3, LNA 1 = 14.7dB */
  643. (0 << 10) | 176, /* RF_RAMP4, LNA 1 */
  644. (63 << 10) | 400, /* RF_RAMP5, LNA 2 = 8dB */
  645. (0 << 10) | 529, /* RF_RAMP6, LNA 2 */
  646. (48 << 10) | 316, /* RF_RAMP7, LNA 3 = 6.8dB */
  647. (0 << 10) | 400, /* RF_RAMP8, LNA 3 */
  648. (29 << 10) | 176, /* GAIN_4_1, LNA 4 = 11.5dB */
  649. (0 << 10) | 316, /* GAIN_4_2, LNA 4 */
  650. };
  651. static const u16 rf_ramp_pwm_uhf_8090[] = {
  652. 388, /* max RF gain in 10th of dB */
  653. 26, /* ramp_slope = 1dB of gain -> clock_ticks_per_db = clk_khz / ramp_slope -> RF_RAMP2 */
  654. 1008, /* ramp_max = maximum X used on the ramp */
  655. (11 << 10) | 0, /* RF_RAMP3, LNA 1 = 14.7dB */
  656. (0 << 10) | 369, /* RF_RAMP4, LNA 1 */
  657. (41 << 10) | 809, /* RF_RAMP5, LNA 2 = 8dB */
  658. (0 << 10) | 1008, /* RF_RAMP6, LNA 2 */
  659. (27 << 10) | 659, /* RF_RAMP7, LNA 3 = 6dB */
  660. (0 << 10) | 809, /* RF_RAMP8, LNA 3 */
  661. (14 << 10) | 369, /* GAIN_4_1, LNA 4 = 11.5dB */
  662. (0 << 10) | 659, /* GAIN_4_2, LNA 4 */
  663. };
  664. /* GENERAL PWM ramp definition for all other Krosus */
  665. static const u16 bb_ramp_pwm_normal[] = {
  666. 500, /* max BB gain in 10th of dB */
  667. 8, /* ramp_slope = 1dB of gain -> clock_ticks_per_db = clk_khz / ramp_slope -> BB_RAMP2 */
  668. 400,
  669. (2 << 9) | 0, /* BB_RAMP3 = 21dB */
  670. (0 << 9) | 168, /* BB_RAMP4 */
  671. (2 << 9) | 168, /* BB_RAMP5 = 29dB */
  672. (0 << 9) | 400, /* BB_RAMP6 */
  673. };
  674. static const u16 bb_ramp_pwm_boost[] = {
  675. 550, /* max BB gain in 10th of dB */
  676. 8, /* ramp_slope = 1dB of gain -> clock_ticks_per_db = clk_khz / ramp_slope -> BB_RAMP2 */
  677. 440,
  678. (2 << 9) | 0, /* BB_RAMP3 = 26dB */
  679. (0 << 9) | 208, /* BB_RAMP4 */
  680. (2 << 9) | 208, /* BB_RAMP5 = 29dB */
  681. (0 << 9) | 440, /* BB_RAMP6 */
  682. };
  683. static const u16 rf_ramp_pwm_cband[] = {
  684. 314, /* max RF gain in 10th of dB */
  685. 33, /* ramp_slope = 1dB of gain -> clock_ticks_per_db = clk_khz / ramp_slope -> RF_RAMP2 */
  686. 1023, /* ramp_max = maximum X used on the ramp */
  687. (8 << 10) | 743, /* RF_RAMP3, LNA 1 = 0dB */
  688. (0 << 10) | 1023, /* RF_RAMP4, LNA 1 */
  689. (15 << 10) | 469, /* RF_RAMP5, LNA 2 = 0dB */
  690. (0 << 10) | 742, /* RF_RAMP6, LNA 2 */
  691. (9 << 10) | 234, /* RF_RAMP7, LNA 3 = 0dB */
  692. (0 << 10) | 468, /* RF_RAMP8, LNA 3 */
  693. (9 << 10) | 0, /* GAIN_4_1, LNA 4 = 0dB */
  694. (0 << 10) | 233, /* GAIN_4_2, LNA 4 */
  695. };
  696. static const u16 rf_ramp_pwm_vhf[] = {
  697. 398, /* max RF gain in 10th of dB */
  698. 24, /* ramp_slope = 1dB of gain -> clock_ticks_per_db = clk_khz / ramp_slope -> RF_RAMP2 */
  699. 954, /* ramp_max = maximum X used on the ramp */
  700. (7 << 10) | 0, /* RF_RAMP3, LNA 1 = 13.2dB */
  701. (0 << 10) | 290, /* RF_RAMP4, LNA 1 */
  702. (16 << 10) | 699, /* RF_RAMP5, LNA 2 = 10.5dB */
  703. (0 << 10) | 954, /* RF_RAMP6, LNA 2 */
  704. (17 << 10) | 580, /* RF_RAMP7, LNA 3 = 5dB */
  705. (0 << 10) | 699, /* RF_RAMP8, LNA 3 */
  706. (7 << 10) | 290, /* GAIN_4_1, LNA 4 = 12.5dB */
  707. (0 << 10) | 580, /* GAIN_4_2, LNA 4 */
  708. };
  709. static const u16 rf_ramp_pwm_uhf[] = {
  710. 398, /* max RF gain in 10th of dB */
  711. 24, /* ramp_slope = 1dB of gain -> clock_ticks_per_db = clk_khz / ramp_slope -> RF_RAMP2 */
  712. 954, /* ramp_max = maximum X used on the ramp */
  713. (7 << 10) | 0, /* RF_RAMP3, LNA 1 = 13.2dB */
  714. (0 << 10) | 290, /* RF_RAMP4, LNA 1 */
  715. (16 << 10) | 699, /* RF_RAMP5, LNA 2 = 10.5dB */
  716. (0 << 10) | 954, /* RF_RAMP6, LNA 2 */
  717. (17 << 10) | 580, /* RF_RAMP7, LNA 3 = 5dB */
  718. (0 << 10) | 699, /* RF_RAMP8, LNA 3 */
  719. (7 << 10) | 290, /* GAIN_4_1, LNA 4 = 12.5dB */
  720. (0 << 10) | 580, /* GAIN_4_2, LNA 4 */
  721. };
  722. static const u16 rf_ramp_pwm_sband[] = {
  723. 253, /* max RF gain in 10th of dB */
  724. 38, /* ramp_slope = 1dB of gain -> clock_ticks_per_db = clk_khz / ramp_slope -> RF_RAMP2 */
  725. 961,
  726. (4 << 10) | 0, /* RF_RAMP3, LNA 1 = 14.1dB */
  727. (0 << 10) | 508, /* RF_RAMP4, LNA 1 */
  728. (9 << 10) | 508, /* RF_RAMP5, LNA 2 = 11.2dB */
  729. (0 << 10) | 961, /* RF_RAMP6, LNA 2 */
  730. (0 << 10) | 0, /* RF_RAMP7, LNA 3 = 0dB */
  731. (0 << 10) | 0, /* RF_RAMP8, LNA 3 */
  732. (0 << 10) | 0, /* GAIN_4_1, LNA 4 = 0dB */
  733. (0 << 10) | 0, /* GAIN_4_2, LNA 4 */
  734. };
  735. struct slope {
  736. s16 range;
  737. s16 slope;
  738. };
  739. static u16 slopes_to_scale(const struct slope *slopes, u8 num, s16 val)
  740. {
  741. u8 i;
  742. u16 rest;
  743. u16 ret = 0;
  744. for (i = 0; i < num; i++) {
  745. if (val > slopes[i].range)
  746. rest = slopes[i].range;
  747. else
  748. rest = val;
  749. ret += (rest * slopes[i].slope) / slopes[i].range;
  750. val -= rest;
  751. }
  752. return ret;
  753. }
  754. static const struct slope dib0090_wbd_slopes[3] = {
  755. {66, 120}, /* -64,-52: offset - 65 */
  756. {600, 170}, /* -52,-35: 65 - 665 */
  757. {170, 250}, /* -45,-10: 665 - 835 */
  758. };
  759. static s16 dib0090_wbd_to_db(struct dib0090_state *state, u16 wbd)
  760. {
  761. wbd &= 0x3ff;
  762. if (wbd < state->wbd_offset)
  763. wbd = 0;
  764. else
  765. wbd -= state->wbd_offset;
  766. /* -64dB is the floor */
  767. return -640 + (s16) slopes_to_scale(dib0090_wbd_slopes, ARRAY_SIZE(dib0090_wbd_slopes), wbd);
  768. }
  769. static void dib0090_wbd_target(struct dib0090_state *state, u32 rf)
  770. {
  771. u16 offset = 250;
  772. /* TODO : DAB digital N+/-1 interferer perfs : offset = 10 */
  773. if (state->current_band == BAND_VHF)
  774. offset = 650;
  775. #ifndef FIRMWARE_FIREFLY
  776. if (state->current_band == BAND_VHF)
  777. offset = state->config->wbd_vhf_offset;
  778. if (state->current_band == BAND_CBAND)
  779. offset = state->config->wbd_cband_offset;
  780. #endif
  781. state->wbd_target = dib0090_wbd_to_db(state, state->wbd_offset + offset);
  782. dprintk("wbd-target: %d dB", (u32) state->wbd_target);
  783. }
  784. static const int gain_reg_addr[4] = {
  785. 0x08, 0x0a, 0x0f, 0x01
  786. };
  787. static void dib0090_gain_apply(struct dib0090_state *state, s16 gain_delta, s16 top_delta, u8 force)
  788. {
  789. u16 rf, bb, ref;
  790. u16 i, v, gain_reg[4] = { 0 }, gain;
  791. const u16 *g;
  792. if (top_delta < -511)
  793. top_delta = -511;
  794. if (top_delta > 511)
  795. top_delta = 511;
  796. if (force) {
  797. top_delta *= (1 << WBD_ALPHA);
  798. gain_delta *= (1 << GAIN_ALPHA);
  799. }
  800. if (top_delta >= ((s16) (state->rf_ramp[0] << WBD_ALPHA) - state->rf_gain_limit)) /* overflow */
  801. state->rf_gain_limit = state->rf_ramp[0] << WBD_ALPHA;
  802. else
  803. state->rf_gain_limit += top_delta;
  804. if (state->rf_gain_limit < 0) /*underflow */
  805. state->rf_gain_limit = 0;
  806. /* use gain as a temporary variable and correct current_gain */
  807. gain = ((state->rf_gain_limit >> WBD_ALPHA) + state->bb_ramp[0]) << GAIN_ALPHA;
  808. if (gain_delta >= ((s16) gain - state->current_gain)) /* overflow */
  809. state->current_gain = gain;
  810. else
  811. state->current_gain += gain_delta;
  812. /* cannot be less than 0 (only if gain_delta is less than 0 we can have current_gain < 0) */
  813. if (state->current_gain < 0)
  814. state->current_gain = 0;
  815. /* now split total gain to rf and bb gain */
  816. gain = state->current_gain >> GAIN_ALPHA;
  817. /* requested gain is bigger than rf gain limit - ACI/WBD adjustment */
  818. if (gain > (state->rf_gain_limit >> WBD_ALPHA)) {
  819. rf = state->rf_gain_limit >> WBD_ALPHA;
  820. bb = gain - rf;
  821. if (bb > state->bb_ramp[0])
  822. bb = state->bb_ramp[0];
  823. } else { /* high signal level -> all gains put on RF */
  824. rf = gain;
  825. bb = 0;
  826. }
  827. state->gain[0] = rf;
  828. state->gain[1] = bb;
  829. /* software ramp */
  830. /* Start with RF gains */
  831. g = state->rf_ramp + 1; /* point on RF LNA1 max gain */
  832. ref = rf;
  833. for (i = 0; i < 7; i++) { /* Go over all amplifiers => 5RF amps + 2 BB amps = 7 amps */
  834. 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 */
  835. v = 0; /* force the gain to write for the current amp to be null */
  836. else if (ref >= g[1]) /* Gain to set is higher than the high working point of this amp */
  837. v = g[2]; /* force this amp to be full gain */
  838. else /* compute the value to set to this amp because we are somewhere in his range */
  839. v = ((ref - (g[1] - g[0])) * g[2]) / g[0];
  840. if (i == 0) /* LNA 1 reg mapping */
  841. gain_reg[0] = v;
  842. else if (i == 1) /* LNA 2 reg mapping */
  843. gain_reg[0] |= v << 7;
  844. else if (i == 2) /* LNA 3 reg mapping */
  845. gain_reg[1] = v;
  846. else if (i == 3) /* LNA 4 reg mapping */
  847. gain_reg[1] |= v << 7;
  848. else if (i == 4) /* CBAND LNA reg mapping */
  849. gain_reg[2] = v | state->rf_lt_def;
  850. else if (i == 5) /* BB gain 1 reg mapping */
  851. gain_reg[3] = v << 3;
  852. else if (i == 6) /* BB gain 2 reg mapping */
  853. gain_reg[3] |= v << 8;
  854. g += 3; /* go to next gain bloc */
  855. /* When RF is finished, start with BB */
  856. if (i == 4) {
  857. g = state->bb_ramp + 1; /* point on BB gain 1 max gain */
  858. ref = bb;
  859. }
  860. }
  861. gain_reg[3] |= state->bb_1_def;
  862. gain_reg[3] |= ((bb % 10) * 100) / 125;
  863. #ifdef DEBUG_AGC
  864. 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,
  865. gain_reg[0], gain_reg[1], gain_reg[2], gain_reg[3]);
  866. #endif
  867. /* Write the amplifier regs */
  868. for (i = 0; i < 4; i++) {
  869. v = gain_reg[i];
  870. if (force || state->gain_reg[i] != v) {
  871. state->gain_reg[i] = v;
  872. dib0090_write_reg(state, gain_reg_addr[i], v);
  873. }
  874. }
  875. }
  876. static void dib0090_set_boost(struct dib0090_state *state, int onoff)
  877. {
  878. state->bb_1_def &= 0xdfff;
  879. state->bb_1_def |= onoff << 13;
  880. }
  881. static void dib0090_set_rframp(struct dib0090_state *state, const u16 * cfg)
  882. {
  883. state->rf_ramp = cfg;
  884. }
  885. static void dib0090_set_rframp_pwm(struct dib0090_state *state, const u16 * cfg)
  886. {
  887. state->rf_ramp = cfg;
  888. dib0090_write_reg(state, 0x2a, 0xffff);
  889. dprintk("total RF gain: %ddB, step: %d", (u32) cfg[0], dib0090_read_reg(state, 0x2a));
  890. dib0090_write_regs(state, 0x2c, cfg + 3, 6);
  891. dib0090_write_regs(state, 0x3e, cfg + 9, 2);
  892. }
  893. static void dib0090_set_bbramp(struct dib0090_state *state, const u16 * cfg)
  894. {
  895. state->bb_ramp = cfg;
  896. dib0090_set_boost(state, cfg[0] > 500); /* we want the boost if the gain is higher that 50dB */
  897. }
  898. static void dib0090_set_bbramp_pwm(struct dib0090_state *state, const u16 * cfg)
  899. {
  900. state->bb_ramp = cfg;
  901. dib0090_set_boost(state, cfg[0] > 500); /* we want the boost if the gain is higher that 50dB */
  902. dib0090_write_reg(state, 0x33, 0xffff);
  903. dprintk("total BB gain: %ddB, step: %d", (u32) cfg[0], dib0090_read_reg(state, 0x33));
  904. dib0090_write_regs(state, 0x35, cfg + 3, 4);
  905. }
  906. void dib0090_pwm_gain_reset(struct dvb_frontend *fe)
  907. {
  908. struct dib0090_state *state = fe->tuner_priv;
  909. u16 *bb_ramp = (u16 *)&bb_ramp_pwm_normal; /* default baseband config */
  910. u16 *rf_ramp = NULL;
  911. u8 en_pwm_rf_mux = 1;
  912. /* reset the AGC */
  913. if (state->config->use_pwm_agc) {
  914. if (state->current_band == BAND_CBAND) {
  915. if (state->identity.in_soc) {
  916. bb_ramp = (u16 *)&bb_ramp_pwm_normal_socs;
  917. if (state->identity.version == SOC_8090_P1G_11R1 || state->identity.version == SOC_8090_P1G_21R1)
  918. rf_ramp = (u16 *)&rf_ramp_pwm_cband_8090;
  919. else if (state->identity.version == SOC_7090_P1G_11R1 || state->identity.version == SOC_7090_P1G_21R1) {
  920. if (state->config->is_dib7090e) {
  921. if (state->rf_ramp == NULL)
  922. rf_ramp = (u16 *)&rf_ramp_pwm_cband_7090e_sensitivity;
  923. else
  924. rf_ramp = (u16 *)state->rf_ramp;
  925. } else
  926. rf_ramp = (u16 *)&rf_ramp_pwm_cband_7090p;
  927. }
  928. } else
  929. rf_ramp = (u16 *)&rf_ramp_pwm_cband;
  930. } else
  931. if (state->current_band == BAND_VHF) {
  932. if (state->identity.in_soc) {
  933. bb_ramp = (u16 *)&bb_ramp_pwm_normal_socs;
  934. /* rf_ramp = &rf_ramp_pwm_vhf_socs; */ /* TODO */
  935. } else
  936. rf_ramp = (u16 *)&rf_ramp_pwm_vhf;
  937. } else if (state->current_band == BAND_UHF) {
  938. if (state->identity.in_soc) {
  939. bb_ramp = (u16 *)&bb_ramp_pwm_normal_socs;
  940. if (state->identity.version == SOC_8090_P1G_11R1 || state->identity.version == SOC_8090_P1G_21R1)
  941. rf_ramp = (u16 *)&rf_ramp_pwm_uhf_8090;
  942. else if (state->identity.version == SOC_7090_P1G_11R1 || state->identity.version == SOC_7090_P1G_21R1)
  943. rf_ramp = (u16 *)&rf_ramp_pwm_uhf_7090;
  944. } else
  945. rf_ramp = (u16 *)&rf_ramp_pwm_uhf;
  946. }
  947. if (rf_ramp)
  948. dib0090_set_rframp_pwm(state, rf_ramp);
  949. dib0090_set_bbramp_pwm(state, bb_ramp);
  950. /* activate the ramp generator using PWM control */
  951. dprintk("ramp RF gain = %d BAND = %s version = %d", state->rf_ramp[0], (state->current_band == BAND_CBAND) ? "CBAND" : "NOT CBAND", state->identity.version & 0x1f);
  952. if ((state->rf_ramp[0] == 0) || (state->current_band == BAND_CBAND && (state->identity.version & 0x1f) <= P1D_E_F)) {
  953. dprintk("DE-Engage mux for direct gain reg control");
  954. en_pwm_rf_mux = 0;
  955. } else
  956. dprintk("Engage mux for PWM control");
  957. dib0090_write_reg(state, 0x32, (en_pwm_rf_mux << 12) | (en_pwm_rf_mux << 11));
  958. /* Set fast servo cutoff to start AGC; 0 = 1KHz ; 1 = 50Hz ; 2 = 150Hz ; 3 = 50KHz ; 4 = servo fast*/
  959. if (state->identity.version == SOC_7090_P1G_11R1 || state->identity.version == SOC_7090_P1G_21R1)
  960. dib0090_write_reg(state, 0x04, 3);
  961. else
  962. dib0090_write_reg(state, 0x04, 1);
  963. dib0090_write_reg(state, 0x39, (1 << 10)); /* 0 gain by default */
  964. }
  965. }
  966. EXPORT_SYMBOL(dib0090_pwm_gain_reset);
  967. void dib0090_set_dc_servo(struct dvb_frontend *fe, u8 DC_servo_cutoff)
  968. {
  969. struct dib0090_state *state = fe->tuner_priv;
  970. if (DC_servo_cutoff < 4)
  971. dib0090_write_reg(state, 0x04, DC_servo_cutoff);
  972. }
  973. EXPORT_SYMBOL(dib0090_set_dc_servo);
  974. static u32 dib0090_get_slow_adc_val(struct dib0090_state *state)
  975. {
  976. u16 adc_val = dib0090_read_reg(state, 0x1d);
  977. if (state->identity.in_soc)
  978. adc_val >>= 2;
  979. return adc_val;
  980. }
  981. int dib0090_gain_control(struct dvb_frontend *fe)
  982. {
  983. struct dib0090_state *state = fe->tuner_priv;
  984. enum frontend_tune_state *tune_state = &state->tune_state;
  985. int ret = 10;
  986. u16 wbd_val = 0;
  987. u8 apply_gain_immediatly = 1;
  988. s16 wbd_error = 0, adc_error = 0;
  989. if (*tune_state == CT_AGC_START) {
  990. state->agc_freeze = 0;
  991. dib0090_write_reg(state, 0x04, 0x0);
  992. #ifdef CONFIG_BAND_SBAND
  993. if (state->current_band == BAND_SBAND) {
  994. dib0090_set_rframp(state, rf_ramp_sband);
  995. dib0090_set_bbramp(state, bb_ramp_boost);
  996. } else
  997. #endif
  998. #ifdef CONFIG_BAND_VHF
  999. if (state->current_band == BAND_VHF && !state->identity.p1g) {
  1000. dib0090_set_rframp(state, rf_ramp_pwm_vhf);
  1001. dib0090_set_bbramp(state, bb_ramp_pwm_normal);
  1002. } else
  1003. #endif
  1004. #ifdef CONFIG_BAND_CBAND
  1005. if (state->current_band == BAND_CBAND && !state->identity.p1g) {
  1006. dib0090_set_rframp(state, rf_ramp_pwm_cband);
  1007. dib0090_set_bbramp(state, bb_ramp_pwm_normal);
  1008. } else
  1009. #endif
  1010. if ((state->current_band == BAND_CBAND || state->current_band == BAND_VHF) && state->identity.p1g) {
  1011. dib0090_set_rframp(state, rf_ramp_pwm_cband_7090p);
  1012. dib0090_set_bbramp(state, bb_ramp_pwm_normal_socs);
  1013. } else {
  1014. dib0090_set_rframp(state, rf_ramp_pwm_uhf);
  1015. dib0090_set_bbramp(state, bb_ramp_pwm_normal);
  1016. }
  1017. dib0090_write_reg(state, 0x32, 0);
  1018. dib0090_write_reg(state, 0x39, 0);
  1019. dib0090_wbd_target(state, state->current_rf);
  1020. state->rf_gain_limit = state->rf_ramp[0] << WBD_ALPHA;
  1021. state->current_gain = ((state->rf_ramp[0] + state->bb_ramp[0]) / 2) << GAIN_ALPHA;
  1022. *tune_state = CT_AGC_STEP_0;
  1023. } else if (!state->agc_freeze) {
  1024. s16 wbd = 0, i, cnt;
  1025. int adc;
  1026. wbd_val = dib0090_get_slow_adc_val(state);
  1027. if (*tune_state == CT_AGC_STEP_0)
  1028. cnt = 5;
  1029. else
  1030. cnt = 1;
  1031. for (i = 0; i < cnt; i++) {
  1032. wbd_val = dib0090_get_slow_adc_val(state);
  1033. wbd += dib0090_wbd_to_db(state, wbd_val);
  1034. }
  1035. wbd /= cnt;
  1036. wbd_error = state->wbd_target - wbd;
  1037. if (*tune_state == CT_AGC_STEP_0) {
  1038. if (wbd_error < 0 && state->rf_gain_limit > 0 && !state->identity.p1g) {
  1039. #ifdef CONFIG_BAND_CBAND
  1040. /* in case of CBAND tune reduce first the lt_gain2 before adjusting the RF gain */
  1041. u8 ltg2 = (state->rf_lt_def >> 10) & 0x7;
  1042. if (state->current_band == BAND_CBAND && ltg2) {
  1043. ltg2 >>= 1;
  1044. state->rf_lt_def &= ltg2 << 10; /* reduce in 3 steps from 7 to 0 */
  1045. }
  1046. #endif
  1047. } else {
  1048. state->agc_step = 0;
  1049. *tune_state = CT_AGC_STEP_1;
  1050. }
  1051. } else {
  1052. /* calc the adc power */
  1053. adc = state->config->get_adc_power(fe);
  1054. adc = (adc * ((s32) 355774) + (((s32) 1) << 20)) >> 21; /* included in [0:-700] */
  1055. adc_error = (s16) (((s32) ADC_TARGET) - adc);
  1056. #ifdef CONFIG_STANDARD_DAB
  1057. if (state->fe->dtv_property_cache.delivery_system == STANDARD_DAB)
  1058. adc_error -= 10;
  1059. #endif
  1060. #ifdef CONFIG_STANDARD_DVBT
  1061. if (state->fe->dtv_property_cache.delivery_system == STANDARD_DVBT &&
  1062. (state->fe->dtv_property_cache.modulation == QAM_64 || state->fe->dtv_property_cache.modulation == QAM_16))
  1063. adc_error += 60;
  1064. #endif
  1065. #ifdef CONFIG_SYS_ISDBT
  1066. if ((state->fe->dtv_property_cache.delivery_system == SYS_ISDBT) && (((state->fe->dtv_property_cache.layer[0].segment_count >
  1067. 0)
  1068. &&
  1069. ((state->fe->dtv_property_cache.layer[0].modulation ==
  1070. QAM_64)
  1071. || (state->fe->dtv_property_cache.
  1072. layer[0].modulation == QAM_16)))
  1073. ||
  1074. ((state->fe->dtv_property_cache.layer[1].segment_count >
  1075. 0)
  1076. &&
  1077. ((state->fe->dtv_property_cache.layer[1].modulation ==
  1078. QAM_64)
  1079. || (state->fe->dtv_property_cache.
  1080. layer[1].modulation == QAM_16)))
  1081. ||
  1082. ((state->fe->dtv_property_cache.layer[2].segment_count >
  1083. 0)
  1084. &&
  1085. ((state->fe->dtv_property_cache.layer[2].modulation ==
  1086. QAM_64)
  1087. || (state->fe->dtv_property_cache.
  1088. layer[2].modulation == QAM_16)))
  1089. )
  1090. )
  1091. adc_error += 60;
  1092. #endif
  1093. if (*tune_state == CT_AGC_STEP_1) { /* quickly go to the correct range of the ADC power */
  1094. if (ABS(adc_error) < 50 || state->agc_step++ > 5) {
  1095. #ifdef CONFIG_STANDARD_DAB
  1096. if (state->fe->dtv_property_cache.delivery_system == STANDARD_DAB) {
  1097. dib0090_write_reg(state, 0x02, (1 << 15) | (15 << 11) | (31 << 6) | (63)); /* cap value = 63 : narrow BB filter : Fc = 1.8MHz */
  1098. dib0090_write_reg(state, 0x04, 0x0);
  1099. } else
  1100. #endif
  1101. {
  1102. dib0090_write_reg(state, 0x02, (1 << 15) | (3 << 11) | (6 << 6) | (32));
  1103. dib0090_write_reg(state, 0x04, 0x01); /*0 = 1KHz ; 1 = 150Hz ; 2 = 50Hz ; 3 = 50KHz ; 4 = servo fast */
  1104. }
  1105. *tune_state = CT_AGC_STOP;
  1106. }
  1107. } else {
  1108. /* everything higher than or equal to CT_AGC_STOP means tracking */
  1109. ret = 100; /* 10ms interval */
  1110. apply_gain_immediatly = 0;
  1111. }
  1112. }
  1113. #ifdef DEBUG_AGC
  1114. dprintk
  1115. ("tune state %d, ADC = %3ddB (ADC err %3d) WBD %3ddB (WBD err %3d, WBD val SADC: %4d), RFGainLimit (TOP): %3d, signal: %3ddBm",
  1116. (u32) *tune_state, (u32) adc, (u32) adc_error, (u32) wbd, (u32) wbd_error, (u32) wbd_val,
  1117. (u32) state->rf_gain_limit >> WBD_ALPHA, (s32) 200 + adc - (state->current_gain >> GAIN_ALPHA));
  1118. #endif
  1119. }
  1120. /* apply gain */
  1121. if (!state->agc_freeze)
  1122. dib0090_gain_apply(state, adc_error, wbd_error, apply_gain_immediatly);
  1123. return ret;
  1124. }
  1125. EXPORT_SYMBOL(dib0090_gain_control);
  1126. void dib0090_get_current_gain(struct dvb_frontend *fe, u16 * rf, u16 * bb, u16 * rf_gain_limit, u16 * rflt)
  1127. {
  1128. struct dib0090_state *state = fe->tuner_priv;
  1129. if (rf)
  1130. *rf = state->gain[0];
  1131. if (bb)
  1132. *bb = state->gain[1];
  1133. if (rf_gain_limit)
  1134. *rf_gain_limit = state->rf_gain_limit;
  1135. if (rflt)
  1136. *rflt = (state->rf_lt_def >> 10) & 0x7;
  1137. }
  1138. EXPORT_SYMBOL(dib0090_get_current_gain);
  1139. u16 dib0090_get_wbd_target(struct dvb_frontend *fe)
  1140. {
  1141. struct dib0090_state *state = fe->tuner_priv;
  1142. u32 f_MHz = state->fe->dtv_property_cache.frequency / 1000000;
  1143. s32 current_temp = state->temperature;
  1144. s32 wbd_thot, wbd_tcold;
  1145. const struct dib0090_wbd_slope *wbd = state->current_wbd_table;
  1146. while (f_MHz > wbd->max_freq)
  1147. wbd++;
  1148. dprintk("using wbd-table-entry with max freq %d", wbd->max_freq);
  1149. if (current_temp < 0)
  1150. current_temp = 0;
  1151. if (current_temp > 128)
  1152. current_temp = 128;
  1153. state->wbdmux &= ~(7 << 13);
  1154. if (wbd->wbd_gain != 0)
  1155. state->wbdmux |= (wbd->wbd_gain << 13);
  1156. else
  1157. state->wbdmux |= (4 << 13);
  1158. dib0090_write_reg(state, 0x10, state->wbdmux);
  1159. wbd_thot = wbd->offset_hot - (((u32) wbd->slope_hot * f_MHz) >> 6);
  1160. wbd_tcold = wbd->offset_cold - (((u32) wbd->slope_cold * f_MHz) >> 6);
  1161. wbd_tcold += ((wbd_thot - wbd_tcold) * current_temp) >> 7;
  1162. state->wbd_target = dib0090_wbd_to_db(state, state->wbd_offset + wbd_tcold);
  1163. dprintk("wbd-target: %d dB", (u32) state->wbd_target);
  1164. dprintk("wbd offset applied is %d", wbd_tcold);
  1165. return state->wbd_offset + wbd_tcold;
  1166. }
  1167. EXPORT_SYMBOL(dib0090_get_wbd_target);
  1168. u16 dib0090_get_wbd_offset(struct dvb_frontend *fe)
  1169. {
  1170. struct dib0090_state *state = fe->tuner_priv;
  1171. return state->wbd_offset;
  1172. }
  1173. EXPORT_SYMBOL(dib0090_get_wbd_offset);
  1174. int dib0090_set_switch(struct dvb_frontend *fe, u8 sw1, u8 sw2, u8 sw3)
  1175. {
  1176. struct dib0090_state *state = fe->tuner_priv;
  1177. dib0090_write_reg(state, 0x0b, (dib0090_read_reg(state, 0x0b) & 0xfff8)
  1178. | ((sw3 & 1) << 2) | ((sw2 & 1) << 1) | (sw1 & 1));
  1179. return 0;
  1180. }
  1181. EXPORT_SYMBOL(dib0090_set_switch);
  1182. int dib0090_set_vga(struct dvb_frontend *fe, u8 onoff)
  1183. {
  1184. struct dib0090_state *state = fe->tuner_priv;
  1185. dib0090_write_reg(state, 0x09, (dib0090_read_reg(state, 0x09) & 0x7fff)
  1186. | ((onoff & 1) << 15));
  1187. return 0;
  1188. }
  1189. EXPORT_SYMBOL(dib0090_set_vga);
  1190. int dib0090_update_rframp_7090(struct dvb_frontend *fe, u8 cfg_sensitivity)
  1191. {
  1192. struct dib0090_state *state = fe->tuner_priv;
  1193. if ((!state->identity.p1g) || (!state->identity.in_soc)
  1194. || ((state->identity.version != SOC_7090_P1G_21R1)
  1195. && (state->identity.version != SOC_7090_P1G_11R1))) {
  1196. dprintk("%s() function can only be used for dib7090P", __func__);
  1197. return -ENODEV;
  1198. }
  1199. if (cfg_sensitivity)
  1200. state->rf_ramp = (const u16 *)&rf_ramp_pwm_cband_7090e_sensitivity;
  1201. else
  1202. state->rf_ramp = (const u16 *)&rf_ramp_pwm_cband_7090e_aci;
  1203. dib0090_pwm_gain_reset(fe);
  1204. return 0;
  1205. }
  1206. EXPORT_SYMBOL(dib0090_update_rframp_7090);
  1207. static const u16 dib0090_defaults[] = {
  1208. 25, 0x01,
  1209. 0x0000,
  1210. 0x99a0,
  1211. 0x6008,
  1212. 0x0000,
  1213. 0x8bcb,
  1214. 0x0000,
  1215. 0x0405,
  1216. 0x0000,
  1217. 0x0000,
  1218. 0x0000,
  1219. 0xb802,
  1220. 0x0300,
  1221. 0x2d12,
  1222. 0xbac0,
  1223. 0x7c00,
  1224. 0xdbb9,
  1225. 0x0954,
  1226. 0x0743,
  1227. 0x8000,
  1228. 0x0001,
  1229. 0x0040,
  1230. 0x0100,
  1231. 0x0000,
  1232. 0xe910,
  1233. 0x149e,
  1234. 1, 0x1c,
  1235. 0xff2d,
  1236. 1, 0x39,
  1237. 0x0000,
  1238. 2, 0x1e,
  1239. 0x07FF,
  1240. 0x0007,
  1241. 1, 0x24,
  1242. EN_UHF | EN_CRYSTAL,
  1243. 2, 0x3c,
  1244. 0x3ff,
  1245. 0x111,
  1246. 0
  1247. };
  1248. static const u16 dib0090_p1g_additionnal_defaults[] = {
  1249. 1, 0x05,
  1250. 0xabcd,
  1251. 1, 0x11,
  1252. 0x00b4,
  1253. 1, 0x1c,
  1254. 0xfffd,
  1255. 1, 0x40,
  1256. 0x108,
  1257. 0
  1258. };
  1259. static void dib0090_set_default_config(struct dib0090_state *state, const u16 * n)
  1260. {
  1261. u16 l, r;
  1262. l = pgm_read_word(n++);
  1263. while (l) {
  1264. r = pgm_read_word(n++);
  1265. do {
  1266. dib0090_write_reg(state, r, pgm_read_word(n++));
  1267. r++;
  1268. } while (--l);
  1269. l = pgm_read_word(n++);
  1270. }
  1271. }
  1272. #define CAP_VALUE_MIN (u8) 9
  1273. #define CAP_VALUE_MAX (u8) 40
  1274. #define HR_MIN (u8) 25
  1275. #define HR_MAX (u8) 40
  1276. #define POLY_MIN (u8) 0
  1277. #define POLY_MAX (u8) 8
  1278. static void dib0090_set_EFUSE(struct dib0090_state *state)
  1279. {
  1280. u8 c, h, n;
  1281. u16 e2, e4;
  1282. u16 cal;
  1283. e2 = dib0090_read_reg(state, 0x26);
  1284. e4 = dib0090_read_reg(state, 0x28);
  1285. if ((state->identity.version == P1D_E_F) ||
  1286. (state->identity.version == P1G) || (e2 == 0xffff)) {
  1287. dib0090_write_reg(state, 0x22, 0x10);
  1288. cal = (dib0090_read_reg(state, 0x22) >> 6) & 0x3ff;
  1289. if ((cal < 670) || (cal == 1023))
  1290. cal = 850;
  1291. n = 165 - ((cal * 10)>>6) ;
  1292. e2 = e4 = (3<<12) | (34<<6) | (n);
  1293. }
  1294. if (e2 != e4)
  1295. e2 &= e4; /* Remove the redundancy */
  1296. if (e2 != 0xffff) {
  1297. c = e2 & 0x3f;
  1298. n = (e2 >> 12) & 0xf;
  1299. h = (e2 >> 6) & 0x3f;
  1300. if ((c >= CAP_VALUE_MAX) || (c <= CAP_VALUE_MIN))
  1301. c = 32;
  1302. else
  1303. c += 14;
  1304. if ((h >= HR_MAX) || (h <= HR_MIN))
  1305. h = 34;
  1306. if ((n >= POLY_MAX) || (n <= POLY_MIN))
  1307. n = 3;
  1308. dib0090_write_reg(state, 0x13, (h << 10));
  1309. e2 = (n << 11) | ((h >> 2)<<6) | c;
  1310. dib0090_write_reg(state, 0x2, e2); /* Load the BB_2 */
  1311. }
  1312. }
  1313. static int dib0090_reset(struct dvb_frontend *fe)
  1314. {
  1315. struct dib0090_state *state = fe->tuner_priv;
  1316. dib0090_reset_digital(fe, state->config);
  1317. if (dib0090_identify(fe) < 0)
  1318. return -EIO;
  1319. #ifdef CONFIG_TUNER_DIB0090_P1B_SUPPORT
  1320. if (!(state->identity.version & 0x1)) /* it is P1B - reset is already done */
  1321. return 0;
  1322. #endif
  1323. if (!state->identity.in_soc) {
  1324. if ((dib0090_read_reg(state, 0x1a) >> 5) & 0x2)
  1325. dib0090_write_reg(state, 0x1b, (EN_IQADC | EN_BB | EN_BIAS | EN_DIGCLK | EN_PLL | EN_CRYSTAL));
  1326. else
  1327. dib0090_write_reg(state, 0x1b, (EN_DIGCLK | EN_PLL | EN_CRYSTAL));
  1328. }
  1329. dib0090_set_default_config(state, dib0090_defaults);
  1330. if (state->identity.in_soc)
  1331. dib0090_write_reg(state, 0x18, 0x2910); /* charge pump current = 0 */
  1332. if (state->identity.p1g)
  1333. dib0090_set_default_config(state, dib0090_p1g_additionnal_defaults);
  1334. /* Update the efuse : Only available for KROSUS > P1C and SOC as well*/
  1335. if (((state->identity.version & 0x1f) >= P1D_E_F) || (state->identity.in_soc))
  1336. dib0090_set_EFUSE(state);
  1337. /* Congigure in function of the crystal */
  1338. if (state->config->force_crystal_mode != 0)
  1339. dib0090_write_reg(state, 0x14,
  1340. state->config->force_crystal_mode & 3);
  1341. else if (state->config->io.clock_khz >= 24000)
  1342. dib0090_write_reg(state, 0x14, 1);
  1343. else
  1344. dib0090_write_reg(state, 0x14, 2);
  1345. dprintk("Pll lock : %d", (dib0090_read_reg(state, 0x1a) >> 11) & 0x1);
  1346. state->calibrate = DC_CAL | WBD_CAL | TEMP_CAL; /* enable iq-offset-calibration and wbd-calibration when tuning next time */
  1347. return 0;
  1348. }
  1349. #define steps(u) (((u) > 15) ? ((u)-16) : (u))
  1350. #define INTERN_WAIT 10
  1351. static int dib0090_get_offset(struct dib0090_state *state, enum frontend_tune_state *tune_state)
  1352. {
  1353. int ret = INTERN_WAIT * 10;
  1354. switch (*tune_state) {
  1355. case CT_TUNER_STEP_2:
  1356. /* Turns to positive */
  1357. dib0090_write_reg(state, 0x1f, 0x7);
  1358. *tune_state = CT_TUNER_STEP_3;
  1359. break;
  1360. case CT_TUNER_STEP_3:
  1361. state->adc_diff = dib0090_read_reg(state, 0x1d);
  1362. /* Turns to negative */
  1363. dib0090_write_reg(state, 0x1f, 0x4);
  1364. *tune_state = CT_TUNER_STEP_4;
  1365. break;
  1366. case CT_TUNER_STEP_4:
  1367. state->adc_diff -= dib0090_read_reg(state, 0x1d);
  1368. *tune_state = CT_TUNER_STEP_5;
  1369. ret = 0;
  1370. break;
  1371. default:
  1372. break;
  1373. }
  1374. return ret;
  1375. }
  1376. struct dc_calibration {
  1377. u8 addr;
  1378. u8 offset;
  1379. u8 pga:1;
  1380. u16 bb1;
  1381. u8 i:1;
  1382. };
  1383. static const struct dc_calibration dc_table[] = {
  1384. /* Step1 BB gain1= 26 with boost 1, gain 2 = 0 */
  1385. {0x06, 5, 1, (1 << 13) | (0 << 8) | (26 << 3), 1},
  1386. {0x07, 11, 1, (1 << 13) | (0 << 8) | (26 << 3), 0},
  1387. /* Step 2 BB gain 1 = 26 with boost = 1 & gain 2 = 29 */
  1388. {0x06, 0, 0, (1 << 13) | (29 << 8) | (26 << 3), 1},
  1389. {0x06, 10, 0, (1 << 13) | (29 << 8) | (26 << 3), 0},
  1390. {0},
  1391. };
  1392. static const struct dc_calibration dc_p1g_table[] = {
  1393. /* Step1 BB gain1= 26 with boost 1, gain 2 = 0 */
  1394. /* addr ; trim reg offset ; pga ; CTRL_BB1 value ; i or q */
  1395. {0x06, 5, 1, (1 << 13) | (0 << 8) | (15 << 3), 1},
  1396. {0x07, 11, 1, (1 << 13) | (0 << 8) | (15 << 3), 0},
  1397. /* Step 2 BB gain 1 = 26 with boost = 1 & gain 2 = 29 */
  1398. {0x06, 0, 0, (1 << 13) | (29 << 8) | (15 << 3), 1},
  1399. {0x06, 10, 0, (1 << 13) | (29 << 8) | (15 << 3), 0},
  1400. {0},
  1401. };
  1402. static void dib0090_set_trim(struct dib0090_state *state)
  1403. {
  1404. u16 *val;
  1405. if (state->dc->addr == 0x07)
  1406. val = &state->bb7;
  1407. else
  1408. val = &state->bb6;
  1409. *val &= ~(0x1f << state->dc->offset);
  1410. *val |= state->step << state->dc->offset;
  1411. dib0090_write_reg(state, state->dc->addr, *val);
  1412. }
  1413. static int dib0090_dc_offset_calibration(struct dib0090_state *state, enum frontend_tune_state *tune_state)
  1414. {
  1415. int ret = 0;
  1416. u16 reg;
  1417. switch (*tune_state) {
  1418. case CT_TUNER_START:
  1419. dprintk("Start DC offset calibration");
  1420. /* force vcm2 = 0.8V */
  1421. state->bb6 = 0;
  1422. state->bb7 = 0x040d;
  1423. /* the LNA AND LO are off */
  1424. reg = dib0090_read_reg(state, 0x24) & 0x0ffb; /* shutdown lna and lo */
  1425. dib0090_write_reg(state, 0x24, reg);
  1426. state->wbdmux = dib0090_read_reg(state, 0x10);
  1427. dib0090_write_reg(state, 0x10, (state->wbdmux & ~(0xff << 3)) | (0x7 << 3) | 0x3);
  1428. dib0090_write_reg(state, 0x23, dib0090_read_reg(state, 0x23) & ~(1 << 14));
  1429. state->dc = dc_table;
  1430. if (state->identity.p1g)
  1431. state->dc = dc_p1g_table;
  1432. /* fall through */
  1433. case CT_TUNER_STEP_0:
  1434. dprintk("Start/continue DC calibration for %s path", (state->dc->i == 1) ? "I" : "Q");
  1435. dib0090_write_reg(state, 0x01, state->dc->bb1);
  1436. dib0090_write_reg(state, 0x07, state->bb7 | (state->dc->i << 7));
  1437. state->step = 0;
  1438. state->min_adc_diff = 1023;
  1439. *tune_state = CT_TUNER_STEP_1;
  1440. ret = 50;
  1441. break;
  1442. case CT_TUNER_STEP_1:
  1443. dib0090_set_trim(state);
  1444. *tune_state = CT_TUNER_STEP_2;
  1445. break;
  1446. case CT_TUNER_STEP_2:
  1447. case CT_TUNER_STEP_3:
  1448. case CT_TUNER_STEP_4:
  1449. ret = dib0090_get_offset(state, tune_state);
  1450. break;
  1451. case CT_TUNER_STEP_5: /* found an offset */
  1452. dprintk("adc_diff = %d, current step= %d", (u32) state->adc_diff, state->step);
  1453. if (state->step == 0 && state->adc_diff < 0) {
  1454. state->min_adc_diff = -1023;
  1455. dprintk("Change of sign of the minimum adc diff");
  1456. }
  1457. dprintk("adc_diff = %d, min_adc_diff = %d current_step = %d", state->adc_diff, state->min_adc_diff, state->step);
  1458. /* first turn for this frequency */
  1459. if (state->step == 0) {
  1460. if (state->dc->pga && state->adc_diff < 0)
  1461. state->step = 0x10;
  1462. if (state->dc->pga == 0 && state->adc_diff > 0)
  1463. state->step = 0x10;
  1464. }
  1465. /* Look for a change of Sign in the Adc_diff.min_adc_diff is used to STORE the setp N-1 */
  1466. if ((state->adc_diff & 0x8000) == (state->min_adc_diff & 0x8000) && steps(state->step) < 15) {
  1467. /* stop search when the delta the sign is changing and Steps =15 and Step=0 is force for continuance */
  1468. state->step++;
  1469. state->min_adc_diff = state->adc_diff;
  1470. *tune_state = CT_TUNER_STEP_1;
  1471. } else {
  1472. /* the minimum was what we have seen in the step before */
  1473. if (ABS(state->adc_diff) > ABS(state->min_adc_diff)) {
  1474. dprintk("Since adc_diff N = %d > adc_diff step N-1 = %d, Come back one step", state->adc_diff, state->min_adc_diff);
  1475. state->step--;
  1476. }
  1477. dib0090_set_trim(state);
  1478. dprintk("BB Offset Cal, BBreg=%hd,Offset=%hd,Value Set=%hd", state->dc->addr, state->adc_diff, state->step);
  1479. state->dc++;
  1480. if (state->dc->addr == 0) /* done */
  1481. *tune_state = CT_TUNER_STEP_6;
  1482. else
  1483. *tune_state = CT_TUNER_STEP_0;
  1484. }
  1485. break;
  1486. case CT_TUNER_STEP_6:
  1487. dib0090_write_reg(state, 0x07, state->bb7 & ~0x0008);
  1488. dib0090_write_reg(state, 0x1f, 0x7);
  1489. *tune_state = CT_TUNER_START; /* reset done -> real tuning can now begin */
  1490. state->calibrate &= ~DC_CAL;
  1491. default:
  1492. break;
  1493. }
  1494. return ret;
  1495. }
  1496. static int dib0090_wbd_calibration(struct dib0090_state *state, enum frontend_tune_state *tune_state)
  1497. {
  1498. u8 wbd_gain;
  1499. const struct dib0090_wbd_slope *wbd = state->current_wbd_table;
  1500. switch (*tune_state) {
  1501. case CT_TUNER_START:
  1502. while (state->current_rf / 1000 > wbd->max_freq)
  1503. wbd++;
  1504. if (wbd->wbd_gain != 0)
  1505. wbd_gain = wbd->wbd_gain;
  1506. else {
  1507. wbd_gain = 4;
  1508. #if defined(CONFIG_BAND_LBAND) || defined(CONFIG_BAND_SBAND)
  1509. if ((state->current_band == BAND_LBAND) || (state->current_band == BAND_SBAND))
  1510. wbd_gain = 2;
  1511. #endif
  1512. }
  1513. if (wbd_gain == state->wbd_calibration_gain) { /* the WBD calibration has already been done */
  1514. *tune_state = CT_TUNER_START;
  1515. state->calibrate &= ~WBD_CAL;
  1516. return 0;
  1517. }
  1518. dib0090_write_reg(state, 0x10, 0x1b81 | (1 << 10) | (wbd_gain << 13) | (1 << 3));
  1519. dib0090_write_reg(state, 0x24, ((EN_UHF & 0x0fff) | (1 << 1)));
  1520. *tune_state = CT_TUNER_STEP_0;
  1521. state->wbd_calibration_gain = wbd_gain;
  1522. return 90; /* wait for the WBDMUX to switch and for the ADC to sample */
  1523. case CT_TUNER_STEP_0:
  1524. state->wbd_offset = dib0090_get_slow_adc_val(state);
  1525. dprintk("WBD calibration offset = %d", state->wbd_offset);
  1526. *tune_state = CT_TUNER_START; /* reset done -> real tuning can now begin */
  1527. state->calibrate &= ~WBD_CAL;
  1528. break;
  1529. default:
  1530. break;
  1531. }
  1532. return 0;
  1533. }
  1534. static void dib0090_set_bandwidth(struct dib0090_state *state)
  1535. {
  1536. u16 tmp;
  1537. if (state->fe->dtv_property_cache.bandwidth_hz / 1000 <= 5000)
  1538. tmp = (3 << 14);
  1539. else if (state->fe->dtv_property_cache.bandwidth_hz / 1000 <= 6000)
  1540. tmp = (2 << 14);
  1541. else if (state->fe->dtv_property_cache.bandwidth_hz / 1000 <= 7000)
  1542. tmp = (1 << 14);
  1543. else
  1544. tmp = (0 << 14);
  1545. state->bb_1_def &= 0x3fff;
  1546. state->bb_1_def |= tmp;
  1547. dib0090_write_reg(state, 0x01, state->bb_1_def); /* be sure that we have the right bb-filter */
  1548. dib0090_write_reg(state, 0x03, 0x6008); /* = 0x6008 : vcm3_trim = 1 ; filter2_gm1_trim = 8 ; filter2_cutoff_freq = 0 */
  1549. dib0090_write_reg(state, 0x04, 0x1); /* 0 = 1KHz ; 1 = 50Hz ; 2 = 150Hz ; 3 = 50KHz ; 4 = servo fast */
  1550. if (state->identity.in_soc) {
  1551. 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 */
  1552. } else {
  1553. dib0090_write_reg(state, 0x02, (5 << 11) | (8 << 6) | (22 & 0x3f)); /* 22 = cap_value */
  1554. 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 */
  1555. }
  1556. }
  1557. static const struct dib0090_pll dib0090_pll_table[] = {
  1558. #ifdef CONFIG_BAND_CBAND
  1559. {56000, 0, 9, 48, 6},
  1560. {70000, 1, 9, 48, 6},
  1561. {87000, 0, 8, 32, 4},
  1562. {105000, 1, 8, 32, 4},
  1563. {115000, 0, 7, 24, 6},
  1564. {140000, 1, 7, 24, 6},
  1565. {170000, 0, 6, 16, 4},
  1566. #endif
  1567. #ifdef CONFIG_BAND_VHF
  1568. {200000, 1, 6, 16, 4},
  1569. {230000, 0, 5, 12, 6},
  1570. {280000, 1, 5, 12, 6},
  1571. {340000, 0, 4, 8, 4},
  1572. {380000, 1, 4, 8, 4},
  1573. {450000, 0, 3, 6, 6},
  1574. #endif
  1575. #ifdef CONFIG_BAND_UHF
  1576. {580000, 1, 3, 6, 6},
  1577. {700000, 0, 2, 4, 4},
  1578. {860000, 1, 2, 4, 4},
  1579. #endif
  1580. #ifdef CONFIG_BAND_LBAND
  1581. {1800000, 1, 0, 2, 4},
  1582. #endif
  1583. #ifdef CONFIG_BAND_SBAND
  1584. {2900000, 0, 14, 1, 4},
  1585. #endif
  1586. };
  1587. static const struct dib0090_tuning dib0090_tuning_table_fm_vhf_on_cband[] = {
  1588. #ifdef CONFIG_BAND_CBAND
  1589. {184000, 4, 1, 15, 0x280, 0x2912, 0xb94e, EN_CAB},
  1590. {227000, 4, 3, 15, 0x280, 0x2912, 0xb94e, EN_CAB},
  1591. {380000, 4, 7, 15, 0x280, 0x2912, 0xb94e, EN_CAB},
  1592. #endif
  1593. #ifdef CONFIG_BAND_UHF
  1594. {520000, 2, 0, 15, 0x300, 0x1d12, 0xb9ce, EN_UHF},
  1595. {550000, 2, 2, 15, 0x300, 0x1d12, 0xb9ce, EN_UHF},
  1596. {650000, 2, 3, 15, 0x300, 0x1d12, 0xb9ce, EN_UHF},
  1597. {750000, 2, 5, 15, 0x300, 0x1d12, 0xb9ce, EN_UHF},
  1598. {850000, 2, 6, 15, 0x300, 0x1d12, 0xb9ce, EN_UHF},
  1599. {900000, 2, 7, 15, 0x300, 0x1d12, 0xb9ce, EN_UHF},
  1600. #endif
  1601. #ifdef CONFIG_BAND_LBAND
  1602. {1500000, 4, 0, 20, 0x300, 0x1912, 0x82c9, EN_LBD},
  1603. {1600000, 4, 1, 20, 0x300, 0x1912, 0x82c9, EN_LBD},
  1604. {1800000, 4, 3, 20, 0x300, 0x1912, 0x82c9, EN_LBD},
  1605. #endif
  1606. #ifdef CONFIG_BAND_SBAND
  1607. {2300000, 1, 4, 20, 0x300, 0x2d2A, 0x82c7, EN_SBD},
  1608. {2900000, 1, 7, 20, 0x280, 0x2deb, 0x8347, EN_SBD},
  1609. #endif
  1610. };
  1611. static const struct dib0090_tuning dib0090_tuning_table[] = {
  1612. #ifdef CONFIG_BAND_CBAND
  1613. {170000, 4, 1, 15, 0x280, 0x2912, 0xb94e, EN_CAB},
  1614. #endif
  1615. #ifdef CONFIG_BAND_VHF
  1616. {184000, 1, 1, 15, 0x300, 0x4d12, 0xb94e, EN_VHF},
  1617. {227000, 1, 3, 15, 0x300, 0x4d12, 0xb94e, EN_VHF},
  1618. {380000, 1, 7, 15, 0x300, 0x4d12, 0xb94e, EN_VHF},
  1619. #endif
  1620. #ifdef CONFIG_BAND_UHF
  1621. {520000, 2, 0, 15, 0x300, 0x1d12, 0xb9ce, EN_UHF},
  1622. {550000, 2, 2, 15, 0x300, 0x1d12, 0xb9ce, EN_UHF},
  1623. {650000, 2, 3, 15, 0x300, 0x1d12, 0xb9ce, EN_UHF},
  1624. {750000, 2, 5, 15, 0x300, 0x1d12, 0xb9ce, EN_UHF},
  1625. {850000, 2, 6, 15, 0x300, 0x1d12, 0xb9ce, EN_UHF},
  1626. {900000, 2, 7, 15, 0x300, 0x1d12, 0xb9ce, EN_UHF},
  1627. #endif
  1628. #ifdef CONFIG_BAND_LBAND
  1629. {1500000, 4, 0, 20, 0x300, 0x1912, 0x82c9, EN_LBD},
  1630. {1600000, 4, 1, 20, 0x300, 0x1912, 0x82c9, EN_LBD},
  1631. {1800000, 4, 3, 20, 0x300, 0x1912, 0x82c9, EN_LBD},
  1632. #endif
  1633. #ifdef CONFIG_BAND_SBAND
  1634. {2300000, 1, 4, 20, 0x300, 0x2d2A, 0x82c7, EN_SBD},
  1635. {2900000, 1, 7, 20, 0x280, 0x2deb, 0x8347, EN_SBD},
  1636. #endif
  1637. };
  1638. static const struct dib0090_tuning dib0090_p1g_tuning_table[] = {
  1639. #ifdef CONFIG_BAND_CBAND
  1640. {170000, 4, 1, 0x820f, 0x300, 0x2d22, 0x82cb, EN_CAB},
  1641. #endif
  1642. #ifdef CONFIG_BAND_VHF
  1643. {184000, 1, 1, 15, 0x300, 0x4d12, 0xb94e, EN_VHF},
  1644. {227000, 1, 3, 15, 0x300, 0x4d12, 0xb94e, EN_VHF},
  1645. {380000, 1, 7, 15, 0x300, 0x4d12, 0xb94e, EN_VHF},
  1646. #endif
  1647. #ifdef CONFIG_BAND_UHF
  1648. {510000, 2, 0, 15, 0x300, 0x1d12, 0xb9ce, EN_UHF},
  1649. {540000, 2, 1, 15, 0x300, 0x1d12, 0xb9ce, EN_UHF},
  1650. {600000, 2, 3, 15, 0x300, 0x1d12, 0xb9ce, EN_UHF},
  1651. {630000, 2, 4, 15, 0x300, 0x1d12, 0xb9ce, EN_UHF},
  1652. {680000, 2, 5, 15, 0x300, 0x1d12, 0xb9ce, EN_UHF},
  1653. {720000, 2, 6, 15, 0x300, 0x1d12, 0xb9ce, EN_UHF},
  1654. {900000, 2, 7, 15, 0x300, 0x1d12, 0xb9ce, EN_UHF},
  1655. #endif
  1656. #ifdef CONFIG_BAND_LBAND
  1657. {1500000, 4, 0, 20, 0x300, 0x1912, 0x82c9, EN_LBD},
  1658. {1600000, 4, 1, 20, 0x300, 0x1912, 0x82c9, EN_LBD},
  1659. {1800000, 4, 3, 20, 0x300, 0x1912, 0x82c9, EN_LBD},
  1660. #endif
  1661. #ifdef CONFIG_BAND_SBAND
  1662. {2300000, 1, 4, 20, 0x300, 0x2d2A, 0x82c7, EN_SBD},
  1663. {2900000, 1, 7, 20, 0x280, 0x2deb, 0x8347, EN_SBD},
  1664. #endif
  1665. };
  1666. static const struct dib0090_pll dib0090_p1g_pll_table[] = {
  1667. #ifdef CONFIG_BAND_CBAND
  1668. {57000, 0, 11, 48, 6},
  1669. {70000, 1, 11, 48, 6},
  1670. {86000, 0, 10, 32, 4},
  1671. {105000, 1, 10, 32, 4},
  1672. {115000, 0, 9, 24, 6},
  1673. {140000, 1, 9, 24, 6},
  1674. {170000, 0, 8, 16, 4},
  1675. #endif
  1676. #ifdef CONFIG_BAND_VHF
  1677. {200000, 1, 8, 16, 4},
  1678. {230000, 0, 7, 12, 6},
  1679. {280000, 1, 7, 12, 6},
  1680. {340000, 0, 6, 8, 4},
  1681. {380000, 1, 6, 8, 4},
  1682. {455000, 0, 5, 6, 6},
  1683. #endif
  1684. #ifdef CONFIG_BAND_UHF
  1685. {580000, 1, 5, 6, 6},
  1686. {680000, 0, 4, 4, 4},
  1687. {860000, 1, 4, 4, 4},
  1688. #endif
  1689. #ifdef CONFIG_BAND_LBAND
  1690. {1800000, 1, 2, 2, 4},
  1691. #endif
  1692. #ifdef CONFIG_BAND_SBAND
  1693. {2900000, 0, 1, 1, 6},
  1694. #endif
  1695. };
  1696. static const struct dib0090_tuning dib0090_p1g_tuning_table_fm_vhf_on_cband[] = {
  1697. #ifdef CONFIG_BAND_CBAND
  1698. {184000, 4, 3, 0x4187, 0x2c0, 0x2d22, 0x81cb, EN_CAB},
  1699. {227000, 4, 3, 0x4187, 0x2c0, 0x2d22, 0x81cb, EN_CAB},
  1700. {380000, 4, 3, 0x4187, 0x2c0, 0x2d22, 0x81cb, EN_CAB},
  1701. #endif
  1702. #ifdef CONFIG_BAND_UHF
  1703. {520000, 2, 0, 15, 0x300, 0x1d12, 0xb9ce, EN_UHF},
  1704. {550000, 2, 2, 15, 0x300, 0x1d12, 0xb9ce, EN_UHF},
  1705. {650000, 2, 3, 15, 0x300, 0x1d12, 0xb9ce, EN_UHF},
  1706. {750000, 2, 5, 15, 0x300, 0x1d12, 0xb9ce, EN_UHF},
  1707. {850000, 2, 6, 15, 0x300, 0x1d12, 0xb9ce, EN_UHF},
  1708. {900000, 2, 7, 15, 0x300, 0x1d12, 0xb9ce, EN_UHF},
  1709. #endif
  1710. #ifdef CONFIG_BAND_LBAND
  1711. {1500000, 4, 0, 20, 0x300, 0x1912, 0x82c9, EN_LBD},
  1712. {1600000, 4, 1, 20, 0x300, 0x1912, 0x82c9, EN_LBD},
  1713. {1800000, 4, 3, 20, 0x300, 0x1912, 0x82c9, EN_LBD},
  1714. #endif
  1715. #ifdef CONFIG_BAND_SBAND
  1716. {2300000, 1, 4, 20, 0x300, 0x2d2A, 0x82c7, EN_SBD},
  1717. {2900000, 1, 7, 20, 0x280, 0x2deb, 0x8347, EN_SBD},
  1718. #endif
  1719. };
  1720. static const struct dib0090_tuning dib0090_tuning_table_cband_7090[] = {
  1721. #ifdef CONFIG_BAND_CBAND
  1722. {300000, 4, 3, 0x018F, 0x2c0, 0x2d22, 0xb9ce, EN_CAB},
  1723. {380000, 4, 10, 0x018F, 0x2c0, 0x2d22, 0xb9ce, EN_CAB},
  1724. {570000, 4, 10, 0x8190, 0x2c0, 0x2d22, 0xb9ce, EN_CAB},
  1725. {858000, 4, 5, 0x8190, 0x2c0, 0x2d22, 0xb9ce, EN_CAB},
  1726. #endif
  1727. };
  1728. static const struct dib0090_tuning dib0090_tuning_table_cband_7090e_sensitivity[] = {
  1729. #ifdef CONFIG_BAND_CBAND
  1730. { 300000, 0 , 3, 0x8105, 0x2c0, 0x2d12, 0xb84e, EN_CAB },
  1731. { 380000, 0 , 10, 0x810F, 0x2c0, 0x2d12, 0xb84e, EN_CAB },
  1732. { 600000, 0 , 10, 0x815E, 0x280, 0x2d12, 0xb84e, EN_CAB },
  1733. { 660000, 0 , 5, 0x85E3, 0x280, 0x2d12, 0xb84e, EN_CAB },
  1734. { 720000, 0 , 5, 0x852E, 0x280, 0x2d12, 0xb84e, EN_CAB },
  1735. { 860000, 0 , 4, 0x85E5, 0x280, 0x2d12, 0xb84e, EN_CAB },
  1736. #endif
  1737. };
  1738. int dib0090_update_tuning_table_7090(struct dvb_frontend *fe,
  1739. u8 cfg_sensitivity)
  1740. {
  1741. struct dib0090_state *state = fe->tuner_priv;
  1742. const struct dib0090_tuning *tune =
  1743. dib0090_tuning_table_cband_7090e_sensitivity;
  1744. const struct dib0090_tuning dib0090_tuning_table_cband_7090e_aci[] = {
  1745. { 300000, 0 , 3, 0x8165, 0x2c0, 0x2d12, 0xb84e, EN_CAB },
  1746. { 650000, 0 , 4, 0x815B, 0x280, 0x2d12, 0xb84e, EN_CAB },
  1747. { 860000, 0 , 5, 0x84EF, 0x280, 0x2d12, 0xb84e, EN_CAB },
  1748. };
  1749. if ((!state->identity.p1g) || (!state->identity.in_soc)
  1750. || ((state->identity.version != SOC_7090_P1G_21R1)
  1751. && (state->identity.version != SOC_7090_P1G_11R1))) {
  1752. dprintk("%s() function can only be used for dib7090", __func__);
  1753. return -ENODEV;
  1754. }
  1755. if (cfg_sensitivity)
  1756. tune = dib0090_tuning_table_cband_7090e_sensitivity;
  1757. else
  1758. tune = dib0090_tuning_table_cband_7090e_aci;
  1759. while (state->rf_request > tune->max_freq)
  1760. tune++;
  1761. dib0090_write_reg(state, 0x09, (dib0090_read_reg(state, 0x09) & 0x8000)
  1762. | (tune->lna_bias & 0x7fff));
  1763. dib0090_write_reg(state, 0x0b, (dib0090_read_reg(state, 0x0b) & 0xf83f)
  1764. | ((tune->lna_tune << 6) & 0x07c0));
  1765. return 0;
  1766. }
  1767. EXPORT_SYMBOL(dib0090_update_tuning_table_7090);
  1768. static int dib0090_captrim_search(struct dib0090_state *state, enum frontend_tune_state *tune_state)
  1769. {
  1770. int ret = 0;
  1771. u16 lo4 = 0xe900;
  1772. s16 adc_target;
  1773. u16 adc;
  1774. s8 step_sign;
  1775. u8 force_soft_search = 0;
  1776. if (state->identity.version == SOC_8090_P1G_11R1 || state->identity.version == SOC_8090_P1G_21R1)
  1777. force_soft_search = 1;
  1778. if (*tune_state == CT_TUNER_START) {
  1779. dprintk("Start Captrim search : %s", (force_soft_search == 1) ? "FORCE SOFT SEARCH" : "AUTO");
  1780. dib0090_write_reg(state, 0x10, 0x2B1);
  1781. dib0090_write_reg(state, 0x1e, 0x0032);
  1782. if (!state->tuner_is_tuned) {
  1783. /* prepare a complete captrim */
  1784. if (!state->identity.p1g || force_soft_search)
  1785. state->step = state->captrim = state->fcaptrim = 64;
  1786. state->current_rf = state->rf_request;
  1787. } else { /* we are already tuned to this frequency - the configuration is correct */
  1788. if (!state->identity.p1g || force_soft_search) {
  1789. /* do a minimal captrim even if the frequency has not changed */
  1790. state->step = 4;
  1791. state->captrim = state->fcaptrim = dib0090_read_reg(state, 0x18) & 0x7f;
  1792. }
  1793. }
  1794. state->adc_diff = 3000;
  1795. *tune_state = CT_TUNER_STEP_0;
  1796. } else if (*tune_state == CT_TUNER_STEP_0) {
  1797. if (state->identity.p1g && !force_soft_search) {
  1798. u8 ratio = 31;
  1799. dib0090_write_reg(state, 0x40, (3 << 7) | (ratio << 2) | (1 << 1) | 1);
  1800. dib0090_read_reg(state, 0x40);
  1801. ret = 50;
  1802. } else {
  1803. state->step /= 2;
  1804. dib0090_write_reg(state, 0x18, lo4 | state->captrim);
  1805. if (state->identity.in_soc)
  1806. ret = 25;
  1807. }
  1808. *tune_state = CT_TUNER_STEP_1;
  1809. } else if (*tune_state == CT_TUNER_STEP_1) {
  1810. if (state->identity.p1g && !force_soft_search) {
  1811. dib0090_write_reg(state, 0x40, 0x18c | (0 << 1) | 0);
  1812. dib0090_read_reg(state, 0x40);
  1813. state->fcaptrim = dib0090_read_reg(state, 0x18) & 0x7F;
  1814. dprintk("***Final Captrim= 0x%x", state->fcaptrim);
  1815. *tune_state = CT_TUNER_STEP_3;
  1816. } else {
  1817. /* MERGE for all krosus before P1G */
  1818. adc = dib0090_get_slow_adc_val(state);
  1819. dprintk("CAPTRIM=%d; ADC = %d (ADC) & %dmV", (u32) state->captrim, (u32) adc, (u32) (adc) * (u32) 1800 / (u32) 1024);
  1820. 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 !!! */
  1821. adc_target = 200;
  1822. } else
  1823. adc_target = 400;
  1824. if (adc >= adc_target) {
  1825. adc -= adc_target;
  1826. step_sign = -1;
  1827. } else {
  1828. adc = adc_target - adc;
  1829. step_sign = 1;
  1830. }
  1831. if (adc < state->adc_diff) {
  1832. dprintk("CAPTRIM=%d is closer to target (%d/%d)", (u32) state->captrim, (u32) adc, (u32) state->adc_diff);
  1833. state->adc_diff = adc;
  1834. state->fcaptrim = state->captrim;
  1835. }
  1836. state->captrim += step_sign * state->step;
  1837. if (state->step >= 1)
  1838. *tune_state = CT_TUNER_STEP_0;
  1839. else
  1840. *tune_state = CT_TUNER_STEP_2;
  1841. ret = 25;
  1842. }
  1843. } else if (*tune_state == CT_TUNER_STEP_2) { /* this step is only used by krosus < P1G */
  1844. /*write the final cptrim config */
  1845. dib0090_write_reg(state, 0x18, lo4 | state->fcaptrim);
  1846. *tune_state = CT_TUNER_STEP_3;
  1847. } else if (*tune_state == CT_TUNER_STEP_3) {
  1848. state->calibrate &= ~CAPTRIM_CAL;
  1849. *tune_state = CT_TUNER_STEP_0;
  1850. }
  1851. return ret;
  1852. }
  1853. static int dib0090_get_temperature(struct dib0090_state *state, enum frontend_tune_state *tune_state)
  1854. {
  1855. int ret = 15;
  1856. s16 val;
  1857. switch (*tune_state) {
  1858. case CT_TUNER_START:
  1859. state->wbdmux = dib0090_read_reg(state, 0x10);
  1860. dib0090_write_reg(state, 0x10, (state->wbdmux & ~(0xff << 3)) | (0x8 << 3));
  1861. state->bias = dib0090_read_reg(state, 0x13);
  1862. dib0090_write_reg(state, 0x13, state->bias | (0x3 << 8));
  1863. *tune_state = CT_TUNER_STEP_0;
  1864. /* wait for the WBDMUX to switch and for the ADC to sample */
  1865. break;
  1866. case CT_TUNER_STEP_0:
  1867. state->adc_diff = dib0090_get_slow_adc_val(state);
  1868. dib0090_write_reg(state, 0x13, (state->bias & ~(0x3 << 8)) | (0x2 << 8));
  1869. *tune_state = CT_TUNER_STEP_1;
  1870. break;
  1871. case CT_TUNER_STEP_1:
  1872. val = dib0090_get_slow_adc_val(state);
  1873. state->temperature = ((s16) ((val - state->adc_diff) * 180) >> 8) + 55;
  1874. dprintk("temperature: %d C", state->temperature - 30);
  1875. *tune_state = CT_TUNER_STEP_2;
  1876. break;
  1877. case CT_TUNER_STEP_2:
  1878. dib0090_write_reg(state, 0x13, state->bias);
  1879. dib0090_write_reg(state, 0x10, state->wbdmux); /* write back original WBDMUX */
  1880. *tune_state = CT_TUNER_START;
  1881. state->calibrate &= ~TEMP_CAL;
  1882. if (state->config->analog_output == 0)
  1883. dib0090_write_reg(state, 0x23, dib0090_read_reg(state, 0x23) | (1 << 14));
  1884. break;
  1885. default:
  1886. ret = 0;
  1887. break;
  1888. }
  1889. return ret;
  1890. }
  1891. #define WBD 0x781 /* 1 1 1 1 0000 0 0 1 */
  1892. static int dib0090_tune(struct dvb_frontend *fe)
  1893. {
  1894. struct dib0090_state *state = fe->tuner_priv;
  1895. const struct dib0090_tuning *tune = state->current_tune_table_index;
  1896. const struct dib0090_pll *pll = state->current_pll_table_index;
  1897. enum frontend_tune_state *tune_state = &state->tune_state;
  1898. u16 lo5, lo6, Den, tmp;
  1899. u32 FBDiv, Rest, FREF, VCOF_kHz = 0;
  1900. int ret = 10; /* 1ms is the default delay most of the time */
  1901. u8 c, i;
  1902. /************************* VCO ***************************/
  1903. /* Default values for FG */
  1904. /* from these are needed : */
  1905. /* Cp,HFdiv,VCOband,SD,Num,Den,FB and REFDiv */
  1906. /* in any case we first need to do a calibration if needed */
  1907. if (*tune_state == CT_TUNER_START) {
  1908. /* deactivate DataTX before some calibrations */
  1909. if (state->calibrate & (DC_CAL | TEMP_CAL | WBD_CAL))
  1910. dib0090_write_reg(state, 0x23, dib0090_read_reg(state, 0x23) & ~(1 << 14));
  1911. else
  1912. /* Activate DataTX in case a calibration has been done before */
  1913. if (state->config->analog_output == 0)
  1914. dib0090_write_reg(state, 0x23, dib0090_read_reg(state, 0x23) | (1 << 14));
  1915. }
  1916. if (state->calibrate & DC_CAL)
  1917. return dib0090_dc_offset_calibration(state, tune_state);
  1918. else if (state->calibrate & WBD_CAL) {
  1919. if (state->current_rf == 0)
  1920. state->current_rf = state->fe->dtv_property_cache.frequency / 1000;
  1921. return dib0090_wbd_calibration(state, tune_state);
  1922. } else if (state->calibrate & TEMP_CAL)
  1923. return dib0090_get_temperature(state, tune_state);
  1924. else if (state->calibrate & CAPTRIM_CAL)
  1925. return dib0090_captrim_search(state, tune_state);
  1926. if (*tune_state == CT_TUNER_START) {
  1927. /* 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 */
  1928. if (state->config->use_pwm_agc && state->identity.in_soc) {
  1929. tmp = dib0090_read_reg(state, 0x39);
  1930. if ((tmp >> 10) & 0x1)
  1931. dib0090_write_reg(state, 0x39, tmp & ~(1 << 10));
  1932. }
  1933. state->current_band = (u8) BAND_OF_FREQUENCY(state->fe->dtv_property_cache.frequency / 1000);
  1934. state->rf_request =
  1935. state->fe->dtv_property_cache.frequency / 1000 + (state->current_band ==
  1936. BAND_UHF ? state->config->freq_offset_khz_uhf : state->config->
  1937. freq_offset_khz_vhf);
  1938. /* in ISDB-T 1seg we shift tuning frequency */
  1939. if ((state->fe->dtv_property_cache.delivery_system == SYS_ISDBT && state->fe->dtv_property_cache.isdbt_sb_mode == 1
  1940. && state->fe->dtv_property_cache.isdbt_partial_reception == 0)) {
  1941. const struct dib0090_low_if_offset_table *LUT_offset = state->config->low_if;
  1942. u8 found_offset = 0;
  1943. u32 margin_khz = 100;
  1944. if (LUT_offset != NULL) {
  1945. while (LUT_offset->RF_freq != 0xffff) {
  1946. if (((state->rf_request > (LUT_offset->RF_freq - margin_khz))
  1947. && (state->rf_request < (LUT_offset->RF_freq + margin_khz)))
  1948. && LUT_offset->std == state->fe->dtv_property_cache.delivery_system) {
  1949. state->rf_request += LUT_offset->offset_khz;
  1950. found_offset = 1;
  1951. break;
  1952. }
  1953. LUT_offset++;
  1954. }
  1955. }
  1956. if (found_offset == 0)
  1957. state->rf_request += 400;
  1958. }
  1959. if (state->current_rf != state->rf_request || (state->current_standard != state->fe->dtv_property_cache.delivery_system)) {
  1960. state->tuner_is_tuned = 0;
  1961. state->current_rf = 0;
  1962. state->current_standard = 0;
  1963. tune = dib0090_tuning_table;
  1964. if (state->identity.p1g)
  1965. tune = dib0090_p1g_tuning_table;
  1966. tmp = (state->identity.version >> 5) & 0x7;
  1967. if (state->identity.in_soc) {
  1968. if (state->config->force_cband_input) { /* Use the CBAND input for all band */
  1969. if (state->current_band & BAND_CBAND || state->current_band & BAND_FM || state->current_band & BAND_VHF
  1970. || state->current_band & BAND_UHF) {
  1971. state->current_band = BAND_CBAND;
  1972. if (state->config->is_dib7090e)
  1973. tune = dib0090_tuning_table_cband_7090e_sensitivity;
  1974. else
  1975. tune = dib0090_tuning_table_cband_7090;
  1976. }
  1977. } else { /* Use the CBAND input for all band under UHF */
  1978. if (state->current_band & BAND_CBAND || state->current_band & BAND_FM || state->current_band & BAND_VHF) {
  1979. state->current_band = BAND_CBAND;
  1980. if (state->config->is_dib7090e)
  1981. tune = dib0090_tuning_table_cband_7090e_sensitivity;
  1982. else
  1983. tune = dib0090_tuning_table_cband_7090;
  1984. }
  1985. }
  1986. } else
  1987. if (tmp == 0x4 || tmp == 0x7) {
  1988. /* CBAND tuner version for VHF */
  1989. if (state->current_band == BAND_FM || state->current_band == BAND_CBAND || state->current_band == BAND_VHF) {
  1990. state->current_band = BAND_CBAND; /* Force CBAND */
  1991. tune = dib0090_tuning_table_fm_vhf_on_cband;
  1992. if (state->identity.p1g)
  1993. tune = dib0090_p1g_tuning_table_fm_vhf_on_cband;
  1994. }
  1995. }
  1996. pll = dib0090_pll_table;
  1997. if (state->identity.p1g)
  1998. pll = dib0090_p1g_pll_table;
  1999. /* Look for the interval */
  2000. while (state->rf_request > tune->max_freq)
  2001. tune++;
  2002. while (state->rf_request > pll->max_freq)
  2003. pll++;
  2004. state->current_tune_table_index = tune;
  2005. state->current_pll_table_index = pll;
  2006. dib0090_write_reg(state, 0x0b, 0xb800 | (tune->switch_trim));
  2007. VCOF_kHz = (pll->hfdiv * state->rf_request) * 2;
  2008. FREF = state->config->io.clock_khz;
  2009. if (state->config->fref_clock_ratio != 0)
  2010. FREF /= state->config->fref_clock_ratio;
  2011. FBDiv = (VCOF_kHz / pll->topresc / FREF);
  2012. Rest = (VCOF_kHz / pll->topresc) - FBDiv * FREF;
  2013. if (Rest < LPF)
  2014. Rest = 0;
  2015. else if (Rest < 2 * LPF)
  2016. Rest = 2 * LPF;
  2017. else if (Rest > (FREF - LPF)) {
  2018. Rest = 0;
  2019. FBDiv += 1;
  2020. } else if (Rest > (FREF - 2 * LPF))
  2021. Rest = FREF - 2 * LPF;
  2022. Rest = (Rest * 6528) / (FREF / 10);
  2023. state->rest = Rest;
  2024. /* external loop filter, otherwise:
  2025. * lo5 = (0 << 15) | (0 << 12) | (0 << 11) | (3 << 9) | (4 << 6) | (3 << 4) | 4;
  2026. * lo6 = 0x0e34 */
  2027. if (Rest == 0) {
  2028. if (pll->vco_band)
  2029. lo5 = 0x049f;
  2030. else
  2031. lo5 = 0x041f;
  2032. } else {
  2033. if (pll->vco_band)
  2034. lo5 = 0x049e;
  2035. else if (state->config->analog_output)
  2036. lo5 = 0x041d;
  2037. else
  2038. lo5 = 0x041c;
  2039. }
  2040. if (state->identity.p1g) { /* Bias is done automatically in P1G */
  2041. if (state->identity.in_soc) {
  2042. if (state->identity.version == SOC_8090_P1G_11R1)
  2043. lo5 = 0x46f;
  2044. else
  2045. lo5 = 0x42f;
  2046. } else
  2047. lo5 = 0x42c;
  2048. }
  2049. lo5 |= (pll->hfdiv_code << 11) | (pll->vco_band << 7); /* bit 15 is the split to the slave, we do not do it here */
  2050. if (!state->config->io.pll_int_loop_filt) {
  2051. if (state->identity.in_soc)
  2052. lo6 = 0xff98;
  2053. else if (state->identity.p1g || (Rest == 0))
  2054. lo6 = 0xfff8;
  2055. else
  2056. lo6 = 0xff28;
  2057. } else
  2058. lo6 = (state->config->io.pll_int_loop_filt << 3);
  2059. Den = 1;
  2060. if (Rest > 0) {
  2061. if (state->config->analog_output)
  2062. lo6 |= (1 << 2) | 2;
  2063. else {
  2064. if (state->identity.in_soc)
  2065. lo6 |= (1 << 2) | 2;
  2066. else
  2067. lo6 |= (1 << 2) | 2;
  2068. }
  2069. Den = 255;
  2070. }
  2071. dib0090_write_reg(state, 0x15, (u16) FBDiv);
  2072. if (state->config->fref_clock_ratio != 0)
  2073. dib0090_write_reg(state, 0x16, (Den << 8) | state->config->fref_clock_ratio);
  2074. else
  2075. dib0090_write_reg(state, 0x16, (Den << 8) | 1);
  2076. dib0090_write_reg(state, 0x17, (u16) Rest);
  2077. dib0090_write_reg(state, 0x19, lo5);
  2078. dib0090_write_reg(state, 0x1c, lo6);
  2079. lo6 = tune->tuner_enable;
  2080. if (state->config->analog_output)
  2081. lo6 = (lo6 & 0xff9f) | 0x2;
  2082. dib0090_write_reg(state, 0x24, lo6 | EN_LO | state->config->use_pwm_agc * EN_CRYSTAL);
  2083. }
  2084. state->current_rf = state->rf_request;
  2085. state->current_standard = state->fe->dtv_property_cache.delivery_system;
  2086. ret = 20;
  2087. state->calibrate = CAPTRIM_CAL; /* captrim serach now */
  2088. }
  2089. 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 */
  2090. const struct dib0090_wbd_slope *wbd = state->current_wbd_table;
  2091. while (state->current_rf / 1000 > wbd->max_freq)
  2092. wbd++;
  2093. dib0090_write_reg(state, 0x1e, 0x07ff);
  2094. dprintk("Final Captrim: %d", (u32) state->fcaptrim);
  2095. dprintk("HFDIV code: %d", (u32) pll->hfdiv_code);
  2096. dprintk("VCO = %d", (u32) pll->vco_band);
  2097. dprintk("VCOF in kHz: %d ((%d*%d) << 1))", (u32) ((pll->hfdiv * state->rf_request) * 2), (u32) pll->hfdiv, (u32) state->rf_request);
  2098. dprintk("REFDIV: %d, FREF: %d", (u32) 1, (u32) state->config->io.clock_khz);
  2099. dprintk("FBDIV: %d, Rest: %d", (u32) dib0090_read_reg(state, 0x15), (u32) dib0090_read_reg(state, 0x17));
  2100. dprintk("Num: %d, Den: %d, SD: %d", (u32) dib0090_read_reg(state, 0x17), (u32) (dib0090_read_reg(state, 0x16) >> 8),
  2101. (u32) dib0090_read_reg(state, 0x1c) & 0x3);
  2102. #define WBD 0x781 /* 1 1 1 1 0000 0 0 1 */
  2103. c = 4;
  2104. i = 3;
  2105. if (wbd->wbd_gain != 0)
  2106. c = wbd->wbd_gain;
  2107. state->wbdmux = (c << 13) | (i << 11) | (WBD | (state->config->use_pwm_agc << 1));
  2108. dib0090_write_reg(state, 0x10, state->wbdmux);
  2109. if ((tune->tuner_enable == EN_CAB) && state->identity.p1g) {
  2110. dprintk("P1G : The cable band is selected and lna_tune = %d", tune->lna_tune);
  2111. dib0090_write_reg(state, 0x09, tune->lna_bias);
  2112. dib0090_write_reg(state, 0x0b, 0xb800 | (tune->lna_tune << 6) | (tune->switch_trim));
  2113. } else
  2114. dib0090_write_reg(state, 0x09, (tune->lna_tune << 5) | tune->lna_bias);
  2115. dib0090_write_reg(state, 0x0c, tune->v2i);
  2116. dib0090_write_reg(state, 0x0d, tune->mix);
  2117. dib0090_write_reg(state, 0x0e, tune->load);
  2118. *tune_state = CT_TUNER_STEP_1;
  2119. } else if (*tune_state == CT_TUNER_STEP_1) {
  2120. /* initialize the lt gain register */
  2121. state->rf_lt_def = 0x7c00;
  2122. dib0090_set_bandwidth(state);
  2123. state->tuner_is_tuned = 1;
  2124. state->calibrate |= WBD_CAL;
  2125. state->calibrate |= TEMP_CAL;
  2126. *tune_state = CT_TUNER_STOP;
  2127. } else
  2128. ret = FE_CALLBACK_TIME_NEVER;
  2129. return ret;
  2130. }
  2131. static int dib0090_release(struct dvb_frontend *fe)
  2132. {
  2133. kfree(fe->tuner_priv);
  2134. fe->tuner_priv = NULL;
  2135. return 0;
  2136. }
  2137. enum frontend_tune_state dib0090_get_tune_state(struct dvb_frontend *fe)
  2138. {
  2139. struct dib0090_state *state = fe->tuner_priv;
  2140. return state->tune_state;
  2141. }
  2142. EXPORT_SYMBOL(dib0090_get_tune_state);
  2143. int dib0090_set_tune_state(struct dvb_frontend *fe, enum frontend_tune_state tune_state)
  2144. {
  2145. struct dib0090_state *state = fe->tuner_priv;
  2146. state->tune_state = tune_state;
  2147. return 0;
  2148. }
  2149. EXPORT_SYMBOL(dib0090_set_tune_state);
  2150. static int dib0090_get_frequency(struct dvb_frontend *fe, u32 * frequency)
  2151. {
  2152. struct dib0090_state *state = fe->tuner_priv;
  2153. *frequency = 1000 * state->current_rf;
  2154. return 0;
  2155. }
  2156. static int dib0090_set_params(struct dvb_frontend *fe)
  2157. {
  2158. struct dib0090_state *state = fe->tuner_priv;
  2159. u32 ret;
  2160. state->tune_state = CT_TUNER_START;
  2161. do {
  2162. ret = dib0090_tune(fe);
  2163. if (ret == FE_CALLBACK_TIME_NEVER)
  2164. break;
  2165. /*
  2166. * Despite dib0090_tune returns time at a 0.1 ms range,
  2167. * the actual sleep time depends on CONFIG_HZ. The worse case
  2168. * is when CONFIG_HZ=100. In such case, the minimum granularity
  2169. * is 10ms. On some real field tests, the tuner sometimes don't
  2170. * lock when this timer is lower than 10ms. So, enforce a 10ms
  2171. * granularity and use usleep_range() instead of msleep().
  2172. */
  2173. ret = 10 * (ret + 99)/100;
  2174. usleep_range(ret * 1000, (ret + 1) * 1000);
  2175. } while (state->tune_state != CT_TUNER_STOP);
  2176. return 0;
  2177. }
  2178. static const struct dvb_tuner_ops dib0090_ops = {
  2179. .info = {
  2180. .name = "DiBcom DiB0090",
  2181. .frequency_min = 45000000,
  2182. .frequency_max = 860000000,
  2183. .frequency_step = 1000,
  2184. },
  2185. .release = dib0090_release,
  2186. .init = dib0090_wakeup,
  2187. .sleep = dib0090_sleep,
  2188. .set_params = dib0090_set_params,
  2189. .get_frequency = dib0090_get_frequency,
  2190. };
  2191. static const struct dvb_tuner_ops dib0090_fw_ops = {
  2192. .info = {
  2193. .name = "DiBcom DiB0090",
  2194. .frequency_min = 45000000,
  2195. .frequency_max = 860000000,
  2196. .frequency_step = 1000,
  2197. },
  2198. .release = dib0090_release,
  2199. .init = NULL,
  2200. .sleep = NULL,
  2201. .set_params = NULL,
  2202. .get_frequency = NULL,
  2203. };
  2204. static const struct dib0090_wbd_slope dib0090_wbd_table_default[] = {
  2205. {470, 0, 250, 0, 100, 4},
  2206. {860, 51, 866, 21, 375, 4},
  2207. {1700, 0, 800, 0, 850, 4},
  2208. {2900, 0, 250, 0, 100, 6},
  2209. {0xFFFF, 0, 0, 0, 0, 0},
  2210. };
  2211. struct dvb_frontend *dib0090_register(struct dvb_frontend *fe, struct i2c_adapter *i2c, const struct dib0090_config *config)
  2212. {
  2213. struct dib0090_state *st = kzalloc(sizeof(struct dib0090_state), GFP_KERNEL);
  2214. if (st == NULL)
  2215. return NULL;
  2216. st->config = config;
  2217. st->i2c = i2c;
  2218. st->fe = fe;
  2219. mutex_init(&st->i2c_buffer_lock);
  2220. fe->tuner_priv = st;
  2221. if (config->wbd == NULL)
  2222. st->current_wbd_table = dib0090_wbd_table_default;
  2223. else
  2224. st->current_wbd_table = config->wbd;
  2225. if (dib0090_reset(fe) != 0)
  2226. goto free_mem;
  2227. printk(KERN_INFO "DiB0090: successfully identified\n");
  2228. memcpy(&fe->ops.tuner_ops, &dib0090_ops, sizeof(struct dvb_tuner_ops));
  2229. return fe;
  2230. free_mem:
  2231. kfree(st);
  2232. fe->tuner_priv = NULL;
  2233. return NULL;
  2234. }
  2235. EXPORT_SYMBOL(dib0090_register);
  2236. struct dvb_frontend *dib0090_fw_register(struct dvb_frontend *fe, struct i2c_adapter *i2c, const struct dib0090_config *config)
  2237. {
  2238. struct dib0090_fw_state *st = kzalloc(sizeof(struct dib0090_fw_state), GFP_KERNEL);
  2239. if (st == NULL)
  2240. return NULL;
  2241. st->config = config;
  2242. st->i2c = i2c;
  2243. st->fe = fe;
  2244. mutex_init(&st->i2c_buffer_lock);
  2245. fe->tuner_priv = st;
  2246. if (dib0090_fw_reset_digital(fe, st->config) != 0)
  2247. goto free_mem;
  2248. dprintk("DiB0090 FW: successfully identified");
  2249. memcpy(&fe->ops.tuner_ops, &dib0090_fw_ops, sizeof(struct dvb_tuner_ops));
  2250. return fe;
  2251. free_mem:
  2252. kfree(st);
  2253. fe->tuner_priv = NULL;
  2254. return NULL;
  2255. }
  2256. EXPORT_SYMBOL(dib0090_fw_register);
  2257. MODULE_AUTHOR("Patrick Boettcher <pboettcher@dibcom.fr>");
  2258. MODULE_AUTHOR("Olivier Grenie <olivier.grenie@dibcom.fr>");
  2259. MODULE_DESCRIPTION("Driver for the DiBcom 0090 base-band RF Tuner");
  2260. MODULE_LICENSE("GPL");