dib7000p.c 64 KB

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