dib0090.c 76 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668
  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. fallthrough;
  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=%u,Offset=%d,Value Set=%d\n",
  1475. state->dc->addr, state->adc_diff, state->step);
  1476. state->dc++;
  1477. if (state->dc->addr == 0) /* done */
  1478. *tune_state = CT_TUNER_STEP_6;
  1479. else
  1480. *tune_state = CT_TUNER_STEP_0;
  1481. }
  1482. break;
  1483. case CT_TUNER_STEP_6:
  1484. dib0090_write_reg(state, 0x07, state->bb7 & ~0x0008);
  1485. dib0090_write_reg(state, 0x1f, 0x7);
  1486. *tune_state = CT_TUNER_START; /* reset done -> real tuning can now begin */
  1487. state->calibrate &= ~DC_CAL;
  1488. break;
  1489. default:
  1490. break;
  1491. }
  1492. return ret;
  1493. }
  1494. static int dib0090_wbd_calibration(struct dib0090_state *state, enum frontend_tune_state *tune_state)
  1495. {
  1496. u8 wbd_gain;
  1497. const struct dib0090_wbd_slope *wbd = state->current_wbd_table;
  1498. switch (*tune_state) {
  1499. case CT_TUNER_START:
  1500. while (state->current_rf / 1000 > wbd->max_freq)
  1501. wbd++;
  1502. if (wbd->wbd_gain != 0)
  1503. wbd_gain = wbd->wbd_gain;
  1504. else {
  1505. wbd_gain = 4;
  1506. #if defined(CONFIG_BAND_LBAND) || defined(CONFIG_BAND_SBAND)
  1507. if ((state->current_band == BAND_LBAND) || (state->current_band == BAND_SBAND))
  1508. wbd_gain = 2;
  1509. #endif
  1510. }
  1511. if (wbd_gain == state->wbd_calibration_gain) { /* the WBD calibration has already been done */
  1512. *tune_state = CT_TUNER_START;
  1513. state->calibrate &= ~WBD_CAL;
  1514. return 0;
  1515. }
  1516. dib0090_write_reg(state, 0x10, 0x1b81 | (1 << 10) | (wbd_gain << 13) | (1 << 3));
  1517. dib0090_write_reg(state, 0x24, ((EN_UHF & 0x0fff) | (1 << 1)));
  1518. *tune_state = CT_TUNER_STEP_0;
  1519. state->wbd_calibration_gain = wbd_gain;
  1520. return 90; /* wait for the WBDMUX to switch and for the ADC to sample */
  1521. case CT_TUNER_STEP_0:
  1522. state->wbd_offset = dib0090_get_slow_adc_val(state);
  1523. dprintk("WBD calibration offset = %d\n", state->wbd_offset);
  1524. *tune_state = CT_TUNER_START; /* reset done -> real tuning can now begin */
  1525. state->calibrate &= ~WBD_CAL;
  1526. break;
  1527. default:
  1528. break;
  1529. }
  1530. return 0;
  1531. }
  1532. static void dib0090_set_bandwidth(struct dib0090_state *state)
  1533. {
  1534. u16 tmp;
  1535. if (state->fe->dtv_property_cache.bandwidth_hz / 1000 <= 5000)
  1536. tmp = (3 << 14);
  1537. else if (state->fe->dtv_property_cache.bandwidth_hz / 1000 <= 6000)
  1538. tmp = (2 << 14);
  1539. else if (state->fe->dtv_property_cache.bandwidth_hz / 1000 <= 7000)
  1540. tmp = (1 << 14);
  1541. else
  1542. tmp = (0 << 14);
  1543. state->bb_1_def &= 0x3fff;
  1544. state->bb_1_def |= tmp;
  1545. dib0090_write_reg(state, 0x01, state->bb_1_def); /* be sure that we have the right bb-filter */
  1546. dib0090_write_reg(state, 0x03, 0x6008); /* = 0x6008 : vcm3_trim = 1 ; filter2_gm1_trim = 8 ; filter2_cutoff_freq = 0 */
  1547. dib0090_write_reg(state, 0x04, 0x1); /* 0 = 1KHz ; 1 = 50Hz ; 2 = 150Hz ; 3 = 50KHz ; 4 = servo fast */
  1548. if (state->identity.in_soc) {
  1549. 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 */
  1550. } else {
  1551. dib0090_write_reg(state, 0x02, (5 << 11) | (8 << 6) | (22 & 0x3f)); /* 22 = cap_value */
  1552. 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 */
  1553. }
  1554. }
  1555. static const struct dib0090_pll dib0090_pll_table[] = {
  1556. #ifdef CONFIG_BAND_CBAND
  1557. {56000, 0, 9, 48, 6},
  1558. {70000, 1, 9, 48, 6},
  1559. {87000, 0, 8, 32, 4},
  1560. {105000, 1, 8, 32, 4},
  1561. {115000, 0, 7, 24, 6},
  1562. {140000, 1, 7, 24, 6},
  1563. {170000, 0, 6, 16, 4},
  1564. #endif
  1565. #ifdef CONFIG_BAND_VHF
  1566. {200000, 1, 6, 16, 4},
  1567. {230000, 0, 5, 12, 6},
  1568. {280000, 1, 5, 12, 6},
  1569. {340000, 0, 4, 8, 4},
  1570. {380000, 1, 4, 8, 4},
  1571. {450000, 0, 3, 6, 6},
  1572. #endif
  1573. #ifdef CONFIG_BAND_UHF
  1574. {580000, 1, 3, 6, 6},
  1575. {700000, 0, 2, 4, 4},
  1576. {860000, 1, 2, 4, 4},
  1577. #endif
  1578. #ifdef CONFIG_BAND_LBAND
  1579. {1800000, 1, 0, 2, 4},
  1580. #endif
  1581. #ifdef CONFIG_BAND_SBAND
  1582. {2900000, 0, 14, 1, 4},
  1583. #endif
  1584. };
  1585. static const struct dib0090_tuning dib0090_tuning_table_fm_vhf_on_cband[] = {
  1586. #ifdef CONFIG_BAND_CBAND
  1587. {184000, 4, 1, 15, 0x280, 0x2912, 0xb94e, EN_CAB},
  1588. {227000, 4, 3, 15, 0x280, 0x2912, 0xb94e, EN_CAB},
  1589. {380000, 4, 7, 15, 0x280, 0x2912, 0xb94e, EN_CAB},
  1590. #endif
  1591. #ifdef CONFIG_BAND_UHF
  1592. {520000, 2, 0, 15, 0x300, 0x1d12, 0xb9ce, EN_UHF},
  1593. {550000, 2, 2, 15, 0x300, 0x1d12, 0xb9ce, EN_UHF},
  1594. {650000, 2, 3, 15, 0x300, 0x1d12, 0xb9ce, EN_UHF},
  1595. {750000, 2, 5, 15, 0x300, 0x1d12, 0xb9ce, EN_UHF},
  1596. {850000, 2, 6, 15, 0x300, 0x1d12, 0xb9ce, EN_UHF},
  1597. {900000, 2, 7, 15, 0x300, 0x1d12, 0xb9ce, EN_UHF},
  1598. #endif
  1599. #ifdef CONFIG_BAND_LBAND
  1600. {1500000, 4, 0, 20, 0x300, 0x1912, 0x82c9, EN_LBD},
  1601. {1600000, 4, 1, 20, 0x300, 0x1912, 0x82c9, EN_LBD},
  1602. {1800000, 4, 3, 20, 0x300, 0x1912, 0x82c9, EN_LBD},
  1603. #endif
  1604. #ifdef CONFIG_BAND_SBAND
  1605. {2300000, 1, 4, 20, 0x300, 0x2d2A, 0x82c7, EN_SBD},
  1606. {2900000, 1, 7, 20, 0x280, 0x2deb, 0x8347, EN_SBD},
  1607. #endif
  1608. };
  1609. static const struct dib0090_tuning dib0090_tuning_table[] = {
  1610. #ifdef CONFIG_BAND_CBAND
  1611. {170000, 4, 1, 15, 0x280, 0x2912, 0xb94e, EN_CAB},
  1612. #endif
  1613. #ifdef CONFIG_BAND_VHF
  1614. {184000, 1, 1, 15, 0x300, 0x4d12, 0xb94e, EN_VHF},
  1615. {227000, 1, 3, 15, 0x300, 0x4d12, 0xb94e, EN_VHF},
  1616. {380000, 1, 7, 15, 0x300, 0x4d12, 0xb94e, EN_VHF},
  1617. #endif
  1618. #ifdef CONFIG_BAND_UHF
  1619. {520000, 2, 0, 15, 0x300, 0x1d12, 0xb9ce, EN_UHF},
  1620. {550000, 2, 2, 15, 0x300, 0x1d12, 0xb9ce, EN_UHF},
  1621. {650000, 2, 3, 15, 0x300, 0x1d12, 0xb9ce, EN_UHF},
  1622. {750000, 2, 5, 15, 0x300, 0x1d12, 0xb9ce, EN_UHF},
  1623. {850000, 2, 6, 15, 0x300, 0x1d12, 0xb9ce, EN_UHF},
  1624. {900000, 2, 7, 15, 0x300, 0x1d12, 0xb9ce, EN_UHF},
  1625. #endif
  1626. #ifdef CONFIG_BAND_LBAND
  1627. {1500000, 4, 0, 20, 0x300, 0x1912, 0x82c9, EN_LBD},
  1628. {1600000, 4, 1, 20, 0x300, 0x1912, 0x82c9, EN_LBD},
  1629. {1800000, 4, 3, 20, 0x300, 0x1912, 0x82c9, EN_LBD},
  1630. #endif
  1631. #ifdef CONFIG_BAND_SBAND
  1632. {2300000, 1, 4, 20, 0x300, 0x2d2A, 0x82c7, EN_SBD},
  1633. {2900000, 1, 7, 20, 0x280, 0x2deb, 0x8347, EN_SBD},
  1634. #endif
  1635. };
  1636. static const struct dib0090_tuning dib0090_p1g_tuning_table[] = {
  1637. #ifdef CONFIG_BAND_CBAND
  1638. {170000, 4, 1, 0x820f, 0x300, 0x2d22, 0x82cb, EN_CAB},
  1639. #endif
  1640. #ifdef CONFIG_BAND_VHF
  1641. {184000, 1, 1, 15, 0x300, 0x4d12, 0xb94e, EN_VHF},
  1642. {227000, 1, 3, 15, 0x300, 0x4d12, 0xb94e, EN_VHF},
  1643. {380000, 1, 7, 15, 0x300, 0x4d12, 0xb94e, EN_VHF},
  1644. #endif
  1645. #ifdef CONFIG_BAND_UHF
  1646. {510000, 2, 0, 15, 0x300, 0x1d12, 0xb9ce, EN_UHF},
  1647. {540000, 2, 1, 15, 0x300, 0x1d12, 0xb9ce, EN_UHF},
  1648. {600000, 2, 3, 15, 0x300, 0x1d12, 0xb9ce, EN_UHF},
  1649. {630000, 2, 4, 15, 0x300, 0x1d12, 0xb9ce, EN_UHF},
  1650. {680000, 2, 5, 15, 0x300, 0x1d12, 0xb9ce, EN_UHF},
  1651. {720000, 2, 6, 15, 0x300, 0x1d12, 0xb9ce, EN_UHF},
  1652. {900000, 2, 7, 15, 0x300, 0x1d12, 0xb9ce, EN_UHF},
  1653. #endif
  1654. #ifdef CONFIG_BAND_LBAND
  1655. {1500000, 4, 0, 20, 0x300, 0x1912, 0x82c9, EN_LBD},
  1656. {1600000, 4, 1, 20, 0x300, 0x1912, 0x82c9, EN_LBD},
  1657. {1800000, 4, 3, 20, 0x300, 0x1912, 0x82c9, EN_LBD},
  1658. #endif
  1659. #ifdef CONFIG_BAND_SBAND
  1660. {2300000, 1, 4, 20, 0x300, 0x2d2A, 0x82c7, EN_SBD},
  1661. {2900000, 1, 7, 20, 0x280, 0x2deb, 0x8347, EN_SBD},
  1662. #endif
  1663. };
  1664. static const struct dib0090_pll dib0090_p1g_pll_table[] = {
  1665. #ifdef CONFIG_BAND_CBAND
  1666. {57000, 0, 11, 48, 6},
  1667. {70000, 1, 11, 48, 6},
  1668. {86000, 0, 10, 32, 4},
  1669. {105000, 1, 10, 32, 4},
  1670. {115000, 0, 9, 24, 6},
  1671. {140000, 1, 9, 24, 6},
  1672. {170000, 0, 8, 16, 4},
  1673. #endif
  1674. #ifdef CONFIG_BAND_VHF
  1675. {200000, 1, 8, 16, 4},
  1676. {230000, 0, 7, 12, 6},
  1677. {280000, 1, 7, 12, 6},
  1678. {340000, 0, 6, 8, 4},
  1679. {380000, 1, 6, 8, 4},
  1680. {455000, 0, 5, 6, 6},
  1681. #endif
  1682. #ifdef CONFIG_BAND_UHF
  1683. {580000, 1, 5, 6, 6},
  1684. {680000, 0, 4, 4, 4},
  1685. {860000, 1, 4, 4, 4},
  1686. #endif
  1687. #ifdef CONFIG_BAND_LBAND
  1688. {1800000, 1, 2, 2, 4},
  1689. #endif
  1690. #ifdef CONFIG_BAND_SBAND
  1691. {2900000, 0, 1, 1, 6},
  1692. #endif
  1693. };
  1694. static const struct dib0090_tuning dib0090_p1g_tuning_table_fm_vhf_on_cband[] = {
  1695. #ifdef CONFIG_BAND_CBAND
  1696. {184000, 4, 3, 0x4187, 0x2c0, 0x2d22, 0x81cb, EN_CAB},
  1697. {227000, 4, 3, 0x4187, 0x2c0, 0x2d22, 0x81cb, EN_CAB},
  1698. {380000, 4, 3, 0x4187, 0x2c0, 0x2d22, 0x81cb, EN_CAB},
  1699. #endif
  1700. #ifdef CONFIG_BAND_UHF
  1701. {520000, 2, 0, 15, 0x300, 0x1d12, 0xb9ce, EN_UHF},
  1702. {550000, 2, 2, 15, 0x300, 0x1d12, 0xb9ce, EN_UHF},
  1703. {650000, 2, 3, 15, 0x300, 0x1d12, 0xb9ce, EN_UHF},
  1704. {750000, 2, 5, 15, 0x300, 0x1d12, 0xb9ce, EN_UHF},
  1705. {850000, 2, 6, 15, 0x300, 0x1d12, 0xb9ce, EN_UHF},
  1706. {900000, 2, 7, 15, 0x300, 0x1d12, 0xb9ce, EN_UHF},
  1707. #endif
  1708. #ifdef CONFIG_BAND_LBAND
  1709. {1500000, 4, 0, 20, 0x300, 0x1912, 0x82c9, EN_LBD},
  1710. {1600000, 4, 1, 20, 0x300, 0x1912, 0x82c9, EN_LBD},
  1711. {1800000, 4, 3, 20, 0x300, 0x1912, 0x82c9, EN_LBD},
  1712. #endif
  1713. #ifdef CONFIG_BAND_SBAND
  1714. {2300000, 1, 4, 20, 0x300, 0x2d2A, 0x82c7, EN_SBD},
  1715. {2900000, 1, 7, 20, 0x280, 0x2deb, 0x8347, EN_SBD},
  1716. #endif
  1717. };
  1718. static const struct dib0090_tuning dib0090_tuning_table_cband_7090[] = {
  1719. #ifdef CONFIG_BAND_CBAND
  1720. {300000, 4, 3, 0x018F, 0x2c0, 0x2d22, 0xb9ce, EN_CAB},
  1721. {380000, 4, 10, 0x018F, 0x2c0, 0x2d22, 0xb9ce, EN_CAB},
  1722. {570000, 4, 10, 0x8190, 0x2c0, 0x2d22, 0xb9ce, EN_CAB},
  1723. {858000, 4, 5, 0x8190, 0x2c0, 0x2d22, 0xb9ce, EN_CAB},
  1724. #endif
  1725. };
  1726. static const struct dib0090_tuning dib0090_tuning_table_cband_7090e_sensitivity[] = {
  1727. #ifdef CONFIG_BAND_CBAND
  1728. { 300000, 0 , 3, 0x8105, 0x2c0, 0x2d12, 0xb84e, EN_CAB },
  1729. { 380000, 0 , 10, 0x810F, 0x2c0, 0x2d12, 0xb84e, EN_CAB },
  1730. { 600000, 0 , 10, 0x815E, 0x280, 0x2d12, 0xb84e, EN_CAB },
  1731. { 660000, 0 , 5, 0x85E3, 0x280, 0x2d12, 0xb84e, EN_CAB },
  1732. { 720000, 0 , 5, 0x852E, 0x280, 0x2d12, 0xb84e, EN_CAB },
  1733. { 860000, 0 , 4, 0x85E5, 0x280, 0x2d12, 0xb84e, EN_CAB },
  1734. #endif
  1735. };
  1736. int dib0090_update_tuning_table_7090(struct dvb_frontend *fe,
  1737. u8 cfg_sensitivity)
  1738. {
  1739. struct dib0090_state *state = fe->tuner_priv;
  1740. const struct dib0090_tuning *tune =
  1741. dib0090_tuning_table_cband_7090e_sensitivity;
  1742. static const struct dib0090_tuning dib0090_tuning_table_cband_7090e_aci[] = {
  1743. { 300000, 0 , 3, 0x8165, 0x2c0, 0x2d12, 0xb84e, EN_CAB },
  1744. { 650000, 0 , 4, 0x815B, 0x280, 0x2d12, 0xb84e, EN_CAB },
  1745. { 860000, 0 , 5, 0x84EF, 0x280, 0x2d12, 0xb84e, EN_CAB },
  1746. };
  1747. if ((!state->identity.p1g) || (!state->identity.in_soc)
  1748. || ((state->identity.version != SOC_7090_P1G_21R1)
  1749. && (state->identity.version != SOC_7090_P1G_11R1))) {
  1750. dprintk("%s() function can only be used for dib7090\n", __func__);
  1751. return -ENODEV;
  1752. }
  1753. if (cfg_sensitivity)
  1754. tune = dib0090_tuning_table_cband_7090e_sensitivity;
  1755. else
  1756. tune = dib0090_tuning_table_cband_7090e_aci;
  1757. while (state->rf_request > tune->max_freq)
  1758. tune++;
  1759. dib0090_write_reg(state, 0x09, (dib0090_read_reg(state, 0x09) & 0x8000)
  1760. | (tune->lna_bias & 0x7fff));
  1761. dib0090_write_reg(state, 0x0b, (dib0090_read_reg(state, 0x0b) & 0xf83f)
  1762. | ((tune->lna_tune << 6) & 0x07c0));
  1763. return 0;
  1764. }
  1765. EXPORT_SYMBOL(dib0090_update_tuning_table_7090);
  1766. static int dib0090_captrim_search(struct dib0090_state *state, enum frontend_tune_state *tune_state)
  1767. {
  1768. int ret = 0;
  1769. u16 lo4 = 0xe900;
  1770. s16 adc_target;
  1771. u16 adc;
  1772. s8 step_sign;
  1773. u8 force_soft_search = 0;
  1774. if (state->identity.version == SOC_8090_P1G_11R1 || state->identity.version == SOC_8090_P1G_21R1)
  1775. force_soft_search = 1;
  1776. if (*tune_state == CT_TUNER_START) {
  1777. dprintk("Start Captrim search : %s\n",
  1778. (force_soft_search == 1) ? "FORCE SOFT SEARCH" : "AUTO");
  1779. dib0090_write_reg(state, 0x10, 0x2B1);
  1780. dib0090_write_reg(state, 0x1e, 0x0032);
  1781. if (!state->tuner_is_tuned) {
  1782. /* prepare a complete captrim */
  1783. if (!state->identity.p1g || force_soft_search)
  1784. state->step = state->captrim = state->fcaptrim = 64;
  1785. state->current_rf = state->rf_request;
  1786. } else { /* we are already tuned to this frequency - the configuration is correct */
  1787. if (!state->identity.p1g || force_soft_search) {
  1788. /* do a minimal captrim even if the frequency has not changed */
  1789. state->step = 4;
  1790. state->captrim = state->fcaptrim = dib0090_read_reg(state, 0x18) & 0x7f;
  1791. }
  1792. }
  1793. state->adc_diff = 3000;
  1794. *tune_state = CT_TUNER_STEP_0;
  1795. } else if (*tune_state == CT_TUNER_STEP_0) {
  1796. if (state->identity.p1g && !force_soft_search) {
  1797. u8 ratio = 31;
  1798. dib0090_write_reg(state, 0x40, (3 << 7) | (ratio << 2) | (1 << 1) | 1);
  1799. dib0090_read_reg(state, 0x40);
  1800. ret = 50;
  1801. } else {
  1802. state->step /= 2;
  1803. dib0090_write_reg(state, 0x18, lo4 | state->captrim);
  1804. if (state->identity.in_soc)
  1805. ret = 25;
  1806. }
  1807. *tune_state = CT_TUNER_STEP_1;
  1808. } else if (*tune_state == CT_TUNER_STEP_1) {
  1809. if (state->identity.p1g && !force_soft_search) {
  1810. dib0090_write_reg(state, 0x40, 0x18c | (0 << 1) | 0);
  1811. dib0090_read_reg(state, 0x40);
  1812. state->fcaptrim = dib0090_read_reg(state, 0x18) & 0x7F;
  1813. dprintk("***Final Captrim= 0x%x\n", state->fcaptrim);
  1814. *tune_state = CT_TUNER_STEP_3;
  1815. } else {
  1816. /* MERGE for all krosus before P1G */
  1817. adc = dib0090_get_slow_adc_val(state);
  1818. dprintk("CAPTRIM=%d; ADC = %d (ADC) & %dmV\n", (u32) state->captrim, (u32) adc, (u32) (adc) * (u32) 1800 / (u32) 1024);
  1819. 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 !!! */
  1820. adc_target = 200;
  1821. } else
  1822. adc_target = 400;
  1823. if (adc >= adc_target) {
  1824. adc -= adc_target;
  1825. step_sign = -1;
  1826. } else {
  1827. adc = adc_target - adc;
  1828. step_sign = 1;
  1829. }
  1830. if (adc < state->adc_diff) {
  1831. dprintk("CAPTRIM=%d is closer to target (%d/%d)\n", (u32) state->captrim, (u32) adc, (u32) state->adc_diff);
  1832. state->adc_diff = adc;
  1833. state->fcaptrim = state->captrim;
  1834. }
  1835. state->captrim += step_sign * state->step;
  1836. if (state->step >= 1)
  1837. *tune_state = CT_TUNER_STEP_0;
  1838. else
  1839. *tune_state = CT_TUNER_STEP_2;
  1840. ret = 25;
  1841. }
  1842. } else if (*tune_state == CT_TUNER_STEP_2) { /* this step is only used by krosus < P1G */
  1843. /*write the final cptrim config */
  1844. dib0090_write_reg(state, 0x18, lo4 | state->fcaptrim);
  1845. *tune_state = CT_TUNER_STEP_3;
  1846. } else if (*tune_state == CT_TUNER_STEP_3) {
  1847. state->calibrate &= ~CAPTRIM_CAL;
  1848. *tune_state = CT_TUNER_STEP_0;
  1849. }
  1850. return ret;
  1851. }
  1852. static int dib0090_get_temperature(struct dib0090_state *state, enum frontend_tune_state *tune_state)
  1853. {
  1854. int ret = 15;
  1855. s16 val;
  1856. switch (*tune_state) {
  1857. case CT_TUNER_START:
  1858. state->wbdmux = dib0090_read_reg(state, 0x10);
  1859. dib0090_write_reg(state, 0x10, (state->wbdmux & ~(0xff << 3)) | (0x8 << 3));
  1860. state->bias = dib0090_read_reg(state, 0x13);
  1861. dib0090_write_reg(state, 0x13, state->bias | (0x3 << 8));
  1862. *tune_state = CT_TUNER_STEP_0;
  1863. /* wait for the WBDMUX to switch and for the ADC to sample */
  1864. break;
  1865. case CT_TUNER_STEP_0:
  1866. state->adc_diff = dib0090_get_slow_adc_val(state);
  1867. dib0090_write_reg(state, 0x13, (state->bias & ~(0x3 << 8)) | (0x2 << 8));
  1868. *tune_state = CT_TUNER_STEP_1;
  1869. break;
  1870. case CT_TUNER_STEP_1:
  1871. val = dib0090_get_slow_adc_val(state);
  1872. state->temperature = ((s16) ((val - state->adc_diff) * 180) >> 8) + 55;
  1873. dprintk("temperature: %d C\n", state->temperature - 30);
  1874. *tune_state = CT_TUNER_STEP_2;
  1875. break;
  1876. case CT_TUNER_STEP_2:
  1877. dib0090_write_reg(state, 0x13, state->bias);
  1878. dib0090_write_reg(state, 0x10, state->wbdmux); /* write back original WBDMUX */
  1879. *tune_state = CT_TUNER_START;
  1880. state->calibrate &= ~TEMP_CAL;
  1881. if (state->config->analog_output == 0)
  1882. dib0090_write_reg(state, 0x23, dib0090_read_reg(state, 0x23) | (1 << 14));
  1883. break;
  1884. default:
  1885. ret = 0;
  1886. break;
  1887. }
  1888. return ret;
  1889. }
  1890. #define WBD 0x781 /* 1 1 1 1 0000 0 0 1 */
  1891. static int dib0090_tune(struct dvb_frontend *fe)
  1892. {
  1893. struct dib0090_state *state = fe->tuner_priv;
  1894. const struct dib0090_tuning *tune = state->current_tune_table_index;
  1895. const struct dib0090_pll *pll = state->current_pll_table_index;
  1896. enum frontend_tune_state *tune_state = &state->tune_state;
  1897. u16 lo5, lo6, Den, tmp;
  1898. u32 FBDiv, Rest, FREF, VCOF_kHz = 0;
  1899. int ret = 10; /* 1ms is the default delay most of the time */
  1900. u8 c, i;
  1901. /************************* VCO ***************************/
  1902. /* Default values for FG */
  1903. /* from these are needed : */
  1904. /* Cp,HFdiv,VCOband,SD,Num,Den,FB and REFDiv */
  1905. /* in any case we first need to do a calibration if needed */
  1906. if (*tune_state == CT_TUNER_START) {
  1907. /* deactivate DataTX before some calibrations */
  1908. if (state->calibrate & (DC_CAL | TEMP_CAL | WBD_CAL))
  1909. dib0090_write_reg(state, 0x23, dib0090_read_reg(state, 0x23) & ~(1 << 14));
  1910. else
  1911. /* Activate DataTX in case a calibration has been done before */
  1912. if (state->config->analog_output == 0)
  1913. dib0090_write_reg(state, 0x23, dib0090_read_reg(state, 0x23) | (1 << 14));
  1914. }
  1915. if (state->calibrate & DC_CAL)
  1916. return dib0090_dc_offset_calibration(state, tune_state);
  1917. else if (state->calibrate & WBD_CAL) {
  1918. if (state->current_rf == 0)
  1919. state->current_rf = state->fe->dtv_property_cache.frequency / 1000;
  1920. return dib0090_wbd_calibration(state, tune_state);
  1921. } else if (state->calibrate & TEMP_CAL)
  1922. return dib0090_get_temperature(state, tune_state);
  1923. else if (state->calibrate & CAPTRIM_CAL)
  1924. return dib0090_captrim_search(state, tune_state);
  1925. if (*tune_state == CT_TUNER_START) {
  1926. /* 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 */
  1927. if (state->config->use_pwm_agc && state->identity.in_soc) {
  1928. tmp = dib0090_read_reg(state, 0x39);
  1929. if ((tmp >> 10) & 0x1)
  1930. dib0090_write_reg(state, 0x39, tmp & ~(1 << 10));
  1931. }
  1932. state->current_band = (u8) BAND_OF_FREQUENCY(state->fe->dtv_property_cache.frequency / 1000);
  1933. state->rf_request =
  1934. state->fe->dtv_property_cache.frequency / 1000 + (state->current_band ==
  1935. BAND_UHF ? state->config->freq_offset_khz_uhf : state->config->
  1936. freq_offset_khz_vhf);
  1937. /* in ISDB-T 1seg we shift tuning frequency */
  1938. if ((state->fe->dtv_property_cache.delivery_system == SYS_ISDBT && state->fe->dtv_property_cache.isdbt_sb_mode == 1
  1939. && state->fe->dtv_property_cache.isdbt_partial_reception == 0)) {
  1940. const struct dib0090_low_if_offset_table *LUT_offset = state->config->low_if;
  1941. u8 found_offset = 0;
  1942. u32 margin_khz = 100;
  1943. if (LUT_offset != NULL) {
  1944. while (LUT_offset->RF_freq != 0xffff) {
  1945. if (((state->rf_request > (LUT_offset->RF_freq - margin_khz))
  1946. && (state->rf_request < (LUT_offset->RF_freq + margin_khz)))
  1947. && LUT_offset->std == state->fe->dtv_property_cache.delivery_system) {
  1948. state->rf_request += LUT_offset->offset_khz;
  1949. found_offset = 1;
  1950. break;
  1951. }
  1952. LUT_offset++;
  1953. }
  1954. }
  1955. if (found_offset == 0)
  1956. state->rf_request += 400;
  1957. }
  1958. if (state->current_rf != state->rf_request || (state->current_standard != state->fe->dtv_property_cache.delivery_system)) {
  1959. state->tuner_is_tuned = 0;
  1960. state->current_rf = 0;
  1961. state->current_standard = 0;
  1962. tune = dib0090_tuning_table;
  1963. if (state->identity.p1g)
  1964. tune = dib0090_p1g_tuning_table;
  1965. tmp = (state->identity.version >> 5) & 0x7;
  1966. if (state->identity.in_soc) {
  1967. if (state->config->force_cband_input) { /* Use the CBAND input for all band */
  1968. if (state->current_band & BAND_CBAND || state->current_band & BAND_FM || state->current_band & BAND_VHF
  1969. || state->current_band & BAND_UHF) {
  1970. state->current_band = BAND_CBAND;
  1971. if (state->config->is_dib7090e)
  1972. tune = dib0090_tuning_table_cband_7090e_sensitivity;
  1973. else
  1974. tune = dib0090_tuning_table_cband_7090;
  1975. }
  1976. } else { /* Use the CBAND input for all band under UHF */
  1977. if (state->current_band & BAND_CBAND || state->current_band & BAND_FM || state->current_band & BAND_VHF) {
  1978. state->current_band = BAND_CBAND;
  1979. if (state->config->is_dib7090e)
  1980. tune = dib0090_tuning_table_cband_7090e_sensitivity;
  1981. else
  1982. tune = dib0090_tuning_table_cband_7090;
  1983. }
  1984. }
  1985. } else
  1986. if (tmp == 0x4 || tmp == 0x7) {
  1987. /* CBAND tuner version for VHF */
  1988. if (state->current_band == BAND_FM || state->current_band == BAND_CBAND || state->current_band == BAND_VHF) {
  1989. state->current_band = BAND_CBAND; /* Force CBAND */
  1990. tune = dib0090_tuning_table_fm_vhf_on_cband;
  1991. if (state->identity.p1g)
  1992. tune = dib0090_p1g_tuning_table_fm_vhf_on_cband;
  1993. }
  1994. }
  1995. pll = dib0090_pll_table;
  1996. if (state->identity.p1g)
  1997. pll = dib0090_p1g_pll_table;
  1998. /* Look for the interval */
  1999. while (state->rf_request > tune->max_freq)
  2000. tune++;
  2001. while (state->rf_request > pll->max_freq)
  2002. pll++;
  2003. state->current_tune_table_index = tune;
  2004. state->current_pll_table_index = pll;
  2005. dib0090_write_reg(state, 0x0b, 0xb800 | (tune->switch_trim));
  2006. VCOF_kHz = (pll->hfdiv * state->rf_request) * 2;
  2007. FREF = state->config->io.clock_khz;
  2008. if (state->config->fref_clock_ratio != 0)
  2009. FREF /= state->config->fref_clock_ratio;
  2010. FBDiv = (VCOF_kHz / pll->topresc / FREF);
  2011. Rest = (VCOF_kHz / pll->topresc) - FBDiv * FREF;
  2012. if (Rest < LPF)
  2013. Rest = 0;
  2014. else if (Rest < 2 * LPF)
  2015. Rest = 2 * LPF;
  2016. else if (Rest > (FREF - LPF)) {
  2017. Rest = 0;
  2018. FBDiv += 1;
  2019. } else if (Rest > (FREF - 2 * LPF))
  2020. Rest = FREF - 2 * LPF;
  2021. Rest = (Rest * 6528) / (FREF / 10);
  2022. state->rest = Rest;
  2023. /* external loop filter, otherwise:
  2024. * lo5 = (0 << 15) | (0 << 12) | (0 << 11) | (3 << 9) | (4 << 6) | (3 << 4) | 4;
  2025. * lo6 = 0x0e34 */
  2026. if (Rest == 0) {
  2027. if (pll->vco_band)
  2028. lo5 = 0x049f;
  2029. else
  2030. lo5 = 0x041f;
  2031. } else {
  2032. if (pll->vco_band)
  2033. lo5 = 0x049e;
  2034. else if (state->config->analog_output)
  2035. lo5 = 0x041d;
  2036. else
  2037. lo5 = 0x041c;
  2038. }
  2039. if (state->identity.p1g) { /* Bias is done automatically in P1G */
  2040. if (state->identity.in_soc) {
  2041. if (state->identity.version == SOC_8090_P1G_11R1)
  2042. lo5 = 0x46f;
  2043. else
  2044. lo5 = 0x42f;
  2045. } else
  2046. lo5 = 0x42c;
  2047. }
  2048. lo5 |= (pll->hfdiv_code << 11) | (pll->vco_band << 7); /* bit 15 is the split to the slave, we do not do it here */
  2049. if (!state->config->io.pll_int_loop_filt) {
  2050. if (state->identity.in_soc)
  2051. lo6 = 0xff98;
  2052. else if (state->identity.p1g || (Rest == 0))
  2053. lo6 = 0xfff8;
  2054. else
  2055. lo6 = 0xff28;
  2056. } else
  2057. lo6 = (state->config->io.pll_int_loop_filt << 3);
  2058. Den = 1;
  2059. if (Rest > 0) {
  2060. lo6 |= (1 << 2) | 2;
  2061. Den = 255;
  2062. }
  2063. dib0090_write_reg(state, 0x15, (u16) FBDiv);
  2064. if (state->config->fref_clock_ratio != 0)
  2065. dib0090_write_reg(state, 0x16, (Den << 8) | state->config->fref_clock_ratio);
  2066. else
  2067. dib0090_write_reg(state, 0x16, (Den << 8) | 1);
  2068. dib0090_write_reg(state, 0x17, (u16) Rest);
  2069. dib0090_write_reg(state, 0x19, lo5);
  2070. dib0090_write_reg(state, 0x1c, lo6);
  2071. lo6 = tune->tuner_enable;
  2072. if (state->config->analog_output)
  2073. lo6 = (lo6 & 0xff9f) | 0x2;
  2074. dib0090_write_reg(state, 0x24, lo6 | EN_LO | state->config->use_pwm_agc * EN_CRYSTAL);
  2075. }
  2076. state->current_rf = state->rf_request;
  2077. state->current_standard = state->fe->dtv_property_cache.delivery_system;
  2078. ret = 20;
  2079. state->calibrate = CAPTRIM_CAL; /* captrim search now */
  2080. }
  2081. 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 */
  2082. const struct dib0090_wbd_slope *wbd = state->current_wbd_table;
  2083. while (state->current_rf / 1000 > wbd->max_freq)
  2084. wbd++;
  2085. dib0090_write_reg(state, 0x1e, 0x07ff);
  2086. dprintk("Final Captrim: %d\n", (u32) state->fcaptrim);
  2087. dprintk("HFDIV code: %d\n", (u32) pll->hfdiv_code);
  2088. dprintk("VCO = %d\n", (u32) pll->vco_band);
  2089. dprintk("VCOF in kHz: %d ((%d*%d) << 1))\n", (u32) ((pll->hfdiv * state->rf_request) * 2), (u32) pll->hfdiv, (u32) state->rf_request);
  2090. dprintk("REFDIV: %d, FREF: %d\n", (u32) 1, (u32) state->config->io.clock_khz);
  2091. dprintk("FBDIV: %d, Rest: %d\n", (u32) dib0090_read_reg(state, 0x15), (u32) dib0090_read_reg(state, 0x17));
  2092. dprintk("Num: %d, Den: %d, SD: %d\n", (u32) dib0090_read_reg(state, 0x17), (u32) (dib0090_read_reg(state, 0x16) >> 8),
  2093. (u32) dib0090_read_reg(state, 0x1c) & 0x3);
  2094. #define WBD 0x781 /* 1 1 1 1 0000 0 0 1 */
  2095. c = 4;
  2096. i = 3;
  2097. if (wbd->wbd_gain != 0)
  2098. c = wbd->wbd_gain;
  2099. state->wbdmux = (c << 13) | (i << 11) | (WBD | (state->config->use_pwm_agc << 1));
  2100. dib0090_write_reg(state, 0x10, state->wbdmux);
  2101. if ((tune->tuner_enable == EN_CAB) && state->identity.p1g) {
  2102. dprintk("P1G : The cable band is selected and lna_tune = %d\n", tune->lna_tune);
  2103. dib0090_write_reg(state, 0x09, tune->lna_bias);
  2104. dib0090_write_reg(state, 0x0b, 0xb800 | (tune->lna_tune << 6) | (tune->switch_trim));
  2105. } else
  2106. dib0090_write_reg(state, 0x09, (tune->lna_tune << 5) | tune->lna_bias);
  2107. dib0090_write_reg(state, 0x0c, tune->v2i);
  2108. dib0090_write_reg(state, 0x0d, tune->mix);
  2109. dib0090_write_reg(state, 0x0e, tune->load);
  2110. *tune_state = CT_TUNER_STEP_1;
  2111. } else if (*tune_state == CT_TUNER_STEP_1) {
  2112. /* initialize the lt gain register */
  2113. state->rf_lt_def = 0x7c00;
  2114. dib0090_set_bandwidth(state);
  2115. state->tuner_is_tuned = 1;
  2116. state->calibrate |= WBD_CAL;
  2117. state->calibrate |= TEMP_CAL;
  2118. *tune_state = CT_TUNER_STOP;
  2119. } else
  2120. ret = FE_CALLBACK_TIME_NEVER;
  2121. return ret;
  2122. }
  2123. static void dib0090_release(struct dvb_frontend *fe)
  2124. {
  2125. kfree(fe->tuner_priv);
  2126. fe->tuner_priv = NULL;
  2127. }
  2128. enum frontend_tune_state dib0090_get_tune_state(struct dvb_frontend *fe)
  2129. {
  2130. struct dib0090_state *state = fe->tuner_priv;
  2131. return state->tune_state;
  2132. }
  2133. EXPORT_SYMBOL(dib0090_get_tune_state);
  2134. int dib0090_set_tune_state(struct dvb_frontend *fe, enum frontend_tune_state tune_state)
  2135. {
  2136. struct dib0090_state *state = fe->tuner_priv;
  2137. state->tune_state = tune_state;
  2138. return 0;
  2139. }
  2140. EXPORT_SYMBOL(dib0090_set_tune_state);
  2141. static int dib0090_get_frequency(struct dvb_frontend *fe, u32 * frequency)
  2142. {
  2143. struct dib0090_state *state = fe->tuner_priv;
  2144. *frequency = 1000 * state->current_rf;
  2145. return 0;
  2146. }
  2147. static int dib0090_set_params(struct dvb_frontend *fe)
  2148. {
  2149. struct dib0090_state *state = fe->tuner_priv;
  2150. u32 ret;
  2151. state->tune_state = CT_TUNER_START;
  2152. do {
  2153. ret = dib0090_tune(fe);
  2154. if (ret == FE_CALLBACK_TIME_NEVER)
  2155. break;
  2156. /*
  2157. * Despite dib0090_tune returns time at a 0.1 ms range,
  2158. * the actual sleep time depends on CONFIG_HZ. The worse case
  2159. * is when CONFIG_HZ=100. In such case, the minimum granularity
  2160. * is 10ms. On some real field tests, the tuner sometimes don't
  2161. * lock when this timer is lower than 10ms. So, enforce a 10ms
  2162. * granularity and use usleep_range() instead of msleep().
  2163. */
  2164. ret = 10 * (ret + 99)/100;
  2165. usleep_range(ret * 1000, (ret + 1) * 1000);
  2166. } while (state->tune_state != CT_TUNER_STOP);
  2167. return 0;
  2168. }
  2169. static const struct dvb_tuner_ops dib0090_ops = {
  2170. .info = {
  2171. .name = "DiBcom DiB0090",
  2172. .frequency_min_hz = 45 * MHz,
  2173. .frequency_max_hz = 860 * MHz,
  2174. .frequency_step_hz = 1 * kHz,
  2175. },
  2176. .release = dib0090_release,
  2177. .init = dib0090_wakeup,
  2178. .sleep = dib0090_sleep,
  2179. .set_params = dib0090_set_params,
  2180. .get_frequency = dib0090_get_frequency,
  2181. };
  2182. static const struct dvb_tuner_ops dib0090_fw_ops = {
  2183. .info = {
  2184. .name = "DiBcom DiB0090",
  2185. .frequency_min_hz = 45 * MHz,
  2186. .frequency_max_hz = 860 * MHz,
  2187. .frequency_step_hz = 1 * kHz,
  2188. },
  2189. .release = dib0090_release,
  2190. .init = NULL,
  2191. .sleep = NULL,
  2192. .set_params = NULL,
  2193. .get_frequency = NULL,
  2194. };
  2195. static const struct dib0090_wbd_slope dib0090_wbd_table_default[] = {
  2196. {470, 0, 250, 0, 100, 4},
  2197. {860, 51, 866, 21, 375, 4},
  2198. {1700, 0, 800, 0, 850, 4},
  2199. {2900, 0, 250, 0, 100, 6},
  2200. {0xFFFF, 0, 0, 0, 0, 0},
  2201. };
  2202. struct dvb_frontend *dib0090_register(struct dvb_frontend *fe, struct i2c_adapter *i2c, const struct dib0090_config *config)
  2203. {
  2204. struct dib0090_state *st = kzalloc(sizeof(struct dib0090_state), GFP_KERNEL);
  2205. if (st == NULL)
  2206. return NULL;
  2207. st->config = config;
  2208. st->i2c = i2c;
  2209. st->fe = fe;
  2210. mutex_init(&st->i2c_buffer_lock);
  2211. fe->tuner_priv = st;
  2212. if (config->wbd == NULL)
  2213. st->current_wbd_table = dib0090_wbd_table_default;
  2214. else
  2215. st->current_wbd_table = config->wbd;
  2216. if (dib0090_reset(fe) != 0)
  2217. goto free_mem;
  2218. pr_info("DiB0090: successfully identified\n");
  2219. memcpy(&fe->ops.tuner_ops, &dib0090_ops, sizeof(struct dvb_tuner_ops));
  2220. return fe;
  2221. free_mem:
  2222. kfree(st);
  2223. fe->tuner_priv = NULL;
  2224. return NULL;
  2225. }
  2226. EXPORT_SYMBOL_GPL(dib0090_register);
  2227. struct dvb_frontend *dib0090_fw_register(struct dvb_frontend *fe, struct i2c_adapter *i2c, const struct dib0090_config *config)
  2228. {
  2229. struct dib0090_fw_state *st = kzalloc(sizeof(struct dib0090_fw_state), GFP_KERNEL);
  2230. if (st == NULL)
  2231. return NULL;
  2232. st->config = config;
  2233. st->i2c = i2c;
  2234. st->fe = fe;
  2235. mutex_init(&st->i2c_buffer_lock);
  2236. fe->tuner_priv = st;
  2237. if (dib0090_fw_reset_digital(fe, st->config) != 0)
  2238. goto free_mem;
  2239. dprintk("DiB0090 FW: successfully identified\n");
  2240. memcpy(&fe->ops.tuner_ops, &dib0090_fw_ops, sizeof(struct dvb_tuner_ops));
  2241. return fe;
  2242. free_mem:
  2243. kfree(st);
  2244. fe->tuner_priv = NULL;
  2245. return NULL;
  2246. }
  2247. EXPORT_SYMBOL_GPL(dib0090_fw_register);
  2248. MODULE_AUTHOR("Patrick Boettcher <[email protected]>");
  2249. MODULE_AUTHOR("Olivier Grenie <[email protected]>");
  2250. MODULE_DESCRIPTION("Driver for the DiBcom 0090 base-band RF Tuner");
  2251. MODULE_LICENSE("GPL");