dib7000p.c 64 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456
  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, struct dvb_frontend_parameters *ch)
  653. {
  654. struct dib7000p_state *state = demod->demodulator_priv;
  655. int ret = -1;
  656. u8 *agc_state = &state->agc_state;
  657. u8 agc_split;
  658. u16 reg;
  659. u32 upd_demod_gain_period = 0x1000;
  660. switch (state->agc_state) {
  661. case 0:
  662. dib7000p_set_power_mode(state, DIB7000P_POWER_ALL);
  663. if (state->version == SOC7090) {
  664. reg = dib7000p_read_word(state, 0x79b) & 0xff00;
  665. dib7000p_write_word(state, 0x79a, upd_demod_gain_period & 0xFFFF); /* lsb */
  666. dib7000p_write_word(state, 0x79b, reg | (1 << 14) | ((upd_demod_gain_period >> 16) & 0xFF));
  667. /* enable adc i & q */
  668. reg = dib7000p_read_word(state, 0x780);
  669. dib7000p_write_word(state, 0x780, (reg | (0x3)) & (~(1 << 7)));
  670. } else {
  671. dib7000p_set_adc_state(state, DIBX000_ADC_ON);
  672. dib7000p_pll_clk_cfg(state);
  673. }
  674. if (dib7000p_set_agc_config(state, BAND_OF_FREQUENCY(ch->frequency / 1000)) != 0)
  675. return -1;
  676. dib7000p_set_dds(state, 0);
  677. ret = 7;
  678. (*agc_state)++;
  679. break;
  680. case 1:
  681. if (state->cfg.agc_control)
  682. state->cfg.agc_control(&state->demod, 1);
  683. dib7000p_write_word(state, 78, 32768);
  684. if (!state->current_agc->perform_agc_softsplit) {
  685. /* we are using the wbd - so slow AGC startup */
  686. /* force 0 split on WBD and restart AGC */
  687. dib7000p_write_word(state, 106, (state->current_agc->wbd_sel << 13) | (state->current_agc->wbd_alpha << 9) | (1 << 8));
  688. (*agc_state)++;
  689. ret = 5;
  690. } else {
  691. /* default AGC startup */
  692. (*agc_state) = 4;
  693. /* wait AGC rough lock time */
  694. ret = 7;
  695. }
  696. dib7000p_restart_agc(state);
  697. break;
  698. case 2: /* fast split search path after 5sec */
  699. dib7000p_write_word(state, 75, state->current_agc->setup | (1 << 4)); /* freeze AGC loop */
  700. dib7000p_write_word(state, 106, (state->current_agc->wbd_sel << 13) | (2 << 9) | (0 << 8)); /* fast split search 0.25kHz */
  701. (*agc_state)++;
  702. ret = 14;
  703. break;
  704. case 3: /* split search ended */
  705. agc_split = (u8) dib7000p_read_word(state, 396); /* store the split value for the next time */
  706. dib7000p_write_word(state, 78, dib7000p_read_word(state, 394)); /* set AGC gain start value */
  707. dib7000p_write_word(state, 75, state->current_agc->setup); /* std AGC loop */
  708. dib7000p_write_word(state, 106, (state->current_agc->wbd_sel << 13) | (state->current_agc->wbd_alpha << 9) | agc_split); /* standard split search */
  709. dib7000p_restart_agc(state);
  710. dprintk("SPLIT %p: %hd", demod, agc_split);
  711. (*agc_state)++;
  712. ret = 5;
  713. break;
  714. case 4: /* LNA startup */
  715. ret = 7;
  716. if (dib7000p_update_lna(state))
  717. ret = 5;
  718. else
  719. (*agc_state)++;
  720. break;
  721. case 5:
  722. if (state->cfg.agc_control)
  723. state->cfg.agc_control(&state->demod, 0);
  724. (*agc_state)++;
  725. break;
  726. default:
  727. break;
  728. }
  729. return ret;
  730. }
  731. static void dib7000p_update_timf(struct dib7000p_state *state)
  732. {
  733. u32 timf = (dib7000p_read_word(state, 427) << 16) | dib7000p_read_word(state, 428);
  734. state->timf = timf * 160 / (state->current_bandwidth / 50);
  735. dib7000p_write_word(state, 23, (u16) (timf >> 16));
  736. dib7000p_write_word(state, 24, (u16) (timf & 0xffff));
  737. dprintk("updated timf_frequency: %d (default: %d)", state->timf, state->cfg.bw->timf);
  738. }
  739. u32 dib7000p_ctrl_timf(struct dvb_frontend *fe, u8 op, u32 timf)
  740. {
  741. struct dib7000p_state *state = fe->demodulator_priv;
  742. switch (op) {
  743. case DEMOD_TIMF_SET:
  744. state->timf = timf;
  745. break;
  746. case DEMOD_TIMF_UPDATE:
  747. dib7000p_update_timf(state);
  748. break;
  749. case DEMOD_TIMF_GET:
  750. break;
  751. }
  752. dib7000p_set_bandwidth(state, state->current_bandwidth);
  753. return state->timf;
  754. }
  755. EXPORT_SYMBOL(dib7000p_ctrl_timf);
  756. static void dib7000p_set_channel(struct dib7000p_state *state, struct dvb_frontend_parameters *ch, u8 seq)
  757. {
  758. u16 value, est[4];
  759. dib7000p_set_bandwidth(state, BANDWIDTH_TO_KHZ(ch->u.ofdm.bandwidth));
  760. /* nfft, guard, qam, alpha */
  761. value = 0;
  762. switch (ch->u.ofdm.transmission_mode) {
  763. case TRANSMISSION_MODE_2K:
  764. value |= (0 << 7);
  765. break;
  766. case TRANSMISSION_MODE_4K:
  767. value |= (2 << 7);
  768. break;
  769. default:
  770. case TRANSMISSION_MODE_8K:
  771. value |= (1 << 7);
  772. break;
  773. }
  774. switch (ch->u.ofdm.guard_interval) {
  775. case GUARD_INTERVAL_1_32:
  776. value |= (0 << 5);
  777. break;
  778. case GUARD_INTERVAL_1_16:
  779. value |= (1 << 5);
  780. break;
  781. case GUARD_INTERVAL_1_4:
  782. value |= (3 << 5);
  783. break;
  784. default:
  785. case GUARD_INTERVAL_1_8:
  786. value |= (2 << 5);
  787. break;
  788. }
  789. switch (ch->u.ofdm.constellation) {
  790. case QPSK:
  791. value |= (0 << 3);
  792. break;
  793. case QAM_16:
  794. value |= (1 << 3);
  795. break;
  796. default:
  797. case QAM_64:
  798. value |= (2 << 3);
  799. break;
  800. }
  801. switch (HIERARCHY_1) {
  802. case HIERARCHY_2:
  803. value |= 2;
  804. break;
  805. case HIERARCHY_4:
  806. value |= 4;
  807. break;
  808. default:
  809. case HIERARCHY_1:
  810. value |= 1;
  811. break;
  812. }
  813. dib7000p_write_word(state, 0, value);
  814. dib7000p_write_word(state, 5, (seq << 4) | 1); /* do not force tps, search list 0 */
  815. /* P_dintl_native, P_dintlv_inv, P_hrch, P_code_rate, P_select_hp */
  816. value = 0;
  817. if (1 != 0)
  818. value |= (1 << 6);
  819. if (ch->u.ofdm.hierarchy_information == 1)
  820. value |= (1 << 4);
  821. if (1 == 1)
  822. value |= 1;
  823. switch ((ch->u.ofdm.hierarchy_information == 0 || 1 == 1) ? ch->u.ofdm.code_rate_HP : ch->u.ofdm.code_rate_LP) {
  824. case FEC_2_3:
  825. value |= (2 << 1);
  826. break;
  827. case FEC_3_4:
  828. value |= (3 << 1);
  829. break;
  830. case FEC_5_6:
  831. value |= (5 << 1);
  832. break;
  833. case FEC_7_8:
  834. value |= (7 << 1);
  835. break;
  836. default:
  837. case FEC_1_2:
  838. value |= (1 << 1);
  839. break;
  840. }
  841. dib7000p_write_word(state, 208, value);
  842. /* offset loop parameters */
  843. dib7000p_write_word(state, 26, 0x6680);
  844. dib7000p_write_word(state, 32, 0x0003);
  845. dib7000p_write_word(state, 29, 0x1273);
  846. dib7000p_write_word(state, 33, 0x0005);
  847. /* P_dvsy_sync_wait */
  848. switch (ch->u.ofdm.transmission_mode) {
  849. case TRANSMISSION_MODE_8K:
  850. value = 256;
  851. break;
  852. case TRANSMISSION_MODE_4K:
  853. value = 128;
  854. break;
  855. case TRANSMISSION_MODE_2K:
  856. default:
  857. value = 64;
  858. break;
  859. }
  860. switch (ch->u.ofdm.guard_interval) {
  861. case GUARD_INTERVAL_1_16:
  862. value *= 2;
  863. break;
  864. case GUARD_INTERVAL_1_8:
  865. value *= 4;
  866. break;
  867. case GUARD_INTERVAL_1_4:
  868. value *= 8;
  869. break;
  870. default:
  871. case GUARD_INTERVAL_1_32:
  872. value *= 1;
  873. break;
  874. }
  875. if (state->cfg.diversity_delay == 0)
  876. state->div_sync_wait = (value * 3) / 2 + 48;
  877. else
  878. state->div_sync_wait = (value * 3) / 2 + state->cfg.diversity_delay;
  879. /* deactive the possibility of diversity reception if extended interleaver */
  880. state->div_force_off = !1 && ch->u.ofdm.transmission_mode != TRANSMISSION_MODE_8K;
  881. dib7000p_set_diversity_in(&state->demod, state->div_state);
  882. /* channel estimation fine configuration */
  883. switch (ch->u.ofdm.constellation) {
  884. case QAM_64:
  885. est[0] = 0x0148; /* P_adp_regul_cnt 0.04 */
  886. est[1] = 0xfff0; /* P_adp_noise_cnt -0.002 */
  887. est[2] = 0x00a4; /* P_adp_regul_ext 0.02 */
  888. est[3] = 0xfff8; /* P_adp_noise_ext -0.001 */
  889. break;
  890. case QAM_16:
  891. est[0] = 0x023d; /* P_adp_regul_cnt 0.07 */
  892. est[1] = 0xffdf; /* P_adp_noise_cnt -0.004 */
  893. est[2] = 0x00a4; /* P_adp_regul_ext 0.02 */
  894. est[3] = 0xfff0; /* P_adp_noise_ext -0.002 */
  895. break;
  896. default:
  897. est[0] = 0x099a; /* P_adp_regul_cnt 0.3 */
  898. est[1] = 0xffae; /* P_adp_noise_cnt -0.01 */
  899. est[2] = 0x0333; /* P_adp_regul_ext 0.1 */
  900. est[3] = 0xfff8; /* P_adp_noise_ext -0.002 */
  901. break;
  902. }
  903. for (value = 0; value < 4; value++)
  904. dib7000p_write_word(state, 187 + value, est[value]);
  905. }
  906. static int dib7000p_autosearch_start(struct dvb_frontend *demod, struct dvb_frontend_parameters *ch)
  907. {
  908. struct dib7000p_state *state = demod->demodulator_priv;
  909. struct dvb_frontend_parameters schan;
  910. u32 value, factor;
  911. u32 internal = dib7000p_get_internal_freq(state);
  912. schan = *ch;
  913. schan.u.ofdm.constellation = QAM_64;
  914. schan.u.ofdm.guard_interval = GUARD_INTERVAL_1_32;
  915. schan.u.ofdm.transmission_mode = TRANSMISSION_MODE_8K;
  916. schan.u.ofdm.code_rate_HP = FEC_2_3;
  917. schan.u.ofdm.code_rate_LP = FEC_3_4;
  918. schan.u.ofdm.hierarchy_information = 0;
  919. dib7000p_set_channel(state, &schan, 7);
  920. factor = BANDWIDTH_TO_KHZ(ch->u.ofdm.bandwidth);
  921. if (factor >= 5000) {
  922. if (state->version == SOC7090)
  923. factor = 2;
  924. else
  925. factor = 1;
  926. } else
  927. factor = 6;
  928. value = 30 * internal * factor;
  929. dib7000p_write_word(state, 6, (u16) ((value >> 16) & 0xffff));
  930. dib7000p_write_word(state, 7, (u16) (value & 0xffff));
  931. value = 100 * internal * factor;
  932. dib7000p_write_word(state, 8, (u16) ((value >> 16) & 0xffff));
  933. dib7000p_write_word(state, 9, (u16) (value & 0xffff));
  934. value = 500 * internal * factor;
  935. dib7000p_write_word(state, 10, (u16) ((value >> 16) & 0xffff));
  936. dib7000p_write_word(state, 11, (u16) (value & 0xffff));
  937. value = dib7000p_read_word(state, 0);
  938. dib7000p_write_word(state, 0, (u16) ((1 << 9) | value));
  939. dib7000p_read_word(state, 1284);
  940. dib7000p_write_word(state, 0, (u16) value);
  941. return 0;
  942. }
  943. static int dib7000p_autosearch_is_irq(struct dvb_frontend *demod)
  944. {
  945. struct dib7000p_state *state = demod->demodulator_priv;
  946. u16 irq_pending = dib7000p_read_word(state, 1284);
  947. if (irq_pending & 0x1)
  948. return 1;
  949. if (irq_pending & 0x2)
  950. return 2;
  951. return 0;
  952. }
  953. static void dib7000p_spur_protect(struct dib7000p_state *state, u32 rf_khz, u32 bw)
  954. {
  955. static s16 notch[] = { 16143, 14402, 12238, 9713, 6902, 3888, 759, -2392 };
  956. static u8 sine[] = { 0, 2, 3, 5, 6, 8, 9, 11, 13, 14, 16, 17, 19, 20, 22,
  957. 24, 25, 27, 28, 30, 31, 33, 34, 36, 38, 39, 41, 42, 44, 45, 47, 48, 50, 51,
  958. 53, 55, 56, 58, 59, 61, 62, 64, 65, 67, 68, 70, 71, 73, 74, 76, 77, 79, 80,
  959. 82, 83, 85, 86, 88, 89, 91, 92, 94, 95, 97, 98, 99, 101, 102, 104, 105,
  960. 107, 108, 109, 111, 112, 114, 115, 117, 118, 119, 121, 122, 123, 125, 126,
  961. 128, 129, 130, 132, 133, 134, 136, 137, 138, 140, 141, 142, 144, 145, 146,
  962. 147, 149, 150, 151, 152, 154, 155, 156, 157, 159, 160, 161, 162, 164, 165,
  963. 166, 167, 168, 170, 171, 172, 173, 174, 175, 177, 178, 179, 180, 181, 182,
  964. 183, 184, 185, 186, 188, 189, 190, 191, 192, 193, 194, 195, 196, 197, 198,
  965. 199, 200, 201, 202, 203, 204, 205, 206, 207, 207, 208, 209, 210, 211, 212,
  966. 213, 214, 215, 215, 216, 217, 218, 219, 220, 220, 221, 222, 223, 224, 224,
  967. 225, 226, 227, 227, 228, 229, 229, 230, 231, 231, 232, 233, 233, 234, 235,
  968. 235, 236, 237, 237, 238, 238, 239, 239, 240, 241, 241, 242, 242, 243, 243,
  969. 244, 244, 245, 245, 245, 246, 246, 247, 247, 248, 248, 248, 249, 249, 249,
  970. 250, 250, 250, 251, 251, 251, 252, 252, 252, 252, 253, 253, 253, 253, 254,
  971. 254, 254, 254, 254, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
  972. 255, 255, 255, 255, 255, 255
  973. };
  974. u32 xtal = state->cfg.bw->xtal_hz / 1000;
  975. int f_rel = DIV_ROUND_CLOSEST(rf_khz, xtal) * xtal - rf_khz;
  976. int k;
  977. int coef_re[8], coef_im[8];
  978. int bw_khz = bw;
  979. u32 pha;
  980. dprintk("relative position of the Spur: %dk (RF: %dk, XTAL: %dk)", f_rel, rf_khz, xtal);
  981. if (f_rel < -bw_khz / 2 || f_rel > bw_khz / 2)
  982. return;
  983. bw_khz /= 100;
  984. dib7000p_write_word(state, 142, 0x0610);
  985. for (k = 0; k < 8; k++) {
  986. pha = ((f_rel * (k + 1) * 112 * 80 / bw_khz) / 1000) & 0x3ff;
  987. if (pha == 0) {
  988. coef_re[k] = 256;
  989. coef_im[k] = 0;
  990. } else if (pha < 256) {
  991. coef_re[k] = sine[256 - (pha & 0xff)];
  992. coef_im[k] = sine[pha & 0xff];
  993. } else if (pha == 256) {
  994. coef_re[k] = 0;
  995. coef_im[k] = 256;
  996. } else if (pha < 512) {
  997. coef_re[k] = -sine[pha & 0xff];
  998. coef_im[k] = sine[256 - (pha & 0xff)];
  999. } else if (pha == 512) {
  1000. coef_re[k] = -256;
  1001. coef_im[k] = 0;
  1002. } else if (pha < 768) {
  1003. coef_re[k] = -sine[256 - (pha & 0xff)];
  1004. coef_im[k] = -sine[pha & 0xff];
  1005. } else if (pha == 768) {
  1006. coef_re[k] = 0;
  1007. coef_im[k] = -256;
  1008. } else {
  1009. coef_re[k] = sine[pha & 0xff];
  1010. coef_im[k] = -sine[256 - (pha & 0xff)];
  1011. }
  1012. coef_re[k] *= notch[k];
  1013. coef_re[k] += (1 << 14);
  1014. if (coef_re[k] >= (1 << 24))
  1015. coef_re[k] = (1 << 24) - 1;
  1016. coef_re[k] /= (1 << 15);
  1017. coef_im[k] *= notch[k];
  1018. coef_im[k] += (1 << 14);
  1019. if (coef_im[k] >= (1 << 24))
  1020. coef_im[k] = (1 << 24) - 1;
  1021. coef_im[k] /= (1 << 15);
  1022. dprintk("PALF COEF: %d re: %d im: %d", k, coef_re[k], coef_im[k]);
  1023. dib7000p_write_word(state, 143, (0 << 14) | (k << 10) | (coef_re[k] & 0x3ff));
  1024. dib7000p_write_word(state, 144, coef_im[k] & 0x3ff);
  1025. dib7000p_write_word(state, 143, (1 << 14) | (k << 10) | (coef_re[k] & 0x3ff));
  1026. }
  1027. dib7000p_write_word(state, 143, 0);
  1028. }
  1029. static int dib7000p_tune(struct dvb_frontend *demod, struct dvb_frontend_parameters *ch)
  1030. {
  1031. struct dib7000p_state *state = demod->demodulator_priv;
  1032. u16 tmp = 0;
  1033. if (ch != NULL)
  1034. dib7000p_set_channel(state, ch, 0);
  1035. else
  1036. return -EINVAL;
  1037. // restart demod
  1038. dib7000p_write_word(state, 770, 0x4000);
  1039. dib7000p_write_word(state, 770, 0x0000);
  1040. msleep(45);
  1041. /* 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 */
  1042. tmp = (0 << 14) | (4 << 10) | (0 << 9) | (3 << 5) | (1 << 4) | (0x3);
  1043. if (state->sfn_workaround_active) {
  1044. dprintk("SFN workaround is active");
  1045. tmp |= (1 << 9);
  1046. dib7000p_write_word(state, 166, 0x4000);
  1047. } else {
  1048. dib7000p_write_word(state, 166, 0x0000);
  1049. }
  1050. dib7000p_write_word(state, 29, tmp);
  1051. // never achieved a lock with that bandwidth so far - wait for osc-freq to update
  1052. if (state->timf == 0)
  1053. msleep(200);
  1054. /* offset loop parameters */
  1055. /* P_timf_alpha, P_corm_alpha=6, P_corm_thres=0x80 */
  1056. tmp = (6 << 8) | 0x80;
  1057. switch (ch->u.ofdm.transmission_mode) {
  1058. case TRANSMISSION_MODE_2K:
  1059. tmp |= (2 << 12);
  1060. break;
  1061. case TRANSMISSION_MODE_4K:
  1062. tmp |= (3 << 12);
  1063. break;
  1064. default:
  1065. case TRANSMISSION_MODE_8K:
  1066. tmp |= (4 << 12);
  1067. break;
  1068. }
  1069. dib7000p_write_word(state, 26, tmp); /* timf_a(6xxx) */
  1070. /* P_ctrl_freeze_pha_shift=0, P_ctrl_pha_off_max */
  1071. tmp = (0 << 4);
  1072. switch (ch->u.ofdm.transmission_mode) {
  1073. case TRANSMISSION_MODE_2K:
  1074. tmp |= 0x6;
  1075. break;
  1076. case TRANSMISSION_MODE_4K:
  1077. tmp |= 0x7;
  1078. break;
  1079. default:
  1080. case TRANSMISSION_MODE_8K:
  1081. tmp |= 0x8;
  1082. break;
  1083. }
  1084. dib7000p_write_word(state, 32, tmp);
  1085. /* P_ctrl_sfreq_inh=0, P_ctrl_sfreq_step */
  1086. tmp = (0 << 4);
  1087. switch (ch->u.ofdm.transmission_mode) {
  1088. case TRANSMISSION_MODE_2K:
  1089. tmp |= 0x6;
  1090. break;
  1091. case TRANSMISSION_MODE_4K:
  1092. tmp |= 0x7;
  1093. break;
  1094. default:
  1095. case TRANSMISSION_MODE_8K:
  1096. tmp |= 0x8;
  1097. break;
  1098. }
  1099. dib7000p_write_word(state, 33, tmp);
  1100. tmp = dib7000p_read_word(state, 509);
  1101. if (!((tmp >> 6) & 0x1)) {
  1102. /* restart the fec */
  1103. tmp = dib7000p_read_word(state, 771);
  1104. dib7000p_write_word(state, 771, tmp | (1 << 1));
  1105. dib7000p_write_word(state, 771, tmp);
  1106. msleep(40);
  1107. tmp = dib7000p_read_word(state, 509);
  1108. }
  1109. // we achieved a lock - it's time to update the osc freq
  1110. if ((tmp >> 6) & 0x1) {
  1111. dib7000p_update_timf(state);
  1112. /* P_timf_alpha += 2 */
  1113. tmp = dib7000p_read_word(state, 26);
  1114. dib7000p_write_word(state, 26, (tmp & ~(0xf << 12)) | ((((tmp >> 12) & 0xf) + 5) << 12));
  1115. }
  1116. if (state->cfg.spur_protect)
  1117. dib7000p_spur_protect(state, ch->frequency / 1000, BANDWIDTH_TO_KHZ(ch->u.ofdm.bandwidth));
  1118. dib7000p_set_bandwidth(state, BANDWIDTH_TO_KHZ(ch->u.ofdm.bandwidth));
  1119. return 0;
  1120. }
  1121. static int dib7000p_wakeup(struct dvb_frontend *demod)
  1122. {
  1123. struct dib7000p_state *state = demod->demodulator_priv;
  1124. dib7000p_set_power_mode(state, DIB7000P_POWER_ALL);
  1125. dib7000p_set_adc_state(state, DIBX000_SLOW_ADC_ON);
  1126. if (state->version == SOC7090)
  1127. dib7000p_sad_calib(state);
  1128. return 0;
  1129. }
  1130. static int dib7000p_sleep(struct dvb_frontend *demod)
  1131. {
  1132. struct dib7000p_state *state = demod->demodulator_priv;
  1133. if (state->version == SOC7090)
  1134. return dib7000p_set_power_mode(state, DIB7000P_POWER_INTERFACE_ONLY);
  1135. return dib7000p_set_output_mode(state, OUTMODE_HIGH_Z) | dib7000p_set_power_mode(state, DIB7000P_POWER_INTERFACE_ONLY);
  1136. }
  1137. static int dib7000p_identify(struct dib7000p_state *st)
  1138. {
  1139. u16 value;
  1140. dprintk("checking demod on I2C address: %d (%x)", st->i2c_addr, st->i2c_addr);
  1141. if ((value = dib7000p_read_word(st, 768)) != 0x01b3) {
  1142. dprintk("wrong Vendor ID (read=0x%x)", value);
  1143. return -EREMOTEIO;
  1144. }
  1145. if ((value = dib7000p_read_word(st, 769)) != 0x4000) {
  1146. dprintk("wrong Device ID (%x)", value);
  1147. return -EREMOTEIO;
  1148. }
  1149. return 0;
  1150. }
  1151. static int dib7000p_get_frontend(struct dvb_frontend *fe, struct dvb_frontend_parameters *fep)
  1152. {
  1153. struct dib7000p_state *state = fe->demodulator_priv;
  1154. u16 tps = dib7000p_read_word(state, 463);
  1155. fep->inversion = INVERSION_AUTO;
  1156. fep->u.ofdm.bandwidth = BANDWIDTH_TO_INDEX(state->current_bandwidth);
  1157. switch ((tps >> 8) & 0x3) {
  1158. case 0:
  1159. fep->u.ofdm.transmission_mode = TRANSMISSION_MODE_2K;
  1160. break;
  1161. case 1:
  1162. fep->u.ofdm.transmission_mode = TRANSMISSION_MODE_8K;
  1163. break;
  1164. /* case 2: fep->u.ofdm.transmission_mode = TRANSMISSION_MODE_4K; break; */
  1165. }
  1166. switch (tps & 0x3) {
  1167. case 0:
  1168. fep->u.ofdm.guard_interval = GUARD_INTERVAL_1_32;
  1169. break;
  1170. case 1:
  1171. fep->u.ofdm.guard_interval = GUARD_INTERVAL_1_16;
  1172. break;
  1173. case 2:
  1174. fep->u.ofdm.guard_interval = GUARD_INTERVAL_1_8;
  1175. break;
  1176. case 3:
  1177. fep->u.ofdm.guard_interval = GUARD_INTERVAL_1_4;
  1178. break;
  1179. }
  1180. switch ((tps >> 14) & 0x3) {
  1181. case 0:
  1182. fep->u.ofdm.constellation = QPSK;
  1183. break;
  1184. case 1:
  1185. fep->u.ofdm.constellation = QAM_16;
  1186. break;
  1187. case 2:
  1188. default:
  1189. fep->u.ofdm.constellation = QAM_64;
  1190. break;
  1191. }
  1192. /* as long as the frontend_param structure is fixed for hierarchical transmission I refuse to use it */
  1193. /* (tps >> 13) & 0x1 == hrch is used, (tps >> 10) & 0x7 == alpha */
  1194. fep->u.ofdm.hierarchy_information = HIERARCHY_NONE;
  1195. switch ((tps >> 5) & 0x7) {
  1196. case 1:
  1197. fep->u.ofdm.code_rate_HP = FEC_1_2;
  1198. break;
  1199. case 2:
  1200. fep->u.ofdm.code_rate_HP = FEC_2_3;
  1201. break;
  1202. case 3:
  1203. fep->u.ofdm.code_rate_HP = FEC_3_4;
  1204. break;
  1205. case 5:
  1206. fep->u.ofdm.code_rate_HP = FEC_5_6;
  1207. break;
  1208. case 7:
  1209. default:
  1210. fep->u.ofdm.code_rate_HP = FEC_7_8;
  1211. break;
  1212. }
  1213. switch ((tps >> 2) & 0x7) {
  1214. case 1:
  1215. fep->u.ofdm.code_rate_LP = FEC_1_2;
  1216. break;
  1217. case 2:
  1218. fep->u.ofdm.code_rate_LP = FEC_2_3;
  1219. break;
  1220. case 3:
  1221. fep->u.ofdm.code_rate_LP = FEC_3_4;
  1222. break;
  1223. case 5:
  1224. fep->u.ofdm.code_rate_LP = FEC_5_6;
  1225. break;
  1226. case 7:
  1227. default:
  1228. fep->u.ofdm.code_rate_LP = FEC_7_8;
  1229. break;
  1230. }
  1231. /* native interleaver: (dib7000p_read_word(state, 464) >> 5) & 0x1 */
  1232. return 0;
  1233. }
  1234. static int dib7000p_set_frontend(struct dvb_frontend *fe, struct dvb_frontend_parameters *fep)
  1235. {
  1236. struct dib7000p_state *state = fe->demodulator_priv;
  1237. int time, ret;
  1238. if (state->version == SOC7090)
  1239. dib7090_set_diversity_in(fe, 0);
  1240. else
  1241. dib7000p_set_output_mode(state, OUTMODE_HIGH_Z);
  1242. /* maybe the parameter has been changed */
  1243. state->sfn_workaround_active = buggy_sfn_workaround;
  1244. if (fe->ops.tuner_ops.set_params)
  1245. fe->ops.tuner_ops.set_params(fe, fep);
  1246. /* start up the AGC */
  1247. state->agc_state = 0;
  1248. do {
  1249. time = dib7000p_agc_startup(fe, fep);
  1250. if (time != -1)
  1251. msleep(time);
  1252. } while (time != -1);
  1253. if (fep->u.ofdm.transmission_mode == TRANSMISSION_MODE_AUTO ||
  1254. fep->u.ofdm.guard_interval == GUARD_INTERVAL_AUTO || fep->u.ofdm.constellation == QAM_AUTO || fep->u.ofdm.code_rate_HP == FEC_AUTO) {
  1255. int i = 800, found;
  1256. dib7000p_autosearch_start(fe, fep);
  1257. do {
  1258. msleep(1);
  1259. found = dib7000p_autosearch_is_irq(fe);
  1260. } while (found == 0 && i--);
  1261. dprintk("autosearch returns: %d", found);
  1262. if (found == 0 || found == 1)
  1263. return 0;
  1264. dib7000p_get_frontend(fe, fep);
  1265. }
  1266. ret = dib7000p_tune(fe, fep);
  1267. /* make this a config parameter */
  1268. if (state->version == SOC7090) {
  1269. dib7090_set_output_mode(fe, state->cfg.output_mode);
  1270. if (state->cfg.enMpegOutput == 0) {
  1271. dib7090_setDibTxMux(state, MPEG_ON_DIBTX);
  1272. dib7090_setHostBusMux(state, DIBTX_ON_HOSTBUS);
  1273. }
  1274. } else
  1275. dib7000p_set_output_mode(state, state->cfg.output_mode);
  1276. return ret;
  1277. }
  1278. static int dib7000p_read_status(struct dvb_frontend *fe, fe_status_t * stat)
  1279. {
  1280. struct dib7000p_state *state = fe->demodulator_priv;
  1281. u16 lock = dib7000p_read_word(state, 509);
  1282. *stat = 0;
  1283. if (lock & 0x8000)
  1284. *stat |= FE_HAS_SIGNAL;
  1285. if (lock & 0x3000)
  1286. *stat |= FE_HAS_CARRIER;
  1287. if (lock & 0x0100)
  1288. *stat |= FE_HAS_VITERBI;
  1289. if (lock & 0x0010)
  1290. *stat |= FE_HAS_SYNC;
  1291. if ((lock & 0x0038) == 0x38)
  1292. *stat |= FE_HAS_LOCK;
  1293. return 0;
  1294. }
  1295. static int dib7000p_read_ber(struct dvb_frontend *fe, u32 * ber)
  1296. {
  1297. struct dib7000p_state *state = fe->demodulator_priv;
  1298. *ber = (dib7000p_read_word(state, 500) << 16) | dib7000p_read_word(state, 501);
  1299. return 0;
  1300. }
  1301. static int dib7000p_read_unc_blocks(struct dvb_frontend *fe, u32 * unc)
  1302. {
  1303. struct dib7000p_state *state = fe->demodulator_priv;
  1304. *unc = dib7000p_read_word(state, 506);
  1305. return 0;
  1306. }
  1307. static int dib7000p_read_signal_strength(struct dvb_frontend *fe, u16 * strength)
  1308. {
  1309. struct dib7000p_state *state = fe->demodulator_priv;
  1310. u16 val = dib7000p_read_word(state, 394);
  1311. *strength = 65535 - val;
  1312. return 0;
  1313. }
  1314. static int dib7000p_read_snr(struct dvb_frontend *fe, u16 * snr)
  1315. {
  1316. struct dib7000p_state *state = fe->demodulator_priv;
  1317. u16 val;
  1318. s32 signal_mant, signal_exp, noise_mant, noise_exp;
  1319. u32 result = 0;
  1320. val = dib7000p_read_word(state, 479);
  1321. noise_mant = (val >> 4) & 0xff;
  1322. noise_exp = ((val & 0xf) << 2);
  1323. val = dib7000p_read_word(state, 480);
  1324. noise_exp += ((val >> 14) & 0x3);
  1325. if ((noise_exp & 0x20) != 0)
  1326. noise_exp -= 0x40;
  1327. signal_mant = (val >> 6) & 0xFF;
  1328. signal_exp = (val & 0x3F);
  1329. if ((signal_exp & 0x20) != 0)
  1330. signal_exp -= 0x40;
  1331. if (signal_mant != 0)
  1332. result = intlog10(2) * 10 * signal_exp + 10 * intlog10(signal_mant);
  1333. else
  1334. result = intlog10(2) * 10 * signal_exp - 100;
  1335. if (noise_mant != 0)
  1336. result -= intlog10(2) * 10 * noise_exp + 10 * intlog10(noise_mant);
  1337. else
  1338. result -= intlog10(2) * 10 * noise_exp - 100;
  1339. *snr = result / ((1 << 24) / 10);
  1340. return 0;
  1341. }
  1342. static int dib7000p_fe_get_tune_settings(struct dvb_frontend *fe, struct dvb_frontend_tune_settings *tune)
  1343. {
  1344. tune->min_delay_ms = 1000;
  1345. return 0;
  1346. }
  1347. static void dib7000p_release(struct dvb_frontend *demod)
  1348. {
  1349. struct dib7000p_state *st = demod->demodulator_priv;
  1350. dibx000_exit_i2c_master(&st->i2c_master);
  1351. i2c_del_adapter(&st->dib7090_tuner_adap);
  1352. kfree(st);
  1353. }
  1354. int dib7000pc_detection(struct i2c_adapter *i2c_adap)
  1355. {
  1356. u8 *tx, *rx;
  1357. struct i2c_msg msg[2] = {
  1358. {.addr = 18 >> 1, .flags = 0, .len = 2},
  1359. {.addr = 18 >> 1, .flags = I2C_M_RD, .len = 2},
  1360. };
  1361. int ret = 0;
  1362. tx = kzalloc(2*sizeof(u8), GFP_KERNEL);
  1363. if (!tx)
  1364. return -ENOMEM;
  1365. rx = kzalloc(2*sizeof(u8), GFP_KERNEL);
  1366. if (!rx) {
  1367. ret = -ENOMEM;
  1368. goto rx_memory_error;
  1369. }
  1370. msg[0].buf = tx;
  1371. msg[1].buf = rx;
  1372. tx[0] = 0x03;
  1373. tx[1] = 0x00;
  1374. if (i2c_transfer(i2c_adap, msg, 2) == 2)
  1375. if (rx[0] == 0x01 && rx[1] == 0xb3) {
  1376. dprintk("-D- DiB7000PC detected");
  1377. return 1;
  1378. }
  1379. msg[0].addr = msg[1].addr = 0x40;
  1380. if (i2c_transfer(i2c_adap, msg, 2) == 2)
  1381. if (rx[0] == 0x01 && rx[1] == 0xb3) {
  1382. dprintk("-D- DiB7000PC detected");
  1383. return 1;
  1384. }
  1385. dprintk("-D- DiB7000PC not detected");
  1386. kfree(rx);
  1387. rx_memory_error:
  1388. kfree(tx);
  1389. return ret;
  1390. }
  1391. EXPORT_SYMBOL(dib7000pc_detection);
  1392. struct i2c_adapter *dib7000p_get_i2c_master(struct dvb_frontend *demod, enum dibx000_i2c_interface intf, int gating)
  1393. {
  1394. struct dib7000p_state *st = demod->demodulator_priv;
  1395. return dibx000_get_i2c_adapter(&st->i2c_master, intf, gating);
  1396. }
  1397. EXPORT_SYMBOL(dib7000p_get_i2c_master);
  1398. int dib7000p_pid_filter_ctrl(struct dvb_frontend *fe, u8 onoff)
  1399. {
  1400. struct dib7000p_state *state = fe->demodulator_priv;
  1401. u16 val = dib7000p_read_word(state, 235) & 0xffef;
  1402. val |= (onoff & 0x1) << 4;
  1403. dprintk("PID filter enabled %d", onoff);
  1404. return dib7000p_write_word(state, 235, val);
  1405. }
  1406. EXPORT_SYMBOL(dib7000p_pid_filter_ctrl);
  1407. int dib7000p_pid_filter(struct dvb_frontend *fe, u8 id, u16 pid, u8 onoff)
  1408. {
  1409. struct dib7000p_state *state = fe->demodulator_priv;
  1410. dprintk("PID filter: index %x, PID %d, OnOff %d", id, pid, onoff);
  1411. return dib7000p_write_word(state, 241 + id, onoff ? (1 << 13) | pid : 0);
  1412. }
  1413. EXPORT_SYMBOL(dib7000p_pid_filter);
  1414. int dib7000p_i2c_enumeration(struct i2c_adapter *i2c, int no_of_demods, u8 default_addr, struct dib7000p_config cfg[])
  1415. {
  1416. struct dib7000p_state *dpst;
  1417. int k = 0;
  1418. u8 new_addr = 0;
  1419. dpst = kzalloc(sizeof(struct dib7000p_state), GFP_KERNEL);
  1420. if (!dpst)
  1421. return -ENOMEM;
  1422. dpst->i2c_adap = i2c;
  1423. mutex_init(&dpst->i2c_buffer_lock);
  1424. for (k = no_of_demods - 1; k >= 0; k--) {
  1425. dpst->cfg = cfg[k];
  1426. /* designated i2c address */
  1427. if (cfg[k].default_i2c_addr != 0)
  1428. new_addr = cfg[k].default_i2c_addr + (k << 1);
  1429. else
  1430. new_addr = (0x40 + k) << 1;
  1431. dpst->i2c_addr = new_addr;
  1432. dib7000p_write_word(dpst, 1287, 0x0003); /* sram lead in, rdy */
  1433. if (dib7000p_identify(dpst) != 0) {
  1434. dpst->i2c_addr = default_addr;
  1435. dib7000p_write_word(dpst, 1287, 0x0003); /* sram lead in, rdy */
  1436. if (dib7000p_identify(dpst) != 0) {
  1437. dprintk("DiB7000P #%d: not identified\n", k);
  1438. kfree(dpst);
  1439. return -EIO;
  1440. }
  1441. }
  1442. /* start diversity to pull_down div_str - just for i2c-enumeration */
  1443. dib7000p_set_output_mode(dpst, OUTMODE_DIVERSITY);
  1444. /* set new i2c address and force divstart */
  1445. dib7000p_write_word(dpst, 1285, (new_addr << 2) | 0x2);
  1446. dprintk("IC %d initialized (to i2c_address 0x%x)", k, new_addr);
  1447. }
  1448. for (k = 0; k < no_of_demods; k++) {
  1449. dpst->cfg = cfg[k];
  1450. if (cfg[k].default_i2c_addr != 0)
  1451. dpst->i2c_addr = (cfg[k].default_i2c_addr + k) << 1;
  1452. else
  1453. dpst->i2c_addr = (0x40 + k) << 1;
  1454. // unforce divstr
  1455. dib7000p_write_word(dpst, 1285, dpst->i2c_addr << 2);
  1456. /* deactivate div - it was just for i2c-enumeration */
  1457. dib7000p_set_output_mode(dpst, OUTMODE_HIGH_Z);
  1458. }
  1459. kfree(dpst);
  1460. return 0;
  1461. }
  1462. EXPORT_SYMBOL(dib7000p_i2c_enumeration);
  1463. static const s32 lut_1000ln_mant[] = {
  1464. 6908, 6956, 7003, 7047, 7090, 7131, 7170, 7208, 7244, 7279, 7313, 7346, 7377, 7408, 7438, 7467, 7495, 7523, 7549, 7575, 7600
  1465. };
  1466. static s32 dib7000p_get_adc_power(struct dvb_frontend *fe)
  1467. {
  1468. struct dib7000p_state *state = fe->demodulator_priv;
  1469. u32 tmp_val = 0, exp = 0, mant = 0;
  1470. s32 pow_i;
  1471. u16 buf[2];
  1472. u8 ix = 0;
  1473. buf[0] = dib7000p_read_word(state, 0x184);
  1474. buf[1] = dib7000p_read_word(state, 0x185);
  1475. pow_i = (buf[0] << 16) | buf[1];
  1476. dprintk("raw pow_i = %d", pow_i);
  1477. tmp_val = pow_i;
  1478. while (tmp_val >>= 1)
  1479. exp++;
  1480. mant = (pow_i * 1000 / (1 << exp));
  1481. dprintk(" mant = %d exp = %d", mant / 1000, exp);
  1482. ix = (u8) ((mant - 1000) / 100); /* index of the LUT */
  1483. dprintk(" ix = %d", ix);
  1484. pow_i = (lut_1000ln_mant[ix] + 693 * (exp - 20) - 6908);
  1485. pow_i = (pow_i << 8) / 1000;
  1486. dprintk(" pow_i = %d", pow_i);
  1487. return pow_i;
  1488. }
  1489. static int map_addr_to_serpar_number(struct i2c_msg *msg)
  1490. {
  1491. if ((msg->buf[0] <= 15))
  1492. msg->buf[0] -= 1;
  1493. else if (msg->buf[0] == 17)
  1494. msg->buf[0] = 15;
  1495. else if (msg->buf[0] == 16)
  1496. msg->buf[0] = 17;
  1497. else if (msg->buf[0] == 19)
  1498. msg->buf[0] = 16;
  1499. else if (msg->buf[0] >= 21 && msg->buf[0] <= 25)
  1500. msg->buf[0] -= 3;
  1501. else if (msg->buf[0] == 28)
  1502. msg->buf[0] = 23;
  1503. else
  1504. return -EINVAL;
  1505. return 0;
  1506. }
  1507. static int w7090p_tuner_write_serpar(struct i2c_adapter *i2c_adap, struct i2c_msg msg[], int num)
  1508. {
  1509. struct dib7000p_state *state = i2c_get_adapdata(i2c_adap);
  1510. u8 n_overflow = 1;
  1511. u16 i = 1000;
  1512. u16 serpar_num = msg[0].buf[0];
  1513. while (n_overflow == 1 && i) {
  1514. n_overflow = (dib7000p_read_word(state, 1984) >> 1) & 0x1;
  1515. i--;
  1516. if (i == 0)
  1517. dprintk("Tuner ITF: write busy (overflow)");
  1518. }
  1519. dib7000p_write_word(state, 1985, (1 << 6) | (serpar_num & 0x3f));
  1520. dib7000p_write_word(state, 1986, (msg[0].buf[1] << 8) | msg[0].buf[2]);
  1521. return num;
  1522. }
  1523. static int w7090p_tuner_read_serpar(struct i2c_adapter *i2c_adap, struct i2c_msg msg[], int num)
  1524. {
  1525. struct dib7000p_state *state = i2c_get_adapdata(i2c_adap);
  1526. u8 n_overflow = 1, n_empty = 1;
  1527. u16 i = 1000;
  1528. u16 serpar_num = msg[0].buf[0];
  1529. u16 read_word;
  1530. while (n_overflow == 1 && i) {
  1531. n_overflow = (dib7000p_read_word(state, 1984) >> 1) & 0x1;
  1532. i--;
  1533. if (i == 0)
  1534. dprintk("TunerITF: read busy (overflow)");
  1535. }
  1536. dib7000p_write_word(state, 1985, (0 << 6) | (serpar_num & 0x3f));
  1537. i = 1000;
  1538. while (n_empty == 1 && i) {
  1539. n_empty = dib7000p_read_word(state, 1984) & 0x1;
  1540. i--;
  1541. if (i == 0)
  1542. dprintk("TunerITF: read busy (empty)");
  1543. }
  1544. read_word = dib7000p_read_word(state, 1987);
  1545. msg[1].buf[0] = (read_word >> 8) & 0xff;
  1546. msg[1].buf[1] = (read_word) & 0xff;
  1547. return num;
  1548. }
  1549. static int w7090p_tuner_rw_serpar(struct i2c_adapter *i2c_adap, struct i2c_msg msg[], int num)
  1550. {
  1551. 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... */
  1552. if (num == 1) { /* write */
  1553. return w7090p_tuner_write_serpar(i2c_adap, msg, 1);
  1554. } else { /* read */
  1555. return w7090p_tuner_read_serpar(i2c_adap, msg, 2);
  1556. }
  1557. }
  1558. return num;
  1559. }
  1560. static int dib7090p_rw_on_apb(struct i2c_adapter *i2c_adap,
  1561. struct i2c_msg msg[], int num, u16 apb_address)
  1562. {
  1563. struct dib7000p_state *state = i2c_get_adapdata(i2c_adap);
  1564. u16 word;
  1565. if (num == 1) { /* write */
  1566. dib7000p_write_word(state, apb_address, ((msg[0].buf[1] << 8) | (msg[0].buf[2])));
  1567. } else {
  1568. word = dib7000p_read_word(state, apb_address);
  1569. msg[1].buf[0] = (word >> 8) & 0xff;
  1570. msg[1].buf[1] = (word) & 0xff;
  1571. }
  1572. return num;
  1573. }
  1574. static int dib7090_tuner_xfer(struct i2c_adapter *i2c_adap, struct i2c_msg msg[], int num)
  1575. {
  1576. struct dib7000p_state *state = i2c_get_adapdata(i2c_adap);
  1577. u16 apb_address = 0, word;
  1578. int i = 0;
  1579. switch (msg[0].buf[0]) {
  1580. case 0x12:
  1581. apb_address = 1920;
  1582. break;
  1583. case 0x14:
  1584. apb_address = 1921;
  1585. break;
  1586. case 0x24:
  1587. apb_address = 1922;
  1588. break;
  1589. case 0x1a:
  1590. apb_address = 1923;
  1591. break;
  1592. case 0x22:
  1593. apb_address = 1924;
  1594. break;
  1595. case 0x33:
  1596. apb_address = 1926;
  1597. break;
  1598. case 0x34:
  1599. apb_address = 1927;
  1600. break;
  1601. case 0x35:
  1602. apb_address = 1928;
  1603. break;
  1604. case 0x36:
  1605. apb_address = 1929;
  1606. break;
  1607. case 0x37:
  1608. apb_address = 1930;
  1609. break;
  1610. case 0x38:
  1611. apb_address = 1931;
  1612. break;
  1613. case 0x39:
  1614. apb_address = 1932;
  1615. break;
  1616. case 0x2a:
  1617. apb_address = 1935;
  1618. break;
  1619. case 0x2b:
  1620. apb_address = 1936;
  1621. break;
  1622. case 0x2c:
  1623. apb_address = 1937;
  1624. break;
  1625. case 0x2d:
  1626. apb_address = 1938;
  1627. break;
  1628. case 0x2e:
  1629. apb_address = 1939;
  1630. break;
  1631. case 0x2f:
  1632. apb_address = 1940;
  1633. break;
  1634. case 0x30:
  1635. apb_address = 1941;
  1636. break;
  1637. case 0x31:
  1638. apb_address = 1942;
  1639. break;
  1640. case 0x32:
  1641. apb_address = 1943;
  1642. break;
  1643. case 0x3e:
  1644. apb_address = 1944;
  1645. break;
  1646. case 0x3f:
  1647. apb_address = 1945;
  1648. break;
  1649. case 0x40:
  1650. apb_address = 1948;
  1651. break;
  1652. case 0x25:
  1653. apb_address = 914;
  1654. break;
  1655. case 0x26:
  1656. apb_address = 915;
  1657. break;
  1658. case 0x27:
  1659. apb_address = 917;
  1660. break;
  1661. case 0x28:
  1662. apb_address = 916;
  1663. break;
  1664. case 0x1d:
  1665. i = ((dib7000p_read_word(state, 72) >> 12) & 0x3);
  1666. word = dib7000p_read_word(state, 384 + i);
  1667. msg[1].buf[0] = (word >> 8) & 0xff;
  1668. msg[1].buf[1] = (word) & 0xff;
  1669. return num;
  1670. case 0x1f:
  1671. if (num == 1) { /* write */
  1672. word = (u16) ((msg[0].buf[1] << 8) | msg[0].buf[2]);
  1673. word &= 0x3;
  1674. word = (dib7000p_read_word(state, 72) & ~(3 << 12)) | (word << 12);
  1675. dib7000p_write_word(state, 72, word); /* Set the proper input */
  1676. return num;
  1677. }
  1678. }
  1679. if (apb_address != 0) /* R/W acces via APB */
  1680. return dib7090p_rw_on_apb(i2c_adap, msg, num, apb_address);
  1681. else /* R/W access via SERPAR */
  1682. return w7090p_tuner_rw_serpar(i2c_adap, msg, num);
  1683. return 0;
  1684. }
  1685. static u32 dib7000p_i2c_func(struct i2c_adapter *adapter)
  1686. {
  1687. return I2C_FUNC_I2C;
  1688. }
  1689. static struct i2c_algorithm dib7090_tuner_xfer_algo = {
  1690. .master_xfer = dib7090_tuner_xfer,
  1691. .functionality = dib7000p_i2c_func,
  1692. };
  1693. struct i2c_adapter *dib7090_get_i2c_tuner(struct dvb_frontend *fe)
  1694. {
  1695. struct dib7000p_state *st = fe->demodulator_priv;
  1696. return &st->dib7090_tuner_adap;
  1697. }
  1698. EXPORT_SYMBOL(dib7090_get_i2c_tuner);
  1699. static int dib7090_host_bus_drive(struct dib7000p_state *state, u8 drive)
  1700. {
  1701. u16 reg;
  1702. /* drive host bus 2, 3, 4 */
  1703. reg = dib7000p_read_word(state, 1798) & ~((0x7) | (0x7 << 6) | (0x7 << 12));
  1704. reg |= (drive << 12) | (drive << 6) | drive;
  1705. dib7000p_write_word(state, 1798, reg);
  1706. /* drive host bus 5,6 */
  1707. reg = dib7000p_read_word(state, 1799) & ~((0x7 << 2) | (0x7 << 8));
  1708. reg |= (drive << 8) | (drive << 2);
  1709. dib7000p_write_word(state, 1799, reg);
  1710. /* drive host bus 7, 8, 9 */
  1711. reg = dib7000p_read_word(state, 1800) & ~((0x7) | (0x7 << 6) | (0x7 << 12));
  1712. reg |= (drive << 12) | (drive << 6) | drive;
  1713. dib7000p_write_word(state, 1800, reg);
  1714. /* drive host bus 10, 11 */
  1715. reg = dib7000p_read_word(state, 1801) & ~((0x7 << 2) | (0x7 << 8));
  1716. reg |= (drive << 8) | (drive << 2);
  1717. dib7000p_write_word(state, 1801, reg);
  1718. /* drive host bus 12, 13, 14 */
  1719. reg = dib7000p_read_word(state, 1802) & ~((0x7) | (0x7 << 6) | (0x7 << 12));
  1720. reg |= (drive << 12) | (drive << 6) | drive;
  1721. dib7000p_write_word(state, 1802, reg);
  1722. return 0;
  1723. }
  1724. static u32 dib7090_calcSyncFreq(u32 P_Kin, u32 P_Kout, u32 insertExtSynchro, u32 syncSize)
  1725. {
  1726. u32 quantif = 3;
  1727. u32 nom = (insertExtSynchro * P_Kin + syncSize);
  1728. u32 denom = P_Kout;
  1729. u32 syncFreq = ((nom << quantif) / denom);
  1730. if ((syncFreq & ((1 << quantif) - 1)) != 0)
  1731. syncFreq = (syncFreq >> quantif) + 1;
  1732. else
  1733. syncFreq = (syncFreq >> quantif);
  1734. if (syncFreq != 0)
  1735. syncFreq = syncFreq - 1;
  1736. return syncFreq;
  1737. }
  1738. static int dib7090_cfg_DibTx(struct dib7000p_state *state, u32 P_Kin, u32 P_Kout, u32 insertExtSynchro, u32 synchroMode, u32 syncWord, u32 syncSize)
  1739. {
  1740. dprintk("Configure DibStream Tx");
  1741. dib7000p_write_word(state, 1615, 1);
  1742. dib7000p_write_word(state, 1603, P_Kin);
  1743. dib7000p_write_word(state, 1605, P_Kout);
  1744. dib7000p_write_word(state, 1606, insertExtSynchro);
  1745. dib7000p_write_word(state, 1608, synchroMode);
  1746. dib7000p_write_word(state, 1609, (syncWord >> 16) & 0xffff);
  1747. dib7000p_write_word(state, 1610, syncWord & 0xffff);
  1748. dib7000p_write_word(state, 1612, syncSize);
  1749. dib7000p_write_word(state, 1615, 0);
  1750. return 0;
  1751. }
  1752. static int dib7090_cfg_DibRx(struct dib7000p_state *state, u32 P_Kin, u32 P_Kout, u32 synchroMode, u32 insertExtSynchro, u32 syncWord, u32 syncSize,
  1753. u32 dataOutRate)
  1754. {
  1755. u32 syncFreq;
  1756. dprintk("Configure DibStream Rx");
  1757. if ((P_Kin != 0) && (P_Kout != 0)) {
  1758. syncFreq = dib7090_calcSyncFreq(P_Kin, P_Kout, insertExtSynchro, syncSize);
  1759. dib7000p_write_word(state, 1542, syncFreq);
  1760. }
  1761. dib7000p_write_word(state, 1554, 1);
  1762. dib7000p_write_word(state, 1536, P_Kin);
  1763. dib7000p_write_word(state, 1537, P_Kout);
  1764. dib7000p_write_word(state, 1539, synchroMode);
  1765. dib7000p_write_word(state, 1540, (syncWord >> 16) & 0xffff);
  1766. dib7000p_write_word(state, 1541, syncWord & 0xffff);
  1767. dib7000p_write_word(state, 1543, syncSize);
  1768. dib7000p_write_word(state, 1544, dataOutRate);
  1769. dib7000p_write_word(state, 1554, 0);
  1770. return 0;
  1771. }
  1772. static void dib7090_enMpegMux(struct dib7000p_state *state, int onoff)
  1773. {
  1774. u16 reg_1287 = dib7000p_read_word(state, 1287);
  1775. switch (onoff) {
  1776. case 1:
  1777. reg_1287 &= ~(1<<7);
  1778. break;
  1779. case 0:
  1780. reg_1287 |= (1<<7);
  1781. break;
  1782. }
  1783. dib7000p_write_word(state, 1287, reg_1287);
  1784. }
  1785. static void dib7090_configMpegMux(struct dib7000p_state *state,
  1786. u16 pulseWidth, u16 enSerialMode, u16 enSerialClkDiv2)
  1787. {
  1788. dprintk("Enable Mpeg mux");
  1789. dib7090_enMpegMux(state, 0);
  1790. /* If the input mode is MPEG do not divide the serial clock */
  1791. if ((enSerialMode == 1) && (state->input_mode_mpeg == 1))
  1792. enSerialClkDiv2 = 0;
  1793. dib7000p_write_word(state, 1287, ((pulseWidth & 0x1f) << 2)
  1794. | ((enSerialMode & 0x1) << 1)
  1795. | (enSerialClkDiv2 & 0x1));
  1796. dib7090_enMpegMux(state, 1);
  1797. }
  1798. static void dib7090_setDibTxMux(struct dib7000p_state *state, int mode)
  1799. {
  1800. u16 reg_1288 = dib7000p_read_word(state, 1288) & ~(0x7 << 7);
  1801. switch (mode) {
  1802. case MPEG_ON_DIBTX:
  1803. dprintk("SET MPEG ON DIBSTREAM TX");
  1804. dib7090_cfg_DibTx(state, 8, 5, 0, 0, 0, 0);
  1805. reg_1288 |= (1<<9);
  1806. break;
  1807. case DIV_ON_DIBTX:
  1808. dprintk("SET DIV_OUT ON DIBSTREAM TX");
  1809. dib7090_cfg_DibTx(state, 5, 5, 0, 0, 0, 0);
  1810. reg_1288 |= (1<<8);
  1811. break;
  1812. case ADC_ON_DIBTX:
  1813. dprintk("SET ADC_OUT ON DIBSTREAM TX");
  1814. dib7090_cfg_DibTx(state, 20, 5, 10, 0, 0, 0);
  1815. reg_1288 |= (1<<7);
  1816. break;
  1817. default:
  1818. break;
  1819. }
  1820. dib7000p_write_word(state, 1288, reg_1288);
  1821. }
  1822. static void dib7090_setHostBusMux(struct dib7000p_state *state, int mode)
  1823. {
  1824. u16 reg_1288 = dib7000p_read_word(state, 1288) & ~(0x7 << 4);
  1825. switch (mode) {
  1826. case DEMOUT_ON_HOSTBUS:
  1827. dprintk("SET DEM OUT OLD INTERF ON HOST BUS");
  1828. dib7090_enMpegMux(state, 0);
  1829. reg_1288 |= (1<<6);
  1830. break;
  1831. case DIBTX_ON_HOSTBUS:
  1832. dprintk("SET DIBSTREAM TX ON HOST BUS");
  1833. dib7090_enMpegMux(state, 0);
  1834. reg_1288 |= (1<<5);
  1835. break;
  1836. case MPEG_ON_HOSTBUS:
  1837. dprintk("SET MPEG MUX ON HOST BUS");
  1838. reg_1288 |= (1<<4);
  1839. break;
  1840. default:
  1841. break;
  1842. }
  1843. dib7000p_write_word(state, 1288, reg_1288);
  1844. }
  1845. int dib7090_set_diversity_in(struct dvb_frontend *fe, int onoff)
  1846. {
  1847. struct dib7000p_state *state = fe->demodulator_priv;
  1848. u16 reg_1287;
  1849. switch (onoff) {
  1850. case 0: /* only use the internal way - not the diversity input */
  1851. dprintk("%s mode OFF : by default Enable Mpeg INPUT", __func__);
  1852. dib7090_cfg_DibRx(state, 8, 5, 0, 0, 0, 8, 0);
  1853. /* Do not divide the serial clock of MPEG MUX */
  1854. /* in SERIAL MODE in case input mode MPEG is used */
  1855. reg_1287 = dib7000p_read_word(state, 1287);
  1856. /* enSerialClkDiv2 == 1 ? */
  1857. if ((reg_1287 & 0x1) == 1) {
  1858. /* force enSerialClkDiv2 = 0 */
  1859. reg_1287 &= ~0x1;
  1860. dib7000p_write_word(state, 1287, reg_1287);
  1861. }
  1862. state->input_mode_mpeg = 1;
  1863. break;
  1864. case 1: /* both ways */
  1865. case 2: /* only the diversity input */
  1866. dprintk("%s ON : Enable diversity INPUT", __func__);
  1867. dib7090_cfg_DibRx(state, 5, 5, 0, 0, 0, 0, 0);
  1868. state->input_mode_mpeg = 0;
  1869. break;
  1870. }
  1871. dib7000p_set_diversity_in(&state->demod, onoff);
  1872. return 0;
  1873. }
  1874. static int dib7090_set_output_mode(struct dvb_frontend *fe, int mode)
  1875. {
  1876. struct dib7000p_state *state = fe->demodulator_priv;
  1877. u16 outreg, smo_mode, fifo_threshold;
  1878. u8 prefer_mpeg_mux_use = 1;
  1879. int ret = 0;
  1880. dib7090_host_bus_drive(state, 1);
  1881. fifo_threshold = 1792;
  1882. smo_mode = (dib7000p_read_word(state, 235) & 0x0050) | (1 << 1);
  1883. outreg = dib7000p_read_word(state, 1286) & ~((1 << 10) | (0x7 << 6) | (1 << 1));
  1884. switch (mode) {
  1885. case OUTMODE_HIGH_Z:
  1886. outreg = 0;
  1887. break;
  1888. case OUTMODE_MPEG2_SERIAL:
  1889. if (prefer_mpeg_mux_use) {
  1890. dprintk("setting output mode TS_SERIAL using Mpeg Mux");
  1891. dib7090_configMpegMux(state, 3, 1, 1);
  1892. dib7090_setHostBusMux(state, MPEG_ON_HOSTBUS);
  1893. } else {/* Use Smooth block */
  1894. dprintk("setting output mode TS_SERIAL using Smooth bloc");
  1895. dib7090_setHostBusMux(state, DEMOUT_ON_HOSTBUS);
  1896. outreg |= (2<<6) | (0 << 1);
  1897. }
  1898. break;
  1899. case OUTMODE_MPEG2_PAR_GATED_CLK:
  1900. if (prefer_mpeg_mux_use) {
  1901. dprintk("setting output mode TS_PARALLEL_GATED using Mpeg Mux");
  1902. dib7090_configMpegMux(state, 2, 0, 0);
  1903. dib7090_setHostBusMux(state, MPEG_ON_HOSTBUS);
  1904. } else { /* Use Smooth block */
  1905. dprintk("setting output mode TS_PARALLEL_GATED using Smooth block");
  1906. dib7090_setHostBusMux(state, DEMOUT_ON_HOSTBUS);
  1907. outreg |= (0<<6);
  1908. }
  1909. break;
  1910. case OUTMODE_MPEG2_PAR_CONT_CLK: /* Using Smooth block only */
  1911. dprintk("setting output mode TS_PARALLEL_CONT using Smooth block");
  1912. dib7090_setHostBusMux(state, DEMOUT_ON_HOSTBUS);
  1913. outreg |= (1<<6);
  1914. break;
  1915. case OUTMODE_MPEG2_FIFO: /* Using Smooth block because not supported by new Mpeg Mux bloc */
  1916. dprintk("setting output mode TS_FIFO using Smooth block");
  1917. dib7090_setHostBusMux(state, DEMOUT_ON_HOSTBUS);
  1918. outreg |= (5<<6);
  1919. smo_mode |= (3 << 1);
  1920. fifo_threshold = 512;
  1921. break;
  1922. case OUTMODE_DIVERSITY:
  1923. dprintk("setting output mode MODE_DIVERSITY");
  1924. dib7090_setDibTxMux(state, DIV_ON_DIBTX);
  1925. dib7090_setHostBusMux(state, DIBTX_ON_HOSTBUS);
  1926. break;
  1927. case OUTMODE_ANALOG_ADC:
  1928. dprintk("setting output mode MODE_ANALOG_ADC");
  1929. dib7090_setDibTxMux(state, ADC_ON_DIBTX);
  1930. dib7090_setHostBusMux(state, DIBTX_ON_HOSTBUS);
  1931. break;
  1932. }
  1933. if (mode != OUTMODE_HIGH_Z)
  1934. outreg |= (1 << 10);
  1935. if (state->cfg.output_mpeg2_in_188_bytes)
  1936. smo_mode |= (1 << 5);
  1937. ret |= dib7000p_write_word(state, 235, smo_mode);
  1938. ret |= dib7000p_write_word(state, 236, fifo_threshold); /* synchronous fread */
  1939. ret |= dib7000p_write_word(state, 1286, outreg);
  1940. return ret;
  1941. }
  1942. int dib7090_tuner_sleep(struct dvb_frontend *fe, int onoff)
  1943. {
  1944. struct dib7000p_state *state = fe->demodulator_priv;
  1945. u16 en_cur_state;
  1946. dprintk("sleep dib7090: %d", onoff);
  1947. en_cur_state = dib7000p_read_word(state, 1922);
  1948. if (en_cur_state > 0xff)
  1949. state->tuner_enable = en_cur_state;
  1950. if (onoff)
  1951. en_cur_state &= 0x00ff;
  1952. else {
  1953. if (state->tuner_enable != 0)
  1954. en_cur_state = state->tuner_enable;
  1955. }
  1956. dib7000p_write_word(state, 1922, en_cur_state);
  1957. return 0;
  1958. }
  1959. EXPORT_SYMBOL(dib7090_tuner_sleep);
  1960. int dib7090_get_adc_power(struct dvb_frontend *fe)
  1961. {
  1962. return dib7000p_get_adc_power(fe);
  1963. }
  1964. EXPORT_SYMBOL(dib7090_get_adc_power);
  1965. int dib7090_slave_reset(struct dvb_frontend *fe)
  1966. {
  1967. struct dib7000p_state *state = fe->demodulator_priv;
  1968. u16 reg;
  1969. reg = dib7000p_read_word(state, 1794);
  1970. dib7000p_write_word(state, 1794, reg | (4 << 12));
  1971. dib7000p_write_word(state, 1032, 0xffff);
  1972. return 0;
  1973. }
  1974. EXPORT_SYMBOL(dib7090_slave_reset);
  1975. static struct dvb_frontend_ops dib7000p_ops;
  1976. struct dvb_frontend *dib7000p_attach(struct i2c_adapter *i2c_adap, u8 i2c_addr, struct dib7000p_config *cfg)
  1977. {
  1978. struct dvb_frontend *demod;
  1979. struct dib7000p_state *st;
  1980. st = kzalloc(sizeof(struct dib7000p_state), GFP_KERNEL);
  1981. if (st == NULL)
  1982. return NULL;
  1983. memcpy(&st->cfg, cfg, sizeof(struct dib7000p_config));
  1984. st->i2c_adap = i2c_adap;
  1985. st->i2c_addr = i2c_addr;
  1986. st->gpio_val = cfg->gpio_val;
  1987. st->gpio_dir = cfg->gpio_dir;
  1988. /* Ensure the output mode remains at the previous default if it's
  1989. * not specifically set by the caller.
  1990. */
  1991. if ((st->cfg.output_mode != OUTMODE_MPEG2_SERIAL) && (st->cfg.output_mode != OUTMODE_MPEG2_PAR_GATED_CLK))
  1992. st->cfg.output_mode = OUTMODE_MPEG2_FIFO;
  1993. demod = &st->demod;
  1994. demod->demodulator_priv = st;
  1995. memcpy(&st->demod.ops, &dib7000p_ops, sizeof(struct dvb_frontend_ops));
  1996. mutex_init(&st->i2c_buffer_lock);
  1997. dib7000p_write_word(st, 1287, 0x0003); /* sram lead in, rdy */
  1998. if (dib7000p_identify(st) != 0)
  1999. goto error;
  2000. st->version = dib7000p_read_word(st, 897);
  2001. /* FIXME: make sure the dev.parent field is initialized, or else
  2002. request_firmware() will hit an OOPS (this should be moved somewhere
  2003. more common) */
  2004. st->i2c_master.gated_tuner_i2c_adap.dev.parent = i2c_adap->dev.parent;
  2005. /* FIXME: make sure the dev.parent field is initialized, or else
  2006. request_firmware() will hit an OOPS (this should be moved somewhere
  2007. more common) */
  2008. st->i2c_master.gated_tuner_i2c_adap.dev.parent = i2c_adap->dev.parent;
  2009. dibx000_init_i2c_master(&st->i2c_master, DIB7000P, st->i2c_adap, st->i2c_addr);
  2010. /* init 7090 tuner adapter */
  2011. strncpy(st->dib7090_tuner_adap.name, "DiB7090 tuner interface", sizeof(st->dib7090_tuner_adap.name));
  2012. st->dib7090_tuner_adap.algo = &dib7090_tuner_xfer_algo;
  2013. st->dib7090_tuner_adap.algo_data = NULL;
  2014. st->dib7090_tuner_adap.dev.parent = st->i2c_adap->dev.parent;
  2015. i2c_set_adapdata(&st->dib7090_tuner_adap, st);
  2016. i2c_add_adapter(&st->dib7090_tuner_adap);
  2017. dib7000p_demod_reset(st);
  2018. if (st->version == SOC7090) {
  2019. dib7090_set_output_mode(demod, st->cfg.output_mode);
  2020. dib7090_set_diversity_in(demod, 0);
  2021. }
  2022. return demod;
  2023. error:
  2024. kfree(st);
  2025. return NULL;
  2026. }
  2027. EXPORT_SYMBOL(dib7000p_attach);
  2028. static struct dvb_frontend_ops dib7000p_ops = {
  2029. .info = {
  2030. .name = "DiBcom 7000PC",
  2031. .type = FE_OFDM,
  2032. .frequency_min = 44250000,
  2033. .frequency_max = 867250000,
  2034. .frequency_stepsize = 62500,
  2035. .caps = FE_CAN_INVERSION_AUTO |
  2036. FE_CAN_FEC_1_2 | FE_CAN_FEC_2_3 | FE_CAN_FEC_3_4 |
  2037. FE_CAN_FEC_5_6 | FE_CAN_FEC_7_8 | FE_CAN_FEC_AUTO |
  2038. FE_CAN_QPSK | FE_CAN_QAM_16 | FE_CAN_QAM_64 | FE_CAN_QAM_AUTO |
  2039. FE_CAN_TRANSMISSION_MODE_AUTO | FE_CAN_GUARD_INTERVAL_AUTO | FE_CAN_RECOVER | FE_CAN_HIERARCHY_AUTO,
  2040. },
  2041. .release = dib7000p_release,
  2042. .init = dib7000p_wakeup,
  2043. .sleep = dib7000p_sleep,
  2044. .set_frontend = dib7000p_set_frontend,
  2045. .get_tune_settings = dib7000p_fe_get_tune_settings,
  2046. .get_frontend = dib7000p_get_frontend,
  2047. .read_status = dib7000p_read_status,
  2048. .read_ber = dib7000p_read_ber,
  2049. .read_signal_strength = dib7000p_read_signal_strength,
  2050. .read_snr = dib7000p_read_snr,
  2051. .read_ucblocks = dib7000p_read_unc_blocks,
  2052. };
  2053. MODULE_AUTHOR("Olivier Grenie <ogrenie@dibcom.fr>");
  2054. MODULE_AUTHOR("Patrick Boettcher <pboettcher@dibcom.fr>");
  2055. MODULE_DESCRIPTION("Driver for the DiBcom 7000PC COFDM demodulator");
  2056. MODULE_LICENSE("GPL");