dib0090.c 76 KB

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