dib7000p.c 64 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462
  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. {
  1157. struct dtv_frontend_properties *fep = &fe->dtv_property_cache;
  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;
  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. dib7000p_autosearch_start(fe);
  1263. do {
  1264. msleep(1);
  1265. found = dib7000p_autosearch_is_irq(fe);
  1266. } while (found == 0 && i--);
  1267. dprintk("autosearch returns: %d", found);
  1268. if (found == 0 || found == 1)
  1269. return 0;
  1270. dib7000p_get_frontend(fe);
  1271. }
  1272. ret = dib7000p_tune(fe);
  1273. /* make this a config parameter */
  1274. if (state->version == SOC7090) {
  1275. dib7090_set_output_mode(fe, state->cfg.output_mode);
  1276. if (state->cfg.enMpegOutput == 0) {
  1277. dib7090_setDibTxMux(state, MPEG_ON_DIBTX);
  1278. dib7090_setHostBusMux(state, DIBTX_ON_HOSTBUS);
  1279. }
  1280. } else
  1281. dib7000p_set_output_mode(state, state->cfg.output_mode);
  1282. return ret;
  1283. }
  1284. static int dib7000p_read_status(struct dvb_frontend *fe, fe_status_t * stat)
  1285. {
  1286. struct dib7000p_state *state = fe->demodulator_priv;
  1287. u16 lock = dib7000p_read_word(state, 509);
  1288. *stat = 0;
  1289. if (lock & 0x8000)
  1290. *stat |= FE_HAS_SIGNAL;
  1291. if (lock & 0x3000)
  1292. *stat |= FE_HAS_CARRIER;
  1293. if (lock & 0x0100)
  1294. *stat |= FE_HAS_VITERBI;
  1295. if (lock & 0x0010)
  1296. *stat |= FE_HAS_SYNC;
  1297. if ((lock & 0x0038) == 0x38)
  1298. *stat |= FE_HAS_LOCK;
  1299. return 0;
  1300. }
  1301. static int dib7000p_read_ber(struct dvb_frontend *fe, u32 * ber)
  1302. {
  1303. struct dib7000p_state *state = fe->demodulator_priv;
  1304. *ber = (dib7000p_read_word(state, 500) << 16) | dib7000p_read_word(state, 501);
  1305. return 0;
  1306. }
  1307. static int dib7000p_read_unc_blocks(struct dvb_frontend *fe, u32 * unc)
  1308. {
  1309. struct dib7000p_state *state = fe->demodulator_priv;
  1310. *unc = dib7000p_read_word(state, 506);
  1311. return 0;
  1312. }
  1313. static int dib7000p_read_signal_strength(struct dvb_frontend *fe, u16 * strength)
  1314. {
  1315. struct dib7000p_state *state = fe->demodulator_priv;
  1316. u16 val = dib7000p_read_word(state, 394);
  1317. *strength = 65535 - val;
  1318. return 0;
  1319. }
  1320. static int dib7000p_read_snr(struct dvb_frontend *fe, u16 * snr)
  1321. {
  1322. struct dib7000p_state *state = fe->demodulator_priv;
  1323. u16 val;
  1324. s32 signal_mant, signal_exp, noise_mant, noise_exp;
  1325. u32 result = 0;
  1326. val = dib7000p_read_word(state, 479);
  1327. noise_mant = (val >> 4) & 0xff;
  1328. noise_exp = ((val & 0xf) << 2);
  1329. val = dib7000p_read_word(state, 480);
  1330. noise_exp += ((val >> 14) & 0x3);
  1331. if ((noise_exp & 0x20) != 0)
  1332. noise_exp -= 0x40;
  1333. signal_mant = (val >> 6) & 0xFF;
  1334. signal_exp = (val & 0x3F);
  1335. if ((signal_exp & 0x20) != 0)
  1336. signal_exp -= 0x40;
  1337. if (signal_mant != 0)
  1338. result = intlog10(2) * 10 * signal_exp + 10 * intlog10(signal_mant);
  1339. else
  1340. result = intlog10(2) * 10 * signal_exp - 100;
  1341. if (noise_mant != 0)
  1342. result -= intlog10(2) * 10 * noise_exp + 10 * intlog10(noise_mant);
  1343. else
  1344. result -= intlog10(2) * 10 * noise_exp - 100;
  1345. *snr = result / ((1 << 24) / 10);
  1346. return 0;
  1347. }
  1348. static int dib7000p_fe_get_tune_settings(struct dvb_frontend *fe, struct dvb_frontend_tune_settings *tune)
  1349. {
  1350. tune->min_delay_ms = 1000;
  1351. return 0;
  1352. }
  1353. static void dib7000p_release(struct dvb_frontend *demod)
  1354. {
  1355. struct dib7000p_state *st = demod->demodulator_priv;
  1356. dibx000_exit_i2c_master(&st->i2c_master);
  1357. i2c_del_adapter(&st->dib7090_tuner_adap);
  1358. kfree(st);
  1359. }
  1360. int dib7000pc_detection(struct i2c_adapter *i2c_adap)
  1361. {
  1362. u8 *tx, *rx;
  1363. struct i2c_msg msg[2] = {
  1364. {.addr = 18 >> 1, .flags = 0, .len = 2},
  1365. {.addr = 18 >> 1, .flags = I2C_M_RD, .len = 2},
  1366. };
  1367. int ret = 0;
  1368. tx = kzalloc(2*sizeof(u8), GFP_KERNEL);
  1369. if (!tx)
  1370. return -ENOMEM;
  1371. rx = kzalloc(2*sizeof(u8), GFP_KERNEL);
  1372. if (!rx) {
  1373. ret = -ENOMEM;
  1374. goto rx_memory_error;
  1375. }
  1376. msg[0].buf = tx;
  1377. msg[1].buf = rx;
  1378. tx[0] = 0x03;
  1379. tx[1] = 0x00;
  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. msg[0].addr = msg[1].addr = 0x40;
  1386. if (i2c_transfer(i2c_adap, msg, 2) == 2)
  1387. if (rx[0] == 0x01 && rx[1] == 0xb3) {
  1388. dprintk("-D- DiB7000PC detected");
  1389. return 1;
  1390. }
  1391. dprintk("-D- DiB7000PC not detected");
  1392. kfree(rx);
  1393. rx_memory_error:
  1394. kfree(tx);
  1395. return ret;
  1396. }
  1397. EXPORT_SYMBOL(dib7000pc_detection);
  1398. struct i2c_adapter *dib7000p_get_i2c_master(struct dvb_frontend *demod, enum dibx000_i2c_interface intf, int gating)
  1399. {
  1400. struct dib7000p_state *st = demod->demodulator_priv;
  1401. return dibx000_get_i2c_adapter(&st->i2c_master, intf, gating);
  1402. }
  1403. EXPORT_SYMBOL(dib7000p_get_i2c_master);
  1404. int dib7000p_pid_filter_ctrl(struct dvb_frontend *fe, u8 onoff)
  1405. {
  1406. struct dib7000p_state *state = fe->demodulator_priv;
  1407. u16 val = dib7000p_read_word(state, 235) & 0xffef;
  1408. val |= (onoff & 0x1) << 4;
  1409. dprintk("PID filter enabled %d", onoff);
  1410. return dib7000p_write_word(state, 235, val);
  1411. }
  1412. EXPORT_SYMBOL(dib7000p_pid_filter_ctrl);
  1413. int dib7000p_pid_filter(struct dvb_frontend *fe, u8 id, u16 pid, u8 onoff)
  1414. {
  1415. struct dib7000p_state *state = fe->demodulator_priv;
  1416. dprintk("PID filter: index %x, PID %d, OnOff %d", id, pid, onoff);
  1417. return dib7000p_write_word(state, 241 + id, onoff ? (1 << 13) | pid : 0);
  1418. }
  1419. EXPORT_SYMBOL(dib7000p_pid_filter);
  1420. int dib7000p_i2c_enumeration(struct i2c_adapter *i2c, int no_of_demods, u8 default_addr, struct dib7000p_config cfg[])
  1421. {
  1422. struct dib7000p_state *dpst;
  1423. int k = 0;
  1424. u8 new_addr = 0;
  1425. dpst = kzalloc(sizeof(struct dib7000p_state), GFP_KERNEL);
  1426. if (!dpst)
  1427. return -ENOMEM;
  1428. dpst->i2c_adap = i2c;
  1429. mutex_init(&dpst->i2c_buffer_lock);
  1430. for (k = no_of_demods - 1; k >= 0; k--) {
  1431. dpst->cfg = cfg[k];
  1432. /* designated i2c address */
  1433. if (cfg[k].default_i2c_addr != 0)
  1434. new_addr = cfg[k].default_i2c_addr + (k << 1);
  1435. else
  1436. new_addr = (0x40 + k) << 1;
  1437. dpst->i2c_addr = new_addr;
  1438. dib7000p_write_word(dpst, 1287, 0x0003); /* sram lead in, rdy */
  1439. if (dib7000p_identify(dpst) != 0) {
  1440. dpst->i2c_addr = default_addr;
  1441. dib7000p_write_word(dpst, 1287, 0x0003); /* sram lead in, rdy */
  1442. if (dib7000p_identify(dpst) != 0) {
  1443. dprintk("DiB7000P #%d: not identified\n", k);
  1444. kfree(dpst);
  1445. return -EIO;
  1446. }
  1447. }
  1448. /* start diversity to pull_down div_str - just for i2c-enumeration */
  1449. dib7000p_set_output_mode(dpst, OUTMODE_DIVERSITY);
  1450. /* set new i2c address and force divstart */
  1451. dib7000p_write_word(dpst, 1285, (new_addr << 2) | 0x2);
  1452. dprintk("IC %d initialized (to i2c_address 0x%x)", k, new_addr);
  1453. }
  1454. for (k = 0; k < no_of_demods; k++) {
  1455. dpst->cfg = cfg[k];
  1456. if (cfg[k].default_i2c_addr != 0)
  1457. dpst->i2c_addr = (cfg[k].default_i2c_addr + k) << 1;
  1458. else
  1459. dpst->i2c_addr = (0x40 + k) << 1;
  1460. // unforce divstr
  1461. dib7000p_write_word(dpst, 1285, dpst->i2c_addr << 2);
  1462. /* deactivate div - it was just for i2c-enumeration */
  1463. dib7000p_set_output_mode(dpst, OUTMODE_HIGH_Z);
  1464. }
  1465. kfree(dpst);
  1466. return 0;
  1467. }
  1468. EXPORT_SYMBOL(dib7000p_i2c_enumeration);
  1469. static const s32 lut_1000ln_mant[] = {
  1470. 6908, 6956, 7003, 7047, 7090, 7131, 7170, 7208, 7244, 7279, 7313, 7346, 7377, 7408, 7438, 7467, 7495, 7523, 7549, 7575, 7600
  1471. };
  1472. static s32 dib7000p_get_adc_power(struct dvb_frontend *fe)
  1473. {
  1474. struct dib7000p_state *state = fe->demodulator_priv;
  1475. u32 tmp_val = 0, exp = 0, mant = 0;
  1476. s32 pow_i;
  1477. u16 buf[2];
  1478. u8 ix = 0;
  1479. buf[0] = dib7000p_read_word(state, 0x184);
  1480. buf[1] = dib7000p_read_word(state, 0x185);
  1481. pow_i = (buf[0] << 16) | buf[1];
  1482. dprintk("raw pow_i = %d", pow_i);
  1483. tmp_val = pow_i;
  1484. while (tmp_val >>= 1)
  1485. exp++;
  1486. mant = (pow_i * 1000 / (1 << exp));
  1487. dprintk(" mant = %d exp = %d", mant / 1000, exp);
  1488. ix = (u8) ((mant - 1000) / 100); /* index of the LUT */
  1489. dprintk(" ix = %d", ix);
  1490. pow_i = (lut_1000ln_mant[ix] + 693 * (exp - 20) - 6908);
  1491. pow_i = (pow_i << 8) / 1000;
  1492. dprintk(" pow_i = %d", pow_i);
  1493. return pow_i;
  1494. }
  1495. static int map_addr_to_serpar_number(struct i2c_msg *msg)
  1496. {
  1497. if ((msg->buf[0] <= 15))
  1498. msg->buf[0] -= 1;
  1499. else if (msg->buf[0] == 17)
  1500. msg->buf[0] = 15;
  1501. else if (msg->buf[0] == 16)
  1502. msg->buf[0] = 17;
  1503. else if (msg->buf[0] == 19)
  1504. msg->buf[0] = 16;
  1505. else if (msg->buf[0] >= 21 && msg->buf[0] <= 25)
  1506. msg->buf[0] -= 3;
  1507. else if (msg->buf[0] == 28)
  1508. msg->buf[0] = 23;
  1509. else
  1510. return -EINVAL;
  1511. return 0;
  1512. }
  1513. static int w7090p_tuner_write_serpar(struct i2c_adapter *i2c_adap, struct i2c_msg msg[], int num)
  1514. {
  1515. struct dib7000p_state *state = i2c_get_adapdata(i2c_adap);
  1516. u8 n_overflow = 1;
  1517. u16 i = 1000;
  1518. u16 serpar_num = msg[0].buf[0];
  1519. while (n_overflow == 1 && i) {
  1520. n_overflow = (dib7000p_read_word(state, 1984) >> 1) & 0x1;
  1521. i--;
  1522. if (i == 0)
  1523. dprintk("Tuner ITF: write busy (overflow)");
  1524. }
  1525. dib7000p_write_word(state, 1985, (1 << 6) | (serpar_num & 0x3f));
  1526. dib7000p_write_word(state, 1986, (msg[0].buf[1] << 8) | msg[0].buf[2]);
  1527. return num;
  1528. }
  1529. static int w7090p_tuner_read_serpar(struct i2c_adapter *i2c_adap, struct i2c_msg msg[], int num)
  1530. {
  1531. struct dib7000p_state *state = i2c_get_adapdata(i2c_adap);
  1532. u8 n_overflow = 1, n_empty = 1;
  1533. u16 i = 1000;
  1534. u16 serpar_num = msg[0].buf[0];
  1535. u16 read_word;
  1536. while (n_overflow == 1 && i) {
  1537. n_overflow = (dib7000p_read_word(state, 1984) >> 1) & 0x1;
  1538. i--;
  1539. if (i == 0)
  1540. dprintk("TunerITF: read busy (overflow)");
  1541. }
  1542. dib7000p_write_word(state, 1985, (0 << 6) | (serpar_num & 0x3f));
  1543. i = 1000;
  1544. while (n_empty == 1 && i) {
  1545. n_empty = dib7000p_read_word(state, 1984) & 0x1;
  1546. i--;
  1547. if (i == 0)
  1548. dprintk("TunerITF: read busy (empty)");
  1549. }
  1550. read_word = dib7000p_read_word(state, 1987);
  1551. msg[1].buf[0] = (read_word >> 8) & 0xff;
  1552. msg[1].buf[1] = (read_word) & 0xff;
  1553. return num;
  1554. }
  1555. static int w7090p_tuner_rw_serpar(struct i2c_adapter *i2c_adap, struct i2c_msg msg[], int num)
  1556. {
  1557. 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... */
  1558. if (num == 1) { /* write */
  1559. return w7090p_tuner_write_serpar(i2c_adap, msg, 1);
  1560. } else { /* read */
  1561. return w7090p_tuner_read_serpar(i2c_adap, msg, 2);
  1562. }
  1563. }
  1564. return num;
  1565. }
  1566. static int dib7090p_rw_on_apb(struct i2c_adapter *i2c_adap,
  1567. struct i2c_msg msg[], int num, u16 apb_address)
  1568. {
  1569. struct dib7000p_state *state = i2c_get_adapdata(i2c_adap);
  1570. u16 word;
  1571. if (num == 1) { /* write */
  1572. dib7000p_write_word(state, apb_address, ((msg[0].buf[1] << 8) | (msg[0].buf[2])));
  1573. } else {
  1574. word = dib7000p_read_word(state, apb_address);
  1575. msg[1].buf[0] = (word >> 8) & 0xff;
  1576. msg[1].buf[1] = (word) & 0xff;
  1577. }
  1578. return num;
  1579. }
  1580. static int dib7090_tuner_xfer(struct i2c_adapter *i2c_adap, struct i2c_msg msg[], int num)
  1581. {
  1582. struct dib7000p_state *state = i2c_get_adapdata(i2c_adap);
  1583. u16 apb_address = 0, word;
  1584. int i = 0;
  1585. switch (msg[0].buf[0]) {
  1586. case 0x12:
  1587. apb_address = 1920;
  1588. break;
  1589. case 0x14:
  1590. apb_address = 1921;
  1591. break;
  1592. case 0x24:
  1593. apb_address = 1922;
  1594. break;
  1595. case 0x1a:
  1596. apb_address = 1923;
  1597. break;
  1598. case 0x22:
  1599. apb_address = 1924;
  1600. break;
  1601. case 0x33:
  1602. apb_address = 1926;
  1603. break;
  1604. case 0x34:
  1605. apb_address = 1927;
  1606. break;
  1607. case 0x35:
  1608. apb_address = 1928;
  1609. break;
  1610. case 0x36:
  1611. apb_address = 1929;
  1612. break;
  1613. case 0x37:
  1614. apb_address = 1930;
  1615. break;
  1616. case 0x38:
  1617. apb_address = 1931;
  1618. break;
  1619. case 0x39:
  1620. apb_address = 1932;
  1621. break;
  1622. case 0x2a:
  1623. apb_address = 1935;
  1624. break;
  1625. case 0x2b:
  1626. apb_address = 1936;
  1627. break;
  1628. case 0x2c:
  1629. apb_address = 1937;
  1630. break;
  1631. case 0x2d:
  1632. apb_address = 1938;
  1633. break;
  1634. case 0x2e:
  1635. apb_address = 1939;
  1636. break;
  1637. case 0x2f:
  1638. apb_address = 1940;
  1639. break;
  1640. case 0x30:
  1641. apb_address = 1941;
  1642. break;
  1643. case 0x31:
  1644. apb_address = 1942;
  1645. break;
  1646. case 0x32:
  1647. apb_address = 1943;
  1648. break;
  1649. case 0x3e:
  1650. apb_address = 1944;
  1651. break;
  1652. case 0x3f:
  1653. apb_address = 1945;
  1654. break;
  1655. case 0x40:
  1656. apb_address = 1948;
  1657. break;
  1658. case 0x25:
  1659. apb_address = 914;
  1660. break;
  1661. case 0x26:
  1662. apb_address = 915;
  1663. break;
  1664. case 0x27:
  1665. apb_address = 917;
  1666. break;
  1667. case 0x28:
  1668. apb_address = 916;
  1669. break;
  1670. case 0x1d:
  1671. i = ((dib7000p_read_word(state, 72) >> 12) & 0x3);
  1672. word = dib7000p_read_word(state, 384 + i);
  1673. msg[1].buf[0] = (word >> 8) & 0xff;
  1674. msg[1].buf[1] = (word) & 0xff;
  1675. return num;
  1676. case 0x1f:
  1677. if (num == 1) { /* write */
  1678. word = (u16) ((msg[0].buf[1] << 8) | msg[0].buf[2]);
  1679. word &= 0x3;
  1680. word = (dib7000p_read_word(state, 72) & ~(3 << 12)) | (word << 12);
  1681. dib7000p_write_word(state, 72, word); /* Set the proper input */
  1682. return num;
  1683. }
  1684. }
  1685. if (apb_address != 0) /* R/W acces via APB */
  1686. return dib7090p_rw_on_apb(i2c_adap, msg, num, apb_address);
  1687. else /* R/W access via SERPAR */
  1688. return w7090p_tuner_rw_serpar(i2c_adap, msg, num);
  1689. return 0;
  1690. }
  1691. static u32 dib7000p_i2c_func(struct i2c_adapter *adapter)
  1692. {
  1693. return I2C_FUNC_I2C;
  1694. }
  1695. static struct i2c_algorithm dib7090_tuner_xfer_algo = {
  1696. .master_xfer = dib7090_tuner_xfer,
  1697. .functionality = dib7000p_i2c_func,
  1698. };
  1699. struct i2c_adapter *dib7090_get_i2c_tuner(struct dvb_frontend *fe)
  1700. {
  1701. struct dib7000p_state *st = fe->demodulator_priv;
  1702. return &st->dib7090_tuner_adap;
  1703. }
  1704. EXPORT_SYMBOL(dib7090_get_i2c_tuner);
  1705. static int dib7090_host_bus_drive(struct dib7000p_state *state, u8 drive)
  1706. {
  1707. u16 reg;
  1708. /* drive host bus 2, 3, 4 */
  1709. reg = dib7000p_read_word(state, 1798) & ~((0x7) | (0x7 << 6) | (0x7 << 12));
  1710. reg |= (drive << 12) | (drive << 6) | drive;
  1711. dib7000p_write_word(state, 1798, reg);
  1712. /* drive host bus 5,6 */
  1713. reg = dib7000p_read_word(state, 1799) & ~((0x7 << 2) | (0x7 << 8));
  1714. reg |= (drive << 8) | (drive << 2);
  1715. dib7000p_write_word(state, 1799, reg);
  1716. /* drive host bus 7, 8, 9 */
  1717. reg = dib7000p_read_word(state, 1800) & ~((0x7) | (0x7 << 6) | (0x7 << 12));
  1718. reg |= (drive << 12) | (drive << 6) | drive;
  1719. dib7000p_write_word(state, 1800, reg);
  1720. /* drive host bus 10, 11 */
  1721. reg = dib7000p_read_word(state, 1801) & ~((0x7 << 2) | (0x7 << 8));
  1722. reg |= (drive << 8) | (drive << 2);
  1723. dib7000p_write_word(state, 1801, reg);
  1724. /* drive host bus 12, 13, 14 */
  1725. reg = dib7000p_read_word(state, 1802) & ~((0x7) | (0x7 << 6) | (0x7 << 12));
  1726. reg |= (drive << 12) | (drive << 6) | drive;
  1727. dib7000p_write_word(state, 1802, reg);
  1728. return 0;
  1729. }
  1730. static u32 dib7090_calcSyncFreq(u32 P_Kin, u32 P_Kout, u32 insertExtSynchro, u32 syncSize)
  1731. {
  1732. u32 quantif = 3;
  1733. u32 nom = (insertExtSynchro * P_Kin + syncSize);
  1734. u32 denom = P_Kout;
  1735. u32 syncFreq = ((nom << quantif) / denom);
  1736. if ((syncFreq & ((1 << quantif) - 1)) != 0)
  1737. syncFreq = (syncFreq >> quantif) + 1;
  1738. else
  1739. syncFreq = (syncFreq >> quantif);
  1740. if (syncFreq != 0)
  1741. syncFreq = syncFreq - 1;
  1742. return syncFreq;
  1743. }
  1744. static int dib7090_cfg_DibTx(struct dib7000p_state *state, u32 P_Kin, u32 P_Kout, u32 insertExtSynchro, u32 synchroMode, u32 syncWord, u32 syncSize)
  1745. {
  1746. dprintk("Configure DibStream Tx");
  1747. dib7000p_write_word(state, 1615, 1);
  1748. dib7000p_write_word(state, 1603, P_Kin);
  1749. dib7000p_write_word(state, 1605, P_Kout);
  1750. dib7000p_write_word(state, 1606, insertExtSynchro);
  1751. dib7000p_write_word(state, 1608, synchroMode);
  1752. dib7000p_write_word(state, 1609, (syncWord >> 16) & 0xffff);
  1753. dib7000p_write_word(state, 1610, syncWord & 0xffff);
  1754. dib7000p_write_word(state, 1612, syncSize);
  1755. dib7000p_write_word(state, 1615, 0);
  1756. return 0;
  1757. }
  1758. static int dib7090_cfg_DibRx(struct dib7000p_state *state, u32 P_Kin, u32 P_Kout, u32 synchroMode, u32 insertExtSynchro, u32 syncWord, u32 syncSize,
  1759. u32 dataOutRate)
  1760. {
  1761. u32 syncFreq;
  1762. dprintk("Configure DibStream Rx");
  1763. if ((P_Kin != 0) && (P_Kout != 0)) {
  1764. syncFreq = dib7090_calcSyncFreq(P_Kin, P_Kout, insertExtSynchro, syncSize);
  1765. dib7000p_write_word(state, 1542, syncFreq);
  1766. }
  1767. dib7000p_write_word(state, 1554, 1);
  1768. dib7000p_write_word(state, 1536, P_Kin);
  1769. dib7000p_write_word(state, 1537, P_Kout);
  1770. dib7000p_write_word(state, 1539, synchroMode);
  1771. dib7000p_write_word(state, 1540, (syncWord >> 16) & 0xffff);
  1772. dib7000p_write_word(state, 1541, syncWord & 0xffff);
  1773. dib7000p_write_word(state, 1543, syncSize);
  1774. dib7000p_write_word(state, 1544, dataOutRate);
  1775. dib7000p_write_word(state, 1554, 0);
  1776. return 0;
  1777. }
  1778. static void dib7090_enMpegMux(struct dib7000p_state *state, int onoff)
  1779. {
  1780. u16 reg_1287 = dib7000p_read_word(state, 1287);
  1781. switch (onoff) {
  1782. case 1:
  1783. reg_1287 &= ~(1<<7);
  1784. break;
  1785. case 0:
  1786. reg_1287 |= (1<<7);
  1787. break;
  1788. }
  1789. dib7000p_write_word(state, 1287, reg_1287);
  1790. }
  1791. static void dib7090_configMpegMux(struct dib7000p_state *state,
  1792. u16 pulseWidth, u16 enSerialMode, u16 enSerialClkDiv2)
  1793. {
  1794. dprintk("Enable Mpeg mux");
  1795. dib7090_enMpegMux(state, 0);
  1796. /* If the input mode is MPEG do not divide the serial clock */
  1797. if ((enSerialMode == 1) && (state->input_mode_mpeg == 1))
  1798. enSerialClkDiv2 = 0;
  1799. dib7000p_write_word(state, 1287, ((pulseWidth & 0x1f) << 2)
  1800. | ((enSerialMode & 0x1) << 1)
  1801. | (enSerialClkDiv2 & 0x1));
  1802. dib7090_enMpegMux(state, 1);
  1803. }
  1804. static void dib7090_setDibTxMux(struct dib7000p_state *state, int mode)
  1805. {
  1806. u16 reg_1288 = dib7000p_read_word(state, 1288) & ~(0x7 << 7);
  1807. switch (mode) {
  1808. case MPEG_ON_DIBTX:
  1809. dprintk("SET MPEG ON DIBSTREAM TX");
  1810. dib7090_cfg_DibTx(state, 8, 5, 0, 0, 0, 0);
  1811. reg_1288 |= (1<<9);
  1812. break;
  1813. case DIV_ON_DIBTX:
  1814. dprintk("SET DIV_OUT ON DIBSTREAM TX");
  1815. dib7090_cfg_DibTx(state, 5, 5, 0, 0, 0, 0);
  1816. reg_1288 |= (1<<8);
  1817. break;
  1818. case ADC_ON_DIBTX:
  1819. dprintk("SET ADC_OUT ON DIBSTREAM TX");
  1820. dib7090_cfg_DibTx(state, 20, 5, 10, 0, 0, 0);
  1821. reg_1288 |= (1<<7);
  1822. break;
  1823. default:
  1824. break;
  1825. }
  1826. dib7000p_write_word(state, 1288, reg_1288);
  1827. }
  1828. static void dib7090_setHostBusMux(struct dib7000p_state *state, int mode)
  1829. {
  1830. u16 reg_1288 = dib7000p_read_word(state, 1288) & ~(0x7 << 4);
  1831. switch (mode) {
  1832. case DEMOUT_ON_HOSTBUS:
  1833. dprintk("SET DEM OUT OLD INTERF ON HOST BUS");
  1834. dib7090_enMpegMux(state, 0);
  1835. reg_1288 |= (1<<6);
  1836. break;
  1837. case DIBTX_ON_HOSTBUS:
  1838. dprintk("SET DIBSTREAM TX ON HOST BUS");
  1839. dib7090_enMpegMux(state, 0);
  1840. reg_1288 |= (1<<5);
  1841. break;
  1842. case MPEG_ON_HOSTBUS:
  1843. dprintk("SET MPEG MUX ON HOST BUS");
  1844. reg_1288 |= (1<<4);
  1845. break;
  1846. default:
  1847. break;
  1848. }
  1849. dib7000p_write_word(state, 1288, reg_1288);
  1850. }
  1851. int dib7090_set_diversity_in(struct dvb_frontend *fe, int onoff)
  1852. {
  1853. struct dib7000p_state *state = fe->demodulator_priv;
  1854. u16 reg_1287;
  1855. switch (onoff) {
  1856. case 0: /* only use the internal way - not the diversity input */
  1857. dprintk("%s mode OFF : by default Enable Mpeg INPUT", __func__);
  1858. dib7090_cfg_DibRx(state, 8, 5, 0, 0, 0, 8, 0);
  1859. /* Do not divide the serial clock of MPEG MUX */
  1860. /* in SERIAL MODE in case input mode MPEG is used */
  1861. reg_1287 = dib7000p_read_word(state, 1287);
  1862. /* enSerialClkDiv2 == 1 ? */
  1863. if ((reg_1287 & 0x1) == 1) {
  1864. /* force enSerialClkDiv2 = 0 */
  1865. reg_1287 &= ~0x1;
  1866. dib7000p_write_word(state, 1287, reg_1287);
  1867. }
  1868. state->input_mode_mpeg = 1;
  1869. break;
  1870. case 1: /* both ways */
  1871. case 2: /* only the diversity input */
  1872. dprintk("%s ON : Enable diversity INPUT", __func__);
  1873. dib7090_cfg_DibRx(state, 5, 5, 0, 0, 0, 0, 0);
  1874. state->input_mode_mpeg = 0;
  1875. break;
  1876. }
  1877. dib7000p_set_diversity_in(&state->demod, onoff);
  1878. return 0;
  1879. }
  1880. static int dib7090_set_output_mode(struct dvb_frontend *fe, int mode)
  1881. {
  1882. struct dib7000p_state *state = fe->demodulator_priv;
  1883. u16 outreg, smo_mode, fifo_threshold;
  1884. u8 prefer_mpeg_mux_use = 1;
  1885. int ret = 0;
  1886. dib7090_host_bus_drive(state, 1);
  1887. fifo_threshold = 1792;
  1888. smo_mode = (dib7000p_read_word(state, 235) & 0x0050) | (1 << 1);
  1889. outreg = dib7000p_read_word(state, 1286) & ~((1 << 10) | (0x7 << 6) | (1 << 1));
  1890. switch (mode) {
  1891. case OUTMODE_HIGH_Z:
  1892. outreg = 0;
  1893. break;
  1894. case OUTMODE_MPEG2_SERIAL:
  1895. if (prefer_mpeg_mux_use) {
  1896. dprintk("setting output mode TS_SERIAL using Mpeg Mux");
  1897. dib7090_configMpegMux(state, 3, 1, 1);
  1898. dib7090_setHostBusMux(state, MPEG_ON_HOSTBUS);
  1899. } else {/* Use Smooth block */
  1900. dprintk("setting output mode TS_SERIAL using Smooth bloc");
  1901. dib7090_setHostBusMux(state, DEMOUT_ON_HOSTBUS);
  1902. outreg |= (2<<6) | (0 << 1);
  1903. }
  1904. break;
  1905. case OUTMODE_MPEG2_PAR_GATED_CLK:
  1906. if (prefer_mpeg_mux_use) {
  1907. dprintk("setting output mode TS_PARALLEL_GATED using Mpeg Mux");
  1908. dib7090_configMpegMux(state, 2, 0, 0);
  1909. dib7090_setHostBusMux(state, MPEG_ON_HOSTBUS);
  1910. } else { /* Use Smooth block */
  1911. dprintk("setting output mode TS_PARALLEL_GATED using Smooth block");
  1912. dib7090_setHostBusMux(state, DEMOUT_ON_HOSTBUS);
  1913. outreg |= (0<<6);
  1914. }
  1915. break;
  1916. case OUTMODE_MPEG2_PAR_CONT_CLK: /* Using Smooth block only */
  1917. dprintk("setting output mode TS_PARALLEL_CONT using Smooth block");
  1918. dib7090_setHostBusMux(state, DEMOUT_ON_HOSTBUS);
  1919. outreg |= (1<<6);
  1920. break;
  1921. case OUTMODE_MPEG2_FIFO: /* Using Smooth block because not supported by new Mpeg Mux bloc */
  1922. dprintk("setting output mode TS_FIFO using Smooth block");
  1923. dib7090_setHostBusMux(state, DEMOUT_ON_HOSTBUS);
  1924. outreg |= (5<<6);
  1925. smo_mode |= (3 << 1);
  1926. fifo_threshold = 512;
  1927. break;
  1928. case OUTMODE_DIVERSITY:
  1929. dprintk("setting output mode MODE_DIVERSITY");
  1930. dib7090_setDibTxMux(state, DIV_ON_DIBTX);
  1931. dib7090_setHostBusMux(state, DIBTX_ON_HOSTBUS);
  1932. break;
  1933. case OUTMODE_ANALOG_ADC:
  1934. dprintk("setting output mode MODE_ANALOG_ADC");
  1935. dib7090_setDibTxMux(state, ADC_ON_DIBTX);
  1936. dib7090_setHostBusMux(state, DIBTX_ON_HOSTBUS);
  1937. break;
  1938. }
  1939. if (mode != OUTMODE_HIGH_Z)
  1940. outreg |= (1 << 10);
  1941. if (state->cfg.output_mpeg2_in_188_bytes)
  1942. smo_mode |= (1 << 5);
  1943. ret |= dib7000p_write_word(state, 235, smo_mode);
  1944. ret |= dib7000p_write_word(state, 236, fifo_threshold); /* synchronous fread */
  1945. ret |= dib7000p_write_word(state, 1286, outreg);
  1946. return ret;
  1947. }
  1948. int dib7090_tuner_sleep(struct dvb_frontend *fe, int onoff)
  1949. {
  1950. struct dib7000p_state *state = fe->demodulator_priv;
  1951. u16 en_cur_state;
  1952. dprintk("sleep dib7090: %d", onoff);
  1953. en_cur_state = dib7000p_read_word(state, 1922);
  1954. if (en_cur_state > 0xff)
  1955. state->tuner_enable = en_cur_state;
  1956. if (onoff)
  1957. en_cur_state &= 0x00ff;
  1958. else {
  1959. if (state->tuner_enable != 0)
  1960. en_cur_state = state->tuner_enable;
  1961. }
  1962. dib7000p_write_word(state, 1922, en_cur_state);
  1963. return 0;
  1964. }
  1965. EXPORT_SYMBOL(dib7090_tuner_sleep);
  1966. int dib7090_get_adc_power(struct dvb_frontend *fe)
  1967. {
  1968. return dib7000p_get_adc_power(fe);
  1969. }
  1970. EXPORT_SYMBOL(dib7090_get_adc_power);
  1971. int dib7090_slave_reset(struct dvb_frontend *fe)
  1972. {
  1973. struct dib7000p_state *state = fe->demodulator_priv;
  1974. u16 reg;
  1975. reg = dib7000p_read_word(state, 1794);
  1976. dib7000p_write_word(state, 1794, reg | (4 << 12));
  1977. dib7000p_write_word(state, 1032, 0xffff);
  1978. return 0;
  1979. }
  1980. EXPORT_SYMBOL(dib7090_slave_reset);
  1981. static struct dvb_frontend_ops dib7000p_ops;
  1982. struct dvb_frontend *dib7000p_attach(struct i2c_adapter *i2c_adap, u8 i2c_addr, struct dib7000p_config *cfg)
  1983. {
  1984. struct dvb_frontend *demod;
  1985. struct dib7000p_state *st;
  1986. st = kzalloc(sizeof(struct dib7000p_state), GFP_KERNEL);
  1987. if (st == NULL)
  1988. return NULL;
  1989. memcpy(&st->cfg, cfg, sizeof(struct dib7000p_config));
  1990. st->i2c_adap = i2c_adap;
  1991. st->i2c_addr = i2c_addr;
  1992. st->gpio_val = cfg->gpio_val;
  1993. st->gpio_dir = cfg->gpio_dir;
  1994. /* Ensure the output mode remains at the previous default if it's
  1995. * not specifically set by the caller.
  1996. */
  1997. if ((st->cfg.output_mode != OUTMODE_MPEG2_SERIAL) && (st->cfg.output_mode != OUTMODE_MPEG2_PAR_GATED_CLK))
  1998. st->cfg.output_mode = OUTMODE_MPEG2_FIFO;
  1999. demod = &st->demod;
  2000. demod->demodulator_priv = st;
  2001. memcpy(&st->demod.ops, &dib7000p_ops, sizeof(struct dvb_frontend_ops));
  2002. mutex_init(&st->i2c_buffer_lock);
  2003. dib7000p_write_word(st, 1287, 0x0003); /* sram lead in, rdy */
  2004. if (dib7000p_identify(st) != 0)
  2005. goto error;
  2006. st->version = dib7000p_read_word(st, 897);
  2007. /* FIXME: make sure the dev.parent field is initialized, or else
  2008. request_firmware() will hit an OOPS (this should be moved somewhere
  2009. more common) */
  2010. st->i2c_master.gated_tuner_i2c_adap.dev.parent = i2c_adap->dev.parent;
  2011. /* FIXME: make sure the dev.parent field is initialized, or else
  2012. request_firmware() will hit an OOPS (this should be moved somewhere
  2013. more common) */
  2014. st->i2c_master.gated_tuner_i2c_adap.dev.parent = i2c_adap->dev.parent;
  2015. dibx000_init_i2c_master(&st->i2c_master, DIB7000P, st->i2c_adap, st->i2c_addr);
  2016. /* init 7090 tuner adapter */
  2017. strncpy(st->dib7090_tuner_adap.name, "DiB7090 tuner interface", sizeof(st->dib7090_tuner_adap.name));
  2018. st->dib7090_tuner_adap.algo = &dib7090_tuner_xfer_algo;
  2019. st->dib7090_tuner_adap.algo_data = NULL;
  2020. st->dib7090_tuner_adap.dev.parent = st->i2c_adap->dev.parent;
  2021. i2c_set_adapdata(&st->dib7090_tuner_adap, st);
  2022. i2c_add_adapter(&st->dib7090_tuner_adap);
  2023. dib7000p_demod_reset(st);
  2024. if (st->version == SOC7090) {
  2025. dib7090_set_output_mode(demod, st->cfg.output_mode);
  2026. dib7090_set_diversity_in(demod, 0);
  2027. }
  2028. return demod;
  2029. error:
  2030. kfree(st);
  2031. return NULL;
  2032. }
  2033. EXPORT_SYMBOL(dib7000p_attach);
  2034. static struct dvb_frontend_ops dib7000p_ops = {
  2035. .delsys = { SYS_DVBT },
  2036. .info = {
  2037. .name = "DiBcom 7000PC",
  2038. .frequency_min = 44250000,
  2039. .frequency_max = 867250000,
  2040. .frequency_stepsize = 62500,
  2041. .caps = FE_CAN_INVERSION_AUTO |
  2042. FE_CAN_FEC_1_2 | FE_CAN_FEC_2_3 | FE_CAN_FEC_3_4 |
  2043. FE_CAN_FEC_5_6 | FE_CAN_FEC_7_8 | FE_CAN_FEC_AUTO |
  2044. FE_CAN_QPSK | FE_CAN_QAM_16 | FE_CAN_QAM_64 | FE_CAN_QAM_AUTO |
  2045. FE_CAN_TRANSMISSION_MODE_AUTO | FE_CAN_GUARD_INTERVAL_AUTO | FE_CAN_RECOVER | FE_CAN_HIERARCHY_AUTO,
  2046. },
  2047. .release = dib7000p_release,
  2048. .init = dib7000p_wakeup,
  2049. .sleep = dib7000p_sleep,
  2050. .set_frontend = dib7000p_set_frontend,
  2051. .get_tune_settings = dib7000p_fe_get_tune_settings,
  2052. .get_frontend = dib7000p_get_frontend,
  2053. .read_status = dib7000p_read_status,
  2054. .read_ber = dib7000p_read_ber,
  2055. .read_signal_strength = dib7000p_read_signal_strength,
  2056. .read_snr = dib7000p_read_snr,
  2057. .read_ucblocks = dib7000p_read_unc_blocks,
  2058. };
  2059. MODULE_AUTHOR("Olivier Grenie <ogrenie@dibcom.fr>");
  2060. MODULE_AUTHOR("Patrick Boettcher <pboettcher@dibcom.fr>");
  2061. MODULE_DESCRIPTION("Driver for the DiBcom 7000PC COFDM demodulator");
  2062. MODULE_LICENSE("GPL");