dib7000p.c 64 KB

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