dib0090.c 76 KB

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