dib0090.c 75 KB

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