dib8000.c 82 KB

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