dib8000.c 71 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279
  1. /*
  2. * Linux-DVB Driver for DiBcom's DiB8000 chip (ISDB-T).
  3. *
  4. * Copyright (C) 2009 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/i2c.h>
  12. #include "dvb_math.h"
  13. #include "dvb_frontend.h"
  14. #include "dib8000.h"
  15. #define LAYER_ALL -1
  16. #define LAYER_A 1
  17. #define LAYER_B 2
  18. #define LAYER_C 3
  19. #define FE_CALLBACK_TIME_NEVER 0xffffffff
  20. static int debug = 0;
  21. module_param(debug, int, 0644);
  22. MODULE_PARM_DESC(debug, "turn on debugging (default: 0)");
  23. #define dprintk(args...) do { if (debug) { printk(KERN_DEBUG "DiB8000: "); printk(args); printk("\n"); } } while (0)
  24. enum frontend_tune_state {
  25. CT_AGC_START = 20,
  26. CT_AGC_STEP_0,
  27. CT_AGC_STEP_1,
  28. CT_AGC_STEP_2,
  29. CT_AGC_STEP_3,
  30. CT_AGC_STEP_4,
  31. CT_AGC_STOP,
  32. CT_DEMOD_START = 30,
  33. };
  34. #define FE_STATUS_TUNE_FAILED 0
  35. struct i2c_device {
  36. struct i2c_adapter *adap;
  37. u8 addr;
  38. };
  39. struct dib8000_state {
  40. struct dvb_frontend fe;
  41. struct dib8000_config cfg;
  42. struct i2c_device i2c;
  43. struct dibx000_i2c_master i2c_master;
  44. u16 wbd_ref;
  45. u8 current_band;
  46. u32 current_bandwidth;
  47. struct dibx000_agc_config *current_agc;
  48. u32 timf;
  49. u32 timf_default;
  50. u8 div_force_off:1;
  51. u8 div_state:1;
  52. u16 div_sync_wait;
  53. u8 agc_state;
  54. u8 differential_constellation;
  55. u8 diversity_onoff;
  56. s16 ber_monitored_layer;
  57. u16 gpio_dir;
  58. u16 gpio_val;
  59. u16 revision;
  60. u8 isdbt_cfg_loaded;
  61. enum frontend_tune_state tune_state;
  62. u32 status;
  63. };
  64. enum dib8000_power_mode {
  65. DIB8000M_POWER_ALL = 0,
  66. DIB8000M_POWER_INTERFACE_ONLY,
  67. };
  68. static u16 dib8000_i2c_read16(struct i2c_device *i2c, u16 reg)
  69. {
  70. u8 wb[2] = { reg >> 8, reg & 0xff };
  71. u8 rb[2];
  72. struct i2c_msg msg[2] = {
  73. {.addr = i2c->addr >> 1,.flags = 0,.buf = wb,.len = 2},
  74. {.addr = i2c->addr >> 1,.flags = I2C_M_RD,.buf = rb,.len = 2},
  75. };
  76. if (i2c_transfer(i2c->adap, msg, 2) != 2)
  77. dprintk("i2c read error on %d", reg);
  78. return (rb[0] << 8) | rb[1];
  79. }
  80. static u16 dib8000_read_word(struct dib8000_state *state, u16 reg)
  81. {
  82. return dib8000_i2c_read16(&state->i2c, reg);
  83. }
  84. static u32 dib8000_read32(struct dib8000_state *state, u16 reg)
  85. {
  86. u16 rw[2];
  87. rw[0] = dib8000_read_word(state, reg + 0);
  88. rw[1] = dib8000_read_word(state, reg + 1);
  89. return ((rw[0] << 16) | (rw[1]));
  90. }
  91. static int dib8000_i2c_write16(struct i2c_device *i2c, u16 reg, u16 val)
  92. {
  93. u8 b[4] = {
  94. (reg >> 8) & 0xff, reg & 0xff,
  95. (val >> 8) & 0xff, val & 0xff,
  96. };
  97. struct i2c_msg msg = {
  98. .addr = i2c->addr >> 1,.flags = 0,.buf = b,.len = 4
  99. };
  100. return i2c_transfer(i2c->adap, &msg, 1) != 1 ? -EREMOTEIO : 0;
  101. }
  102. static int dib8000_write_word(struct dib8000_state *state, u16 reg, u16 val)
  103. {
  104. return dib8000_i2c_write16(&state->i2c, reg, val);
  105. }
  106. const int16_t coeff_2k_sb_1seg_dqpsk[8] = {
  107. (769 << 5) | 0x0a, (745 << 5) | 0x03, (595 << 5) | 0x0d, (769 << 5) | 0x0a, (920 << 5) | 0x09, (784 << 5) | 0x02, (519 << 5) | 0x0c,
  108. (920 << 5) | 0x09
  109. };
  110. const int16_t coeff_2k_sb_1seg[8] = {
  111. (692 << 5) | 0x0b, (683 << 5) | 0x01, (519 << 5) | 0x09, (692 << 5) | 0x0b, 0 | 0x1f, 0 | 0x1f, 0 | 0x1f, 0 | 0x1f
  112. };
  113. const int16_t coeff_2k_sb_3seg_0dqpsk_1dqpsk[8] = {
  114. (832 << 5) | 0x10, (912 << 5) | 0x05, (900 << 5) | 0x12, (832 << 5) | 0x10, (-931 << 5) | 0x0f, (912 << 5) | 0x04, (807 << 5) | 0x11,
  115. (-931 << 5) | 0x0f
  116. };
  117. const int16_t coeff_2k_sb_3seg_0dqpsk[8] = {
  118. (622 << 5) | 0x0c, (941 << 5) | 0x04, (796 << 5) | 0x10, (622 << 5) | 0x0c, (982 << 5) | 0x0c, (519 << 5) | 0x02, (572 << 5) | 0x0e,
  119. (982 << 5) | 0x0c
  120. };
  121. const int16_t coeff_2k_sb_3seg_1dqpsk[8] = {
  122. (699 << 5) | 0x14, (607 << 5) | 0x04, (944 << 5) | 0x13, (699 << 5) | 0x14, (-720 << 5) | 0x0d, (640 << 5) | 0x03, (866 << 5) | 0x12,
  123. (-720 << 5) | 0x0d
  124. };
  125. const int16_t coeff_2k_sb_3seg[8] = {
  126. (664 << 5) | 0x0c, (925 << 5) | 0x03, (937 << 5) | 0x10, (664 << 5) | 0x0c, (-610 << 5) | 0x0a, (697 << 5) | 0x01, (836 << 5) | 0x0e,
  127. (-610 << 5) | 0x0a
  128. };
  129. const int16_t coeff_4k_sb_1seg_dqpsk[8] = {
  130. (-955 << 5) | 0x0e, (687 << 5) | 0x04, (818 << 5) | 0x10, (-955 << 5) | 0x0e, (-922 << 5) | 0x0d, (750 << 5) | 0x03, (665 << 5) | 0x0f,
  131. (-922 << 5) | 0x0d
  132. };
  133. const int16_t coeff_4k_sb_1seg[8] = {
  134. (638 << 5) | 0x0d, (683 << 5) | 0x02, (638 << 5) | 0x0d, (638 << 5) | 0x0d, (-655 << 5) | 0x0a, (517 << 5) | 0x00, (698 << 5) | 0x0d,
  135. (-655 << 5) | 0x0a
  136. };
  137. const int16_t coeff_4k_sb_3seg_0dqpsk_1dqpsk[8] = {
  138. (-707 << 5) | 0x14, (910 << 5) | 0x06, (889 << 5) | 0x16, (-707 << 5) | 0x14, (-958 << 5) | 0x13, (993 << 5) | 0x05, (523 << 5) | 0x14,
  139. (-958 << 5) | 0x13
  140. };
  141. const int16_t coeff_4k_sb_3seg_0dqpsk[8] = {
  142. (-723 << 5) | 0x13, (910 << 5) | 0x05, (777 << 5) | 0x14, (-723 << 5) | 0x13, (-568 << 5) | 0x0f, (547 << 5) | 0x03, (696 << 5) | 0x12,
  143. (-568 << 5) | 0x0f
  144. };
  145. const int16_t coeff_4k_sb_3seg_1dqpsk[8] = {
  146. (-940 << 5) | 0x15, (607 << 5) | 0x05, (915 << 5) | 0x16, (-940 << 5) | 0x15, (-848 << 5) | 0x13, (683 << 5) | 0x04, (543 << 5) | 0x14,
  147. (-848 << 5) | 0x13
  148. };
  149. const int16_t coeff_4k_sb_3seg[8] = {
  150. (612 << 5) | 0x12, (910 << 5) | 0x04, (864 << 5) | 0x14, (612 << 5) | 0x12, (-869 << 5) | 0x13, (683 << 5) | 0x02, (869 << 5) | 0x12,
  151. (-869 << 5) | 0x13
  152. };
  153. const int16_t coeff_8k_sb_1seg_dqpsk[8] = {
  154. (-835 << 5) | 0x12, (684 << 5) | 0x05, (735 << 5) | 0x14, (-835 << 5) | 0x12, (-598 << 5) | 0x10, (781 << 5) | 0x04, (739 << 5) | 0x13,
  155. (-598 << 5) | 0x10
  156. };
  157. const int16_t coeff_8k_sb_1seg[8] = {
  158. (673 << 5) | 0x0f, (683 << 5) | 0x03, (808 << 5) | 0x12, (673 << 5) | 0x0f, (585 << 5) | 0x0f, (512 << 5) | 0x01, (780 << 5) | 0x0f,
  159. (585 << 5) | 0x0f
  160. };
  161. const int16_t coeff_8k_sb_3seg_0dqpsk_1dqpsk[8] = {
  162. (863 << 5) | 0x17, (930 << 5) | 0x07, (878 << 5) | 0x19, (863 << 5) | 0x17, (0 << 5) | 0x14, (521 << 5) | 0x05, (980 << 5) | 0x18,
  163. (0 << 5) | 0x14
  164. };
  165. const int16_t coeff_8k_sb_3seg_0dqpsk[8] = {
  166. (-924 << 5) | 0x17, (910 << 5) | 0x06, (774 << 5) | 0x17, (-924 << 5) | 0x17, (-877 << 5) | 0x15, (565 << 5) | 0x04, (553 << 5) | 0x15,
  167. (-877 << 5) | 0x15
  168. };
  169. const int16_t coeff_8k_sb_3seg_1dqpsk[8] = {
  170. (-921 << 5) | 0x19, (607 << 5) | 0x06, (881 << 5) | 0x19, (-921 << 5) | 0x19, (-921 << 5) | 0x14, (713 << 5) | 0x05, (1018 << 5) | 0x18,
  171. (-921 << 5) | 0x14
  172. };
  173. const int16_t coeff_8k_sb_3seg[8] = {
  174. (514 << 5) | 0x14, (910 << 5) | 0x05, (861 << 5) | 0x17, (514 << 5) | 0x14, (690 << 5) | 0x14, (683 << 5) | 0x03, (662 << 5) | 0x15,
  175. (690 << 5) | 0x14
  176. };
  177. const int16_t ana_fe_coeff_3seg[24] = {
  178. 81, 80, 78, 74, 68, 61, 54, 45, 37, 28, 19, 11, 4, 1022, 1017, 1013, 1010, 1008, 1008, 1008, 1008, 1010, 1014, 1017
  179. };
  180. const int16_t ana_fe_coeff_1seg[24] = {
  181. 249, 226, 164, 82, 5, 981, 970, 988, 1018, 20, 31, 26, 8, 1012, 1000, 1018, 1012, 8, 15, 14, 9, 3, 1017, 1003
  182. };
  183. const int16_t ana_fe_coeff_13seg[24] = {
  184. 396, 305, 105, -51, -77, -12, 41, 31, -11, -30, -11, 14, 15, -2, -13, -7, 5, 8, 1, -6, -7, -3, 0, 1
  185. };
  186. static u16 fft_to_mode(struct dib8000_state *state)
  187. {
  188. u16 mode;
  189. switch (state->fe.dtv_property_cache.transmission_mode) {
  190. case TRANSMISSION_MODE_2K:
  191. mode = 1;
  192. break;
  193. case TRANSMISSION_MODE_4K:
  194. mode = 2;
  195. break;
  196. default:
  197. case TRANSMISSION_MODE_AUTO:
  198. case TRANSMISSION_MODE_8K:
  199. mode = 3;
  200. break;
  201. }
  202. return mode;
  203. }
  204. static void dib8000_set_acquisition_mode(struct dib8000_state *state)
  205. {
  206. u16 nud = dib8000_read_word(state, 298);
  207. nud |= (1 << 3) | (1 << 0);
  208. dprintk("acquisition mode activated");
  209. dib8000_write_word(state, 298, nud);
  210. }
  211. static int dib8000_set_output_mode(struct dib8000_state *state, int mode)
  212. {
  213. u16 outreg, fifo_threshold, smo_mode, sram = 0x0205; /* by default SDRAM deintlv is enabled */
  214. outreg = 0;
  215. fifo_threshold = 1792;
  216. smo_mode = (dib8000_read_word(state, 299) & 0x0050) | (1 << 1);
  217. dprintk("-I- Setting output mode for demod %p to %d", &state->fe, mode);
  218. switch (mode) {
  219. case OUTMODE_MPEG2_PAR_GATED_CLK: // STBs with parallel gated clock
  220. outreg = (1 << 10); /* 0x0400 */
  221. break;
  222. case OUTMODE_MPEG2_PAR_CONT_CLK: // STBs with parallel continues clock
  223. outreg = (1 << 10) | (1 << 6); /* 0x0440 */
  224. break;
  225. case OUTMODE_MPEG2_SERIAL: // STBs with serial input
  226. outreg = (1 << 10) | (2 << 6) | (0 << 1); /* 0x0482 */
  227. break;
  228. case OUTMODE_DIVERSITY:
  229. if (state->cfg.hostbus_diversity) {
  230. outreg = (1 << 10) | (4 << 6); /* 0x0500 */
  231. sram &= 0xfdff;
  232. } else
  233. sram |= 0x0c00;
  234. break;
  235. case OUTMODE_MPEG2_FIFO: // e.g. USB feeding
  236. smo_mode |= (3 << 1);
  237. fifo_threshold = 512;
  238. outreg = (1 << 10) | (5 << 6);
  239. break;
  240. case OUTMODE_HIGH_Z: // disable
  241. outreg = 0;
  242. break;
  243. case OUTMODE_ANALOG_ADC:
  244. outreg = (1 << 10) | (3 << 6);
  245. dib8000_set_acquisition_mode(state);
  246. break;
  247. default:
  248. dprintk("Unhandled output_mode passed to be set for demod %p", &state->fe);
  249. return -EINVAL;
  250. }
  251. if (state->cfg.output_mpeg2_in_188_bytes)
  252. smo_mode |= (1 << 5);
  253. dib8000_write_word(state, 299, smo_mode);
  254. dib8000_write_word(state, 300, fifo_threshold); /* synchronous fread */
  255. dib8000_write_word(state, 1286, outreg);
  256. dib8000_write_word(state, 1291, sram);
  257. return 0;
  258. }
  259. static int dib8000_set_diversity_in(struct dvb_frontend *fe, int onoff)
  260. {
  261. struct dib8000_state *state = fe->demodulator_priv;
  262. u16 sync_wait = dib8000_read_word(state, 273) & 0xfff0;
  263. if (!state->differential_constellation) {
  264. dib8000_write_word(state, 272, 1 << 9); //dvsy_off_lmod4 = 1
  265. dib8000_write_word(state, 273, sync_wait | (1 << 2) | 2); // sync_enable = 1; comb_mode = 2
  266. } else {
  267. dib8000_write_word(state, 272, 0); //dvsy_off_lmod4 = 0
  268. dib8000_write_word(state, 273, sync_wait); // sync_enable = 0; comb_mode = 0
  269. }
  270. state->diversity_onoff = onoff;
  271. switch (onoff) {
  272. case 0: /* only use the internal way - not the diversity input */
  273. dib8000_write_word(state, 270, 1);
  274. dib8000_write_word(state, 271, 0);
  275. break;
  276. case 1: /* both ways */
  277. dib8000_write_word(state, 270, 6);
  278. dib8000_write_word(state, 271, 6);
  279. break;
  280. case 2: /* only the diversity input */
  281. dib8000_write_word(state, 270, 0);
  282. dib8000_write_word(state, 271, 1);
  283. break;
  284. }
  285. return 0;
  286. }
  287. static void dib8000_set_power_mode(struct dib8000_state *state, enum dib8000_power_mode mode)
  288. {
  289. /* by default everything is going to be powered off */
  290. u16 reg_774 = 0x3fff, reg_775 = 0xffff, reg_776 = 0xffff,
  291. reg_900 = (dib8000_read_word(state, 900) & 0xfffc) | 0x3, reg_1280 = (dib8000_read_word(state, 1280) & 0x00ff) | 0xff00;
  292. /* now, depending on the requested mode, we power on */
  293. switch (mode) {
  294. /* power up everything in the demod */
  295. case DIB8000M_POWER_ALL:
  296. reg_774 = 0x0000;
  297. reg_775 = 0x0000;
  298. reg_776 = 0x0000;
  299. reg_900 &= 0xfffc;
  300. reg_1280 &= 0x00ff;
  301. break;
  302. case DIB8000M_POWER_INTERFACE_ONLY:
  303. reg_1280 &= 0x00ff;
  304. break;
  305. }
  306. dprintk("powermode : 774 : %x ; 775 : %x; 776 : %x ; 900 : %x; 1280 : %x", reg_774, reg_775, reg_776, reg_900, reg_1280);
  307. dib8000_write_word(state, 774, reg_774);
  308. dib8000_write_word(state, 775, reg_775);
  309. dib8000_write_word(state, 776, reg_776);
  310. dib8000_write_word(state, 900, reg_900);
  311. dib8000_write_word(state, 1280, reg_1280);
  312. }
  313. static int dib8000_set_adc_state(struct dib8000_state *state, enum dibx000_adc_states no)
  314. {
  315. int ret = 0;
  316. u16 reg_907 = dib8000_read_word(state, 907), reg_908 = dib8000_read_word(state, 908);
  317. switch (no) {
  318. case DIBX000_SLOW_ADC_ON:
  319. reg_908 |= (1 << 1) | (1 << 0);
  320. ret |= dib8000_write_word(state, 908, reg_908);
  321. reg_908 &= ~(1 << 1);
  322. break;
  323. case DIBX000_SLOW_ADC_OFF:
  324. reg_908 |= (1 << 1) | (1 << 0);
  325. break;
  326. case DIBX000_ADC_ON:
  327. reg_907 &= 0x0fff;
  328. reg_908 &= 0x0003;
  329. break;
  330. case DIBX000_ADC_OFF: // leave the VBG voltage on
  331. reg_907 |= (1 << 14) | (1 << 13) | (1 << 12);
  332. reg_908 |= (1 << 5) | (1 << 4) | (1 << 3) | (1 << 2);
  333. break;
  334. case DIBX000_VBG_ENABLE:
  335. reg_907 &= ~(1 << 15);
  336. break;
  337. case DIBX000_VBG_DISABLE:
  338. reg_907 |= (1 << 15);
  339. break;
  340. default:
  341. break;
  342. }
  343. ret |= dib8000_write_word(state, 907, reg_907);
  344. ret |= dib8000_write_word(state, 908, reg_908);
  345. return ret;
  346. }
  347. static int dib8000_set_bandwidth(struct dib8000_state *state, u32 bw)
  348. {
  349. u32 timf;
  350. if (bw == 0)
  351. bw = 6000;
  352. if (state->timf == 0) {
  353. dprintk("using default timf");
  354. timf = state->timf_default;
  355. } else {
  356. dprintk("using updated timf");
  357. timf = state->timf;
  358. }
  359. dib8000_write_word(state, 29, (u16) ((timf >> 16) & 0xffff));
  360. dib8000_write_word(state, 30, (u16) ((timf) & 0xffff));
  361. return 0;
  362. }
  363. static int dib8000_sad_calib(struct dib8000_state *state)
  364. {
  365. /* internal */
  366. dib8000_write_word(state, 923, (0 << 1) | (0 << 0));
  367. dib8000_write_word(state, 924, 776); // 0.625*3.3 / 4096
  368. /* do the calibration */
  369. dib8000_write_word(state, 923, (1 << 0));
  370. dib8000_write_word(state, 923, (0 << 0));
  371. msleep(1);
  372. return 0;
  373. }
  374. int dib8000_set_wbd_ref(struct dvb_frontend *fe, u16 value)
  375. {
  376. struct dib8000_state *state = fe->demodulator_priv;
  377. if (value > 4095)
  378. value = 4095;
  379. state->wbd_ref = value;
  380. return dib8000_write_word(state, 106, value);
  381. }
  382. EXPORT_SYMBOL(dib8000_set_wbd_ref);
  383. static void dib8000_reset_pll_common(struct dib8000_state *state, const struct dibx000_bandwidth_config *bw)
  384. {
  385. dprintk("ifreq: %d %x, inversion: %d", bw->ifreq, bw->ifreq, bw->ifreq >> 25);
  386. dib8000_write_word(state, 23, (u16) (((bw->internal * 1000) >> 16) & 0xffff)); /* P_sec_len */
  387. dib8000_write_word(state, 24, (u16) ((bw->internal * 1000) & 0xffff));
  388. dib8000_write_word(state, 27, (u16) ((bw->ifreq >> 16) & 0x01ff));
  389. dib8000_write_word(state, 28, (u16) (bw->ifreq & 0xffff));
  390. dib8000_write_word(state, 26, (u16) ((bw->ifreq >> 25) & 0x0003));
  391. dib8000_write_word(state, 922, bw->sad_cfg);
  392. }
  393. static void dib8000_reset_pll(struct dib8000_state *state)
  394. {
  395. const struct dibx000_bandwidth_config *pll = state->cfg.pll;
  396. u16 clk_cfg1;
  397. // clk_cfg0
  398. dib8000_write_word(state, 901, (pll->pll_prediv << 8) | (pll->pll_ratio << 0));
  399. // clk_cfg1
  400. clk_cfg1 = (1 << 10) | (0 << 9) | (pll->IO_CLK_en_core << 8) |
  401. (pll->bypclk_div << 5) | (pll->enable_refdiv << 4) | (1 << 3) | (pll->pll_range << 1) | (pll->pll_reset << 0);
  402. dib8000_write_word(state, 902, clk_cfg1);
  403. clk_cfg1 = (clk_cfg1 & 0xfff7) | (pll->pll_bypass << 3);
  404. dib8000_write_word(state, 902, clk_cfg1);
  405. dprintk("clk_cfg1: 0x%04x", clk_cfg1); /* 0x507 1 0 1 000 0 0 11 1 */
  406. /* smpl_cfg: P_refclksel=2, P_ensmplsel=1 nodivsmpl=1 */
  407. if (state->cfg.pll->ADClkSrc == 0)
  408. dib8000_write_word(state, 904, (0 << 15) | (0 << 12) | (0 << 10) | (pll->modulo << 8) | (pll->ADClkSrc << 7) | (0 << 1));
  409. else if (state->cfg.refclksel != 0)
  410. dib8000_write_word(state, 904,
  411. (0 << 15) | (1 << 12) | ((state->cfg.refclksel & 0x3) << 10) | (pll->modulo << 8) | (pll->
  412. ADClkSrc << 7) | (0 << 1));
  413. else
  414. dib8000_write_word(state, 904, (0 << 15) | (1 << 12) | (3 << 10) | (pll->modulo << 8) | (pll->ADClkSrc << 7) | (0 << 1));
  415. dib8000_reset_pll_common(state, pll);
  416. }
  417. static int dib8000_reset_gpio(struct dib8000_state *st)
  418. {
  419. /* reset the GPIOs */
  420. dib8000_write_word(st, 1029, st->cfg.gpio_dir);
  421. dib8000_write_word(st, 1030, st->cfg.gpio_val);
  422. /* TODO 782 is P_gpio_od */
  423. dib8000_write_word(st, 1032, st->cfg.gpio_pwm_pos);
  424. dib8000_write_word(st, 1037, st->cfg.pwm_freq_div);
  425. return 0;
  426. }
  427. static int dib8000_cfg_gpio(struct dib8000_state *st, u8 num, u8 dir, u8 val)
  428. {
  429. st->cfg.gpio_dir = dib8000_read_word(st, 1029);
  430. st->cfg.gpio_dir &= ~(1 << num); /* reset the direction bit */
  431. st->cfg.gpio_dir |= (dir & 0x1) << num; /* set the new direction */
  432. dib8000_write_word(st, 1029, st->cfg.gpio_dir);
  433. st->cfg.gpio_val = dib8000_read_word(st, 1030);
  434. st->cfg.gpio_val &= ~(1 << num); /* reset the direction bit */
  435. st->cfg.gpio_val |= (val & 0x01) << num; /* set the new value */
  436. dib8000_write_word(st, 1030, st->cfg.gpio_val);
  437. dprintk("gpio dir: %x: gpio val: %x", st->cfg.gpio_dir, st->cfg.gpio_val);
  438. return 0;
  439. }
  440. int dib8000_set_gpio(struct dvb_frontend *fe, u8 num, u8 dir, u8 val)
  441. {
  442. struct dib8000_state *state = fe->demodulator_priv;
  443. return dib8000_cfg_gpio(state, num, dir, val);
  444. }
  445. EXPORT_SYMBOL(dib8000_set_gpio);
  446. static const u16 dib8000_defaults[] = {
  447. /* auto search configuration - lock0 by default waiting
  448. * for cpil_lock; lock1 cpil_lock; lock2 tmcc_sync_lock */
  449. 3, 7,
  450. 0x0004,
  451. 0x0400,
  452. 0x0814,
  453. 12, 11,
  454. 0x001b,
  455. 0x7740,
  456. 0x005b,
  457. 0x8d80,
  458. 0x01c9,
  459. 0xc380,
  460. 0x0000,
  461. 0x0080,
  462. 0x0000,
  463. 0x0090,
  464. 0x0001,
  465. 0xd4c0,
  466. /*1, 32,
  467. 0x6680 // P_corm_thres Lock algorithms configuration */
  468. 11, 80, /* set ADC level to -16 */
  469. (1 << 13) - 825 - 117,
  470. (1 << 13) - 837 - 117,
  471. (1 << 13) - 811 - 117,
  472. (1 << 13) - 766 - 117,
  473. (1 << 13) - 737 - 117,
  474. (1 << 13) - 693 - 117,
  475. (1 << 13) - 648 - 117,
  476. (1 << 13) - 619 - 117,
  477. (1 << 13) - 575 - 117,
  478. (1 << 13) - 531 - 117,
  479. (1 << 13) - 501 - 117,
  480. 4, 108,
  481. 0,
  482. 0,
  483. 0,
  484. 0,
  485. 1, 175,
  486. 0x0410,
  487. 1, 179,
  488. 8192, // P_fft_nb_to_cut
  489. 6, 181,
  490. 0x2800, // P_coff_corthres_ ( 2k 4k 8k ) 0x2800
  491. 0x2800,
  492. 0x2800,
  493. 0x2800, // P_coff_cpilthres_ ( 2k 4k 8k ) 0x2800
  494. 0x2800,
  495. 0x2800,
  496. 2, 193,
  497. 0x0666, // P_pha3_thres
  498. 0x0000, // P_cti_use_cpe, P_cti_use_prog
  499. 2, 205,
  500. 0x200f, // P_cspu_regul, P_cspu_win_cut
  501. 0x000f, // P_des_shift_work
  502. 5, 215,
  503. 0x023d, // P_adp_regul_cnt
  504. 0x00a4, // P_adp_noise_cnt
  505. 0x00a4, // P_adp_regul_ext
  506. 0x7ff0, // P_adp_noise_ext
  507. 0x3ccc, // P_adp_fil
  508. 1, 230,
  509. 0x0000, // P_2d_byp_ti_num
  510. 1, 263,
  511. 0x800, //P_equal_thres_wgn
  512. 1, 268,
  513. (2 << 9) | 39, // P_equal_ctrl_synchro, P_equal_speedmode
  514. 1, 270,
  515. 0x0001, // P_div_lock0_wait
  516. 1, 285,
  517. 0x0020, //p_fec_
  518. 1, 299,
  519. 0x0062, // P_smo_mode, P_smo_rs_discard, P_smo_fifo_flush, P_smo_pid_parse, P_smo_error_discard
  520. 1, 338,
  521. (1 << 12) | // P_ctrl_corm_thres4pre_freq_inh=1
  522. (1 << 10) | // P_ctrl_pre_freq_mode_sat=1
  523. (0 << 9) | // P_ctrl_pre_freq_inh=0
  524. (3 << 5) | // P_ctrl_pre_freq_step=3
  525. (1 << 0), // P_pre_freq_win_len=1
  526. 1, 903,
  527. (0 << 4) | 2, // P_divclksel=0 P_divbitsel=2 (was clk=3,bit=1 for MPW)
  528. 0,
  529. };
  530. static u16 dib8000_identify(struct i2c_device *client)
  531. {
  532. u16 value;
  533. //because of glitches sometimes
  534. value = dib8000_i2c_read16(client, 896);
  535. if ((value = dib8000_i2c_read16(client, 896)) != 0x01b3) {
  536. dprintk("wrong Vendor ID (read=0x%x)", value);
  537. return 0;
  538. }
  539. value = dib8000_i2c_read16(client, 897);
  540. if (value != 0x8000 && value != 0x8001 && value != 0x8002) {
  541. dprintk("wrong Device ID (%x)", value);
  542. return 0;
  543. }
  544. switch (value) {
  545. case 0x8000:
  546. dprintk("found DiB8000A");
  547. break;
  548. case 0x8001:
  549. dprintk("found DiB8000B");
  550. break;
  551. case 0x8002:
  552. dprintk("found DiB8000C");
  553. break;
  554. }
  555. return value;
  556. }
  557. static int dib8000_reset(struct dvb_frontend *fe)
  558. {
  559. struct dib8000_state *state = fe->demodulator_priv;
  560. dib8000_write_word(state, 1287, 0x0003); /* sram lead in, rdy */
  561. if ((state->revision = dib8000_identify(&state->i2c)) == 0)
  562. return -EINVAL;
  563. if (state->revision == 0x8000)
  564. dprintk("error : dib8000 MA not supported");
  565. dibx000_reset_i2c_master(&state->i2c_master);
  566. dib8000_set_power_mode(state, DIB8000M_POWER_ALL);
  567. /* always leave the VBG voltage on - it consumes almost nothing but takes a long time to start */
  568. dib8000_set_adc_state(state, DIBX000_VBG_ENABLE);
  569. /* restart all parts */
  570. dib8000_write_word(state, 770, 0xffff);
  571. dib8000_write_word(state, 771, 0xffff);
  572. dib8000_write_word(state, 772, 0xfffc);
  573. dib8000_write_word(state, 898, 0x000c); // sad
  574. dib8000_write_word(state, 1280, 0x004d);
  575. dib8000_write_word(state, 1281, 0x000c);
  576. dib8000_write_word(state, 770, 0x0000);
  577. dib8000_write_word(state, 771, 0x0000);
  578. dib8000_write_word(state, 772, 0x0000);
  579. dib8000_write_word(state, 898, 0x0004); // sad
  580. dib8000_write_word(state, 1280, 0x0000);
  581. dib8000_write_word(state, 1281, 0x0000);
  582. /* drives */
  583. if (state->cfg.drives)
  584. dib8000_write_word(state, 906, state->cfg.drives);
  585. else {
  586. dprintk("using standard PAD-drive-settings, please adjust settings in config-struct to be optimal.");
  587. dib8000_write_word(state, 906, 0x2d98); // min drive SDRAM - not optimal - adjust
  588. }
  589. dib8000_reset_pll(state);
  590. if (dib8000_reset_gpio(state) != 0)
  591. dprintk("GPIO reset was not successful.");
  592. if (dib8000_set_output_mode(state, OUTMODE_HIGH_Z) != 0)
  593. dprintk("OUTPUT_MODE could not be resetted.");
  594. state->current_agc = NULL;
  595. // P_iqc_alpha_pha, P_iqc_alpha_amp, P_iqc_dcc_alpha, ...
  596. /* P_iqc_ca2 = 0; P_iqc_impnc_on = 0; P_iqc_mode = 0; */
  597. if (state->cfg.pll->ifreq == 0)
  598. dib8000_write_word(state, 40, 0x0755); /* P_iqc_corr_inh = 0 enable IQcorr block */
  599. else
  600. dib8000_write_word(state, 40, 0x1f55); /* P_iqc_corr_inh = 1 disable IQcorr block */
  601. {
  602. u16 l = 0, r;
  603. const u16 *n;
  604. n = dib8000_defaults;
  605. l = *n++;
  606. while (l) {
  607. r = *n++;
  608. do {
  609. dib8000_write_word(state, r, *n++);
  610. r++;
  611. } while (--l);
  612. l = *n++;
  613. }
  614. }
  615. state->isdbt_cfg_loaded = 0;
  616. //div_cfg override for special configs
  617. if (state->cfg.div_cfg != 0)
  618. dib8000_write_word(state, 903, state->cfg.div_cfg);
  619. /* unforce divstr regardless whether i2c enumeration was done or not */
  620. dib8000_write_word(state, 1285, dib8000_read_word(state, 1285) & ~(1 << 1));
  621. dib8000_set_bandwidth(state, 6000);
  622. dib8000_set_adc_state(state, DIBX000_SLOW_ADC_ON);
  623. dib8000_sad_calib(state);
  624. dib8000_set_adc_state(state, DIBX000_SLOW_ADC_OFF);
  625. dib8000_set_power_mode(state, DIB8000M_POWER_INTERFACE_ONLY);
  626. return 0;
  627. }
  628. static void dib8000_restart_agc(struct dib8000_state *state)
  629. {
  630. // P_restart_iqc & P_restart_agc
  631. dib8000_write_word(state, 770, 0x0a00);
  632. dib8000_write_word(state, 770, 0x0000);
  633. }
  634. static int dib8000_update_lna(struct dib8000_state *state)
  635. {
  636. u16 dyn_gain;
  637. if (state->cfg.update_lna) {
  638. // read dyn_gain here (because it is demod-dependent and not tuner)
  639. dyn_gain = dib8000_read_word(state, 390);
  640. if (state->cfg.update_lna(&state->fe, dyn_gain)) { // LNA has changed
  641. dib8000_restart_agc(state);
  642. return 1;
  643. }
  644. }
  645. return 0;
  646. }
  647. static int dib8000_set_agc_config(struct dib8000_state *state, u8 band)
  648. {
  649. struct dibx000_agc_config *agc = NULL;
  650. int i;
  651. if (state->current_band == band && state->current_agc != NULL)
  652. return 0;
  653. state->current_band = band;
  654. for (i = 0; i < state->cfg.agc_config_count; i++)
  655. if (state->cfg.agc[i].band_caps & band) {
  656. agc = &state->cfg.agc[i];
  657. break;
  658. }
  659. if (agc == NULL) {
  660. dprintk("no valid AGC configuration found for band 0x%02x", band);
  661. return -EINVAL;
  662. }
  663. state->current_agc = agc;
  664. /* AGC */
  665. dib8000_write_word(state, 76, agc->setup);
  666. dib8000_write_word(state, 77, agc->inv_gain);
  667. dib8000_write_word(state, 78, agc->time_stabiliz);
  668. dib8000_write_word(state, 101, (agc->alpha_level << 12) | agc->thlock);
  669. // Demod AGC loop configuration
  670. dib8000_write_word(state, 102, (agc->alpha_mant << 5) | agc->alpha_exp);
  671. dib8000_write_word(state, 103, (agc->beta_mant << 6) | agc->beta_exp);
  672. dprintk("WBD: ref: %d, sel: %d, active: %d, alpha: %d",
  673. state->wbd_ref != 0 ? state->wbd_ref : agc->wbd_ref, agc->wbd_sel, !agc->perform_agc_softsplit, agc->wbd_sel);
  674. /* AGC continued */
  675. if (state->wbd_ref != 0)
  676. dib8000_write_word(state, 106, state->wbd_ref);
  677. else // use default
  678. dib8000_write_word(state, 106, agc->wbd_ref);
  679. dib8000_write_word(state, 107, (agc->wbd_alpha << 9) | (agc->perform_agc_softsplit << 8));
  680. dib8000_write_word(state, 108, agc->agc1_max);
  681. dib8000_write_word(state, 109, agc->agc1_min);
  682. dib8000_write_word(state, 110, agc->agc2_max);
  683. dib8000_write_word(state, 111, agc->agc2_min);
  684. dib8000_write_word(state, 112, (agc->agc1_pt1 << 8) | agc->agc1_pt2);
  685. dib8000_write_word(state, 113, (agc->agc1_slope1 << 8) | agc->agc1_slope2);
  686. dib8000_write_word(state, 114, (agc->agc2_pt1 << 8) | agc->agc2_pt2);
  687. dib8000_write_word(state, 115, (agc->agc2_slope1 << 8) | agc->agc2_slope2);
  688. dib8000_write_word(state, 75, agc->agc1_pt3);
  689. dib8000_write_word(state, 923, (dib8000_read_word(state, 923) & 0xffe3) | (agc->wbd_inv << 4) | (agc->wbd_sel << 2)); /*LB : 929 -> 923 */
  690. return 0;
  691. }
  692. static int dib8000_agc_soft_split(struct dib8000_state *state)
  693. {
  694. u16 agc, split_offset;
  695. if (!state->current_agc || !state->current_agc->perform_agc_softsplit || state->current_agc->split.max == 0)
  696. return FE_CALLBACK_TIME_NEVER;
  697. // n_agc_global
  698. agc = dib8000_read_word(state, 390);
  699. if (agc > state->current_agc->split.min_thres)
  700. split_offset = state->current_agc->split.min;
  701. else if (agc < state->current_agc->split.max_thres)
  702. split_offset = state->current_agc->split.max;
  703. else
  704. split_offset = state->current_agc->split.max *
  705. (agc - state->current_agc->split.min_thres) / (state->current_agc->split.max_thres - state->current_agc->split.min_thres);
  706. dprintk("AGC split_offset: %d", split_offset);
  707. // P_agc_force_split and P_agc_split_offset
  708. dib8000_write_word(state, 107, (dib8000_read_word(state, 107) & 0xff00) | split_offset);
  709. return 5000;
  710. }
  711. static int dib8000_agc_startup(struct dvb_frontend *fe)
  712. {
  713. struct dib8000_state *state = fe->demodulator_priv;
  714. enum frontend_tune_state *tune_state = &state->tune_state;
  715. int ret = 0;
  716. switch (*tune_state) {
  717. case CT_AGC_START:
  718. // set power-up level: interf+analog+AGC
  719. dib8000_set_adc_state(state, DIBX000_ADC_ON);
  720. if (dib8000_set_agc_config(state, (unsigned char)(BAND_OF_FREQUENCY(fe->dtv_property_cache.frequency / 1000))) != 0) {
  721. *tune_state = CT_AGC_STOP;
  722. state->status = FE_STATUS_TUNE_FAILED;
  723. break;
  724. }
  725. ret = 70;
  726. *tune_state = CT_AGC_STEP_0;
  727. break;
  728. case CT_AGC_STEP_0:
  729. //AGC initialization
  730. if (state->cfg.agc_control)
  731. state->cfg.agc_control(&state->fe, 1);
  732. dib8000_restart_agc(state);
  733. // wait AGC rough lock time
  734. ret = 50;
  735. *tune_state = CT_AGC_STEP_1;
  736. break;
  737. case CT_AGC_STEP_1:
  738. // wait AGC accurate lock time
  739. ret = 70;
  740. if (dib8000_update_lna(state))
  741. // wait only AGC rough lock time
  742. ret = 50;
  743. else
  744. *tune_state = CT_AGC_STEP_2;
  745. break;
  746. case CT_AGC_STEP_2:
  747. dib8000_agc_soft_split(state);
  748. if (state->cfg.agc_control)
  749. state->cfg.agc_control(&state->fe, 0);
  750. *tune_state = CT_AGC_STOP;
  751. break;
  752. default:
  753. ret = dib8000_agc_soft_split(state);
  754. break;
  755. }
  756. return ret;
  757. }
  758. static void dib8000_update_timf(struct dib8000_state *state)
  759. {
  760. u32 timf = state->timf = dib8000_read32(state, 435);
  761. dib8000_write_word(state, 29, (u16) (timf >> 16));
  762. dib8000_write_word(state, 30, (u16) (timf & 0xffff));
  763. dprintk("Updated timing frequency: %d (default: %d)", state->timf, state->timf_default);
  764. }
  765. static void dib8000_set_channel(struct dib8000_state *state, u8 seq, u8 autosearching)
  766. {
  767. u16 mode, max_constellation, seg_diff_mask = 0, nbseg_diff = 0;
  768. u8 guard, crate, constellation, timeI;
  769. u8 permu_seg[] = { 6, 5, 7, 4, 8, 3, 9, 2, 10, 1, 11, 0, 12 };
  770. u16 i, coeff[4], P_cfr_left_edge = 0, P_cfr_right_edge = 0, seg_mask13 = 0x1fff; // All 13 segments enabled
  771. const s16 *ncoeff, *ana_fe;
  772. u16 tmcc_pow = 0;
  773. u16 coff_pow = 0x2800;
  774. u16 init_prbs = 0xfff;
  775. u16 ana_gain = 0;
  776. u16 adc_target_16dB[11] = {
  777. (1 << 13) - 825 - 117,
  778. (1 << 13) - 837 - 117,
  779. (1 << 13) - 811 - 117,
  780. (1 << 13) - 766 - 117,
  781. (1 << 13) - 737 - 117,
  782. (1 << 13) - 693 - 117,
  783. (1 << 13) - 648 - 117,
  784. (1 << 13) - 619 - 117,
  785. (1 << 13) - 575 - 117,
  786. (1 << 13) - 531 - 117,
  787. (1 << 13) - 501 - 117
  788. };
  789. if (state->ber_monitored_layer != LAYER_ALL)
  790. dib8000_write_word(state, 285, (dib8000_read_word(state, 285) & 0x60) | state->ber_monitored_layer);
  791. else
  792. dib8000_write_word(state, 285, dib8000_read_word(state, 285) & 0x60);
  793. i = dib8000_read_word(state, 26) & 1; // P_dds_invspec
  794. dib8000_write_word(state, 26, state->fe.dtv_property_cache.inversion ^ i);
  795. if (state->fe.dtv_property_cache.isdbt_sb_mode) {
  796. //compute new dds_freq for the seg and adjust prbs
  797. int seg_offset =
  798. state->fe.dtv_property_cache.isdbt_sb_segment_idx - (state->fe.dtv_property_cache.isdbt_sb_segment_count / 2) -
  799. (state->fe.dtv_property_cache.isdbt_sb_segment_count % 2);
  800. int clk = state->cfg.pll->internal;
  801. u32 segtodds = ((u32) (430 << 23) / clk) << 3; // segtodds = SegBW / Fclk * pow(2,26)
  802. int dds_offset = seg_offset * segtodds;
  803. int new_dds, sub_channel;
  804. if ((state->fe.dtv_property_cache.isdbt_sb_segment_count % 2) == 0) // if even
  805. dds_offset -= (int)(segtodds / 2);
  806. if (state->cfg.pll->ifreq == 0) {
  807. if ((state->fe.dtv_property_cache.inversion ^ i) == 0) {
  808. dib8000_write_word(state, 26, dib8000_read_word(state, 26) | 1);
  809. new_dds = dds_offset;
  810. } else
  811. new_dds = dds_offset;
  812. // We shift tuning frequency if the wanted segment is :
  813. // - the segment of center frequency with an odd total number of segments
  814. // - the segment to the left of center frequency with an even total number of segments
  815. // - the segment to the right of center frequency with an even total number of segments
  816. if ((state->fe.dtv_property_cache.delivery_system == SYS_ISDBT) && (state->fe.dtv_property_cache.isdbt_sb_mode == 1)
  817. &&
  818. (((state->fe.dtv_property_cache.isdbt_sb_segment_count % 2)
  819. && (state->fe.dtv_property_cache.isdbt_sb_segment_idx ==
  820. ((state->fe.dtv_property_cache.isdbt_sb_segment_count / 2) + 1)))
  821. || (((state->fe.dtv_property_cache.isdbt_sb_segment_count % 2) == 0)
  822. && (state->fe.dtv_property_cache.isdbt_sb_segment_idx == (state->fe.dtv_property_cache.isdbt_sb_segment_count / 2)))
  823. || (((state->fe.dtv_property_cache.isdbt_sb_segment_count % 2) == 0)
  824. && (state->fe.dtv_property_cache.isdbt_sb_segment_idx ==
  825. ((state->fe.dtv_property_cache.isdbt_sb_segment_count / 2) + 1)))
  826. )) {
  827. new_dds -= ((u32) (850 << 22) / clk) << 4; // new_dds = 850 (freq shift in KHz) / Fclk * pow(2,26)
  828. }
  829. } else {
  830. if ((state->fe.dtv_property_cache.inversion ^ i) == 0)
  831. new_dds = state->cfg.pll->ifreq - dds_offset;
  832. else
  833. new_dds = state->cfg.pll->ifreq + dds_offset;
  834. }
  835. dib8000_write_word(state, 27, (u16) ((new_dds >> 16) & 0x01ff));
  836. dib8000_write_word(state, 28, (u16) (new_dds & 0xffff));
  837. if (state->fe.dtv_property_cache.isdbt_sb_segment_count % 2) // if odd
  838. sub_channel = ((state->fe.dtv_property_cache.isdbt_sb_subchannel + (3 * seg_offset) + 1) % 41) / 3;
  839. else // if even
  840. sub_channel = ((state->fe.dtv_property_cache.isdbt_sb_subchannel + (3 * seg_offset)) % 41) / 3;
  841. sub_channel -= 6;
  842. if (state->fe.dtv_property_cache.transmission_mode == TRANSMISSION_MODE_2K
  843. || state->fe.dtv_property_cache.transmission_mode == TRANSMISSION_MODE_4K) {
  844. dib8000_write_word(state, 219, dib8000_read_word(state, 219) | 0x1); //adp_pass =1
  845. dib8000_write_word(state, 190, dib8000_read_word(state, 190) | (0x1 << 14)); //pha3_force_pha_shift = 1
  846. } else {
  847. dib8000_write_word(state, 219, dib8000_read_word(state, 219) & 0xfffe); //adp_pass =0
  848. dib8000_write_word(state, 190, dib8000_read_word(state, 190) & 0xbfff); //pha3_force_pha_shift = 0
  849. }
  850. switch (state->fe.dtv_property_cache.transmission_mode) {
  851. case TRANSMISSION_MODE_2K:
  852. switch (sub_channel) {
  853. case -6:
  854. init_prbs = 0x0;
  855. break; // 41, 0, 1
  856. case -5:
  857. init_prbs = 0x423;
  858. break; // 02~04
  859. case -4:
  860. init_prbs = 0x9;
  861. break; // 05~07
  862. case -3:
  863. init_prbs = 0x5C7;
  864. break; // 08~10
  865. case -2:
  866. init_prbs = 0x7A6;
  867. break; // 11~13
  868. case -1:
  869. init_prbs = 0x3D8;
  870. break; // 14~16
  871. case 0:
  872. init_prbs = 0x527;
  873. break; // 17~19
  874. case 1:
  875. init_prbs = 0x7FF;
  876. break; // 20~22
  877. case 2:
  878. init_prbs = 0x79B;
  879. break; // 23~25
  880. case 3:
  881. init_prbs = 0x3D6;
  882. break; // 26~28
  883. case 4:
  884. init_prbs = 0x3A2;
  885. break; // 29~31
  886. case 5:
  887. init_prbs = 0x53B;
  888. break; // 32~34
  889. case 6:
  890. init_prbs = 0x2F4;
  891. break; // 35~37
  892. default:
  893. case 7:
  894. init_prbs = 0x213;
  895. break; // 38~40
  896. }
  897. break;
  898. case TRANSMISSION_MODE_4K:
  899. switch (sub_channel) {
  900. case -6:
  901. init_prbs = 0x0;
  902. break; // 41, 0, 1
  903. case -5:
  904. init_prbs = 0x208;
  905. break; // 02~04
  906. case -4:
  907. init_prbs = 0xC3;
  908. break; // 05~07
  909. case -3:
  910. init_prbs = 0x7B9;
  911. break; // 08~10
  912. case -2:
  913. init_prbs = 0x423;
  914. break; // 11~13
  915. case -1:
  916. init_prbs = 0x5C7;
  917. break; // 14~16
  918. case 0:
  919. init_prbs = 0x3D8;
  920. break; // 17~19
  921. case 1:
  922. init_prbs = 0x7FF;
  923. break; // 20~22
  924. case 2:
  925. init_prbs = 0x3D6;
  926. break; // 23~25
  927. case 3:
  928. init_prbs = 0x53B;
  929. break; // 26~28
  930. case 4:
  931. init_prbs = 0x213;
  932. break; // 29~31
  933. case 5:
  934. init_prbs = 0x29;
  935. break; // 32~34
  936. case 6:
  937. init_prbs = 0xD0;
  938. break; // 35~37
  939. default:
  940. case 7:
  941. init_prbs = 0x48E;
  942. break; // 38~40
  943. }
  944. break;
  945. default:
  946. case TRANSMISSION_MODE_8K:
  947. switch (sub_channel) {
  948. case -6:
  949. init_prbs = 0x0;
  950. break; // 41, 0, 1
  951. case -5:
  952. init_prbs = 0x740;
  953. break; // 02~04
  954. case -4:
  955. init_prbs = 0x069;
  956. break; // 05~07
  957. case -3:
  958. init_prbs = 0x7DD;
  959. break; // 08~10
  960. case -2:
  961. init_prbs = 0x208;
  962. break; // 11~13
  963. case -1:
  964. init_prbs = 0x7B9;
  965. break; // 14~16
  966. case 0:
  967. init_prbs = 0x5C7;
  968. break; // 17~19
  969. case 1:
  970. init_prbs = 0x7FF;
  971. break; // 20~22
  972. case 2:
  973. init_prbs = 0x53B;
  974. break; // 23~25
  975. case 3:
  976. init_prbs = 0x29;
  977. break; // 26~28
  978. case 4:
  979. init_prbs = 0x48E;
  980. break; // 29~31
  981. case 5:
  982. init_prbs = 0x4C4;
  983. break; // 32~34
  984. case 6:
  985. init_prbs = 0x367;
  986. break; // 33~37
  987. default:
  988. case 7:
  989. init_prbs = 0x684;
  990. break; // 38~40
  991. }
  992. break;
  993. }
  994. } else { // if not state->fe.dtv_property_cache.isdbt_sb_mode
  995. dib8000_write_word(state, 27, (u16) ((state->cfg.pll->ifreq >> 16) & 0x01ff));
  996. dib8000_write_word(state, 28, (u16) (state->cfg.pll->ifreq & 0xffff));
  997. dib8000_write_word(state, 26, (u16) ((state->cfg.pll->ifreq >> 25) & 0x0003));
  998. }
  999. /*P_mode == ?? */
  1000. dib8000_write_word(state, 10, (seq << 4));
  1001. // dib8000_write_word(state, 287, (dib8000_read_word(state, 287) & 0xe000) | 0x1000);
  1002. switch (state->fe.dtv_property_cache.guard_interval) {
  1003. case GUARD_INTERVAL_1_32:
  1004. guard = 0;
  1005. break;
  1006. case GUARD_INTERVAL_1_16:
  1007. guard = 1;
  1008. break;
  1009. case GUARD_INTERVAL_1_8:
  1010. guard = 2;
  1011. break;
  1012. case GUARD_INTERVAL_1_4:
  1013. default:
  1014. guard = 3;
  1015. break;
  1016. }
  1017. dib8000_write_word(state, 1, (init_prbs << 2) | (guard & 0x3)); // ADDR 1
  1018. max_constellation = DQPSK;
  1019. for (i = 0; i < 3; i++) {
  1020. switch (state->fe.dtv_property_cache.layer[i].modulation) {
  1021. case DQPSK:
  1022. constellation = 0;
  1023. break;
  1024. case QPSK:
  1025. constellation = 1;
  1026. break;
  1027. case QAM_16:
  1028. constellation = 2;
  1029. break;
  1030. case QAM_64:
  1031. default:
  1032. constellation = 3;
  1033. break;
  1034. }
  1035. switch (state->fe.dtv_property_cache.layer[i].fec) {
  1036. case FEC_1_2:
  1037. crate = 1;
  1038. break;
  1039. case FEC_2_3:
  1040. crate = 2;
  1041. break;
  1042. case FEC_3_4:
  1043. crate = 3;
  1044. break;
  1045. case FEC_5_6:
  1046. crate = 5;
  1047. break;
  1048. case FEC_7_8:
  1049. default:
  1050. crate = 7;
  1051. break;
  1052. }
  1053. if ((state->fe.dtv_property_cache.layer[i].interleaving > 0) &&
  1054. ((state->fe.dtv_property_cache.layer[i].interleaving <= 3) ||
  1055. (state->fe.dtv_property_cache.layer[i].interleaving == 4 && state->fe.dtv_property_cache.isdbt_sb_mode == 1))
  1056. )
  1057. timeI = state->fe.dtv_property_cache.layer[i].interleaving;
  1058. else
  1059. timeI = 0;
  1060. dib8000_write_word(state, 2 + i, (constellation << 10) | ((state->fe.dtv_property_cache.layer[i].segment_count & 0xf) << 6) |
  1061. (crate << 3) | timeI);
  1062. if (state->fe.dtv_property_cache.layer[i].segment_count > 0) {
  1063. switch (max_constellation) {
  1064. case DQPSK:
  1065. case QPSK:
  1066. if (state->fe.dtv_property_cache.layer[i].modulation == QAM_16 ||
  1067. state->fe.dtv_property_cache.layer[i].modulation == QAM_64)
  1068. max_constellation = state->fe.dtv_property_cache.layer[i].modulation;
  1069. break;
  1070. case QAM_16:
  1071. if (state->fe.dtv_property_cache.layer[i].modulation == QAM_64)
  1072. max_constellation = state->fe.dtv_property_cache.layer[i].modulation;
  1073. break;
  1074. }
  1075. }
  1076. }
  1077. mode = fft_to_mode(state);
  1078. //dib8000_write_word(state, 5, 13); /*p_last_seg = 13*/
  1079. dib8000_write_word(state, 274, (dib8000_read_word(state, 274) & 0xffcf) |
  1080. ((state->fe.dtv_property_cache.isdbt_partial_reception & 1) << 5) | ((state->fe.dtv_property_cache.
  1081. isdbt_sb_mode & 1) << 4));
  1082. dprintk("mode = %d ; guard = %d", mode, state->fe.dtv_property_cache.guard_interval);
  1083. /* signal optimization parameter */
  1084. if (state->fe.dtv_property_cache.isdbt_partial_reception) {
  1085. seg_diff_mask = (state->fe.dtv_property_cache.layer[0].modulation == DQPSK) << permu_seg[0];
  1086. for (i = 1; i < 3; i++)
  1087. nbseg_diff +=
  1088. (state->fe.dtv_property_cache.layer[i].modulation == DQPSK) * state->fe.dtv_property_cache.layer[i].segment_count;
  1089. for (i = 0; i < nbseg_diff; i++)
  1090. seg_diff_mask |= 1 << permu_seg[i + 1];
  1091. } else {
  1092. for (i = 0; i < 3; i++)
  1093. nbseg_diff +=
  1094. (state->fe.dtv_property_cache.layer[i].modulation == DQPSK) * state->fe.dtv_property_cache.layer[i].segment_count;
  1095. for (i = 0; i < nbseg_diff; i++)
  1096. seg_diff_mask |= 1 << permu_seg[i];
  1097. }
  1098. dprintk("nbseg_diff = %X (%d)", seg_diff_mask, seg_diff_mask);
  1099. state->differential_constellation = (seg_diff_mask != 0);
  1100. dib8000_set_diversity_in(&state->fe, state->diversity_onoff);
  1101. if (state->fe.dtv_property_cache.isdbt_sb_mode == 1) { // ISDB-Tsb
  1102. if (state->fe.dtv_property_cache.isdbt_partial_reception == 1) // 3-segments
  1103. seg_mask13 = 0x00E0;
  1104. else // 1-segment
  1105. seg_mask13 = 0x0040;
  1106. } else
  1107. seg_mask13 = 0x1fff;
  1108. // WRITE: Mode & Diff mask
  1109. dib8000_write_word(state, 0, (mode << 13) | seg_diff_mask);
  1110. if ((seg_diff_mask) || (state->fe.dtv_property_cache.isdbt_sb_mode))
  1111. dib8000_write_word(state, 268, (dib8000_read_word(state, 268) & 0xF9FF) | 0x0200);
  1112. else
  1113. dib8000_write_word(state, 268, (2 << 9) | 39); //init value
  1114. // ---- SMALL ----
  1115. // P_small_seg_diff
  1116. dib8000_write_word(state, 352, seg_diff_mask); // ADDR 352
  1117. dib8000_write_word(state, 353, seg_mask13); // ADDR 353
  1118. /* // P_small_narrow_band=0, P_small_last_seg=13, P_small_offset_num_car=5 */
  1119. // dib8000_write_word(state, 351, (state->fe.dtv_property_cache.isdbt_sb_mode << 8) | (13 << 4) | 5 );
  1120. // ---- SMALL ----
  1121. if (state->fe.dtv_property_cache.isdbt_sb_mode == 1) {
  1122. switch (state->fe.dtv_property_cache.transmission_mode) {
  1123. case TRANSMISSION_MODE_2K:
  1124. if (state->fe.dtv_property_cache.isdbt_partial_reception == 0) { // 1-seg
  1125. if (state->fe.dtv_property_cache.layer[0].modulation == DQPSK) // DQPSK
  1126. ncoeff = coeff_2k_sb_1seg_dqpsk;
  1127. else // QPSK or QAM
  1128. ncoeff = coeff_2k_sb_1seg;
  1129. } else { // 3-segments
  1130. if (state->fe.dtv_property_cache.layer[0].modulation == DQPSK) { // DQPSK on central segment
  1131. if (state->fe.dtv_property_cache.layer[1].modulation == DQPSK) // DQPSK on external segments
  1132. ncoeff = coeff_2k_sb_3seg_0dqpsk_1dqpsk;
  1133. else // QPSK or QAM on external segments
  1134. ncoeff = coeff_2k_sb_3seg_0dqpsk;
  1135. } else { // QPSK or QAM on central segment
  1136. if (state->fe.dtv_property_cache.layer[1].modulation == DQPSK) // DQPSK on external segments
  1137. ncoeff = coeff_2k_sb_3seg_1dqpsk;
  1138. else // QPSK or QAM on external segments
  1139. ncoeff = coeff_2k_sb_3seg;
  1140. }
  1141. }
  1142. break;
  1143. case TRANSMISSION_MODE_4K:
  1144. if (state->fe.dtv_property_cache.isdbt_partial_reception == 0) { // 1-seg
  1145. if (state->fe.dtv_property_cache.layer[0].modulation == DQPSK) // DQPSK
  1146. ncoeff = coeff_4k_sb_1seg_dqpsk;
  1147. else // QPSK or QAM
  1148. ncoeff = coeff_4k_sb_1seg;
  1149. } else { // 3-segments
  1150. if (state->fe.dtv_property_cache.layer[0].modulation == DQPSK) { // DQPSK on central segment
  1151. if (state->fe.dtv_property_cache.layer[1].modulation == DQPSK) { // DQPSK on external segments
  1152. ncoeff = coeff_4k_sb_3seg_0dqpsk_1dqpsk;
  1153. } else { // QPSK or QAM on external segments
  1154. ncoeff = coeff_4k_sb_3seg_0dqpsk;
  1155. }
  1156. } else { // QPSK or QAM on central segment
  1157. if (state->fe.dtv_property_cache.layer[1].modulation == DQPSK) { // DQPSK on external segments
  1158. ncoeff = coeff_4k_sb_3seg_1dqpsk;
  1159. } else // QPSK or QAM on external segments
  1160. ncoeff = coeff_4k_sb_3seg;
  1161. }
  1162. }
  1163. break;
  1164. case TRANSMISSION_MODE_AUTO:
  1165. case TRANSMISSION_MODE_8K:
  1166. default:
  1167. if (state->fe.dtv_property_cache.isdbt_partial_reception == 0) { // 1-seg
  1168. if (state->fe.dtv_property_cache.layer[0].modulation == DQPSK) // DQPSK
  1169. ncoeff = coeff_8k_sb_1seg_dqpsk;
  1170. else // QPSK or QAM
  1171. ncoeff = coeff_8k_sb_1seg;
  1172. } else { // 3-segments
  1173. if (state->fe.dtv_property_cache.layer[0].modulation == DQPSK) { // DQPSK on central segment
  1174. if (state->fe.dtv_property_cache.layer[1].modulation == DQPSK) { // DQPSK on external segments
  1175. ncoeff = coeff_8k_sb_3seg_0dqpsk_1dqpsk;
  1176. } else { // QPSK or QAM on external segments
  1177. ncoeff = coeff_8k_sb_3seg_0dqpsk;
  1178. }
  1179. } else { // QPSK or QAM on central segment
  1180. if (state->fe.dtv_property_cache.layer[1].modulation == DQPSK) { // DQPSK on external segments
  1181. ncoeff = coeff_8k_sb_3seg_1dqpsk;
  1182. } else // QPSK or QAM on external segments
  1183. ncoeff = coeff_8k_sb_3seg;
  1184. }
  1185. }
  1186. break;
  1187. }
  1188. }
  1189. if (state->fe.dtv_property_cache.isdbt_sb_mode == 1)
  1190. for (i = 0; i < 8; i++)
  1191. dib8000_write_word(state, 343 + i, ncoeff[i]);
  1192. // P_small_coef_ext_enable=ISDB-Tsb, P_small_narrow_band=ISDB-Tsb, P_small_last_seg=13, P_small_offset_num_car=5
  1193. dib8000_write_word(state, 351,
  1194. (state->fe.dtv_property_cache.isdbt_sb_mode << 9) | (state->fe.dtv_property_cache.isdbt_sb_mode << 8) | (13 << 4) | 5);
  1195. // ---- COFF ----
  1196. // Carloff, the most robust
  1197. if (state->fe.dtv_property_cache.isdbt_sb_mode == 1) { // Sound Broadcasting mode - use both TMCC and AC pilots
  1198. // P_coff_cpil_alpha=4, P_coff_inh=0, P_coff_cpil_winlen=64
  1199. // P_coff_narrow_band=1, P_coff_square_val=1, P_coff_one_seg=~partial_rcpt, P_coff_use_tmcc=1, P_coff_use_ac=1
  1200. dib8000_write_word(state, 187,
  1201. (4 << 12) | (0 << 11) | (63 << 5) | (0x3 << 3) | ((~state->fe.dtv_property_cache.isdbt_partial_reception & 1) << 2)
  1202. | 0x3);
  1203. /* // P_small_coef_ext_enable = 1 */
  1204. /* dib8000_write_word(state, 351, dib8000_read_word(state, 351) | 0x200); */
  1205. if (state->fe.dtv_property_cache.isdbt_partial_reception == 0) { // Sound Broadcasting mode 1 seg
  1206. // P_coff_winlen=63, P_coff_thres_lock=15, P_coff_one_seg_width= (P_mode == 3) , P_coff_one_seg_sym= (P_mode-1)
  1207. if (mode == 3)
  1208. dib8000_write_word(state, 180, 0x1fcf | ((mode - 1) << 14));
  1209. else
  1210. dib8000_write_word(state, 180, 0x0fcf | ((mode - 1) << 14));
  1211. // P_ctrl_corm_thres4pre_freq_inh=1,P_ctrl_pre_freq_mode_sat=1,
  1212. // P_ctrl_pre_freq_inh=0, P_ctrl_pre_freq_step = 5, P_pre_freq_win_len=4
  1213. dib8000_write_word(state, 338, (1 << 12) | (1 << 10) | (0 << 9) | (5 << 5) | 4);
  1214. // P_ctrl_pre_freq_win_len=16, P_ctrl_pre_freq_thres_lockin=8
  1215. dib8000_write_word(state, 340, (16 << 6) | (8 << 0));
  1216. // P_ctrl_pre_freq_thres_lockout=6, P_small_use_tmcc/ac/cp=1
  1217. dib8000_write_word(state, 341, (6 << 3) | (1 << 2) | (1 << 1) | (1 << 0));
  1218. // P_coff_corthres_8k, 4k, 2k and P_coff_cpilthres_8k, 4k, 2k
  1219. dib8000_write_word(state, 181, 300);
  1220. dib8000_write_word(state, 182, 150);
  1221. dib8000_write_word(state, 183, 80);
  1222. dib8000_write_word(state, 184, 300);
  1223. dib8000_write_word(state, 185, 150);
  1224. dib8000_write_word(state, 186, 80);
  1225. } else { // Sound Broadcasting mode 3 seg
  1226. // P_coff_one_seg_sym= 1, P_coff_one_seg_width= 1, P_coff_winlen=63, P_coff_thres_lock=15
  1227. /* if (mode == 3) */
  1228. /* dib8000_write_word(state, 180, 0x2fca | ((0) << 14)); */
  1229. /* else */
  1230. /* dib8000_write_word(state, 180, 0x2fca | ((1) << 14)); */
  1231. dib8000_write_word(state, 180, 0x1fcf | (1 << 14));
  1232. // P_ctrl_corm_thres4pre_freq_inh = 1, P_ctrl_pre_freq_mode_sat=1,
  1233. // P_ctrl_pre_freq_inh=0, P_ctrl_pre_freq_step = 4, P_pre_freq_win_len=4
  1234. dib8000_write_word(state, 338, (1 << 12) | (1 << 10) | (0 << 9) | (4 << 5) | 4);
  1235. // P_ctrl_pre_freq_win_len=16, P_ctrl_pre_freq_thres_lockin=8
  1236. dib8000_write_word(state, 340, (16 << 6) | (8 << 0));
  1237. //P_ctrl_pre_freq_thres_lockout=6, P_small_use_tmcc/ac/cp=1
  1238. dib8000_write_word(state, 341, (6 << 3) | (1 << 2) | (1 << 1) | (1 << 0));
  1239. // P_coff_corthres_8k, 4k, 2k and P_coff_cpilthres_8k, 4k, 2k
  1240. dib8000_write_word(state, 181, 350);
  1241. dib8000_write_word(state, 182, 300);
  1242. dib8000_write_word(state, 183, 250);
  1243. dib8000_write_word(state, 184, 350);
  1244. dib8000_write_word(state, 185, 300);
  1245. dib8000_write_word(state, 186, 250);
  1246. }
  1247. } else if (state->isdbt_cfg_loaded == 0) { // if not Sound Broadcasting mode : put default values for 13 segments
  1248. dib8000_write_word(state, 180, (16 << 6) | 9);
  1249. dib8000_write_word(state, 187, (4 << 12) | (8 << 5) | 0x2);
  1250. coff_pow = 0x2800;
  1251. for (i = 0; i < 6; i++)
  1252. dib8000_write_word(state, 181 + i, coff_pow);
  1253. // P_ctrl_corm_thres4pre_freq_inh=1, P_ctrl_pre_freq_mode_sat=1,
  1254. // P_ctrl_pre_freq_mode_sat=1, P_ctrl_pre_freq_inh=0, P_ctrl_pre_freq_step = 3, P_pre_freq_win_len=1
  1255. dib8000_write_word(state, 338, (1 << 12) | (1 << 10) | (0 << 9) | (3 << 5) | 1);
  1256. // P_ctrl_pre_freq_win_len=8, P_ctrl_pre_freq_thres_lockin=6
  1257. dib8000_write_word(state, 340, (8 << 6) | (6 << 0));
  1258. // P_ctrl_pre_freq_thres_lockout=4, P_small_use_tmcc/ac/cp=1
  1259. dib8000_write_word(state, 341, (4 << 3) | (1 << 2) | (1 << 1) | (1 << 0));
  1260. }
  1261. // ---- FFT ----
  1262. if (state->fe.dtv_property_cache.isdbt_sb_mode == 1 && state->fe.dtv_property_cache.isdbt_partial_reception == 0) // 1-seg
  1263. dib8000_write_word(state, 178, 64); // P_fft_powrange=64
  1264. else
  1265. dib8000_write_word(state, 178, 32); // P_fft_powrange=32
  1266. /* make the cpil_coff_lock more robust but slower p_coff_winlen
  1267. * 6bits; p_coff_thres_lock 6bits (for coff lock if needed)
  1268. */
  1269. /* if ( ( nbseg_diff>0)&&(nbseg_diff<13))
  1270. dib8000_write_word(state, 187, (dib8000_read_word(state, 187) & 0xfffb) | (1 << 3)); */
  1271. dib8000_write_word(state, 189, ~seg_mask13 | seg_diff_mask); /* P_lmod4_seg_inh */
  1272. dib8000_write_word(state, 192, ~seg_mask13 | seg_diff_mask); /* P_pha3_seg_inh */
  1273. dib8000_write_word(state, 225, ~seg_mask13 | seg_diff_mask); /* P_tac_seg_inh */
  1274. if ((!state->fe.dtv_property_cache.isdbt_sb_mode) && (state->cfg.pll->ifreq == 0))
  1275. dib8000_write_word(state, 266, ~seg_mask13 | seg_diff_mask | 0x40); /* P_equal_noise_seg_inh */
  1276. else
  1277. dib8000_write_word(state, 266, ~seg_mask13 | seg_diff_mask); /* P_equal_noise_seg_inh */
  1278. dib8000_write_word(state, 287, ~seg_mask13 | 0x1000); /* P_tmcc_seg_inh */
  1279. //dib8000_write_word(state, 288, ~seg_mask13 | seg_diff_mask); /* P_tmcc_seg_eq_inh */
  1280. if (!autosearching)
  1281. dib8000_write_word(state, 288, (~seg_mask13 | seg_diff_mask) & 0x1fff); /* P_tmcc_seg_eq_inh */
  1282. else
  1283. dib8000_write_word(state, 288, 0x1fff); //disable equalisation of the tmcc when autosearch to be able to find the DQPSK channels.
  1284. dprintk("287 = %X (%d)", ~seg_mask13 | 0x1000, ~seg_mask13 | 0x1000);
  1285. dib8000_write_word(state, 211, seg_mask13 & (~seg_diff_mask)); /* P_des_seg_enabled */
  1286. /* offset loop parameters */
  1287. if (state->fe.dtv_property_cache.isdbt_sb_mode == 1) {
  1288. if (state->fe.dtv_property_cache.isdbt_partial_reception == 0) // Sound Broadcasting mode 1 seg
  1289. /* P_timf_alpha = (11-P_mode), P_corm_alpha=6, P_corm_thres=0x80 */
  1290. dib8000_write_word(state, 32, ((11 - mode) << 12) | (6 << 8) | 0x40);
  1291. else // Sound Broadcasting mode 3 seg
  1292. /* P_timf_alpha = (10-P_mode), P_corm_alpha=6, P_corm_thres=0x80 */
  1293. dib8000_write_word(state, 32, ((10 - mode) << 12) | (6 << 8) | 0x60);
  1294. } else
  1295. // TODO in 13 seg, timf_alpha can always be the same or not ?
  1296. /* P_timf_alpha = (9-P_mode, P_corm_alpha=6, P_corm_thres=0x80 */
  1297. dib8000_write_word(state, 32, ((9 - mode) << 12) | (6 << 8) | 0x80);
  1298. if (state->fe.dtv_property_cache.isdbt_sb_mode == 1) {
  1299. if (state->fe.dtv_property_cache.isdbt_partial_reception == 0) // Sound Broadcasting mode 1 seg
  1300. /* P_ctrl_pha_off_max=3 P_ctrl_sfreq_inh =0 P_ctrl_sfreq_step = (11-P_mode) */
  1301. dib8000_write_word(state, 37, (3 << 5) | (0 << 4) | (10 - mode));
  1302. else // Sound Broadcasting mode 3 seg
  1303. /* P_ctrl_pha_off_max=3 P_ctrl_sfreq_inh =0 P_ctrl_sfreq_step = (10-P_mode) */
  1304. dib8000_write_word(state, 37, (3 << 5) | (0 << 4) | (9 - mode));
  1305. } else
  1306. /* P_ctrl_pha_off_max=3 P_ctrl_sfreq_inh =0 P_ctrl_sfreq_step = 9 */
  1307. dib8000_write_word(state, 37, (3 << 5) | (0 << 4) | (8 - mode));
  1308. /* P_dvsy_sync_wait - reuse mode */
  1309. switch (state->fe.dtv_property_cache.transmission_mode) {
  1310. case TRANSMISSION_MODE_8K:
  1311. mode = 256;
  1312. break;
  1313. case TRANSMISSION_MODE_4K:
  1314. mode = 128;
  1315. break;
  1316. default:
  1317. case TRANSMISSION_MODE_2K:
  1318. mode = 64;
  1319. break;
  1320. }
  1321. if (state->cfg.diversity_delay == 0)
  1322. mode = (mode * (1 << (guard)) * 3) / 2 + 48; // add 50% SFN margin + compensate for one DVSY-fifo
  1323. else
  1324. mode = (mode * (1 << (guard)) * 3) / 2 + state->cfg.diversity_delay; // add 50% SFN margin + compensate for DVSY-fifo
  1325. mode <<= 4;
  1326. dib8000_write_word(state, 273, (dib8000_read_word(state, 273) & 0x000f) | mode);
  1327. /* channel estimation fine configuration */
  1328. switch (max_constellation) {
  1329. case QAM_64:
  1330. ana_gain = 0x7; // -1 : avoid def_est saturation when ADC target is -16dB
  1331. coeff[0] = 0x0148; /* P_adp_regul_cnt 0.04 */
  1332. coeff[1] = 0xfff0; /* P_adp_noise_cnt -0.002 */
  1333. coeff[2] = 0x00a4; /* P_adp_regul_ext 0.02 */
  1334. coeff[3] = 0xfff8; /* P_adp_noise_ext -0.001 */
  1335. //if (!state->cfg.hostbus_diversity) //if diversity, we should prehaps use the configuration of the max_constallation -1
  1336. break;
  1337. case QAM_16:
  1338. ana_gain = 0x7; // -1 : avoid def_est saturation when ADC target is -16dB
  1339. coeff[0] = 0x023d; /* P_adp_regul_cnt 0.07 */
  1340. coeff[1] = 0xffdf; /* P_adp_noise_cnt -0.004 */
  1341. coeff[2] = 0x00a4; /* P_adp_regul_ext 0.02 */
  1342. coeff[3] = 0xfff0; /* P_adp_noise_ext -0.002 */
  1343. //if (!((state->cfg.hostbus_diversity) && (max_constellation == QAM_16)))
  1344. break;
  1345. default:
  1346. ana_gain = 0; // 0 : goes along with ADC target at -22dB to keep good mobile performance and lock at sensitivity level
  1347. coeff[0] = 0x099a; /* P_adp_regul_cnt 0.3 */
  1348. coeff[1] = 0xffae; /* P_adp_noise_cnt -0.01 */
  1349. coeff[2] = 0x0333; /* P_adp_regul_ext 0.1 */
  1350. coeff[3] = 0xfff8; /* P_adp_noise_ext -0.002 */
  1351. break;
  1352. }
  1353. for (mode = 0; mode < 4; mode++)
  1354. dib8000_write_word(state, 215 + mode, coeff[mode]);
  1355. // update ana_gain depending on max constellation
  1356. dib8000_write_word(state, 116, ana_gain);
  1357. // update ADC target depending on ana_gain
  1358. if (ana_gain) { // set -16dB ADC target for ana_gain=-1
  1359. for (i = 0; i < 10; i++)
  1360. dib8000_write_word(state, 80 + i, adc_target_16dB[i]);
  1361. } else { // set -22dB ADC target for ana_gain=0
  1362. for (i = 0; i < 10; i++)
  1363. dib8000_write_word(state, 80 + i, adc_target_16dB[i] - 355);
  1364. }
  1365. // ---- ANA_FE ----
  1366. if (state->fe.dtv_property_cache.isdbt_sb_mode) {
  1367. if (state->fe.dtv_property_cache.isdbt_partial_reception == 1) // 3-segments
  1368. ana_fe = ana_fe_coeff_3seg;
  1369. else // 1-segment
  1370. ana_fe = ana_fe_coeff_1seg;
  1371. } else
  1372. ana_fe = ana_fe_coeff_13seg;
  1373. if (state->fe.dtv_property_cache.isdbt_sb_mode == 1 || state->isdbt_cfg_loaded == 0)
  1374. for (mode = 0; mode < 24; mode++)
  1375. dib8000_write_word(state, 117 + mode, ana_fe[mode]);
  1376. // ---- CHAN_BLK ----
  1377. for (i = 0; i < 13; i++) {
  1378. if ((((~seg_diff_mask) >> i) & 1) == 1) {
  1379. P_cfr_left_edge += (1 << i) * ((i == 0) || ((((seg_mask13 & (~seg_diff_mask)) >> (i - 1)) & 1) == 0));
  1380. P_cfr_right_edge += (1 << i) * ((i == 12) || ((((seg_mask13 & (~seg_diff_mask)) >> (i + 1)) & 1) == 0));
  1381. }
  1382. }
  1383. dib8000_write_word(state, 222, P_cfr_left_edge); // P_cfr_left_edge
  1384. dib8000_write_word(state, 223, P_cfr_right_edge); // P_cfr_right_edge
  1385. // "P_cspu_left_edge" not used => do not care
  1386. // "P_cspu_right_edge" not used => do not care
  1387. if (state->fe.dtv_property_cache.isdbt_sb_mode == 1) { // ISDB-Tsb
  1388. dib8000_write_word(state, 228, 1); // P_2d_mode_byp=1
  1389. dib8000_write_word(state, 205, dib8000_read_word(state, 205) & 0xfff0); // P_cspu_win_cut = 0
  1390. if (state->fe.dtv_property_cache.isdbt_partial_reception == 0 // 1-segment
  1391. && state->fe.dtv_property_cache.transmission_mode == TRANSMISSION_MODE_2K) {
  1392. //dib8000_write_word(state, 219, dib8000_read_word(state, 219) & 0xfffe); // P_adp_pass = 0
  1393. dib8000_write_word(state, 265, 15); // P_equal_noise_sel = 15
  1394. }
  1395. } else if (state->isdbt_cfg_loaded == 0) {
  1396. dib8000_write_word(state, 228, 0); // default value
  1397. dib8000_write_word(state, 265, 31); // default value
  1398. dib8000_write_word(state, 205, 0x200f); // init value
  1399. }
  1400. // ---- TMCC ----
  1401. for (i = 0; i < 3; i++)
  1402. tmcc_pow +=
  1403. (((state->fe.dtv_property_cache.layer[i].modulation == DQPSK) * 4 + 1) * state->fe.dtv_property_cache.layer[i].segment_count);
  1404. // Quantif of "P_tmcc_dec_thres_?k" is (0, 5+mode, 9);
  1405. // Threshold is set at 1/4 of max power.
  1406. tmcc_pow *= (1 << (9 - 2));
  1407. dib8000_write_word(state, 290, tmcc_pow); // P_tmcc_dec_thres_2k
  1408. dib8000_write_word(state, 291, tmcc_pow); // P_tmcc_dec_thres_4k
  1409. dib8000_write_word(state, 292, tmcc_pow); // P_tmcc_dec_thres_8k
  1410. //dib8000_write_word(state, 287, (1 << 13) | 0x1000 );
  1411. // ---- PHA3 ----
  1412. if (state->isdbt_cfg_loaded == 0)
  1413. dib8000_write_word(state, 250, 3285); /*p_2d_hspeed_thr0 */
  1414. if (state->fe.dtv_property_cache.isdbt_sb_mode == 1)
  1415. state->isdbt_cfg_loaded = 0;
  1416. else
  1417. state->isdbt_cfg_loaded = 1;
  1418. }
  1419. static int dib8000_autosearch_start(struct dvb_frontend *fe)
  1420. {
  1421. u8 factor;
  1422. u32 value;
  1423. struct dib8000_state *state = fe->demodulator_priv;
  1424. int slist = 0;
  1425. state->fe.dtv_property_cache.transmission_mode = TRANSMISSION_MODE_8K;
  1426. state->fe.dtv_property_cache.guard_interval = GUARD_INTERVAL_1_8;
  1427. //state->fe.dtv_property_cache.isdbt_sb_mode = 0;
  1428. //state->fe.dtv_property_cache.isdbt_partial_reception = 0;
  1429. state->fe.dtv_property_cache.inversion = 0;
  1430. if (!state->fe.dtv_property_cache.isdbt_sb_mode)
  1431. state->fe.dtv_property_cache.layer[0].segment_count = 13;
  1432. state->fe.dtv_property_cache.layer[0].modulation = QAM_64;
  1433. state->fe.dtv_property_cache.layer[0].fec = FEC_2_3;
  1434. state->fe.dtv_property_cache.layer[0].interleaving = 0;
  1435. //choose the right list, in sb, always do everything
  1436. if (state->fe.dtv_property_cache.isdbt_sb_mode) {
  1437. slist = 7;
  1438. dib8000_write_word(state, 0, (dib8000_read_word(state, 0) & 0x9fff) | (1 << 13));
  1439. } else {
  1440. if (state->fe.dtv_property_cache.guard_interval == GUARD_INTERVAL_AUTO) {
  1441. if (state->fe.dtv_property_cache.transmission_mode == TRANSMISSION_MODE_AUTO) {
  1442. slist = 7;
  1443. dib8000_write_word(state, 0, (dib8000_read_word(state, 0) & 0x9fff) | (1 << 13)); // P_mode = 1 to have autosearch start ok with mode2
  1444. } else {
  1445. slist = 3;
  1446. state->fe.dtv_property_cache.transmission_mode = state->fe.dtv_property_cache.transmission_mode;
  1447. }
  1448. } else {
  1449. if (state->fe.dtv_property_cache.transmission_mode == TRANSMISSION_MODE_AUTO) {
  1450. slist = 2;
  1451. state->fe.dtv_property_cache.guard_interval = state->fe.dtv_property_cache.guard_interval;
  1452. dib8000_write_word(state, 0, (dib8000_read_word(state, 0) & 0x9fff) | (1 << 13)); // P_mode = 1
  1453. } else {
  1454. slist = 0;
  1455. state->fe.dtv_property_cache.transmission_mode = state->fe.dtv_property_cache.transmission_mode;
  1456. state->fe.dtv_property_cache.guard_interval = state->fe.dtv_property_cache.guard_interval;
  1457. }
  1458. }
  1459. dprintk("using list for autosearch : %d", slist);
  1460. dib8000_set_channel(state, (unsigned char)slist, 1);
  1461. //dib8000_write_word(state, 0, (dib8000_read_word(state, 0) & 0x9fff) | (1 << 13)); // P_mode = 1
  1462. factor = 1;
  1463. //set lock_mask values
  1464. dib8000_write_word(state, 6, 0x4);
  1465. dib8000_write_word(state, 7, 0x8);
  1466. dib8000_write_word(state, 8, 0x1000);
  1467. //set lock_mask wait time values
  1468. value = 50 * state->cfg.pll->internal * factor;
  1469. dib8000_write_word(state, 11, (u16) ((value >> 16) & 0xffff)); // lock0 wait time
  1470. dib8000_write_word(state, 12, (u16) (value & 0xffff)); // lock0 wait time
  1471. value = 100 * state->cfg.pll->internal * factor;
  1472. dib8000_write_word(state, 13, (u16) ((value >> 16) & 0xffff)); // lock1 wait time
  1473. dib8000_write_word(state, 14, (u16) (value & 0xffff)); // lock1 wait time
  1474. value = 1000 * state->cfg.pll->internal * factor;
  1475. dib8000_write_word(state, 15, (u16) ((value >> 16) & 0xffff)); // lock2 wait time
  1476. dib8000_write_word(state, 16, (u16) (value & 0xffff)); // lock2 wait time
  1477. value = dib8000_read_word(state, 0);
  1478. dib8000_write_word(state, 0, (u16) ((1 << 15) | value));
  1479. dib8000_read_word(state, 1284); // reset the INT. n_irq_pending
  1480. dib8000_write_word(state, 0, (u16) value);
  1481. }
  1482. return 0;
  1483. }
  1484. static int dib8000_autosearch_irq(struct dvb_frontend *fe)
  1485. {
  1486. struct dib8000_state *state = fe->demodulator_priv;
  1487. u16 irq_pending = dib8000_read_word(state, 1284);
  1488. if (irq_pending & 0x1) { // failed
  1489. dprintk("dib8000_autosearch_irq failed");
  1490. return 1;
  1491. }
  1492. if (irq_pending & 0x2) { // succeeded
  1493. dprintk("dib8000_autosearch_irq succeeded");
  1494. return 2;
  1495. }
  1496. return 0; // still pending
  1497. }
  1498. static int dib8000_tune(struct dvb_frontend *fe)
  1499. {
  1500. struct dib8000_state *state = fe->demodulator_priv;
  1501. int ret = 0;
  1502. u16 value, mode = fft_to_mode(state);
  1503. // we are already tuned - just resuming from suspend
  1504. if (state == NULL)
  1505. return -EINVAL;
  1506. dib8000_set_bandwidth(state, state->fe.dtv_property_cache.bandwidth_hz / 1000);
  1507. dib8000_set_channel(state, 0, 0);
  1508. // restart demod
  1509. ret |= dib8000_write_word(state, 770, 0x4000);
  1510. ret |= dib8000_write_word(state, 770, 0x0000);
  1511. msleep(45);
  1512. /* P_ctrl_inh_cor=0, P_ctrl_alpha_cor=4, P_ctrl_inh_isi=0, P_ctrl_alpha_isi=3 */
  1513. /* ret |= dib8000_write_word(state, 29, (0 << 9) | (4 << 5) | (0 << 4) | (3 << 0) ); workaround inh_isi stays at 1 */
  1514. // never achieved a lock before - wait for timfreq to update
  1515. if (state->timf == 0) {
  1516. if (state->fe.dtv_property_cache.isdbt_sb_mode == 1) {
  1517. if (state->fe.dtv_property_cache.isdbt_partial_reception == 0) // Sound Broadcasting mode 1 seg
  1518. msleep(300);
  1519. else // Sound Broadcasting mode 3 seg
  1520. msleep(500);
  1521. } else // 13 seg
  1522. msleep(200);
  1523. }
  1524. //dump_reg(state);
  1525. if (state->fe.dtv_property_cache.isdbt_sb_mode == 1) {
  1526. if (state->fe.dtv_property_cache.isdbt_partial_reception == 0) { // Sound Broadcasting mode 1 seg
  1527. /* P_timf_alpha = (13-P_mode) , P_corm_alpha=6, P_corm_thres=0x40 alpha to check on board */
  1528. dib8000_write_word(state, 32, ((13 - mode) << 12) | (6 << 8) | 0x40);
  1529. //dib8000_write_word(state, 32, (8 << 12) | (6 << 8) | 0x80);
  1530. /* P_ctrl_sfreq_step= (12-P_mode) P_ctrl_sfreq_inh =0 P_ctrl_pha_off_max */
  1531. ret |= dib8000_write_word(state, 37, (12 - mode) | ((5 + mode) << 5));
  1532. } else { // Sound Broadcasting mode 3 seg
  1533. /* P_timf_alpha = (12-P_mode) , P_corm_alpha=6, P_corm_thres=0x60 alpha to check on board */
  1534. dib8000_write_word(state, 32, ((12 - mode) << 12) | (6 << 8) | 0x60);
  1535. ret |= dib8000_write_word(state, 37, (11 - mode) | ((5 + mode) << 5));
  1536. }
  1537. } else { // 13 seg
  1538. /* P_timf_alpha = 8 , P_corm_alpha=6, P_corm_thres=0x80 alpha to check on board */
  1539. dib8000_write_word(state, 32, ((11 - mode) << 12) | (6 << 8) | 0x80);
  1540. ret |= dib8000_write_word(state, 37, (10 - mode) | ((5 + mode) << 5));
  1541. }
  1542. // we achieved a coff_cpil_lock - it's time to update the timf
  1543. if ((dib8000_read_word(state, 568) >> 11) & 0x1)
  1544. dib8000_update_timf(state);
  1545. //now that tune is finished, lock0 should lock on fec_mpeg to output this lock on MP_LOCK. It's changed in autosearch start
  1546. dib8000_write_word(state, 6, 0x200);
  1547. if (state->revision == 0x8002) {
  1548. value = dib8000_read_word(state, 903);
  1549. dib8000_write_word(state, 903, value & ~(1 << 3));
  1550. msleep(1);
  1551. dib8000_write_word(state, 903, value | (1 << 3));
  1552. }
  1553. return ret;
  1554. }
  1555. static int dib8000_wakeup(struct dvb_frontend *fe)
  1556. {
  1557. struct dib8000_state *state = fe->demodulator_priv;
  1558. dib8000_set_power_mode(state, DIB8000M_POWER_ALL);
  1559. dib8000_set_adc_state(state, DIBX000_ADC_ON);
  1560. if (dib8000_set_adc_state(state, DIBX000_SLOW_ADC_ON) != 0)
  1561. dprintk("could not start Slow ADC");
  1562. return 0;
  1563. }
  1564. static int dib8000_sleep(struct dvb_frontend *fe)
  1565. {
  1566. struct dib8000_state *st = fe->demodulator_priv;
  1567. if (1) {
  1568. dib8000_set_output_mode(st, OUTMODE_HIGH_Z);
  1569. dib8000_set_power_mode(st, DIB8000M_POWER_INTERFACE_ONLY);
  1570. return dib8000_set_adc_state(st, DIBX000_SLOW_ADC_OFF) | dib8000_set_adc_state(st, DIBX000_ADC_OFF);
  1571. } else {
  1572. return 0;
  1573. }
  1574. }
  1575. static int dib8000_get_frontend(struct dvb_frontend *fe, struct dvb_frontend_parameters *fep)
  1576. {
  1577. struct dib8000_state *state = fe->demodulator_priv;
  1578. u16 i, val = 0;
  1579. fe->dtv_property_cache.bandwidth_hz = 6000000;
  1580. fe->dtv_property_cache.isdbt_sb_mode = dib8000_read_word(state, 508) & 0x1;
  1581. val = dib8000_read_word(state, 570);
  1582. fe->dtv_property_cache.inversion = (val & 0x40) >> 6;
  1583. switch ((val & 0x30) >> 4) {
  1584. case 1:
  1585. fe->dtv_property_cache.transmission_mode = TRANSMISSION_MODE_2K;
  1586. break;
  1587. case 3:
  1588. default:
  1589. fe->dtv_property_cache.transmission_mode = TRANSMISSION_MODE_8K;
  1590. break;
  1591. }
  1592. switch (val & 0x3) {
  1593. case 0:
  1594. fe->dtv_property_cache.guard_interval = GUARD_INTERVAL_1_32;
  1595. dprintk("dib8000_get_frontend GI = 1/32 ");
  1596. break;
  1597. case 1:
  1598. fe->dtv_property_cache.guard_interval = GUARD_INTERVAL_1_16;
  1599. dprintk("dib8000_get_frontend GI = 1/16 ");
  1600. break;
  1601. case 2:
  1602. dprintk("dib8000_get_frontend GI = 1/8 ");
  1603. fe->dtv_property_cache.guard_interval = GUARD_INTERVAL_1_8;
  1604. break;
  1605. case 3:
  1606. dprintk("dib8000_get_frontend GI = 1/4 ");
  1607. fe->dtv_property_cache.guard_interval = GUARD_INTERVAL_1_4;
  1608. break;
  1609. }
  1610. val = dib8000_read_word(state, 505);
  1611. fe->dtv_property_cache.isdbt_partial_reception = val & 1;
  1612. dprintk("dib8000_get_frontend : partial_reception = %d ", fe->dtv_property_cache.isdbt_partial_reception);
  1613. for (i = 0; i < 3; i++) {
  1614. val = dib8000_read_word(state, 493 + i);
  1615. fe->dtv_property_cache.layer[i].segment_count = val & 0x0F;
  1616. dprintk("dib8000_get_frontend : Layer %d segments = %d ", i, fe->dtv_property_cache.layer[i].segment_count);
  1617. val = dib8000_read_word(state, 499 + i);
  1618. fe->dtv_property_cache.layer[i].interleaving = val & 0x3;
  1619. dprintk("dib8000_get_frontend : Layer %d time_intlv = %d ", i, fe->dtv_property_cache.layer[i].interleaving);
  1620. val = dib8000_read_word(state, 481 + i);
  1621. switch (val & 0x7) {
  1622. case 1:
  1623. fe->dtv_property_cache.layer[i].fec = FEC_1_2;
  1624. dprintk("dib8000_get_frontend : Layer %d Code Rate = 1/2 ", i);
  1625. break;
  1626. case 2:
  1627. fe->dtv_property_cache.layer[i].fec = FEC_2_3;
  1628. dprintk("dib8000_get_frontend : Layer %d Code Rate = 2/3 ", i);
  1629. break;
  1630. case 3:
  1631. fe->dtv_property_cache.layer[i].fec = FEC_3_4;
  1632. dprintk("dib8000_get_frontend : Layer %d Code Rate = 3/4 ", i);
  1633. break;
  1634. case 5:
  1635. fe->dtv_property_cache.layer[i].fec = FEC_5_6;
  1636. dprintk("dib8000_get_frontend : Layer %d Code Rate = 5/6 ", i);
  1637. break;
  1638. default:
  1639. fe->dtv_property_cache.layer[i].fec = FEC_7_8;
  1640. dprintk("dib8000_get_frontend : Layer %d Code Rate = 7/8 ", i);
  1641. break;
  1642. }
  1643. val = dib8000_read_word(state, 487 + i);
  1644. switch (val & 0x3) {
  1645. case 0:
  1646. dprintk("dib8000_get_frontend : Layer %d DQPSK ", i);
  1647. fe->dtv_property_cache.layer[i].modulation = DQPSK;
  1648. break;
  1649. case 1:
  1650. fe->dtv_property_cache.layer[i].modulation = QPSK;
  1651. dprintk("dib8000_get_frontend : Layer %d QPSK ", i);
  1652. break;
  1653. case 2:
  1654. fe->dtv_property_cache.layer[i].modulation = QAM_16;
  1655. dprintk("dib8000_get_frontend : Layer %d QAM16 ", i);
  1656. break;
  1657. case 3:
  1658. default:
  1659. dprintk("dib8000_get_frontend : Layer %d QAM64 ", i);
  1660. fe->dtv_property_cache.layer[i].modulation = QAM_64;
  1661. break;
  1662. }
  1663. }
  1664. return 0;
  1665. }
  1666. static int dib8000_set_frontend(struct dvb_frontend *fe, struct dvb_frontend_parameters *fep)
  1667. {
  1668. struct dib8000_state *state = fe->demodulator_priv;
  1669. int time, ret;
  1670. dib8000_set_output_mode(state, OUTMODE_HIGH_Z);
  1671. if (fe->ops.tuner_ops.set_params)
  1672. fe->ops.tuner_ops.set_params(fe, fep);
  1673. /* start up the AGC */
  1674. state->tune_state = CT_AGC_START;
  1675. do {
  1676. time = dib8000_agc_startup(fe);
  1677. if (time != FE_CALLBACK_TIME_NEVER)
  1678. msleep(time / 10);
  1679. else
  1680. break;
  1681. } while (state->tune_state != CT_AGC_STOP);
  1682. if (state->fe.dtv_property_cache.frequency == 0) {
  1683. dprintk("dib8000: must at least specify frequency ");
  1684. return 0;
  1685. }
  1686. if (state->fe.dtv_property_cache.bandwidth_hz == 0) {
  1687. dprintk("dib8000: no bandwidth specified, set to default ");
  1688. state->fe.dtv_property_cache.bandwidth_hz = 6000000;
  1689. }
  1690. state->tune_state = CT_DEMOD_START;
  1691. if ((state->fe.dtv_property_cache.delivery_system != SYS_ISDBT) ||
  1692. (state->fe.dtv_property_cache.inversion == INVERSION_AUTO) ||
  1693. (state->fe.dtv_property_cache.transmission_mode == TRANSMISSION_MODE_AUTO) ||
  1694. (state->fe.dtv_property_cache.guard_interval == GUARD_INTERVAL_AUTO) ||
  1695. (((state->fe.dtv_property_cache.isdbt_layer_enabled & (1 << 0)) != 0) &&
  1696. (state->fe.dtv_property_cache.layer[0].segment_count != 0xff) &&
  1697. (state->fe.dtv_property_cache.layer[0].segment_count != 0) &&
  1698. ((state->fe.dtv_property_cache.layer[0].modulation == QAM_AUTO) ||
  1699. (state->fe.dtv_property_cache.layer[0].fec == FEC_AUTO))) ||
  1700. (((state->fe.dtv_property_cache.isdbt_layer_enabled & (1 << 1)) != 0) &&
  1701. (state->fe.dtv_property_cache.layer[1].segment_count != 0xff) &&
  1702. (state->fe.dtv_property_cache.layer[1].segment_count != 0) &&
  1703. ((state->fe.dtv_property_cache.layer[1].modulation == QAM_AUTO) ||
  1704. (state->fe.dtv_property_cache.layer[1].fec == FEC_AUTO))) ||
  1705. (((state->fe.dtv_property_cache.isdbt_layer_enabled & (1 << 2)) != 0) &&
  1706. (state->fe.dtv_property_cache.layer[2].segment_count != 0xff) &&
  1707. (state->fe.dtv_property_cache.layer[2].segment_count != 0) &&
  1708. ((state->fe.dtv_property_cache.layer[2].modulation == QAM_AUTO) ||
  1709. (state->fe.dtv_property_cache.layer[2].fec == FEC_AUTO))) ||
  1710. (((state->fe.dtv_property_cache.layer[0].segment_count == 0) ||
  1711. ((state->fe.dtv_property_cache.isdbt_layer_enabled & (1 << 0)) == 0)) &&
  1712. ((state->fe.dtv_property_cache.layer[1].segment_count == 0) ||
  1713. ((state->fe.dtv_property_cache.isdbt_layer_enabled & (2 << 0)) == 0)) &&
  1714. ((state->fe.dtv_property_cache.layer[2].segment_count == 0) || ((state->fe.dtv_property_cache.isdbt_layer_enabled & (3 << 0)) == 0)))) {
  1715. int i = 800, found;
  1716. dib8000_set_bandwidth(state, fe->dtv_property_cache.bandwidth_hz / 1000);
  1717. dib8000_autosearch_start(fe);
  1718. do {
  1719. msleep(10);
  1720. found = dib8000_autosearch_irq(fe);
  1721. } while (found == 0 && i--);
  1722. dprintk("Frequency %d Hz, autosearch returns: %d", fep->frequency, found);
  1723. if (found == 0 || found == 1)
  1724. return 0; // no channel found
  1725. dib8000_get_frontend(fe, fep);
  1726. }
  1727. ret = dib8000_tune(fe);
  1728. /* make this a config parameter */
  1729. dib8000_set_output_mode(state, state->cfg.output_mode);
  1730. return ret;
  1731. }
  1732. static int dib8000_read_status(struct dvb_frontend *fe, fe_status_t * stat)
  1733. {
  1734. struct dib8000_state *state = fe->demodulator_priv;
  1735. u16 lock = dib8000_read_word(state, 568);
  1736. *stat = 0;
  1737. if ((lock >> 14) & 1) // AGC
  1738. *stat |= FE_HAS_SIGNAL;
  1739. if ((lock >> 8) & 1) // Equal
  1740. *stat |= FE_HAS_CARRIER;
  1741. if ((lock >> 3) & 1) // TMCC_SYNC
  1742. *stat |= FE_HAS_SYNC;
  1743. if ((lock >> 5) & 7) // FEC MPEG
  1744. *stat |= FE_HAS_LOCK;
  1745. lock = dib8000_read_word(state, 554); // Viterbi Layer A
  1746. if (lock & 0x01)
  1747. *stat |= FE_HAS_VITERBI;
  1748. lock = dib8000_read_word(state, 555); // Viterbi Layer B
  1749. if (lock & 0x01)
  1750. *stat |= FE_HAS_VITERBI;
  1751. lock = dib8000_read_word(state, 556); // Viterbi Layer C
  1752. if (lock & 0x01)
  1753. *stat |= FE_HAS_VITERBI;
  1754. return 0;
  1755. }
  1756. static int dib8000_read_ber(struct dvb_frontend *fe, u32 * ber)
  1757. {
  1758. struct dib8000_state *state = fe->demodulator_priv;
  1759. *ber = (dib8000_read_word(state, 560) << 16) | dib8000_read_word(state, 561); // 13 segments
  1760. return 0;
  1761. }
  1762. static int dib8000_read_unc_blocks(struct dvb_frontend *fe, u32 * unc)
  1763. {
  1764. struct dib8000_state *state = fe->demodulator_priv;
  1765. *unc = dib8000_read_word(state, 565); // packet error on 13 seg
  1766. return 0;
  1767. }
  1768. static int dib8000_read_signal_strength(struct dvb_frontend *fe, u16 * strength)
  1769. {
  1770. struct dib8000_state *state = fe->demodulator_priv;
  1771. u16 val = dib8000_read_word(state, 390);
  1772. *strength = 65535 - val;
  1773. return 0;
  1774. }
  1775. static int dib8000_read_snr(struct dvb_frontend *fe, u16 * snr)
  1776. {
  1777. struct dib8000_state *state = fe->demodulator_priv;
  1778. u16 val;
  1779. s32 signal_mant, signal_exp, noise_mant, noise_exp;
  1780. u32 result = 0;
  1781. val = dib8000_read_word(state, 542);
  1782. noise_mant = (val >> 6) & 0xff;
  1783. noise_exp = (val & 0x3f);
  1784. val = dib8000_read_word(state, 543);
  1785. signal_mant = (val >> 6) & 0xff;
  1786. signal_exp = (val & 0x3f);
  1787. if ((noise_exp & 0x20) != 0)
  1788. noise_exp -= 0x40;
  1789. if ((signal_exp & 0x20) != 0)
  1790. signal_exp -= 0x40;
  1791. if (signal_mant != 0)
  1792. result = intlog10(2) * 10 * signal_exp + 10 * intlog10(signal_mant);
  1793. else
  1794. result = intlog10(2) * 10 * signal_exp - 100;
  1795. if (noise_mant != 0)
  1796. result -= intlog10(2) * 10 * noise_exp + 10 * intlog10(noise_mant);
  1797. else
  1798. result -= intlog10(2) * 10 * noise_exp - 100;
  1799. *snr = result / (1 << 24);
  1800. return 0;
  1801. }
  1802. int dib8000_i2c_enumeration(struct i2c_adapter *host, int no_of_demods, u8 default_addr, u8 first_addr)
  1803. {
  1804. int k = 0;
  1805. u8 new_addr = 0;
  1806. struct i2c_device client = {.adap = host };
  1807. for (k = no_of_demods - 1; k >= 0; k--) {
  1808. /* designated i2c address */
  1809. new_addr = first_addr + (k << 1);
  1810. client.addr = new_addr;
  1811. dib8000_i2c_write16(&client, 1287, 0x0003); /* sram lead in, rdy */
  1812. if (dib8000_identify(&client) == 0) {
  1813. dib8000_i2c_write16(&client, 1287, 0x0003); /* sram lead in, rdy */
  1814. client.addr = default_addr;
  1815. if (dib8000_identify(&client) == 0) {
  1816. dprintk("#%d: not identified", k);
  1817. return -EINVAL;
  1818. }
  1819. }
  1820. /* start diversity to pull_down div_str - just for i2c-enumeration */
  1821. dib8000_i2c_write16(&client, 1286, (1 << 10) | (4 << 6));
  1822. /* set new i2c address and force divstart */
  1823. dib8000_i2c_write16(&client, 1285, (new_addr << 2) | 0x2);
  1824. client.addr = new_addr;
  1825. dib8000_identify(&client);
  1826. dprintk("IC %d initialized (to i2c_address 0x%x)", k, new_addr);
  1827. }
  1828. for (k = 0; k < no_of_demods; k++) {
  1829. new_addr = first_addr | (k << 1);
  1830. client.addr = new_addr;
  1831. // unforce divstr
  1832. dib8000_i2c_write16(&client, 1285, new_addr << 2);
  1833. /* deactivate div - it was just for i2c-enumeration */
  1834. dib8000_i2c_write16(&client, 1286, 0);
  1835. }
  1836. return 0;
  1837. }
  1838. EXPORT_SYMBOL(dib8000_i2c_enumeration);
  1839. static int dib8000_fe_get_tune_settings(struct dvb_frontend *fe, struct dvb_frontend_tune_settings *tune)
  1840. {
  1841. tune->min_delay_ms = 1000;
  1842. tune->step_size = 0;
  1843. tune->max_drift = 0;
  1844. return 0;
  1845. }
  1846. static void dib8000_release(struct dvb_frontend *fe)
  1847. {
  1848. struct dib8000_state *st = fe->demodulator_priv;
  1849. dibx000_exit_i2c_master(&st->i2c_master);
  1850. kfree(st);
  1851. }
  1852. struct i2c_adapter *dib8000_get_i2c_master(struct dvb_frontend *fe, enum dibx000_i2c_interface intf, int gating)
  1853. {
  1854. struct dib8000_state *st = fe->demodulator_priv;
  1855. return dibx000_get_i2c_adapter(&st->i2c_master, intf, gating);
  1856. }
  1857. EXPORT_SYMBOL(dib8000_get_i2c_master);
  1858. static const struct dvb_frontend_ops dib8000_ops = {
  1859. .info = {
  1860. .name = "DiBcom 8000 ISDB-T",
  1861. .type = FE_OFDM,
  1862. .frequency_min = 44250000,
  1863. .frequency_max = 867250000,
  1864. .frequency_stepsize = 62500,
  1865. .caps = FE_CAN_INVERSION_AUTO |
  1866. FE_CAN_FEC_1_2 | FE_CAN_FEC_2_3 | FE_CAN_FEC_3_4 |
  1867. FE_CAN_FEC_5_6 | FE_CAN_FEC_7_8 | FE_CAN_FEC_AUTO |
  1868. FE_CAN_QPSK | FE_CAN_QAM_16 | FE_CAN_QAM_64 | FE_CAN_QAM_AUTO |
  1869. FE_CAN_TRANSMISSION_MODE_AUTO | FE_CAN_GUARD_INTERVAL_AUTO | FE_CAN_RECOVER | FE_CAN_HIERARCHY_AUTO,
  1870. },
  1871. .release = dib8000_release,
  1872. .init = dib8000_wakeup,
  1873. .sleep = dib8000_sleep,
  1874. .set_frontend = dib8000_set_frontend,
  1875. .get_tune_settings = dib8000_fe_get_tune_settings,
  1876. .get_frontend = dib8000_get_frontend,
  1877. .read_status = dib8000_read_status,
  1878. .read_ber = dib8000_read_ber,
  1879. .read_signal_strength = dib8000_read_signal_strength,
  1880. .read_snr = dib8000_read_snr,
  1881. .read_ucblocks = dib8000_read_unc_blocks,
  1882. };
  1883. struct dvb_frontend *dib8000_attach(struct i2c_adapter *i2c_adap, u8 i2c_addr, struct dib8000_config *cfg)
  1884. {
  1885. struct dvb_frontend *fe;
  1886. struct dib8000_state *state;
  1887. dprintk("dib8000_attach");
  1888. state = kzalloc(sizeof(struct dib8000_state), GFP_KERNEL);
  1889. if (state == NULL)
  1890. return NULL;
  1891. memcpy(&state->cfg, cfg, sizeof(struct dib8000_config));
  1892. state->i2c.adap = i2c_adap;
  1893. state->i2c.addr = i2c_addr;
  1894. state->gpio_val = cfg->gpio_val;
  1895. state->gpio_dir = cfg->gpio_dir;
  1896. /* Ensure the output mode remains at the previous default if it's
  1897. * not specifically set by the caller.
  1898. */
  1899. if ((state->cfg.output_mode != OUTMODE_MPEG2_SERIAL) && (state->cfg.output_mode != OUTMODE_MPEG2_PAR_GATED_CLK))
  1900. state->cfg.output_mode = OUTMODE_MPEG2_FIFO;
  1901. fe = &state->fe;
  1902. fe->demodulator_priv = state;
  1903. memcpy(&state->fe.ops, &dib8000_ops, sizeof(struct dvb_frontend_ops));
  1904. state->timf_default = cfg->pll->timf;
  1905. if (dib8000_identify(&state->i2c) == 0)
  1906. goto error;
  1907. dibx000_init_i2c_master(&state->i2c_master, DIB8000, state->i2c.adap, state->i2c.addr);
  1908. dib8000_reset(fe);
  1909. dib8000_write_word(state, 285, (dib8000_read_word(state, 285) & ~0x60) | (3 << 5)); /* ber_rs_len = 3 */
  1910. return fe;
  1911. error:
  1912. kfree(state);
  1913. return NULL;
  1914. }
  1915. EXPORT_SYMBOL(dib8000_attach);
  1916. MODULE_AUTHOR("Olivier Grenie <Olivier.Grenie@dibcom.fr, " "Patrick Boettcher <pboettcher@dibcom.fr>");
  1917. MODULE_DESCRIPTION("Driver for the DiBcom 8000 ISDB-T demodulator");
  1918. MODULE_LICENSE("GPL");