dib7000p.c 63 KB

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