dib0090.c 77 KB

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