dib7000p.c 62 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365
  1. /*
  2. * Linux-DVB Driver for DiBcom's second generation DiB7000P (PC).
  3. *
  4. * Copyright (C) 2005-7 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, version 2.
  9. */
  10. #include <linux/kernel.h>
  11. #include <linux/slab.h>
  12. #include <linux/i2c.h>
  13. #include "dvb_math.h"
  14. #include "dvb_frontend.h"
  15. #include "dib7000p.h"
  16. static int debug;
  17. module_param(debug, int, 0644);
  18. MODULE_PARM_DESC(debug, "turn on debugging (default: 0)");
  19. static int buggy_sfn_workaround;
  20. module_param(buggy_sfn_workaround, int, 0644);
  21. MODULE_PARM_DESC(buggy_sfn_workaround, "Enable work-around for buggy SFNs (default: 0)");
  22. #define dprintk(args...) do { if (debug) { printk(KERN_DEBUG "DiB7000P: "); printk(args); printk("\n"); } } while (0)
  23. struct i2c_device {
  24. struct i2c_adapter *i2c_adap;
  25. u8 i2c_addr;
  26. };
  27. struct dib7000p_state {
  28. struct dvb_frontend demod;
  29. struct dib7000p_config cfg;
  30. u8 i2c_addr;
  31. struct i2c_adapter *i2c_adap;
  32. struct dibx000_i2c_master i2c_master;
  33. u16 wbd_ref;
  34. u8 current_band;
  35. u32 current_bandwidth;
  36. struct dibx000_agc_config *current_agc;
  37. u32 timf;
  38. u8 div_force_off:1;
  39. u8 div_state:1;
  40. u16 div_sync_wait;
  41. u8 agc_state;
  42. u16 gpio_dir;
  43. u16 gpio_val;
  44. u8 sfn_workaround_active:1;
  45. #define SOC7090 0x7090
  46. u16 version;
  47. u16 tuner_enable;
  48. struct i2c_adapter dib7090_tuner_adap;
  49. };
  50. enum dib7000p_power_mode {
  51. DIB7000P_POWER_ALL = 0,
  52. DIB7000P_POWER_ANALOG_ADC,
  53. DIB7000P_POWER_INTERFACE_ONLY,
  54. };
  55. static int dib7090_set_output_mode(struct dvb_frontend *fe, int mode);
  56. static int dib7090_set_diversity_in(struct dvb_frontend *fe, int onoff);
  57. static u16 dib7000p_read_word(struct dib7000p_state *state, u16 reg)
  58. {
  59. u8 wb[2] = { reg >> 8, reg & 0xff };
  60. u8 rb[2];
  61. struct i2c_msg msg[2] = {
  62. {.addr = state->i2c_addr >> 1, .flags = 0, .buf = wb, .len = 2},
  63. {.addr = state->i2c_addr >> 1, .flags = I2C_M_RD, .buf = rb, .len = 2},
  64. };
  65. if (i2c_transfer(state->i2c_adap, msg, 2) != 2)
  66. dprintk("i2c read error on %d", reg);
  67. return (rb[0] << 8) | rb[1];
  68. }
  69. static int dib7000p_write_word(struct dib7000p_state *state, u16 reg, u16 val)
  70. {
  71. u8 b[4] = {
  72. (reg >> 8) & 0xff, reg & 0xff,
  73. (val >> 8) & 0xff, val & 0xff,
  74. };
  75. struct i2c_msg msg = {
  76. .addr = state->i2c_addr >> 1, .flags = 0, .buf = b, .len = 4
  77. };
  78. return i2c_transfer(state->i2c_adap, &msg, 1) != 1 ? -EREMOTEIO : 0;
  79. }
  80. static void dib7000p_write_tab(struct dib7000p_state *state, u16 * buf)
  81. {
  82. u16 l = 0, r, *n;
  83. n = buf;
  84. l = *n++;
  85. while (l) {
  86. r = *n++;
  87. do {
  88. dib7000p_write_word(state, r, *n++);
  89. r++;
  90. } while (--l);
  91. l = *n++;
  92. }
  93. }
  94. static int dib7000p_set_output_mode(struct dib7000p_state *state, int mode)
  95. {
  96. int ret = 0;
  97. u16 outreg, fifo_threshold, smo_mode;
  98. outreg = 0;
  99. fifo_threshold = 1792;
  100. smo_mode = (dib7000p_read_word(state, 235) & 0x0050) | (1 << 1);
  101. dprintk("setting output mode for demod %p to %d", &state->demod, mode);
  102. switch (mode) {
  103. case OUTMODE_MPEG2_PAR_GATED_CLK:
  104. outreg = (1 << 10); /* 0x0400 */
  105. break;
  106. case OUTMODE_MPEG2_PAR_CONT_CLK:
  107. outreg = (1 << 10) | (1 << 6); /* 0x0440 */
  108. break;
  109. case OUTMODE_MPEG2_SERIAL:
  110. outreg = (1 << 10) | (2 << 6) | (0 << 1); /* 0x0480 */
  111. break;
  112. case OUTMODE_DIVERSITY:
  113. if (state->cfg.hostbus_diversity)
  114. outreg = (1 << 10) | (4 << 6); /* 0x0500 */
  115. else
  116. outreg = (1 << 11);
  117. break;
  118. case OUTMODE_MPEG2_FIFO:
  119. smo_mode |= (3 << 1);
  120. fifo_threshold = 512;
  121. outreg = (1 << 10) | (5 << 6);
  122. break;
  123. case OUTMODE_ANALOG_ADC:
  124. outreg = (1 << 10) | (3 << 6);
  125. break;
  126. case OUTMODE_HIGH_Z:
  127. outreg = 0;
  128. break;
  129. default:
  130. dprintk("Unhandled output_mode passed to be set for demod %p", &state->demod);
  131. break;
  132. }
  133. if (state->cfg.output_mpeg2_in_188_bytes)
  134. smo_mode |= (1 << 5);
  135. ret |= dib7000p_write_word(state, 235, smo_mode);
  136. ret |= dib7000p_write_word(state, 236, fifo_threshold); /* synchronous fread */
  137. if (state->version != SOC7090)
  138. ret |= dib7000p_write_word(state, 1286, outreg); /* P_Div_active */
  139. return ret;
  140. }
  141. static int dib7000p_set_diversity_in(struct dvb_frontend *demod, int onoff)
  142. {
  143. struct dib7000p_state *state = demod->demodulator_priv;
  144. if (state->div_force_off) {
  145. dprintk("diversity combination deactivated - forced by COFDM parameters");
  146. onoff = 0;
  147. dib7000p_write_word(state, 207, 0);
  148. } else
  149. dib7000p_write_word(state, 207, (state->div_sync_wait << 4) | (1 << 2) | (2 << 0));
  150. state->div_state = (u8) onoff;
  151. if (onoff) {
  152. dib7000p_write_word(state, 204, 6);
  153. dib7000p_write_word(state, 205, 16);
  154. /* P_dvsy_sync_mode = 0, P_dvsy_sync_enable=1, P_dvcb_comb_mode=2 */
  155. } else {
  156. dib7000p_write_word(state, 204, 1);
  157. dib7000p_write_word(state, 205, 0);
  158. }
  159. return 0;
  160. }
  161. static int dib7000p_set_power_mode(struct dib7000p_state *state, enum dib7000p_power_mode mode)
  162. {
  163. /* by default everything is powered off */
  164. u16 reg_774 = 0x3fff, reg_775 = 0xffff, reg_776 = 0x0007, reg_899 = 0x0003, reg_1280 = (0xfe00) | (dib7000p_read_word(state, 1280) & 0x01ff);
  165. /* now, depending on the requested mode, we power on */
  166. switch (mode) {
  167. /* power up everything in the demod */
  168. case DIB7000P_POWER_ALL:
  169. reg_774 = 0x0000;
  170. reg_775 = 0x0000;
  171. reg_776 = 0x0;
  172. reg_899 = 0x0;
  173. if (state->version == SOC7090)
  174. reg_1280 &= 0x001f;
  175. else
  176. reg_1280 &= 0x01ff;
  177. break;
  178. case DIB7000P_POWER_ANALOG_ADC:
  179. /* dem, cfg, iqc, sad, agc */
  180. reg_774 &= ~((1 << 15) | (1 << 14) | (1 << 11) | (1 << 10) | (1 << 9));
  181. /* nud */
  182. reg_776 &= ~((1 << 0));
  183. /* Dout */
  184. if (state->version != SOC7090)
  185. reg_1280 &= ~((1 << 11));
  186. reg_1280 &= ~(1 << 6);
  187. /* fall through wanted to enable the interfaces */
  188. /* just leave power on the control-interfaces: GPIO and (I2C or SDIO) */
  189. case DIB7000P_POWER_INTERFACE_ONLY: /* TODO power up either SDIO or I2C */
  190. if (state->version == SOC7090)
  191. reg_1280 &= ~((1 << 7) | (1 << 5));
  192. else
  193. reg_1280 &= ~((1 << 14) | (1 << 13) | (1 << 12) | (1 << 10));
  194. break;
  195. /* TODO following stuff is just converted from the dib7000-driver - check when is used what */
  196. }
  197. dib7000p_write_word(state, 774, reg_774);
  198. dib7000p_write_word(state, 775, reg_775);
  199. dib7000p_write_word(state, 776, reg_776);
  200. dib7000p_write_word(state, 899, reg_899);
  201. dib7000p_write_word(state, 1280, reg_1280);
  202. return 0;
  203. }
  204. static void dib7000p_set_adc_state(struct dib7000p_state *state, enum dibx000_adc_states no)
  205. {
  206. u16 reg_908 = dib7000p_read_word(state, 908), reg_909 = dib7000p_read_word(state, 909);
  207. u16 reg;
  208. switch (no) {
  209. case DIBX000_SLOW_ADC_ON:
  210. if (state->version == SOC7090) {
  211. reg = dib7000p_read_word(state, 1925);
  212. dib7000p_write_word(state, 1925, reg | (1 << 4) | (1 << 2)); /* en_slowAdc = 1 & reset_sladc = 1 */
  213. reg = dib7000p_read_word(state, 1925); /* read acces to make it works... strange ... */
  214. msleep(200);
  215. dib7000p_write_word(state, 1925, reg & ~(1 << 4)); /* en_slowAdc = 1 & reset_sladc = 0 */
  216. reg = dib7000p_read_word(state, 72) & ~((0x3 << 14) | (0x3 << 12));
  217. dib7000p_write_word(state, 72, reg | (1 << 14) | (3 << 12) | 524); /* ref = Vin1 => Vbg ; sel = Vin0 or Vin3 ; (Vin2 = Vcm) */
  218. } else {
  219. reg_909 |= (1 << 1) | (1 << 0);
  220. dib7000p_write_word(state, 909, reg_909);
  221. reg_909 &= ~(1 << 1);
  222. }
  223. break;
  224. case DIBX000_SLOW_ADC_OFF:
  225. if (state->version == SOC7090) {
  226. reg = dib7000p_read_word(state, 1925);
  227. dib7000p_write_word(state, 1925, (reg & ~(1 << 2)) | (1 << 4)); /* reset_sladc = 1 en_slowAdc = 0 */
  228. } else
  229. reg_909 |= (1 << 1) | (1 << 0);
  230. break;
  231. case DIBX000_ADC_ON:
  232. reg_908 &= 0x0fff;
  233. reg_909 &= 0x0003;
  234. break;
  235. case DIBX000_ADC_OFF:
  236. reg_908 |= (1 << 14) | (1 << 13) | (1 << 12);
  237. reg_909 |= (1 << 5) | (1 << 4) | (1 << 3) | (1 << 2);
  238. break;
  239. case DIBX000_VBG_ENABLE:
  240. reg_908 &= ~(1 << 15);
  241. break;
  242. case DIBX000_VBG_DISABLE:
  243. reg_908 |= (1 << 15);
  244. break;
  245. default:
  246. break;
  247. }
  248. // dprintk( "908: %x, 909: %x\n", reg_908, reg_909);
  249. reg_909 |= (state->cfg.disable_sample_and_hold & 1) << 4;
  250. reg_908 |= (state->cfg.enable_current_mirror & 1) << 7;
  251. dib7000p_write_word(state, 908, reg_908);
  252. dib7000p_write_word(state, 909, reg_909);
  253. }
  254. static int dib7000p_set_bandwidth(struct dib7000p_state *state, u32 bw)
  255. {
  256. u32 timf;
  257. // store the current bandwidth for later use
  258. state->current_bandwidth = bw;
  259. if (state->timf == 0) {
  260. dprintk("using default timf");
  261. timf = state->cfg.bw->timf;
  262. } else {
  263. dprintk("using updated timf");
  264. timf = state->timf;
  265. }
  266. timf = timf * (bw / 50) / 160;
  267. dib7000p_write_word(state, 23, (u16) ((timf >> 16) & 0xffff));
  268. dib7000p_write_word(state, 24, (u16) ((timf) & 0xffff));
  269. return 0;
  270. }
  271. static int dib7000p_sad_calib(struct dib7000p_state *state)
  272. {
  273. /* internal */
  274. dib7000p_write_word(state, 73, (0 << 1) | (0 << 0));
  275. if (state->version == SOC7090)
  276. dib7000p_write_word(state, 74, 2048);
  277. else
  278. dib7000p_write_word(state, 74, 776);
  279. /* do the calibration */
  280. dib7000p_write_word(state, 73, (1 << 0));
  281. dib7000p_write_word(state, 73, (0 << 0));
  282. msleep(1);
  283. return 0;
  284. }
  285. int dib7000p_set_wbd_ref(struct dvb_frontend *demod, u16 value)
  286. {
  287. struct dib7000p_state *state = demod->demodulator_priv;
  288. if (value > 4095)
  289. value = 4095;
  290. state->wbd_ref = value;
  291. return dib7000p_write_word(state, 105, (dib7000p_read_word(state, 105) & 0xf000) | value);
  292. }
  293. EXPORT_SYMBOL(dib7000p_set_wbd_ref);
  294. static void dib7000p_reset_pll(struct dib7000p_state *state)
  295. {
  296. struct dibx000_bandwidth_config *bw = &state->cfg.bw[0];
  297. u16 clk_cfg0;
  298. if (state->version == SOC7090) {
  299. dib7000p_write_word(state, 1856, (!bw->pll_reset << 13) | (bw->pll_range << 12) | (bw->pll_ratio << 6) | (bw->pll_prediv));
  300. while (((dib7000p_read_word(state, 1856) >> 15) & 0x1) != 1)
  301. ;
  302. dib7000p_write_word(state, 1857, dib7000p_read_word(state, 1857) | (!bw->pll_bypass << 15));
  303. } else {
  304. /* force PLL bypass */
  305. clk_cfg0 = (1 << 15) | ((bw->pll_ratio & 0x3f) << 9) |
  306. (bw->modulo << 7) | (bw->ADClkSrc << 6) | (bw->IO_CLK_en_core << 5) | (bw->bypclk_div << 2) | (bw->enable_refdiv << 1) | (0 << 0);
  307. dib7000p_write_word(state, 900, clk_cfg0);
  308. /* P_pll_cfg */
  309. dib7000p_write_word(state, 903, (bw->pll_prediv << 5) | (((bw->pll_ratio >> 6) & 0x3) << 3) | (bw->pll_range << 1) | bw->pll_reset);
  310. clk_cfg0 = (bw->pll_bypass << 15) | (clk_cfg0 & 0x7fff);
  311. dib7000p_write_word(state, 900, clk_cfg0);
  312. }
  313. dib7000p_write_word(state, 18, (u16) (((bw->internal * 1000) >> 16) & 0xffff));
  314. dib7000p_write_word(state, 19, (u16) ((bw->internal * 1000) & 0xffff));
  315. dib7000p_write_word(state, 21, (u16) ((bw->ifreq >> 16) & 0xffff));
  316. dib7000p_write_word(state, 22, (u16) ((bw->ifreq) & 0xffff));
  317. dib7000p_write_word(state, 72, bw->sad_cfg);
  318. }
  319. static u32 dib7000p_get_internal_freq(struct dib7000p_state *state)
  320. {
  321. u32 internal = (u32) dib7000p_read_word(state, 18) << 16;
  322. internal |= (u32) dib7000p_read_word(state, 19);
  323. internal /= 1000;
  324. return internal;
  325. }
  326. int dib7000p_update_pll(struct dvb_frontend *fe, struct dibx000_bandwidth_config *bw)
  327. {
  328. struct dib7000p_state *state = fe->demodulator_priv;
  329. u16 reg_1857, reg_1856 = dib7000p_read_word(state, 1856);
  330. u8 loopdiv, prediv;
  331. u32 internal, xtal;
  332. /* get back old values */
  333. prediv = reg_1856 & 0x3f;
  334. loopdiv = (reg_1856 >> 6) & 0x3f;
  335. if ((bw != NULL) && (bw->pll_prediv != prediv || bw->pll_ratio != loopdiv)) {
  336. dprintk("Updating pll (prediv: old = %d new = %d ; loopdiv : old = %d new = %d)", prediv, bw->pll_prediv, loopdiv, bw->pll_ratio);
  337. reg_1856 &= 0xf000;
  338. reg_1857 = dib7000p_read_word(state, 1857);
  339. dib7000p_write_word(state, 1857, reg_1857 & ~(1 << 15));
  340. dib7000p_write_word(state, 1856, reg_1856 | ((bw->pll_ratio & 0x3f) << 6) | (bw->pll_prediv & 0x3f));
  341. /* write new system clk into P_sec_len */
  342. internal = dib7000p_get_internal_freq(state);
  343. xtal = (internal / loopdiv) * prediv;
  344. internal = 1000 * (xtal / bw->pll_prediv) * bw->pll_ratio; /* new internal */
  345. dib7000p_write_word(state, 18, (u16) ((internal >> 16) & 0xffff));
  346. dib7000p_write_word(state, 19, (u16) (internal & 0xffff));
  347. dib7000p_write_word(state, 1857, reg_1857 | (1 << 15));
  348. while (((dib7000p_read_word(state, 1856) >> 15) & 0x1) != 1)
  349. dprintk("Waiting for PLL to lock");
  350. return 0;
  351. }
  352. return -EIO;
  353. }
  354. EXPORT_SYMBOL(dib7000p_update_pll);
  355. static int dib7000p_reset_gpio(struct dib7000p_state *st)
  356. {
  357. /* reset the GPIOs */
  358. dprintk("gpio dir: %x: val: %x, pwm_pos: %x", st->gpio_dir, st->gpio_val, st->cfg.gpio_pwm_pos);
  359. dib7000p_write_word(st, 1029, st->gpio_dir);
  360. dib7000p_write_word(st, 1030, st->gpio_val);
  361. /* TODO 1031 is P_gpio_od */
  362. dib7000p_write_word(st, 1032, st->cfg.gpio_pwm_pos);
  363. dib7000p_write_word(st, 1037, st->cfg.pwm_freq_div);
  364. return 0;
  365. }
  366. static int dib7000p_cfg_gpio(struct dib7000p_state *st, u8 num, u8 dir, u8 val)
  367. {
  368. st->gpio_dir = dib7000p_read_word(st, 1029);
  369. st->gpio_dir &= ~(1 << num); /* reset the direction bit */
  370. st->gpio_dir |= (dir & 0x1) << num; /* set the new direction */
  371. dib7000p_write_word(st, 1029, st->gpio_dir);
  372. st->gpio_val = dib7000p_read_word(st, 1030);
  373. st->gpio_val &= ~(1 << num); /* reset the direction bit */
  374. st->gpio_val |= (val & 0x01) << num; /* set the new value */
  375. dib7000p_write_word(st, 1030, st->gpio_val);
  376. return 0;
  377. }
  378. int dib7000p_set_gpio(struct dvb_frontend *demod, u8 num, u8 dir, u8 val)
  379. {
  380. struct dib7000p_state *state = demod->demodulator_priv;
  381. return dib7000p_cfg_gpio(state, num, dir, val);
  382. }
  383. EXPORT_SYMBOL(dib7000p_set_gpio);
  384. static u16 dib7000p_defaults[] = {
  385. // auto search configuration
  386. 3, 2,
  387. 0x0004,
  388. 0x1000,
  389. 0x0814, /* Equal Lock */
  390. 12, 6,
  391. 0x001b,
  392. 0x7740,
  393. 0x005b,
  394. 0x8d80,
  395. 0x01c9,
  396. 0xc380,
  397. 0x0000,
  398. 0x0080,
  399. 0x0000,
  400. 0x0090,
  401. 0x0001,
  402. 0xd4c0,
  403. 1, 26,
  404. 0x6680,
  405. /* set ADC level to -16 */
  406. 11, 79,
  407. (1 << 13) - 825 - 117,
  408. (1 << 13) - 837 - 117,
  409. (1 << 13) - 811 - 117,
  410. (1 << 13) - 766 - 117,
  411. (1 << 13) - 737 - 117,
  412. (1 << 13) - 693 - 117,
  413. (1 << 13) - 648 - 117,
  414. (1 << 13) - 619 - 117,
  415. (1 << 13) - 575 - 117,
  416. (1 << 13) - 531 - 117,
  417. (1 << 13) - 501 - 117,
  418. 1, 142,
  419. 0x0410,
  420. /* disable power smoothing */
  421. 8, 145,
  422. 0,
  423. 0,
  424. 0,
  425. 0,
  426. 0,
  427. 0,
  428. 0,
  429. 0,
  430. 1, 154,
  431. 1 << 13,
  432. 1, 168,
  433. 0x0ccd,
  434. 1, 183,
  435. 0x200f,
  436. 1, 212,
  437. 0x169,
  438. 5, 187,
  439. 0x023d,
  440. 0x00a4,
  441. 0x00a4,
  442. 0x7ff0,
  443. 0x3ccc,
  444. 1, 198,
  445. 0x800,
  446. 1, 222,
  447. 0x0010,
  448. 1, 235,
  449. 0x0062,
  450. 2, 901,
  451. 0x0006,
  452. (3 << 10) | (1 << 6),
  453. 1, 905,
  454. 0x2c8e,
  455. 0,
  456. };
  457. static int dib7000p_demod_reset(struct dib7000p_state *state)
  458. {
  459. dib7000p_set_power_mode(state, DIB7000P_POWER_ALL);
  460. if (state->version == SOC7090)
  461. dibx000_reset_i2c_master(&state->i2c_master);
  462. dib7000p_set_adc_state(state, DIBX000_VBG_ENABLE);
  463. /* restart all parts */
  464. dib7000p_write_word(state, 770, 0xffff);
  465. dib7000p_write_word(state, 771, 0xffff);
  466. dib7000p_write_word(state, 772, 0x001f);
  467. dib7000p_write_word(state, 898, 0x0003);
  468. dib7000p_write_word(state, 1280, 0x001f - ((1 << 4) | (1 << 3)));
  469. dib7000p_write_word(state, 770, 0);
  470. dib7000p_write_word(state, 771, 0);
  471. dib7000p_write_word(state, 772, 0);
  472. dib7000p_write_word(state, 898, 0);
  473. dib7000p_write_word(state, 1280, 0);
  474. /* default */
  475. dib7000p_reset_pll(state);
  476. if (dib7000p_reset_gpio(state) != 0)
  477. dprintk("GPIO reset was not successful.");
  478. if (state->version == SOC7090) {
  479. dib7000p_write_word(state, 899, 0);
  480. /* impulse noise */
  481. dib7000p_write_word(state, 42, (1<<5) | 3); /* P_iqc_thsat_ipc = 1 ; P_iqc_win2 = 3 */
  482. dib7000p_write_word(state, 43, 0x2d4); /*-300 fag P_iqc_dect_min = -280 */
  483. dib7000p_write_word(state, 44, 300); /* 300 fag P_iqc_dect_min = +280 */
  484. dib7000p_write_word(state, 273, (1<<6) | 30);
  485. }
  486. if (dib7000p_set_output_mode(state, OUTMODE_HIGH_Z) != 0)
  487. dprintk("OUTPUT_MODE could not be reset.");
  488. dib7000p_set_adc_state(state, DIBX000_SLOW_ADC_ON);
  489. dib7000p_sad_calib(state);
  490. dib7000p_set_adc_state(state, DIBX000_SLOW_ADC_OFF);
  491. /* unforce divstr regardless whether i2c enumeration was done or not */
  492. dib7000p_write_word(state, 1285, dib7000p_read_word(state, 1285) & ~(1 << 1));
  493. dib7000p_set_bandwidth(state, 8000);
  494. if (state->version == SOC7090) {
  495. dib7000p_write_word(state, 36, 0x5755);/* P_iqc_impnc_on =1 & P_iqc_corr_inh = 1 for impulsive noise */
  496. } else {
  497. if (state->cfg.tuner_is_baseband)
  498. dib7000p_write_word(state, 36, 0x0755);
  499. else
  500. dib7000p_write_word(state, 36, 0x1f55);
  501. }
  502. dib7000p_write_tab(state, dib7000p_defaults);
  503. dib7000p_set_power_mode(state, DIB7000P_POWER_INTERFACE_ONLY);
  504. return 0;
  505. }
  506. static void dib7000p_pll_clk_cfg(struct dib7000p_state *state)
  507. {
  508. u16 tmp = 0;
  509. tmp = dib7000p_read_word(state, 903);
  510. dib7000p_write_word(state, 903, (tmp | 0x1));
  511. tmp = dib7000p_read_word(state, 900);
  512. dib7000p_write_word(state, 900, (tmp & 0x7fff) | (1 << 6));
  513. }
  514. static void dib7000p_restart_agc(struct dib7000p_state *state)
  515. {
  516. // P_restart_iqc & P_restart_agc
  517. dib7000p_write_word(state, 770, (1 << 11) | (1 << 9));
  518. dib7000p_write_word(state, 770, 0x0000);
  519. }
  520. static int dib7000p_update_lna(struct dib7000p_state *state)
  521. {
  522. u16 dyn_gain;
  523. if (state->cfg.update_lna) {
  524. dyn_gain = dib7000p_read_word(state, 394);
  525. if (state->cfg.update_lna(&state->demod, dyn_gain)) {
  526. dib7000p_restart_agc(state);
  527. return 1;
  528. }
  529. }
  530. return 0;
  531. }
  532. static int dib7000p_set_agc_config(struct dib7000p_state *state, u8 band)
  533. {
  534. struct dibx000_agc_config *agc = NULL;
  535. int i;
  536. if (state->current_band == band && state->current_agc != NULL)
  537. return 0;
  538. state->current_band = band;
  539. for (i = 0; i < state->cfg.agc_config_count; i++)
  540. if (state->cfg.agc[i].band_caps & band) {
  541. agc = &state->cfg.agc[i];
  542. break;
  543. }
  544. if (agc == NULL) {
  545. dprintk("no valid AGC configuration found for band 0x%02x", band);
  546. return -EINVAL;
  547. }
  548. state->current_agc = agc;
  549. /* AGC */
  550. dib7000p_write_word(state, 75, agc->setup);
  551. dib7000p_write_word(state, 76, agc->inv_gain);
  552. dib7000p_write_word(state, 77, agc->time_stabiliz);
  553. dib7000p_write_word(state, 100, (agc->alpha_level << 12) | agc->thlock);
  554. // Demod AGC loop configuration
  555. dib7000p_write_word(state, 101, (agc->alpha_mant << 5) | agc->alpha_exp);
  556. dib7000p_write_word(state, 102, (agc->beta_mant << 6) | agc->beta_exp);
  557. /* AGC continued */
  558. dprintk("WBD: ref: %d, sel: %d, active: %d, alpha: %d",
  559. state->wbd_ref != 0 ? state->wbd_ref : agc->wbd_ref, agc->wbd_sel, !agc->perform_agc_softsplit, agc->wbd_sel);
  560. if (state->wbd_ref != 0)
  561. dib7000p_write_word(state, 105, (agc->wbd_inv << 12) | state->wbd_ref);
  562. else
  563. dib7000p_write_word(state, 105, (agc->wbd_inv << 12) | agc->wbd_ref);
  564. dib7000p_write_word(state, 106, (agc->wbd_sel << 13) | (agc->wbd_alpha << 9) | (agc->perform_agc_softsplit << 8));
  565. dib7000p_write_word(state, 107, agc->agc1_max);
  566. dib7000p_write_word(state, 108, agc->agc1_min);
  567. dib7000p_write_word(state, 109, agc->agc2_max);
  568. dib7000p_write_word(state, 110, agc->agc2_min);
  569. dib7000p_write_word(state, 111, (agc->agc1_pt1 << 8) | agc->agc1_pt2);
  570. dib7000p_write_word(state, 112, agc->agc1_pt3);
  571. dib7000p_write_word(state, 113, (agc->agc1_slope1 << 8) | agc->agc1_slope2);
  572. dib7000p_write_word(state, 114, (agc->agc2_pt1 << 8) | agc->agc2_pt2);
  573. dib7000p_write_word(state, 115, (agc->agc2_slope1 << 8) | agc->agc2_slope2);
  574. return 0;
  575. }
  576. static void dib7000p_set_dds(struct dib7000p_state *state, s32 offset_khz)
  577. {
  578. u32 internal = dib7000p_get_internal_freq(state);
  579. s32 unit_khz_dds_val = 67108864 / (internal); /* 2**26 / Fsampling is the unit 1KHz offset */
  580. u32 abs_offset_khz = ABS(offset_khz);
  581. u32 dds = state->cfg.bw->ifreq & 0x1ffffff;
  582. u8 invert = !!(state->cfg.bw->ifreq & (1 << 25));
  583. dprintk("setting a frequency offset of %dkHz internal freq = %d invert = %d", offset_khz, internal, invert);
  584. if (offset_khz < 0)
  585. unit_khz_dds_val *= -1;
  586. /* IF tuner */
  587. if (invert)
  588. dds -= (abs_offset_khz * unit_khz_dds_val); /* /100 because of /100 on the unit_khz_dds_val line calc for better accuracy */
  589. else
  590. dds += (abs_offset_khz * unit_khz_dds_val);
  591. if (abs_offset_khz <= (internal / 2)) { /* Max dds offset is the half of the demod freq */
  592. dib7000p_write_word(state, 21, (u16) (((dds >> 16) & 0x1ff) | (0 << 10) | (invert << 9)));
  593. dib7000p_write_word(state, 22, (u16) (dds & 0xffff));
  594. }
  595. }
  596. static int dib7000p_agc_startup(struct dvb_frontend *demod, struct dvb_frontend_parameters *ch)
  597. {
  598. struct dib7000p_state *state = demod->demodulator_priv;
  599. int ret = -1;
  600. u8 *agc_state = &state->agc_state;
  601. u8 agc_split;
  602. u16 reg;
  603. u32 upd_demod_gain_period = 0x1000;
  604. switch (state->agc_state) {
  605. case 0:
  606. dib7000p_set_power_mode(state, DIB7000P_POWER_ALL);
  607. if (state->version == SOC7090) {
  608. reg = dib7000p_read_word(state, 0x79b) & 0xff00;
  609. dib7000p_write_word(state, 0x79a, upd_demod_gain_period & 0xFFFF); /* lsb */
  610. dib7000p_write_word(state, 0x79b, reg | (1 << 14) | ((upd_demod_gain_period >> 16) & 0xFF));
  611. /* enable adc i & q */
  612. reg = dib7000p_read_word(state, 0x780);
  613. dib7000p_write_word(state, 0x780, (reg | (0x3)) & (~(1 << 7)));
  614. } else {
  615. dib7000p_set_adc_state(state, DIBX000_ADC_ON);
  616. dib7000p_pll_clk_cfg(state);
  617. }
  618. if (dib7000p_set_agc_config(state, BAND_OF_FREQUENCY(ch->frequency / 1000)) != 0)
  619. return -1;
  620. dib7000p_set_dds(state, 0);
  621. ret = 7;
  622. (*agc_state)++;
  623. break;
  624. case 1:
  625. if (state->cfg.agc_control)
  626. state->cfg.agc_control(&state->demod, 1);
  627. dib7000p_write_word(state, 78, 32768);
  628. if (!state->current_agc->perform_agc_softsplit) {
  629. /* we are using the wbd - so slow AGC startup */
  630. /* force 0 split on WBD and restart AGC */
  631. dib7000p_write_word(state, 106, (state->current_agc->wbd_sel << 13) | (state->current_agc->wbd_alpha << 9) | (1 << 8));
  632. (*agc_state)++;
  633. ret = 5;
  634. } else {
  635. /* default AGC startup */
  636. (*agc_state) = 4;
  637. /* wait AGC rough lock time */
  638. ret = 7;
  639. }
  640. dib7000p_restart_agc(state);
  641. break;
  642. case 2: /* fast split search path after 5sec */
  643. dib7000p_write_word(state, 75, state->current_agc->setup | (1 << 4)); /* freeze AGC loop */
  644. dib7000p_write_word(state, 106, (state->current_agc->wbd_sel << 13) | (2 << 9) | (0 << 8)); /* fast split search 0.25kHz */
  645. (*agc_state)++;
  646. ret = 14;
  647. break;
  648. case 3: /* split search ended */
  649. agc_split = (u8) dib7000p_read_word(state, 396); /* store the split value for the next time */
  650. dib7000p_write_word(state, 78, dib7000p_read_word(state, 394)); /* set AGC gain start value */
  651. dib7000p_write_word(state, 75, state->current_agc->setup); /* std AGC loop */
  652. dib7000p_write_word(state, 106, (state->current_agc->wbd_sel << 13) | (state->current_agc->wbd_alpha << 9) | agc_split); /* standard split search */
  653. dib7000p_restart_agc(state);
  654. dprintk("SPLIT %p: %hd", demod, agc_split);
  655. (*agc_state)++;
  656. ret = 5;
  657. break;
  658. case 4: /* LNA startup */
  659. ret = 7;
  660. if (dib7000p_update_lna(state))
  661. ret = 5;
  662. else
  663. (*agc_state)++;
  664. break;
  665. case 5:
  666. if (state->cfg.agc_control)
  667. state->cfg.agc_control(&state->demod, 0);
  668. (*agc_state)++;
  669. break;
  670. default:
  671. break;
  672. }
  673. return ret;
  674. }
  675. static void dib7000p_update_timf(struct dib7000p_state *state)
  676. {
  677. u32 timf = (dib7000p_read_word(state, 427) << 16) | dib7000p_read_word(state, 428);
  678. state->timf = timf * 160 / (state->current_bandwidth / 50);
  679. dib7000p_write_word(state, 23, (u16) (timf >> 16));
  680. dib7000p_write_word(state, 24, (u16) (timf & 0xffff));
  681. dprintk("updated timf_frequency: %d (default: %d)", state->timf, state->cfg.bw->timf);
  682. }
  683. u32 dib7000p_ctrl_timf(struct dvb_frontend *fe, u8 op, u32 timf)
  684. {
  685. struct dib7000p_state *state = fe->demodulator_priv;
  686. switch (op) {
  687. case DEMOD_TIMF_SET:
  688. state->timf = timf;
  689. break;
  690. case DEMOD_TIMF_UPDATE:
  691. dib7000p_update_timf(state);
  692. break;
  693. case DEMOD_TIMF_GET:
  694. break;
  695. }
  696. dib7000p_set_bandwidth(state, state->current_bandwidth);
  697. return state->timf;
  698. }
  699. EXPORT_SYMBOL(dib7000p_ctrl_timf);
  700. static void dib7000p_set_channel(struct dib7000p_state *state, struct dvb_frontend_parameters *ch, u8 seq)
  701. {
  702. u16 value, est[4];
  703. dib7000p_set_bandwidth(state, BANDWIDTH_TO_KHZ(ch->u.ofdm.bandwidth));
  704. /* nfft, guard, qam, alpha */
  705. value = 0;
  706. switch (ch->u.ofdm.transmission_mode) {
  707. case TRANSMISSION_MODE_2K:
  708. value |= (0 << 7);
  709. break;
  710. case TRANSMISSION_MODE_4K:
  711. value |= (2 << 7);
  712. break;
  713. default:
  714. case TRANSMISSION_MODE_8K:
  715. value |= (1 << 7);
  716. break;
  717. }
  718. switch (ch->u.ofdm.guard_interval) {
  719. case GUARD_INTERVAL_1_32:
  720. value |= (0 << 5);
  721. break;
  722. case GUARD_INTERVAL_1_16:
  723. value |= (1 << 5);
  724. break;
  725. case GUARD_INTERVAL_1_4:
  726. value |= (3 << 5);
  727. break;
  728. default:
  729. case GUARD_INTERVAL_1_8:
  730. value |= (2 << 5);
  731. break;
  732. }
  733. switch (ch->u.ofdm.constellation) {
  734. case QPSK:
  735. value |= (0 << 3);
  736. break;
  737. case QAM_16:
  738. value |= (1 << 3);
  739. break;
  740. default:
  741. case QAM_64:
  742. value |= (2 << 3);
  743. break;
  744. }
  745. switch (HIERARCHY_1) {
  746. case HIERARCHY_2:
  747. value |= 2;
  748. break;
  749. case HIERARCHY_4:
  750. value |= 4;
  751. break;
  752. default:
  753. case HIERARCHY_1:
  754. value |= 1;
  755. break;
  756. }
  757. dib7000p_write_word(state, 0, value);
  758. dib7000p_write_word(state, 5, (seq << 4) | 1); /* do not force tps, search list 0 */
  759. /* P_dintl_native, P_dintlv_inv, P_hrch, P_code_rate, P_select_hp */
  760. value = 0;
  761. if (1 != 0)
  762. value |= (1 << 6);
  763. if (ch->u.ofdm.hierarchy_information == 1)
  764. value |= (1 << 4);
  765. if (1 == 1)
  766. value |= 1;
  767. switch ((ch->u.ofdm.hierarchy_information == 0 || 1 == 1) ? ch->u.ofdm.code_rate_HP : ch->u.ofdm.code_rate_LP) {
  768. case FEC_2_3:
  769. value |= (2 << 1);
  770. break;
  771. case FEC_3_4:
  772. value |= (3 << 1);
  773. break;
  774. case FEC_5_6:
  775. value |= (5 << 1);
  776. break;
  777. case FEC_7_8:
  778. value |= (7 << 1);
  779. break;
  780. default:
  781. case FEC_1_2:
  782. value |= (1 << 1);
  783. break;
  784. }
  785. dib7000p_write_word(state, 208, value);
  786. /* offset loop parameters */
  787. dib7000p_write_word(state, 26, 0x6680);
  788. dib7000p_write_word(state, 32, 0x0003);
  789. dib7000p_write_word(state, 29, 0x1273);
  790. dib7000p_write_word(state, 33, 0x0005);
  791. /* P_dvsy_sync_wait */
  792. switch (ch->u.ofdm.transmission_mode) {
  793. case TRANSMISSION_MODE_8K:
  794. value = 256;
  795. break;
  796. case TRANSMISSION_MODE_4K:
  797. value = 128;
  798. break;
  799. case TRANSMISSION_MODE_2K:
  800. default:
  801. value = 64;
  802. break;
  803. }
  804. switch (ch->u.ofdm.guard_interval) {
  805. case GUARD_INTERVAL_1_16:
  806. value *= 2;
  807. break;
  808. case GUARD_INTERVAL_1_8:
  809. value *= 4;
  810. break;
  811. case GUARD_INTERVAL_1_4:
  812. value *= 8;
  813. break;
  814. default:
  815. case GUARD_INTERVAL_1_32:
  816. value *= 1;
  817. break;
  818. }
  819. if (state->cfg.diversity_delay == 0)
  820. state->div_sync_wait = (value * 3) / 2 + 48;
  821. else
  822. state->div_sync_wait = (value * 3) / 2 + state->cfg.diversity_delay;
  823. /* deactive the possibility of diversity reception if extended interleaver */
  824. state->div_force_off = !1 && ch->u.ofdm.transmission_mode != TRANSMISSION_MODE_8K;
  825. dib7000p_set_diversity_in(&state->demod, state->div_state);
  826. /* channel estimation fine configuration */
  827. switch (ch->u.ofdm.constellation) {
  828. case QAM_64:
  829. est[0] = 0x0148; /* P_adp_regul_cnt 0.04 */
  830. est[1] = 0xfff0; /* P_adp_noise_cnt -0.002 */
  831. est[2] = 0x00a4; /* P_adp_regul_ext 0.02 */
  832. est[3] = 0xfff8; /* P_adp_noise_ext -0.001 */
  833. break;
  834. case QAM_16:
  835. est[0] = 0x023d; /* P_adp_regul_cnt 0.07 */
  836. est[1] = 0xffdf; /* P_adp_noise_cnt -0.004 */
  837. est[2] = 0x00a4; /* P_adp_regul_ext 0.02 */
  838. est[3] = 0xfff0; /* P_adp_noise_ext -0.002 */
  839. break;
  840. default:
  841. est[0] = 0x099a; /* P_adp_regul_cnt 0.3 */
  842. est[1] = 0xffae; /* P_adp_noise_cnt -0.01 */
  843. est[2] = 0x0333; /* P_adp_regul_ext 0.1 */
  844. est[3] = 0xfff8; /* P_adp_noise_ext -0.002 */
  845. break;
  846. }
  847. for (value = 0; value < 4; value++)
  848. dib7000p_write_word(state, 187 + value, est[value]);
  849. }
  850. static int dib7000p_autosearch_start(struct dvb_frontend *demod, struct dvb_frontend_parameters *ch)
  851. {
  852. struct dib7000p_state *state = demod->demodulator_priv;
  853. struct dvb_frontend_parameters schan;
  854. u32 value, factor;
  855. u32 internal = dib7000p_get_internal_freq(state);
  856. schan = *ch;
  857. schan.u.ofdm.constellation = QAM_64;
  858. schan.u.ofdm.guard_interval = GUARD_INTERVAL_1_32;
  859. schan.u.ofdm.transmission_mode = TRANSMISSION_MODE_8K;
  860. schan.u.ofdm.code_rate_HP = FEC_2_3;
  861. schan.u.ofdm.code_rate_LP = FEC_3_4;
  862. schan.u.ofdm.hierarchy_information = 0;
  863. dib7000p_set_channel(state, &schan, 7);
  864. factor = BANDWIDTH_TO_KHZ(ch->u.ofdm.bandwidth);
  865. if (factor >= 5000)
  866. factor = 1;
  867. else
  868. factor = 6;
  869. value = 30 * internal * factor;
  870. dib7000p_write_word(state, 6, (u16) ((value >> 16) & 0xffff));
  871. dib7000p_write_word(state, 7, (u16) (value & 0xffff));
  872. value = 100 * internal * factor;
  873. dib7000p_write_word(state, 8, (u16) ((value >> 16) & 0xffff));
  874. dib7000p_write_word(state, 9, (u16) (value & 0xffff));
  875. value = 500 * internal * factor;
  876. dib7000p_write_word(state, 10, (u16) ((value >> 16) & 0xffff));
  877. dib7000p_write_word(state, 11, (u16) (value & 0xffff));
  878. value = dib7000p_read_word(state, 0);
  879. dib7000p_write_word(state, 0, (u16) ((1 << 9) | value));
  880. dib7000p_read_word(state, 1284);
  881. dib7000p_write_word(state, 0, (u16) value);
  882. return 0;
  883. }
  884. static int dib7000p_autosearch_is_irq(struct dvb_frontend *demod)
  885. {
  886. struct dib7000p_state *state = demod->demodulator_priv;
  887. u16 irq_pending = dib7000p_read_word(state, 1284);
  888. if (irq_pending & 0x1)
  889. return 1;
  890. if (irq_pending & 0x2)
  891. return 2;
  892. return 0;
  893. }
  894. static void dib7000p_spur_protect(struct dib7000p_state *state, u32 rf_khz, u32 bw)
  895. {
  896. static s16 notch[] = { 16143, 14402, 12238, 9713, 6902, 3888, 759, -2392 };
  897. static u8 sine[] = { 0, 2, 3, 5, 6, 8, 9, 11, 13, 14, 16, 17, 19, 20, 22,
  898. 24, 25, 27, 28, 30, 31, 33, 34, 36, 38, 39, 41, 42, 44, 45, 47, 48, 50, 51,
  899. 53, 55, 56, 58, 59, 61, 62, 64, 65, 67, 68, 70, 71, 73, 74, 76, 77, 79, 80,
  900. 82, 83, 85, 86, 88, 89, 91, 92, 94, 95, 97, 98, 99, 101, 102, 104, 105,
  901. 107, 108, 109, 111, 112, 114, 115, 117, 118, 119, 121, 122, 123, 125, 126,
  902. 128, 129, 130, 132, 133, 134, 136, 137, 138, 140, 141, 142, 144, 145, 146,
  903. 147, 149, 150, 151, 152, 154, 155, 156, 157, 159, 160, 161, 162, 164, 165,
  904. 166, 167, 168, 170, 171, 172, 173, 174, 175, 177, 178, 179, 180, 181, 182,
  905. 183, 184, 185, 186, 188, 189, 190, 191, 192, 193, 194, 195, 196, 197, 198,
  906. 199, 200, 201, 202, 203, 204, 205, 206, 207, 207, 208, 209, 210, 211, 212,
  907. 213, 214, 215, 215, 216, 217, 218, 219, 220, 220, 221, 222, 223, 224, 224,
  908. 225, 226, 227, 227, 228, 229, 229, 230, 231, 231, 232, 233, 233, 234, 235,
  909. 235, 236, 237, 237, 238, 238, 239, 239, 240, 241, 241, 242, 242, 243, 243,
  910. 244, 244, 245, 245, 245, 246, 246, 247, 247, 248, 248, 248, 249, 249, 249,
  911. 250, 250, 250, 251, 251, 251, 252, 252, 252, 252, 253, 253, 253, 253, 254,
  912. 254, 254, 254, 254, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
  913. 255, 255, 255, 255, 255, 255
  914. };
  915. u32 xtal = state->cfg.bw->xtal_hz / 1000;
  916. int f_rel = DIV_ROUND_CLOSEST(rf_khz, xtal) * xtal - rf_khz;
  917. int k;
  918. int coef_re[8], coef_im[8];
  919. int bw_khz = bw;
  920. u32 pha;
  921. dprintk("relative position of the Spur: %dk (RF: %dk, XTAL: %dk)", f_rel, rf_khz, xtal);
  922. if (f_rel < -bw_khz / 2 || f_rel > bw_khz / 2)
  923. return;
  924. bw_khz /= 100;
  925. dib7000p_write_word(state, 142, 0x0610);
  926. for (k = 0; k < 8; k++) {
  927. pha = ((f_rel * (k + 1) * 112 * 80 / bw_khz) / 1000) & 0x3ff;
  928. if (pha == 0) {
  929. coef_re[k] = 256;
  930. coef_im[k] = 0;
  931. } else if (pha < 256) {
  932. coef_re[k] = sine[256 - (pha & 0xff)];
  933. coef_im[k] = sine[pha & 0xff];
  934. } else if (pha == 256) {
  935. coef_re[k] = 0;
  936. coef_im[k] = 256;
  937. } else if (pha < 512) {
  938. coef_re[k] = -sine[pha & 0xff];
  939. coef_im[k] = sine[256 - (pha & 0xff)];
  940. } else if (pha == 512) {
  941. coef_re[k] = -256;
  942. coef_im[k] = 0;
  943. } else if (pha < 768) {
  944. coef_re[k] = -sine[256 - (pha & 0xff)];
  945. coef_im[k] = -sine[pha & 0xff];
  946. } else if (pha == 768) {
  947. coef_re[k] = 0;
  948. coef_im[k] = -256;
  949. } else {
  950. coef_re[k] = sine[pha & 0xff];
  951. coef_im[k] = -sine[256 - (pha & 0xff)];
  952. }
  953. coef_re[k] *= notch[k];
  954. coef_re[k] += (1 << 14);
  955. if (coef_re[k] >= (1 << 24))
  956. coef_re[k] = (1 << 24) - 1;
  957. coef_re[k] /= (1 << 15);
  958. coef_im[k] *= notch[k];
  959. coef_im[k] += (1 << 14);
  960. if (coef_im[k] >= (1 << 24))
  961. coef_im[k] = (1 << 24) - 1;
  962. coef_im[k] /= (1 << 15);
  963. dprintk("PALF COEF: %d re: %d im: %d", k, coef_re[k], coef_im[k]);
  964. dib7000p_write_word(state, 143, (0 << 14) | (k << 10) | (coef_re[k] & 0x3ff));
  965. dib7000p_write_word(state, 144, coef_im[k] & 0x3ff);
  966. dib7000p_write_word(state, 143, (1 << 14) | (k << 10) | (coef_re[k] & 0x3ff));
  967. }
  968. dib7000p_write_word(state, 143, 0);
  969. }
  970. static int dib7000p_tune(struct dvb_frontend *demod, struct dvb_frontend_parameters *ch)
  971. {
  972. struct dib7000p_state *state = demod->demodulator_priv;
  973. u16 tmp = 0;
  974. if (ch != NULL)
  975. dib7000p_set_channel(state, ch, 0);
  976. else
  977. return -EINVAL;
  978. // restart demod
  979. dib7000p_write_word(state, 770, 0x4000);
  980. dib7000p_write_word(state, 770, 0x0000);
  981. msleep(45);
  982. /* P_ctrl_inh_cor=0, P_ctrl_alpha_cor=4, P_ctrl_inh_isi=0, P_ctrl_alpha_isi=3, P_ctrl_inh_cor4=1, P_ctrl_alpha_cor4=3 */
  983. tmp = (0 << 14) | (4 << 10) | (0 << 9) | (3 << 5) | (1 << 4) | (0x3);
  984. if (state->sfn_workaround_active) {
  985. dprintk("SFN workaround is active");
  986. tmp |= (1 << 9);
  987. dib7000p_write_word(state, 166, 0x4000);
  988. } else {
  989. dib7000p_write_word(state, 166, 0x0000);
  990. }
  991. dib7000p_write_word(state, 29, tmp);
  992. // never achieved a lock with that bandwidth so far - wait for osc-freq to update
  993. if (state->timf == 0)
  994. msleep(200);
  995. /* offset loop parameters */
  996. /* P_timf_alpha, P_corm_alpha=6, P_corm_thres=0x80 */
  997. tmp = (6 << 8) | 0x80;
  998. switch (ch->u.ofdm.transmission_mode) {
  999. case TRANSMISSION_MODE_2K:
  1000. tmp |= (2 << 12);
  1001. break;
  1002. case TRANSMISSION_MODE_4K:
  1003. tmp |= (3 << 12);
  1004. break;
  1005. default:
  1006. case TRANSMISSION_MODE_8K:
  1007. tmp |= (4 << 12);
  1008. break;
  1009. }
  1010. dib7000p_write_word(state, 26, tmp); /* timf_a(6xxx) */
  1011. /* P_ctrl_freeze_pha_shift=0, P_ctrl_pha_off_max */
  1012. tmp = (0 << 4);
  1013. switch (ch->u.ofdm.transmission_mode) {
  1014. case TRANSMISSION_MODE_2K:
  1015. tmp |= 0x6;
  1016. break;
  1017. case TRANSMISSION_MODE_4K:
  1018. tmp |= 0x7;
  1019. break;
  1020. default:
  1021. case TRANSMISSION_MODE_8K:
  1022. tmp |= 0x8;
  1023. break;
  1024. }
  1025. dib7000p_write_word(state, 32, tmp);
  1026. /* P_ctrl_sfreq_inh=0, P_ctrl_sfreq_step */
  1027. tmp = (0 << 4);
  1028. switch (ch->u.ofdm.transmission_mode) {
  1029. case TRANSMISSION_MODE_2K:
  1030. tmp |= 0x6;
  1031. break;
  1032. case TRANSMISSION_MODE_4K:
  1033. tmp |= 0x7;
  1034. break;
  1035. default:
  1036. case TRANSMISSION_MODE_8K:
  1037. tmp |= 0x8;
  1038. break;
  1039. }
  1040. dib7000p_write_word(state, 33, tmp);
  1041. tmp = dib7000p_read_word(state, 509);
  1042. if (!((tmp >> 6) & 0x1)) {
  1043. /* restart the fec */
  1044. tmp = dib7000p_read_word(state, 771);
  1045. dib7000p_write_word(state, 771, tmp | (1 << 1));
  1046. dib7000p_write_word(state, 771, tmp);
  1047. msleep(40);
  1048. tmp = dib7000p_read_word(state, 509);
  1049. }
  1050. // we achieved a lock - it's time to update the osc freq
  1051. if ((tmp >> 6) & 0x1) {
  1052. dib7000p_update_timf(state);
  1053. /* P_timf_alpha += 2 */
  1054. tmp = dib7000p_read_word(state, 26);
  1055. dib7000p_write_word(state, 26, (tmp & ~(0xf << 12)) | ((((tmp >> 12) & 0xf) + 5) << 12));
  1056. }
  1057. if (state->cfg.spur_protect)
  1058. dib7000p_spur_protect(state, ch->frequency / 1000, BANDWIDTH_TO_KHZ(ch->u.ofdm.bandwidth));
  1059. dib7000p_set_bandwidth(state, BANDWIDTH_TO_KHZ(ch->u.ofdm.bandwidth));
  1060. return 0;
  1061. }
  1062. static int dib7000p_wakeup(struct dvb_frontend *demod)
  1063. {
  1064. struct dib7000p_state *state = demod->demodulator_priv;
  1065. dib7000p_set_power_mode(state, DIB7000P_POWER_ALL);
  1066. dib7000p_set_adc_state(state, DIBX000_SLOW_ADC_ON);
  1067. if (state->version == SOC7090)
  1068. dib7000p_sad_calib(state);
  1069. return 0;
  1070. }
  1071. static int dib7000p_sleep(struct dvb_frontend *demod)
  1072. {
  1073. struct dib7000p_state *state = demod->demodulator_priv;
  1074. if (state->version == SOC7090)
  1075. return dib7090_set_output_mode(demod, OUTMODE_HIGH_Z) | dib7000p_set_power_mode(state, DIB7000P_POWER_INTERFACE_ONLY);
  1076. return dib7000p_set_output_mode(state, OUTMODE_HIGH_Z) | dib7000p_set_power_mode(state, DIB7000P_POWER_INTERFACE_ONLY);
  1077. }
  1078. static int dib7000p_identify(struct dib7000p_state *st)
  1079. {
  1080. u16 value;
  1081. dprintk("checking demod on I2C address: %d (%x)", st->i2c_addr, st->i2c_addr);
  1082. if ((value = dib7000p_read_word(st, 768)) != 0x01b3) {
  1083. dprintk("wrong Vendor ID (read=0x%x)", value);
  1084. return -EREMOTEIO;
  1085. }
  1086. if ((value = dib7000p_read_word(st, 769)) != 0x4000) {
  1087. dprintk("wrong Device ID (%x)", value);
  1088. return -EREMOTEIO;
  1089. }
  1090. return 0;
  1091. }
  1092. static int dib7000p_get_frontend(struct dvb_frontend *fe, struct dvb_frontend_parameters *fep)
  1093. {
  1094. struct dib7000p_state *state = fe->demodulator_priv;
  1095. u16 tps = dib7000p_read_word(state, 463);
  1096. fep->inversion = INVERSION_AUTO;
  1097. fep->u.ofdm.bandwidth = BANDWIDTH_TO_INDEX(state->current_bandwidth);
  1098. switch ((tps >> 8) & 0x3) {
  1099. case 0:
  1100. fep->u.ofdm.transmission_mode = TRANSMISSION_MODE_2K;
  1101. break;
  1102. case 1:
  1103. fep->u.ofdm.transmission_mode = TRANSMISSION_MODE_8K;
  1104. break;
  1105. /* case 2: fep->u.ofdm.transmission_mode = TRANSMISSION_MODE_4K; break; */
  1106. }
  1107. switch (tps & 0x3) {
  1108. case 0:
  1109. fep->u.ofdm.guard_interval = GUARD_INTERVAL_1_32;
  1110. break;
  1111. case 1:
  1112. fep->u.ofdm.guard_interval = GUARD_INTERVAL_1_16;
  1113. break;
  1114. case 2:
  1115. fep->u.ofdm.guard_interval = GUARD_INTERVAL_1_8;
  1116. break;
  1117. case 3:
  1118. fep->u.ofdm.guard_interval = GUARD_INTERVAL_1_4;
  1119. break;
  1120. }
  1121. switch ((tps >> 14) & 0x3) {
  1122. case 0:
  1123. fep->u.ofdm.constellation = QPSK;
  1124. break;
  1125. case 1:
  1126. fep->u.ofdm.constellation = QAM_16;
  1127. break;
  1128. case 2:
  1129. default:
  1130. fep->u.ofdm.constellation = QAM_64;
  1131. break;
  1132. }
  1133. /* as long as the frontend_param structure is fixed for hierarchical transmission I refuse to use it */
  1134. /* (tps >> 13) & 0x1 == hrch is used, (tps >> 10) & 0x7 == alpha */
  1135. fep->u.ofdm.hierarchy_information = HIERARCHY_NONE;
  1136. switch ((tps >> 5) & 0x7) {
  1137. case 1:
  1138. fep->u.ofdm.code_rate_HP = FEC_1_2;
  1139. break;
  1140. case 2:
  1141. fep->u.ofdm.code_rate_HP = FEC_2_3;
  1142. break;
  1143. case 3:
  1144. fep->u.ofdm.code_rate_HP = FEC_3_4;
  1145. break;
  1146. case 5:
  1147. fep->u.ofdm.code_rate_HP = FEC_5_6;
  1148. break;
  1149. case 7:
  1150. default:
  1151. fep->u.ofdm.code_rate_HP = FEC_7_8;
  1152. break;
  1153. }
  1154. switch ((tps >> 2) & 0x7) {
  1155. case 1:
  1156. fep->u.ofdm.code_rate_LP = FEC_1_2;
  1157. break;
  1158. case 2:
  1159. fep->u.ofdm.code_rate_LP = FEC_2_3;
  1160. break;
  1161. case 3:
  1162. fep->u.ofdm.code_rate_LP = FEC_3_4;
  1163. break;
  1164. case 5:
  1165. fep->u.ofdm.code_rate_LP = FEC_5_6;
  1166. break;
  1167. case 7:
  1168. default:
  1169. fep->u.ofdm.code_rate_LP = FEC_7_8;
  1170. break;
  1171. }
  1172. /* native interleaver: (dib7000p_read_word(state, 464) >> 5) & 0x1 */
  1173. return 0;
  1174. }
  1175. static int dib7000p_set_frontend(struct dvb_frontend *fe, struct dvb_frontend_parameters *fep)
  1176. {
  1177. struct dib7000p_state *state = fe->demodulator_priv;
  1178. int time, ret;
  1179. if (state->version == SOC7090) {
  1180. dib7090_set_diversity_in(fe, 0);
  1181. dib7090_set_output_mode(fe, OUTMODE_HIGH_Z);
  1182. } else
  1183. dib7000p_set_output_mode(state, OUTMODE_HIGH_Z);
  1184. /* maybe the parameter has been changed */
  1185. state->sfn_workaround_active = buggy_sfn_workaround;
  1186. if (fe->ops.tuner_ops.set_params)
  1187. fe->ops.tuner_ops.set_params(fe, fep);
  1188. /* start up the AGC */
  1189. state->agc_state = 0;
  1190. do {
  1191. time = dib7000p_agc_startup(fe, fep);
  1192. if (time != -1)
  1193. msleep(time);
  1194. } while (time != -1);
  1195. if (fep->u.ofdm.transmission_mode == TRANSMISSION_MODE_AUTO ||
  1196. fep->u.ofdm.guard_interval == GUARD_INTERVAL_AUTO || fep->u.ofdm.constellation == QAM_AUTO || fep->u.ofdm.code_rate_HP == FEC_AUTO) {
  1197. int i = 800, found;
  1198. dib7000p_autosearch_start(fe, fep);
  1199. do {
  1200. msleep(1);
  1201. found = dib7000p_autosearch_is_irq(fe);
  1202. } while (found == 0 && i--);
  1203. dprintk("autosearch returns: %d", found);
  1204. if (found == 0 || found == 1)
  1205. return 0;
  1206. dib7000p_get_frontend(fe, fep);
  1207. }
  1208. ret = dib7000p_tune(fe, fep);
  1209. /* make this a config parameter */
  1210. if (state->version == SOC7090)
  1211. dib7090_set_output_mode(fe, state->cfg.output_mode);
  1212. else
  1213. dib7000p_set_output_mode(state, state->cfg.output_mode);
  1214. return ret;
  1215. }
  1216. static int dib7000p_read_status(struct dvb_frontend *fe, fe_status_t * stat)
  1217. {
  1218. struct dib7000p_state *state = fe->demodulator_priv;
  1219. u16 lock = dib7000p_read_word(state, 509);
  1220. *stat = 0;
  1221. if (lock & 0x8000)
  1222. *stat |= FE_HAS_SIGNAL;
  1223. if (lock & 0x3000)
  1224. *stat |= FE_HAS_CARRIER;
  1225. if (lock & 0x0100)
  1226. *stat |= FE_HAS_VITERBI;
  1227. if (lock & 0x0010)
  1228. *stat |= FE_HAS_SYNC;
  1229. if ((lock & 0x0038) == 0x38)
  1230. *stat |= FE_HAS_LOCK;
  1231. return 0;
  1232. }
  1233. static int dib7000p_read_ber(struct dvb_frontend *fe, u32 * ber)
  1234. {
  1235. struct dib7000p_state *state = fe->demodulator_priv;
  1236. *ber = (dib7000p_read_word(state, 500) << 16) | dib7000p_read_word(state, 501);
  1237. return 0;
  1238. }
  1239. static int dib7000p_read_unc_blocks(struct dvb_frontend *fe, u32 * unc)
  1240. {
  1241. struct dib7000p_state *state = fe->demodulator_priv;
  1242. *unc = dib7000p_read_word(state, 506);
  1243. return 0;
  1244. }
  1245. static int dib7000p_read_signal_strength(struct dvb_frontend *fe, u16 * strength)
  1246. {
  1247. struct dib7000p_state *state = fe->demodulator_priv;
  1248. u16 val = dib7000p_read_word(state, 394);
  1249. *strength = 65535 - val;
  1250. return 0;
  1251. }
  1252. static int dib7000p_read_snr(struct dvb_frontend *fe, u16 * snr)
  1253. {
  1254. struct dib7000p_state *state = fe->demodulator_priv;
  1255. u16 val;
  1256. s32 signal_mant, signal_exp, noise_mant, noise_exp;
  1257. u32 result = 0;
  1258. val = dib7000p_read_word(state, 479);
  1259. noise_mant = (val >> 4) & 0xff;
  1260. noise_exp = ((val & 0xf) << 2);
  1261. val = dib7000p_read_word(state, 480);
  1262. noise_exp += ((val >> 14) & 0x3);
  1263. if ((noise_exp & 0x20) != 0)
  1264. noise_exp -= 0x40;
  1265. signal_mant = (val >> 6) & 0xFF;
  1266. signal_exp = (val & 0x3F);
  1267. if ((signal_exp & 0x20) != 0)
  1268. signal_exp -= 0x40;
  1269. if (signal_mant != 0)
  1270. result = intlog10(2) * 10 * signal_exp + 10 * intlog10(signal_mant);
  1271. else
  1272. result = intlog10(2) * 10 * signal_exp - 100;
  1273. if (noise_mant != 0)
  1274. result -= intlog10(2) * 10 * noise_exp + 10 * intlog10(noise_mant);
  1275. else
  1276. result -= intlog10(2) * 10 * noise_exp - 100;
  1277. *snr = result / ((1 << 24) / 10);
  1278. return 0;
  1279. }
  1280. static int dib7000p_fe_get_tune_settings(struct dvb_frontend *fe, struct dvb_frontend_tune_settings *tune)
  1281. {
  1282. tune->min_delay_ms = 1000;
  1283. return 0;
  1284. }
  1285. static void dib7000p_release(struct dvb_frontend *demod)
  1286. {
  1287. struct dib7000p_state *st = demod->demodulator_priv;
  1288. dibx000_exit_i2c_master(&st->i2c_master);
  1289. i2c_del_adapter(&st->dib7090_tuner_adap);
  1290. kfree(st);
  1291. }
  1292. int dib7000pc_detection(struct i2c_adapter *i2c_adap)
  1293. {
  1294. u8 tx[2], rx[2];
  1295. struct i2c_msg msg[2] = {
  1296. {.addr = 18 >> 1, .flags = 0, .buf = tx, .len = 2},
  1297. {.addr = 18 >> 1, .flags = I2C_M_RD, .buf = rx, .len = 2},
  1298. };
  1299. tx[0] = 0x03;
  1300. tx[1] = 0x00;
  1301. if (i2c_transfer(i2c_adap, msg, 2) == 2)
  1302. if (rx[0] == 0x01 && rx[1] == 0xb3) {
  1303. dprintk("-D- DiB7000PC detected");
  1304. return 1;
  1305. }
  1306. msg[0].addr = msg[1].addr = 0x40;
  1307. if (i2c_transfer(i2c_adap, msg, 2) == 2)
  1308. if (rx[0] == 0x01 && rx[1] == 0xb3) {
  1309. dprintk("-D- DiB7000PC detected");
  1310. return 1;
  1311. }
  1312. dprintk("-D- DiB7000PC not detected");
  1313. return 0;
  1314. }
  1315. EXPORT_SYMBOL(dib7000pc_detection);
  1316. struct i2c_adapter *dib7000p_get_i2c_master(struct dvb_frontend *demod, enum dibx000_i2c_interface intf, int gating)
  1317. {
  1318. struct dib7000p_state *st = demod->demodulator_priv;
  1319. return dibx000_get_i2c_adapter(&st->i2c_master, intf, gating);
  1320. }
  1321. EXPORT_SYMBOL(dib7000p_get_i2c_master);
  1322. int dib7000p_pid_filter_ctrl(struct dvb_frontend *fe, u8 onoff)
  1323. {
  1324. struct dib7000p_state *state = fe->demodulator_priv;
  1325. u16 val = dib7000p_read_word(state, 235) & 0xffef;
  1326. val |= (onoff & 0x1) << 4;
  1327. dprintk("PID filter enabled %d", onoff);
  1328. return dib7000p_write_word(state, 235, val);
  1329. }
  1330. EXPORT_SYMBOL(dib7000p_pid_filter_ctrl);
  1331. int dib7000p_pid_filter(struct dvb_frontend *fe, u8 id, u16 pid, u8 onoff)
  1332. {
  1333. struct dib7000p_state *state = fe->demodulator_priv;
  1334. dprintk("PID filter: index %x, PID %d, OnOff %d", id, pid, onoff);
  1335. return dib7000p_write_word(state, 241 + id, onoff ? (1 << 13) | pid : 0);
  1336. }
  1337. EXPORT_SYMBOL(dib7000p_pid_filter);
  1338. int dib7000p_i2c_enumeration(struct i2c_adapter *i2c, int no_of_demods, u8 default_addr, struct dib7000p_config cfg[])
  1339. {
  1340. struct dib7000p_state *dpst;
  1341. int k = 0;
  1342. u8 new_addr = 0;
  1343. dpst = kzalloc(sizeof(struct dib7000p_state), GFP_KERNEL);
  1344. if (!dpst)
  1345. return -ENOMEM;
  1346. dpst->i2c_adap = i2c;
  1347. for (k = no_of_demods - 1; k >= 0; k--) {
  1348. dpst->cfg = cfg[k];
  1349. /* designated i2c address */
  1350. if (cfg[k].default_i2c_addr != 0)
  1351. new_addr = cfg[k].default_i2c_addr + (k << 1);
  1352. else
  1353. new_addr = (0x40 + k) << 1;
  1354. dpst->i2c_addr = new_addr;
  1355. dib7000p_write_word(dpst, 1287, 0x0003); /* sram lead in, rdy */
  1356. if (dib7000p_identify(dpst) != 0) {
  1357. dpst->i2c_addr = default_addr;
  1358. dib7000p_write_word(dpst, 1287, 0x0003); /* sram lead in, rdy */
  1359. if (dib7000p_identify(dpst) != 0) {
  1360. dprintk("DiB7000P #%d: not identified\n", k);
  1361. kfree(dpst);
  1362. return -EIO;
  1363. }
  1364. }
  1365. /* start diversity to pull_down div_str - just for i2c-enumeration */
  1366. dib7000p_set_output_mode(dpst, OUTMODE_DIVERSITY);
  1367. /* set new i2c address and force divstart */
  1368. dib7000p_write_word(dpst, 1285, (new_addr << 2) | 0x2);
  1369. dprintk("IC %d initialized (to i2c_address 0x%x)", k, new_addr);
  1370. }
  1371. for (k = 0; k < no_of_demods; k++) {
  1372. dpst->cfg = cfg[k];
  1373. if (cfg[k].default_i2c_addr != 0)
  1374. dpst->i2c_addr = (cfg[k].default_i2c_addr + k) << 1;
  1375. else
  1376. dpst->i2c_addr = (0x40 + k) << 1;
  1377. // unforce divstr
  1378. dib7000p_write_word(dpst, 1285, dpst->i2c_addr << 2);
  1379. /* deactivate div - it was just for i2c-enumeration */
  1380. dib7000p_set_output_mode(dpst, OUTMODE_HIGH_Z);
  1381. }
  1382. kfree(dpst);
  1383. return 0;
  1384. }
  1385. EXPORT_SYMBOL(dib7000p_i2c_enumeration);
  1386. static const s32 lut_1000ln_mant[] = {
  1387. 6908, 6956, 7003, 7047, 7090, 7131, 7170, 7208, 7244, 7279, 7313, 7346, 7377, 7408, 7438, 7467, 7495, 7523, 7549, 7575, 7600
  1388. };
  1389. static s32 dib7000p_get_adc_power(struct dvb_frontend *fe)
  1390. {
  1391. struct dib7000p_state *state = fe->demodulator_priv;
  1392. u32 tmp_val = 0, exp = 0, mant = 0;
  1393. s32 pow_i;
  1394. u16 buf[2];
  1395. u8 ix = 0;
  1396. buf[0] = dib7000p_read_word(state, 0x184);
  1397. buf[1] = dib7000p_read_word(state, 0x185);
  1398. pow_i = (buf[0] << 16) | buf[1];
  1399. dprintk("raw pow_i = %d", pow_i);
  1400. tmp_val = pow_i;
  1401. while (tmp_val >>= 1)
  1402. exp++;
  1403. mant = (pow_i * 1000 / (1 << exp));
  1404. dprintk(" mant = %d exp = %d", mant / 1000, exp);
  1405. ix = (u8) ((mant - 1000) / 100); /* index of the LUT */
  1406. dprintk(" ix = %d", ix);
  1407. pow_i = (lut_1000ln_mant[ix] + 693 * (exp - 20) - 6908);
  1408. pow_i = (pow_i << 8) / 1000;
  1409. dprintk(" pow_i = %d", pow_i);
  1410. return pow_i;
  1411. }
  1412. static int map_addr_to_serpar_number(struct i2c_msg *msg)
  1413. {
  1414. if ((msg->buf[0] <= 15))
  1415. msg->buf[0] -= 1;
  1416. else if (msg->buf[0] == 17)
  1417. msg->buf[0] = 15;
  1418. else if (msg->buf[0] == 16)
  1419. msg->buf[0] = 17;
  1420. else if (msg->buf[0] == 19)
  1421. msg->buf[0] = 16;
  1422. else if (msg->buf[0] >= 21 && msg->buf[0] <= 25)
  1423. msg->buf[0] -= 3;
  1424. else if (msg->buf[0] == 28)
  1425. msg->buf[0] = 23;
  1426. else
  1427. return -EINVAL;
  1428. return 0;
  1429. }
  1430. static int w7090p_tuner_write_serpar(struct i2c_adapter *i2c_adap, struct i2c_msg msg[], int num)
  1431. {
  1432. struct dib7000p_state *state = i2c_get_adapdata(i2c_adap);
  1433. u8 n_overflow = 1;
  1434. u16 i = 1000;
  1435. u16 serpar_num = msg[0].buf[0];
  1436. while (n_overflow == 1 && i) {
  1437. n_overflow = (dib7000p_read_word(state, 1984) >> 1) & 0x1;
  1438. i--;
  1439. if (i == 0)
  1440. dprintk("Tuner ITF: write busy (overflow)");
  1441. }
  1442. dib7000p_write_word(state, 1985, (1 << 6) | (serpar_num & 0x3f));
  1443. dib7000p_write_word(state, 1986, (msg[0].buf[1] << 8) | msg[0].buf[2]);
  1444. return num;
  1445. }
  1446. static int w7090p_tuner_read_serpar(struct i2c_adapter *i2c_adap, struct i2c_msg msg[], int num)
  1447. {
  1448. struct dib7000p_state *state = i2c_get_adapdata(i2c_adap);
  1449. u8 n_overflow = 1, n_empty = 1;
  1450. u16 i = 1000;
  1451. u16 serpar_num = msg[0].buf[0];
  1452. u16 read_word;
  1453. while (n_overflow == 1 && i) {
  1454. n_overflow = (dib7000p_read_word(state, 1984) >> 1) & 0x1;
  1455. i--;
  1456. if (i == 0)
  1457. dprintk("TunerITF: read busy (overflow)");
  1458. }
  1459. dib7000p_write_word(state, 1985, (0 << 6) | (serpar_num & 0x3f));
  1460. i = 1000;
  1461. while (n_empty == 1 && i) {
  1462. n_empty = dib7000p_read_word(state, 1984) & 0x1;
  1463. i--;
  1464. if (i == 0)
  1465. dprintk("TunerITF: read busy (empty)");
  1466. }
  1467. read_word = dib7000p_read_word(state, 1987);
  1468. msg[1].buf[0] = (read_word >> 8) & 0xff;
  1469. msg[1].buf[1] = (read_word) & 0xff;
  1470. return num;
  1471. }
  1472. static int w7090p_tuner_rw_serpar(struct i2c_adapter *i2c_adap, struct i2c_msg msg[], int num)
  1473. {
  1474. if (map_addr_to_serpar_number(&msg[0]) == 0) { /* else = Tuner regs to ignore : DIG_CFG, CTRL_RF_LT, PLL_CFG, PWM1_REG, ADCCLK, DIG_CFG_3; SLEEP_EN... */
  1475. if (num == 1) { /* write */
  1476. return w7090p_tuner_write_serpar(i2c_adap, msg, 1);
  1477. } else { /* read */
  1478. return w7090p_tuner_read_serpar(i2c_adap, msg, 2);
  1479. }
  1480. }
  1481. return num;
  1482. }
  1483. int dib7090p_rw_on_apb(struct i2c_adapter *i2c_adap, struct i2c_msg msg[], int num, u16 apb_address)
  1484. {
  1485. struct dib7000p_state *state = i2c_get_adapdata(i2c_adap);
  1486. u16 word;
  1487. if (num == 1) { /* write */
  1488. dib7000p_write_word(state, apb_address, ((msg[0].buf[1] << 8) | (msg[0].buf[2])));
  1489. } else {
  1490. word = dib7000p_read_word(state, apb_address);
  1491. msg[1].buf[0] = (word >> 8) & 0xff;
  1492. msg[1].buf[1] = (word) & 0xff;
  1493. }
  1494. return num;
  1495. }
  1496. static int dib7090_tuner_xfer(struct i2c_adapter *i2c_adap, struct i2c_msg msg[], int num)
  1497. {
  1498. struct dib7000p_state *state = i2c_get_adapdata(i2c_adap);
  1499. u16 apb_address = 0, word;
  1500. int i = 0;
  1501. switch (msg[0].buf[0]) {
  1502. case 0x12:
  1503. apb_address = 1920;
  1504. break;
  1505. case 0x14:
  1506. apb_address = 1921;
  1507. break;
  1508. case 0x24:
  1509. apb_address = 1922;
  1510. break;
  1511. case 0x1a:
  1512. apb_address = 1923;
  1513. break;
  1514. case 0x22:
  1515. apb_address = 1924;
  1516. break;
  1517. case 0x33:
  1518. apb_address = 1926;
  1519. break;
  1520. case 0x34:
  1521. apb_address = 1927;
  1522. break;
  1523. case 0x35:
  1524. apb_address = 1928;
  1525. break;
  1526. case 0x36:
  1527. apb_address = 1929;
  1528. break;
  1529. case 0x37:
  1530. apb_address = 1930;
  1531. break;
  1532. case 0x38:
  1533. apb_address = 1931;
  1534. break;
  1535. case 0x39:
  1536. apb_address = 1932;
  1537. break;
  1538. case 0x2a:
  1539. apb_address = 1935;
  1540. break;
  1541. case 0x2b:
  1542. apb_address = 1936;
  1543. break;
  1544. case 0x2c:
  1545. apb_address = 1937;
  1546. break;
  1547. case 0x2d:
  1548. apb_address = 1938;
  1549. break;
  1550. case 0x2e:
  1551. apb_address = 1939;
  1552. break;
  1553. case 0x2f:
  1554. apb_address = 1940;
  1555. break;
  1556. case 0x30:
  1557. apb_address = 1941;
  1558. break;
  1559. case 0x31:
  1560. apb_address = 1942;
  1561. break;
  1562. case 0x32:
  1563. apb_address = 1943;
  1564. break;
  1565. case 0x3e:
  1566. apb_address = 1944;
  1567. break;
  1568. case 0x3f:
  1569. apb_address = 1945;
  1570. break;
  1571. case 0x40:
  1572. apb_address = 1948;
  1573. break;
  1574. case 0x25:
  1575. apb_address = 914;
  1576. break;
  1577. case 0x26:
  1578. apb_address = 915;
  1579. break;
  1580. case 0x27:
  1581. apb_address = 916;
  1582. break;
  1583. case 0x28:
  1584. apb_address = 917;
  1585. break;
  1586. case 0x1d:
  1587. i = ((dib7000p_read_word(state, 72) >> 12) & 0x3);
  1588. word = dib7000p_read_word(state, 384 + i);
  1589. msg[1].buf[0] = (word >> 8) & 0xff;
  1590. msg[1].buf[1] = (word) & 0xff;
  1591. return num;
  1592. case 0x1f:
  1593. if (num == 1) { /* write */
  1594. word = (u16) ((msg[0].buf[1] << 8) | msg[0].buf[2]);
  1595. word &= 0x3;
  1596. word = (dib7000p_read_word(state, 72) & ~(3 << 12)) | (word << 12);
  1597. dib7000p_write_word(state, 72, word); /* Set the proper input */
  1598. return num;
  1599. }
  1600. }
  1601. if (apb_address != 0) /* R/W acces via APB */
  1602. return dib7090p_rw_on_apb(i2c_adap, msg, num, apb_address);
  1603. else /* R/W access via SERPAR */
  1604. return w7090p_tuner_rw_serpar(i2c_adap, msg, num);
  1605. return 0;
  1606. }
  1607. static u32 dib7000p_i2c_func(struct i2c_adapter *adapter)
  1608. {
  1609. return I2C_FUNC_I2C;
  1610. }
  1611. static struct i2c_algorithm dib7090_tuner_xfer_algo = {
  1612. .master_xfer = dib7090_tuner_xfer,
  1613. .functionality = dib7000p_i2c_func,
  1614. };
  1615. struct i2c_adapter *dib7090_get_i2c_tuner(struct dvb_frontend *fe)
  1616. {
  1617. struct dib7000p_state *st = fe->demodulator_priv;
  1618. return &st->dib7090_tuner_adap;
  1619. }
  1620. EXPORT_SYMBOL(dib7090_get_i2c_tuner);
  1621. static int dib7090_host_bus_drive(struct dib7000p_state *state, u8 drive)
  1622. {
  1623. u16 reg;
  1624. /* drive host bus 2, 3, 4 */
  1625. reg = dib7000p_read_word(state, 1798) & ~((0x7) | (0x7 << 6) | (0x7 << 12));
  1626. reg |= (drive << 12) | (drive << 6) | drive;
  1627. dib7000p_write_word(state, 1798, reg);
  1628. /* drive host bus 5,6 */
  1629. reg = dib7000p_read_word(state, 1799) & ~((0x7 << 2) | (0x7 << 8));
  1630. reg |= (drive << 8) | (drive << 2);
  1631. dib7000p_write_word(state, 1799, reg);
  1632. /* drive host bus 7, 8, 9 */
  1633. reg = dib7000p_read_word(state, 1800) & ~((0x7) | (0x7 << 6) | (0x7 << 12));
  1634. reg |= (drive << 12) | (drive << 6) | drive;
  1635. dib7000p_write_word(state, 1800, reg);
  1636. /* drive host bus 10, 11 */
  1637. reg = dib7000p_read_word(state, 1801) & ~((0x7 << 2) | (0x7 << 8));
  1638. reg |= (drive << 8) | (drive << 2);
  1639. dib7000p_write_word(state, 1801, reg);
  1640. /* drive host bus 12, 13, 14 */
  1641. reg = dib7000p_read_word(state, 1802) & ~((0x7) | (0x7 << 6) | (0x7 << 12));
  1642. reg |= (drive << 12) | (drive << 6) | drive;
  1643. dib7000p_write_word(state, 1802, reg);
  1644. return 0;
  1645. }
  1646. static u32 dib7090_calcSyncFreq(u32 P_Kin, u32 P_Kout, u32 insertExtSynchro, u32 syncSize)
  1647. {
  1648. u32 quantif = 3;
  1649. u32 nom = (insertExtSynchro * P_Kin + syncSize);
  1650. u32 denom = P_Kout;
  1651. u32 syncFreq = ((nom << quantif) / denom);
  1652. if ((syncFreq & ((1 << quantif) - 1)) != 0)
  1653. syncFreq = (syncFreq >> quantif) + 1;
  1654. else
  1655. syncFreq = (syncFreq >> quantif);
  1656. if (syncFreq != 0)
  1657. syncFreq = syncFreq - 1;
  1658. return syncFreq;
  1659. }
  1660. static int dib7090_cfg_DibTx(struct dib7000p_state *state, u32 P_Kin, u32 P_Kout, u32 insertExtSynchro, u32 synchroMode, u32 syncWord, u32 syncSize)
  1661. {
  1662. u8 index_buf;
  1663. u16 rx_copy_buf[22];
  1664. dprintk("Configure DibStream Tx");
  1665. for (index_buf = 0; index_buf < 22; index_buf++)
  1666. rx_copy_buf[index_buf] = dib7000p_read_word(state, 1536+index_buf);
  1667. dib7000p_write_word(state, 1615, 1);
  1668. dib7000p_write_word(state, 1603, P_Kin);
  1669. dib7000p_write_word(state, 1605, P_Kout);
  1670. dib7000p_write_word(state, 1606, insertExtSynchro);
  1671. dib7000p_write_word(state, 1608, synchroMode);
  1672. dib7000p_write_word(state, 1609, (syncWord >> 16) & 0xffff);
  1673. dib7000p_write_word(state, 1610, syncWord & 0xffff);
  1674. dib7000p_write_word(state, 1612, syncSize);
  1675. dib7000p_write_word(state, 1615, 0);
  1676. for (index_buf = 0; index_buf < 22; index_buf++)
  1677. dib7000p_write_word(state, 1536+index_buf, rx_copy_buf[index_buf]);
  1678. return 0;
  1679. }
  1680. static int dib7090_cfg_DibRx(struct dib7000p_state *state, u32 P_Kin, u32 P_Kout, u32 synchroMode, u32 insertExtSynchro, u32 syncWord, u32 syncSize,
  1681. u32 dataOutRate)
  1682. {
  1683. u32 syncFreq;
  1684. dprintk("Configure DibStream Rx");
  1685. if ((P_Kin != 0) && (P_Kout != 0)) {
  1686. syncFreq = dib7090_calcSyncFreq(P_Kin, P_Kout, insertExtSynchro, syncSize);
  1687. dib7000p_write_word(state, 1542, syncFreq);
  1688. }
  1689. dib7000p_write_word(state, 1554, 1);
  1690. dib7000p_write_word(state, 1536, P_Kin);
  1691. dib7000p_write_word(state, 1537, P_Kout);
  1692. dib7000p_write_word(state, 1539, synchroMode);
  1693. dib7000p_write_word(state, 1540, (syncWord >> 16) & 0xffff);
  1694. dib7000p_write_word(state, 1541, syncWord & 0xffff);
  1695. dib7000p_write_word(state, 1543, syncSize);
  1696. dib7000p_write_word(state, 1544, dataOutRate);
  1697. dib7000p_write_word(state, 1554, 0);
  1698. return 0;
  1699. }
  1700. static int dib7090_enDivOnHostBus(struct dib7000p_state *state)
  1701. {
  1702. u16 reg;
  1703. dprintk("Enable Diversity on host bus");
  1704. reg = (1 << 8) | (1 << 5);
  1705. dib7000p_write_word(state, 1288, reg);
  1706. return dib7090_cfg_DibTx(state, 5, 5, 0, 0, 0, 0);
  1707. }
  1708. static int dib7090_enAdcOnHostBus(struct dib7000p_state *state)
  1709. {
  1710. u16 reg;
  1711. dprintk("Enable ADC on host bus");
  1712. reg = (1 << 7) | (1 << 5);
  1713. dib7000p_write_word(state, 1288, reg);
  1714. return dib7090_cfg_DibTx(state, 20, 5, 10, 0, 0, 0);
  1715. }
  1716. static int dib7090_enMpegOnHostBus(struct dib7000p_state *state)
  1717. {
  1718. u16 reg;
  1719. dprintk("Enable Mpeg on host bus");
  1720. reg = (1 << 9) | (1 << 5);
  1721. dib7000p_write_word(state, 1288, reg);
  1722. return dib7090_cfg_DibTx(state, 8, 5, 0, 0, 0, 0);
  1723. }
  1724. static int dib7090_enMpegInput(struct dib7000p_state *state)
  1725. {
  1726. dprintk("Enable Mpeg input");
  1727. return dib7090_cfg_DibRx(state, 8, 5, 0, 0, 0, 8, 0); /*outputRate = 8 */
  1728. }
  1729. static int dib7090_enMpegMux(struct dib7000p_state *state, u16 pulseWidth, u16 enSerialMode, u16 enSerialClkDiv2)
  1730. {
  1731. u16 reg = (1 << 7) | ((pulseWidth & 0x1f) << 2) | ((enSerialMode & 0x1) << 1) | (enSerialClkDiv2 & 0x1);
  1732. dprintk("Enable Mpeg mux");
  1733. dib7000p_write_word(state, 1287, reg);
  1734. reg &= ~(1 << 7);
  1735. dib7000p_write_word(state, 1287, reg);
  1736. reg = (1 << 4);
  1737. dib7000p_write_word(state, 1288, reg);
  1738. return 0;
  1739. }
  1740. static int dib7090_disableMpegMux(struct dib7000p_state *state)
  1741. {
  1742. u16 reg;
  1743. dprintk("Disable Mpeg mux");
  1744. dib7000p_write_word(state, 1288, 0);
  1745. reg = dib7000p_read_word(state, 1287);
  1746. reg &= ~(1 << 7);
  1747. dib7000p_write_word(state, 1287, reg);
  1748. return 0;
  1749. }
  1750. static int dib7090_set_input_mode(struct dvb_frontend *fe, int mode)
  1751. {
  1752. struct dib7000p_state *state = fe->demodulator_priv;
  1753. switch (mode) {
  1754. case INPUT_MODE_DIVERSITY:
  1755. dprintk("Enable diversity INPUT");
  1756. dib7090_cfg_DibRx(state, 5, 5, 0, 0, 0, 0, 0);
  1757. break;
  1758. case INPUT_MODE_MPEG:
  1759. dprintk("Enable Mpeg INPUT");
  1760. dib7090_cfg_DibRx(state, 8, 5, 0, 0, 0, 8, 0); /*outputRate = 8 */
  1761. break;
  1762. case INPUT_MODE_OFF:
  1763. default:
  1764. dprintk("Disable INPUT");
  1765. dib7090_cfg_DibRx(state, 0, 0, 0, 0, 0, 0, 0);
  1766. break;
  1767. }
  1768. return 0;
  1769. }
  1770. static int dib7090_set_diversity_in(struct dvb_frontend *fe, int onoff)
  1771. {
  1772. switch (onoff) {
  1773. case 0: /* only use the internal way - not the diversity input */
  1774. dib7090_set_input_mode(fe, INPUT_MODE_MPEG);
  1775. break;
  1776. case 1: /* both ways */
  1777. case 2: /* only the diversity input */
  1778. dib7090_set_input_mode(fe, INPUT_MODE_DIVERSITY);
  1779. break;
  1780. }
  1781. return 0;
  1782. }
  1783. static int dib7090_set_output_mode(struct dvb_frontend *fe, int mode)
  1784. {
  1785. struct dib7000p_state *state = fe->demodulator_priv;
  1786. u16 outreg, smo_mode, fifo_threshold;
  1787. u8 prefer_mpeg_mux_use = 1;
  1788. int ret = 0;
  1789. dib7090_host_bus_drive(state, 1);
  1790. fifo_threshold = 1792;
  1791. smo_mode = (dib7000p_read_word(state, 235) & 0x0050) | (1 << 1);
  1792. outreg = dib7000p_read_word(state, 1286) & ~((1 << 10) | (0x7 << 6) | (1 << 1));
  1793. switch (mode) {
  1794. case OUTMODE_HIGH_Z:
  1795. outreg = 0;
  1796. break;
  1797. case OUTMODE_MPEG2_SERIAL:
  1798. if (prefer_mpeg_mux_use) {
  1799. dprintk("Sip 7090P setting output mode TS_SERIAL using Mpeg Mux");
  1800. dib7090_enMpegOnHostBus(state);
  1801. dib7090_enMpegInput(state);
  1802. if (state->cfg.enMpegOutput == 1)
  1803. dib7090_enMpegMux(state, 3, 1, 1);
  1804. } else { /* Use Smooth block */
  1805. dprintk("Sip 7090P setting output mode TS_SERIAL using Smooth bloc");
  1806. dib7090_disableMpegMux(state);
  1807. dib7000p_write_word(state, 1288, (1 << 6));
  1808. outreg |= (2 << 6) | (0 << 1);
  1809. }
  1810. break;
  1811. case OUTMODE_MPEG2_PAR_GATED_CLK:
  1812. if (prefer_mpeg_mux_use) {
  1813. dprintk("Sip 7090P setting output mode TS_PARALLEL_GATED using Mpeg Mux");
  1814. dib7090_enMpegOnHostBus(state);
  1815. dib7090_enMpegInput(state);
  1816. if (state->cfg.enMpegOutput == 1)
  1817. dib7090_enMpegMux(state, 2, 0, 0);
  1818. } else { /* Use Smooth block */
  1819. dprintk("Sip 7090P setting output mode TS_PARALLEL_GATED using Smooth block");
  1820. dib7090_disableMpegMux(state);
  1821. dib7000p_write_word(state, 1288, (1 << 6));
  1822. outreg |= (0 << 6);
  1823. }
  1824. break;
  1825. case OUTMODE_MPEG2_PAR_CONT_CLK: /* Using Smooth block only */
  1826. dprintk("Sip 7090P setting output mode TS_PARALLEL_CONT using Smooth block");
  1827. dib7090_disableMpegMux(state);
  1828. dib7000p_write_word(state, 1288, (1 << 6));
  1829. outreg |= (1 << 6);
  1830. break;
  1831. case OUTMODE_MPEG2_FIFO: /* Using Smooth block because not supported by new Mpeg Mux bloc */
  1832. dprintk("Sip 7090P setting output mode TS_FIFO using Smooth block");
  1833. dib7090_disableMpegMux(state);
  1834. dib7000p_write_word(state, 1288, (1 << 6));
  1835. outreg |= (5 << 6);
  1836. smo_mode |= (3 << 1);
  1837. fifo_threshold = 512;
  1838. break;
  1839. case OUTMODE_DIVERSITY:
  1840. dprintk("Sip 7090P setting output mode MODE_DIVERSITY");
  1841. dib7090_disableMpegMux(state);
  1842. dib7090_enDivOnHostBus(state);
  1843. break;
  1844. case OUTMODE_ANALOG_ADC:
  1845. dprintk("Sip 7090P setting output mode MODE_ANALOG_ADC");
  1846. dib7090_enAdcOnHostBus(state);
  1847. break;
  1848. }
  1849. if (state->cfg.output_mpeg2_in_188_bytes)
  1850. smo_mode |= (1 << 5);
  1851. ret |= dib7000p_write_word(state, 235, smo_mode);
  1852. ret |= dib7000p_write_word(state, 236, fifo_threshold); /* synchronous fread */
  1853. ret |= dib7000p_write_word(state, 1286, outreg | (1 << 10)); /* allways set Dout active = 1 !!! */
  1854. return ret;
  1855. }
  1856. int dib7090_tuner_sleep(struct dvb_frontend *fe, int onoff)
  1857. {
  1858. struct dib7000p_state *state = fe->demodulator_priv;
  1859. u16 en_cur_state;
  1860. dprintk("sleep dib7090: %d", onoff);
  1861. en_cur_state = dib7000p_read_word(state, 1922);
  1862. if (en_cur_state > 0xff)
  1863. state->tuner_enable = en_cur_state;
  1864. if (onoff)
  1865. en_cur_state &= 0x00ff;
  1866. else {
  1867. if (state->tuner_enable != 0)
  1868. en_cur_state = state->tuner_enable;
  1869. }
  1870. dib7000p_write_word(state, 1922, en_cur_state);
  1871. return 0;
  1872. }
  1873. EXPORT_SYMBOL(dib7090_tuner_sleep);
  1874. int dib7090_agc_restart(struct dvb_frontend *fe, u8 restart)
  1875. {
  1876. dprintk("AGC restart callback: %d", restart);
  1877. return 0;
  1878. }
  1879. EXPORT_SYMBOL(dib7090_agc_restart);
  1880. int dib7090_get_adc_power(struct dvb_frontend *fe)
  1881. {
  1882. return dib7000p_get_adc_power(fe);
  1883. }
  1884. EXPORT_SYMBOL(dib7090_get_adc_power);
  1885. int dib7090_slave_reset(struct dvb_frontend *fe)
  1886. {
  1887. struct dib7000p_state *state = fe->demodulator_priv;
  1888. u16 reg;
  1889. reg = dib7000p_read_word(state, 1794);
  1890. dib7000p_write_word(state, 1794, reg | (4 << 12));
  1891. dib7000p_write_word(state, 1032, 0xffff);
  1892. return 0;
  1893. }
  1894. EXPORT_SYMBOL(dib7090_slave_reset);
  1895. static struct dvb_frontend_ops dib7000p_ops;
  1896. struct dvb_frontend *dib7000p_attach(struct i2c_adapter *i2c_adap, u8 i2c_addr, struct dib7000p_config *cfg)
  1897. {
  1898. struct dvb_frontend *demod;
  1899. struct dib7000p_state *st;
  1900. st = kzalloc(sizeof(struct dib7000p_state), GFP_KERNEL);
  1901. if (st == NULL)
  1902. return NULL;
  1903. memcpy(&st->cfg, cfg, sizeof(struct dib7000p_config));
  1904. st->i2c_adap = i2c_adap;
  1905. st->i2c_addr = i2c_addr;
  1906. st->gpio_val = cfg->gpio_val;
  1907. st->gpio_dir = cfg->gpio_dir;
  1908. /* Ensure the output mode remains at the previous default if it's
  1909. * not specifically set by the caller.
  1910. */
  1911. if ((st->cfg.output_mode != OUTMODE_MPEG2_SERIAL) && (st->cfg.output_mode != OUTMODE_MPEG2_PAR_GATED_CLK))
  1912. st->cfg.output_mode = OUTMODE_MPEG2_FIFO;
  1913. demod = &st->demod;
  1914. demod->demodulator_priv = st;
  1915. memcpy(&st->demod.ops, &dib7000p_ops, sizeof(struct dvb_frontend_ops));
  1916. dib7000p_write_word(st, 1287, 0x0003); /* sram lead in, rdy */
  1917. if (dib7000p_identify(st) != 0)
  1918. goto error;
  1919. st->version = dib7000p_read_word(st, 897);
  1920. /* FIXME: make sure the dev.parent field is initialized, or else
  1921. request_firmware() will hit an OOPS (this should be moved somewhere
  1922. more common) */
  1923. dibx000_init_i2c_master(&st->i2c_master, DIB7000P, st->i2c_adap, st->i2c_addr);
  1924. /* init 7090 tuner adapter */
  1925. strncpy(st->dib7090_tuner_adap.name, "DiB7090 tuner interface", sizeof(st->dib7090_tuner_adap.name));
  1926. st->dib7090_tuner_adap.algo = &dib7090_tuner_xfer_algo;
  1927. st->dib7090_tuner_adap.algo_data = NULL;
  1928. st->dib7090_tuner_adap.dev.parent = st->i2c_adap->dev.parent;
  1929. i2c_set_adapdata(&st->dib7090_tuner_adap, st);
  1930. i2c_add_adapter(&st->dib7090_tuner_adap);
  1931. dib7000p_demod_reset(st);
  1932. if (st->version == SOC7090) {
  1933. dib7090_set_output_mode(demod, st->cfg.output_mode);
  1934. dib7090_set_diversity_in(demod, 0);
  1935. }
  1936. return demod;
  1937. error:
  1938. kfree(st);
  1939. return NULL;
  1940. }
  1941. EXPORT_SYMBOL(dib7000p_attach);
  1942. static struct dvb_frontend_ops dib7000p_ops = {
  1943. .info = {
  1944. .name = "DiBcom 7000PC",
  1945. .type = FE_OFDM,
  1946. .frequency_min = 44250000,
  1947. .frequency_max = 867250000,
  1948. .frequency_stepsize = 62500,
  1949. .caps = FE_CAN_INVERSION_AUTO |
  1950. FE_CAN_FEC_1_2 | FE_CAN_FEC_2_3 | FE_CAN_FEC_3_4 |
  1951. FE_CAN_FEC_5_6 | FE_CAN_FEC_7_8 | FE_CAN_FEC_AUTO |
  1952. FE_CAN_QPSK | FE_CAN_QAM_16 | FE_CAN_QAM_64 | FE_CAN_QAM_AUTO |
  1953. FE_CAN_TRANSMISSION_MODE_AUTO | FE_CAN_GUARD_INTERVAL_AUTO | FE_CAN_RECOVER | FE_CAN_HIERARCHY_AUTO,
  1954. },
  1955. .release = dib7000p_release,
  1956. .init = dib7000p_wakeup,
  1957. .sleep = dib7000p_sleep,
  1958. .set_frontend = dib7000p_set_frontend,
  1959. .get_tune_settings = dib7000p_fe_get_tune_settings,
  1960. .get_frontend = dib7000p_get_frontend,
  1961. .read_status = dib7000p_read_status,
  1962. .read_ber = dib7000p_read_ber,
  1963. .read_signal_strength = dib7000p_read_signal_strength,
  1964. .read_snr = dib7000p_read_snr,
  1965. .read_ucblocks = dib7000p_read_unc_blocks,
  1966. };
  1967. MODULE_AUTHOR("Olivier Grenie <ogrenie@dibcom.fr>");
  1968. MODULE_AUTHOR("Patrick Boettcher <pboettcher@dibcom.fr>");
  1969. MODULE_DESCRIPTION("Driver for the DiBcom 7000PC COFDM demodulator");
  1970. MODULE_LICENSE("GPL");