drxd_hard.c 75 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986
  1. /*
  2. * drxd_hard.c: DVB-T Demodulator Micronas DRX3975D-A2,DRX397xD-B1
  3. *
  4. * Copyright (C) 2003-2007 Micronas
  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
  8. * version 2 only, as published by the Free Software Foundation.
  9. *
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  20. * 02110-1301, USA
  21. * Or, point your browser to http://www.gnu.org/copyleft/gpl.html
  22. */
  23. #include <linux/kernel.h>
  24. #include <linux/module.h>
  25. #include <linux/moduleparam.h>
  26. #include <linux/init.h>
  27. #include <linux/delay.h>
  28. #include <linux/firmware.h>
  29. #include <linux/i2c.h>
  30. #include <asm/div64.h>
  31. #include "dvb_frontend.h"
  32. #include "drxd.h"
  33. #include "drxd_firm.h"
  34. #define DRX_FW_FILENAME_A2 "drxd-a2-1.1.fw"
  35. #define DRX_FW_FILENAME_B1 "drxd-b1-1.1.fw"
  36. #define CHUNK_SIZE 48
  37. #define DRX_I2C_RMW 0x10
  38. #define DRX_I2C_BROADCAST 0x20
  39. #define DRX_I2C_CLEARCRC 0x80
  40. #define DRX_I2C_SINGLE_MASTER 0xC0
  41. #define DRX_I2C_MODEFLAGS 0xC0
  42. #define DRX_I2C_FLAGS 0xF0
  43. #define DEFAULT_LOCK_TIMEOUT 1100
  44. #define DRX_CHANNEL_AUTO 0
  45. #define DRX_CHANNEL_HIGH 1
  46. #define DRX_CHANNEL_LOW 2
  47. #define DRX_LOCK_MPEG 1
  48. #define DRX_LOCK_FEC 2
  49. #define DRX_LOCK_DEMOD 4
  50. /****************************************************************************/
  51. enum CSCDState {
  52. CSCD_INIT = 0,
  53. CSCD_SET,
  54. CSCD_SAVED
  55. };
  56. enum CDrxdState {
  57. DRXD_UNINITIALIZED = 0,
  58. DRXD_STOPPED,
  59. DRXD_STARTED
  60. };
  61. enum AGC_CTRL_MODE {
  62. AGC_CTRL_AUTO = 0,
  63. AGC_CTRL_USER,
  64. AGC_CTRL_OFF
  65. };
  66. enum OperationMode {
  67. OM_Default,
  68. OM_DVBT_Diversity_Front,
  69. OM_DVBT_Diversity_End
  70. };
  71. struct SCfgAgc {
  72. enum AGC_CTRL_MODE ctrlMode;
  73. u16 outputLevel; /* range [0, ... , 1023], 1/n of fullscale range */
  74. u16 settleLevel; /* range [0, ... , 1023], 1/n of fullscale range */
  75. u16 minOutputLevel; /* range [0, ... , 1023], 1/n of fullscale range */
  76. u16 maxOutputLevel; /* range [0, ... , 1023], 1/n of fullscale range */
  77. u16 speed; /* range [0, ... , 1023], 1/n of fullscale range */
  78. u16 R1;
  79. u16 R2;
  80. u16 R3;
  81. };
  82. struct SNoiseCal {
  83. int cpOpt;
  84. short cpNexpOfs;
  85. short tdCal2k;
  86. short tdCal8k;
  87. };
  88. enum app_env {
  89. APPENV_STATIC = 0,
  90. APPENV_PORTABLE = 1,
  91. APPENV_MOBILE = 2
  92. };
  93. enum EIFFilter {
  94. IFFILTER_SAW = 0,
  95. IFFILTER_DISCRETE = 1
  96. };
  97. struct drxd_state {
  98. struct dvb_frontend frontend;
  99. struct dvb_frontend_ops ops;
  100. struct dtv_frontend_properties props;
  101. const struct firmware *fw;
  102. struct device *dev;
  103. struct i2c_adapter *i2c;
  104. void *priv;
  105. struct drxd_config config;
  106. int i2c_access;
  107. int init_done;
  108. struct mutex mutex;
  109. u8 chip_adr;
  110. u16 hi_cfg_timing_div;
  111. u16 hi_cfg_bridge_delay;
  112. u16 hi_cfg_wakeup_key;
  113. u16 hi_cfg_ctrl;
  114. u16 intermediate_freq;
  115. u16 osc_clock_freq;
  116. enum CSCDState cscd_state;
  117. enum CDrxdState drxd_state;
  118. u16 sys_clock_freq;
  119. s16 osc_clock_deviation;
  120. u16 expected_sys_clock_freq;
  121. u16 insert_rs_byte;
  122. u16 enable_parallel;
  123. int operation_mode;
  124. struct SCfgAgc if_agc_cfg;
  125. struct SCfgAgc rf_agc_cfg;
  126. struct SNoiseCal noise_cal;
  127. u32 fe_fs_add_incr;
  128. u32 org_fe_fs_add_incr;
  129. u16 current_fe_if_incr;
  130. u16 m_FeAgRegAgPwd;
  131. u16 m_FeAgRegAgAgcSio;
  132. u16 m_EcOcRegOcModeLop;
  133. u16 m_EcOcRegSncSncLvl;
  134. u8 *m_InitAtomicRead;
  135. u8 *m_HiI2cPatch;
  136. u8 *m_ResetCEFR;
  137. u8 *m_InitFE_1;
  138. u8 *m_InitFE_2;
  139. u8 *m_InitCP;
  140. u8 *m_InitCE;
  141. u8 *m_InitEQ;
  142. u8 *m_InitSC;
  143. u8 *m_InitEC;
  144. u8 *m_ResetECRAM;
  145. u8 *m_InitDiversityFront;
  146. u8 *m_InitDiversityEnd;
  147. u8 *m_DisableDiversity;
  148. u8 *m_StartDiversityFront;
  149. u8 *m_StartDiversityEnd;
  150. u8 *m_DiversityDelay8MHZ;
  151. u8 *m_DiversityDelay6MHZ;
  152. u8 *microcode;
  153. u32 microcode_length;
  154. int type_A;
  155. int PGA;
  156. int diversity;
  157. int tuner_mirrors;
  158. enum app_env app_env_default;
  159. enum app_env app_env_diversity;
  160. };
  161. /****************************************************************************/
  162. /* I2C **********************************************************************/
  163. /****************************************************************************/
  164. static int i2c_write(struct i2c_adapter *adap, u8 adr, u8 * data, int len)
  165. {
  166. struct i2c_msg msg = {.addr = adr, .flags = 0, .buf = data, .len = len };
  167. if (i2c_transfer(adap, &msg, 1) != 1)
  168. return -1;
  169. return 0;
  170. }
  171. static int i2c_read(struct i2c_adapter *adap,
  172. u8 adr, u8 *msg, int len, u8 *answ, int alen)
  173. {
  174. struct i2c_msg msgs[2] = {
  175. {
  176. .addr = adr, .flags = 0,
  177. .buf = msg, .len = len
  178. }, {
  179. .addr = adr, .flags = I2C_M_RD,
  180. .buf = answ, .len = alen
  181. }
  182. };
  183. if (i2c_transfer(adap, msgs, 2) != 2)
  184. return -1;
  185. return 0;
  186. }
  187. static inline u32 MulDiv32(u32 a, u32 b, u32 c)
  188. {
  189. u64 tmp64;
  190. tmp64 = (u64)a * (u64)b;
  191. do_div(tmp64, c);
  192. return (u32) tmp64;
  193. }
  194. static int Read16(struct drxd_state *state, u32 reg, u16 *data, u8 flags)
  195. {
  196. u8 adr = state->config.demod_address;
  197. u8 mm1[4] = { reg & 0xff, (reg >> 16) & 0xff,
  198. flags | ((reg >> 24) & 0xff), (reg >> 8) & 0xff
  199. };
  200. u8 mm2[2];
  201. if (i2c_read(state->i2c, adr, mm1, 4, mm2, 2) < 0)
  202. return -1;
  203. if (data)
  204. *data = mm2[0] | (mm2[1] << 8);
  205. return mm2[0] | (mm2[1] << 8);
  206. }
  207. static int Read32(struct drxd_state *state, u32 reg, u32 *data, u8 flags)
  208. {
  209. u8 adr = state->config.demod_address;
  210. u8 mm1[4] = { reg & 0xff, (reg >> 16) & 0xff,
  211. flags | ((reg >> 24) & 0xff), (reg >> 8) & 0xff
  212. };
  213. u8 mm2[4];
  214. if (i2c_read(state->i2c, adr, mm1, 4, mm2, 4) < 0)
  215. return -1;
  216. if (data)
  217. *data =
  218. mm2[0] | (mm2[1] << 8) | (mm2[2] << 16) | (mm2[3] << 24);
  219. return 0;
  220. }
  221. static int Write16(struct drxd_state *state, u32 reg, u16 data, u8 flags)
  222. {
  223. u8 adr = state->config.demod_address;
  224. u8 mm[6] = { reg & 0xff, (reg >> 16) & 0xff,
  225. flags | ((reg >> 24) & 0xff), (reg >> 8) & 0xff,
  226. data & 0xff, (data >> 8) & 0xff
  227. };
  228. if (i2c_write(state->i2c, adr, mm, 6) < 0)
  229. return -1;
  230. return 0;
  231. }
  232. static int Write32(struct drxd_state *state, u32 reg, u32 data, u8 flags)
  233. {
  234. u8 adr = state->config.demod_address;
  235. u8 mm[8] = { reg & 0xff, (reg >> 16) & 0xff,
  236. flags | ((reg >> 24) & 0xff), (reg >> 8) & 0xff,
  237. data & 0xff, (data >> 8) & 0xff,
  238. (data >> 16) & 0xff, (data >> 24) & 0xff
  239. };
  240. if (i2c_write(state->i2c, adr, mm, 8) < 0)
  241. return -1;
  242. return 0;
  243. }
  244. static int write_chunk(struct drxd_state *state,
  245. u32 reg, u8 *data, u32 len, u8 flags)
  246. {
  247. u8 adr = state->config.demod_address;
  248. u8 mm[CHUNK_SIZE + 4] = { reg & 0xff, (reg >> 16) & 0xff,
  249. flags | ((reg >> 24) & 0xff), (reg >> 8) & 0xff
  250. };
  251. int i;
  252. for (i = 0; i < len; i++)
  253. mm[4 + i] = data[i];
  254. if (i2c_write(state->i2c, adr, mm, 4 + len) < 0) {
  255. printk(KERN_ERR "error in write_chunk\n");
  256. return -1;
  257. }
  258. return 0;
  259. }
  260. static int WriteBlock(struct drxd_state *state,
  261. u32 Address, u16 BlockSize, u8 *pBlock, u8 Flags)
  262. {
  263. while (BlockSize > 0) {
  264. u16 Chunk = BlockSize > CHUNK_SIZE ? CHUNK_SIZE : BlockSize;
  265. if (write_chunk(state, Address, pBlock, Chunk, Flags) < 0)
  266. return -1;
  267. pBlock += Chunk;
  268. Address += (Chunk >> 1);
  269. BlockSize -= Chunk;
  270. }
  271. return 0;
  272. }
  273. static int WriteTable(struct drxd_state *state, u8 * pTable)
  274. {
  275. int status = 0;
  276. if (pTable == NULL)
  277. return 0;
  278. while (!status) {
  279. u16 Length;
  280. u32 Address = pTable[0] | (pTable[1] << 8) |
  281. (pTable[2] << 16) | (pTable[3] << 24);
  282. if (Address == 0xFFFFFFFF)
  283. break;
  284. pTable += sizeof(u32);
  285. Length = pTable[0] | (pTable[1] << 8);
  286. pTable += sizeof(u16);
  287. if (!Length)
  288. break;
  289. status = WriteBlock(state, Address, Length * 2, pTable, 0);
  290. pTable += (Length * 2);
  291. }
  292. return status;
  293. }
  294. /****************************************************************************/
  295. /****************************************************************************/
  296. /****************************************************************************/
  297. static int ResetCEFR(struct drxd_state *state)
  298. {
  299. return WriteTable(state, state->m_ResetCEFR);
  300. }
  301. static int InitCP(struct drxd_state *state)
  302. {
  303. return WriteTable(state, state->m_InitCP);
  304. }
  305. static int InitCE(struct drxd_state *state)
  306. {
  307. int status;
  308. enum app_env AppEnv = state->app_env_default;
  309. do {
  310. status = WriteTable(state, state->m_InitCE);
  311. if (status < 0)
  312. break;
  313. if (state->operation_mode == OM_DVBT_Diversity_Front ||
  314. state->operation_mode == OM_DVBT_Diversity_End) {
  315. AppEnv = state->app_env_diversity;
  316. }
  317. if (AppEnv == APPENV_STATIC) {
  318. status = Write16(state, CE_REG_TAPSET__A, 0x0000, 0);
  319. if (status < 0)
  320. break;
  321. } else if (AppEnv == APPENV_PORTABLE) {
  322. status = Write16(state, CE_REG_TAPSET__A, 0x0001, 0);
  323. if (status < 0)
  324. break;
  325. } else if (AppEnv == APPENV_MOBILE && state->type_A) {
  326. status = Write16(state, CE_REG_TAPSET__A, 0x0002, 0);
  327. if (status < 0)
  328. break;
  329. } else if (AppEnv == APPENV_MOBILE && !state->type_A) {
  330. status = Write16(state, CE_REG_TAPSET__A, 0x0006, 0);
  331. if (status < 0)
  332. break;
  333. }
  334. /* start ce */
  335. status = Write16(state, B_CE_REG_COMM_EXEC__A, 0x0001, 0);
  336. if (status < 0)
  337. break;
  338. } while (0);
  339. return status;
  340. }
  341. static int StopOC(struct drxd_state *state)
  342. {
  343. int status = 0;
  344. u16 ocSyncLvl = 0;
  345. u16 ocModeLop = state->m_EcOcRegOcModeLop;
  346. u16 dtoIncLop = 0;
  347. u16 dtoIncHip = 0;
  348. do {
  349. /* Store output configuration */
  350. status = Read16(state, EC_OC_REG_SNC_ISC_LVL__A, &ocSyncLvl, 0);
  351. if (status < 0)
  352. break;
  353. /* CHK_ERROR(Read16(EC_OC_REG_OC_MODE_LOP__A, &ocModeLop)); */
  354. state->m_EcOcRegSncSncLvl = ocSyncLvl;
  355. /* m_EcOcRegOcModeLop = ocModeLop; */
  356. /* Flush FIFO (byte-boundary) at fixed rate */
  357. status = Read16(state, EC_OC_REG_RCN_MAP_LOP__A, &dtoIncLop, 0);
  358. if (status < 0)
  359. break;
  360. status = Read16(state, EC_OC_REG_RCN_MAP_HIP__A, &dtoIncHip, 0);
  361. if (status < 0)
  362. break;
  363. status = Write16(state, EC_OC_REG_DTO_INC_LOP__A, dtoIncLop, 0);
  364. if (status < 0)
  365. break;
  366. status = Write16(state, EC_OC_REG_DTO_INC_HIP__A, dtoIncHip, 0);
  367. if (status < 0)
  368. break;
  369. ocModeLop &= ~(EC_OC_REG_OC_MODE_LOP_DTO_CTR_SRC__M);
  370. ocModeLop |= EC_OC_REG_OC_MODE_LOP_DTO_CTR_SRC_STATIC;
  371. status = Write16(state, EC_OC_REG_OC_MODE_LOP__A, ocModeLop, 0);
  372. if (status < 0)
  373. break;
  374. status = Write16(state, EC_OC_REG_COMM_EXEC__A, EC_OC_REG_COMM_EXEC_CTL_HOLD, 0);
  375. if (status < 0)
  376. break;
  377. msleep(1);
  378. /* Output pins to '0' */
  379. status = Write16(state, EC_OC_REG_OCR_MPG_UOS__A, EC_OC_REG_OCR_MPG_UOS__M, 0);
  380. if (status < 0)
  381. break;
  382. /* Force the OC out of sync */
  383. ocSyncLvl &= ~(EC_OC_REG_SNC_ISC_LVL_OSC__M);
  384. status = Write16(state, EC_OC_REG_SNC_ISC_LVL__A, ocSyncLvl, 0);
  385. if (status < 0)
  386. break;
  387. ocModeLop &= ~(EC_OC_REG_OC_MODE_LOP_PAR_ENA__M);
  388. ocModeLop |= EC_OC_REG_OC_MODE_LOP_PAR_ENA_ENABLE;
  389. ocModeLop |= 0x2; /* Magically-out-of-sync */
  390. status = Write16(state, EC_OC_REG_OC_MODE_LOP__A, ocModeLop, 0);
  391. if (status < 0)
  392. break;
  393. status = Write16(state, EC_OC_REG_COMM_INT_STA__A, 0x0, 0);
  394. if (status < 0)
  395. break;
  396. status = Write16(state, EC_OC_REG_COMM_EXEC__A, EC_OC_REG_COMM_EXEC_CTL_ACTIVE, 0);
  397. if (status < 0)
  398. break;
  399. } while (0);
  400. return status;
  401. }
  402. static int StartOC(struct drxd_state *state)
  403. {
  404. int status = 0;
  405. do {
  406. /* Stop OC */
  407. status = Write16(state, EC_OC_REG_COMM_EXEC__A, EC_OC_REG_COMM_EXEC_CTL_HOLD, 0);
  408. if (status < 0)
  409. break;
  410. /* Restore output configuration */
  411. status = Write16(state, EC_OC_REG_SNC_ISC_LVL__A, state->m_EcOcRegSncSncLvl, 0);
  412. if (status < 0)
  413. break;
  414. status = Write16(state, EC_OC_REG_OC_MODE_LOP__A, state->m_EcOcRegOcModeLop, 0);
  415. if (status < 0)
  416. break;
  417. /* Output pins active again */
  418. status = Write16(state, EC_OC_REG_OCR_MPG_UOS__A, EC_OC_REG_OCR_MPG_UOS_INIT, 0);
  419. if (status < 0)
  420. break;
  421. /* Start OC */
  422. status = Write16(state, EC_OC_REG_COMM_EXEC__A, EC_OC_REG_COMM_EXEC_CTL_ACTIVE, 0);
  423. if (status < 0)
  424. break;
  425. } while (0);
  426. return status;
  427. }
  428. static int InitEQ(struct drxd_state *state)
  429. {
  430. return WriteTable(state, state->m_InitEQ);
  431. }
  432. static int InitEC(struct drxd_state *state)
  433. {
  434. return WriteTable(state, state->m_InitEC);
  435. }
  436. static int InitSC(struct drxd_state *state)
  437. {
  438. return WriteTable(state, state->m_InitSC);
  439. }
  440. static int InitAtomicRead(struct drxd_state *state)
  441. {
  442. return WriteTable(state, state->m_InitAtomicRead);
  443. }
  444. static int CorrectSysClockDeviation(struct drxd_state *state);
  445. static int DRX_GetLockStatus(struct drxd_state *state, u32 * pLockStatus)
  446. {
  447. u16 ScRaRamLock = 0;
  448. const u16 mpeg_lock_mask = (SC_RA_RAM_LOCK_MPEG__M |
  449. SC_RA_RAM_LOCK_FEC__M |
  450. SC_RA_RAM_LOCK_DEMOD__M);
  451. const u16 fec_lock_mask = (SC_RA_RAM_LOCK_FEC__M |
  452. SC_RA_RAM_LOCK_DEMOD__M);
  453. const u16 demod_lock_mask = SC_RA_RAM_LOCK_DEMOD__M;
  454. int status;
  455. *pLockStatus = 0;
  456. status = Read16(state, SC_RA_RAM_LOCK__A, &ScRaRamLock, 0x0000);
  457. if (status < 0) {
  458. printk(KERN_ERR "Can't read SC_RA_RAM_LOCK__A status = %08x\n", status);
  459. return status;
  460. }
  461. if (state->drxd_state != DRXD_STARTED)
  462. return 0;
  463. if ((ScRaRamLock & mpeg_lock_mask) == mpeg_lock_mask) {
  464. *pLockStatus |= DRX_LOCK_MPEG;
  465. CorrectSysClockDeviation(state);
  466. }
  467. if ((ScRaRamLock & fec_lock_mask) == fec_lock_mask)
  468. *pLockStatus |= DRX_LOCK_FEC;
  469. if ((ScRaRamLock & demod_lock_mask) == demod_lock_mask)
  470. *pLockStatus |= DRX_LOCK_DEMOD;
  471. return 0;
  472. }
  473. /****************************************************************************/
  474. static int SetCfgIfAgc(struct drxd_state *state, struct SCfgAgc *cfg)
  475. {
  476. int status;
  477. if (cfg->outputLevel > DRXD_FE_CTRL_MAX)
  478. return -1;
  479. if (cfg->ctrlMode == AGC_CTRL_USER) {
  480. do {
  481. u16 FeAgRegPm1AgcWri;
  482. u16 FeAgRegAgModeLop;
  483. status = Read16(state, FE_AG_REG_AG_MODE_LOP__A, &FeAgRegAgModeLop, 0);
  484. if (status < 0)
  485. break;
  486. FeAgRegAgModeLop &= (~FE_AG_REG_AG_MODE_LOP_MODE_4__M);
  487. FeAgRegAgModeLop |= FE_AG_REG_AG_MODE_LOP_MODE_4_STATIC;
  488. status = Write16(state, FE_AG_REG_AG_MODE_LOP__A, FeAgRegAgModeLop, 0);
  489. if (status < 0)
  490. break;
  491. FeAgRegPm1AgcWri = (u16) (cfg->outputLevel &
  492. FE_AG_REG_PM1_AGC_WRI__M);
  493. status = Write16(state, FE_AG_REG_PM1_AGC_WRI__A, FeAgRegPm1AgcWri, 0);
  494. if (status < 0)
  495. break;
  496. } while (0);
  497. } else if (cfg->ctrlMode == AGC_CTRL_AUTO) {
  498. if (((cfg->maxOutputLevel) < (cfg->minOutputLevel)) ||
  499. ((cfg->maxOutputLevel) > DRXD_FE_CTRL_MAX) ||
  500. ((cfg->speed) > DRXD_FE_CTRL_MAX) ||
  501. ((cfg->settleLevel) > DRXD_FE_CTRL_MAX)
  502. )
  503. return -1;
  504. do {
  505. u16 FeAgRegAgModeLop;
  506. u16 FeAgRegEgcSetLvl;
  507. u16 slope, offset;
  508. /* == Mode == */
  509. status = Read16(state, FE_AG_REG_AG_MODE_LOP__A, &FeAgRegAgModeLop, 0);
  510. if (status < 0)
  511. break;
  512. FeAgRegAgModeLop &= (~FE_AG_REG_AG_MODE_LOP_MODE_4__M);
  513. FeAgRegAgModeLop |=
  514. FE_AG_REG_AG_MODE_LOP_MODE_4_DYNAMIC;
  515. status = Write16(state, FE_AG_REG_AG_MODE_LOP__A, FeAgRegAgModeLop, 0);
  516. if (status < 0)
  517. break;
  518. /* == Settle level == */
  519. FeAgRegEgcSetLvl = (u16) ((cfg->settleLevel >> 1) &
  520. FE_AG_REG_EGC_SET_LVL__M);
  521. status = Write16(state, FE_AG_REG_EGC_SET_LVL__A, FeAgRegEgcSetLvl, 0);
  522. if (status < 0)
  523. break;
  524. /* == Min/Max == */
  525. slope = (u16) ((cfg->maxOutputLevel -
  526. cfg->minOutputLevel) / 2);
  527. offset = (u16) ((cfg->maxOutputLevel +
  528. cfg->minOutputLevel) / 2 - 511);
  529. status = Write16(state, FE_AG_REG_GC1_AGC_RIC__A, slope, 0);
  530. if (status < 0)
  531. break;
  532. status = Write16(state, FE_AG_REG_GC1_AGC_OFF__A, offset, 0);
  533. if (status < 0)
  534. break;
  535. /* == Speed == */
  536. {
  537. const u16 maxRur = 8;
  538. const u16 slowIncrDecLUT[] = { 3, 4, 4, 5, 6 };
  539. const u16 fastIncrDecLUT[] = { 14, 15, 15, 16,
  540. 17, 18, 18, 19,
  541. 20, 21, 22, 23,
  542. 24, 26, 27, 28,
  543. 29, 31
  544. };
  545. u16 fineSteps = (DRXD_FE_CTRL_MAX + 1) /
  546. (maxRur + 1);
  547. u16 fineSpeed = (u16) (cfg->speed -
  548. ((cfg->speed /
  549. fineSteps) *
  550. fineSteps));
  551. u16 invRurCount = (u16) (cfg->speed /
  552. fineSteps);
  553. u16 rurCount;
  554. if (invRurCount > maxRur) {
  555. rurCount = 0;
  556. fineSpeed += fineSteps;
  557. } else {
  558. rurCount = maxRur - invRurCount;
  559. }
  560. /*
  561. fastInc = default *
  562. (2^(fineSpeed/fineSteps))
  563. => range[default...2*default>
  564. slowInc = default *
  565. (2^(fineSpeed/fineSteps))
  566. */
  567. {
  568. u16 fastIncrDec =
  569. fastIncrDecLUT[fineSpeed /
  570. ((fineSteps /
  571. (14 + 1)) + 1)];
  572. u16 slowIncrDec =
  573. slowIncrDecLUT[fineSpeed /
  574. (fineSteps /
  575. (3 + 1))];
  576. status = Write16(state, FE_AG_REG_EGC_RUR_CNT__A, rurCount, 0);
  577. if (status < 0)
  578. break;
  579. status = Write16(state, FE_AG_REG_EGC_FAS_INC__A, fastIncrDec, 0);
  580. if (status < 0)
  581. break;
  582. status = Write16(state, FE_AG_REG_EGC_FAS_DEC__A, fastIncrDec, 0);
  583. if (status < 0)
  584. break;
  585. status = Write16(state, FE_AG_REG_EGC_SLO_INC__A, slowIncrDec, 0);
  586. if (status < 0)
  587. break;
  588. status = Write16(state, FE_AG_REG_EGC_SLO_DEC__A, slowIncrDec, 0);
  589. if (status < 0)
  590. break;
  591. }
  592. }
  593. } while (0);
  594. } else {
  595. /* No OFF mode for IF control */
  596. return -1;
  597. }
  598. return status;
  599. }
  600. static int SetCfgRfAgc(struct drxd_state *state, struct SCfgAgc *cfg)
  601. {
  602. int status = 0;
  603. if (cfg->outputLevel > DRXD_FE_CTRL_MAX)
  604. return -1;
  605. if (cfg->ctrlMode == AGC_CTRL_USER) {
  606. do {
  607. u16 AgModeLop = 0;
  608. u16 level = (cfg->outputLevel);
  609. if (level == DRXD_FE_CTRL_MAX)
  610. level++;
  611. status = Write16(state, FE_AG_REG_PM2_AGC_WRI__A, level, 0x0000);
  612. if (status < 0)
  613. break;
  614. /*==== Mode ====*/
  615. /* Powerdown PD2, WRI source */
  616. state->m_FeAgRegAgPwd &= ~(FE_AG_REG_AG_PWD_PWD_PD2__M);
  617. state->m_FeAgRegAgPwd |=
  618. FE_AG_REG_AG_PWD_PWD_PD2_DISABLE;
  619. status = Write16(state, FE_AG_REG_AG_PWD__A, state->m_FeAgRegAgPwd, 0x0000);
  620. if (status < 0)
  621. break;
  622. status = Read16(state, FE_AG_REG_AG_MODE_LOP__A, &AgModeLop, 0x0000);
  623. if (status < 0)
  624. break;
  625. AgModeLop &= (~(FE_AG_REG_AG_MODE_LOP_MODE_5__M |
  626. FE_AG_REG_AG_MODE_LOP_MODE_E__M));
  627. AgModeLop |= (FE_AG_REG_AG_MODE_LOP_MODE_5_STATIC |
  628. FE_AG_REG_AG_MODE_LOP_MODE_E_STATIC);
  629. status = Write16(state, FE_AG_REG_AG_MODE_LOP__A, AgModeLop, 0x0000);
  630. if (status < 0)
  631. break;
  632. /* enable AGC2 pin */
  633. {
  634. u16 FeAgRegAgAgcSio = 0;
  635. status = Read16(state, FE_AG_REG_AG_AGC_SIO__A, &FeAgRegAgAgcSio, 0x0000);
  636. if (status < 0)
  637. break;
  638. FeAgRegAgAgcSio &=
  639. ~(FE_AG_REG_AG_AGC_SIO_AGC_SIO_2__M);
  640. FeAgRegAgAgcSio |=
  641. FE_AG_REG_AG_AGC_SIO_AGC_SIO_2_OUTPUT;
  642. status = Write16(state, FE_AG_REG_AG_AGC_SIO__A, FeAgRegAgAgcSio, 0x0000);
  643. if (status < 0)
  644. break;
  645. }
  646. } while (0);
  647. } else if (cfg->ctrlMode == AGC_CTRL_AUTO) {
  648. u16 AgModeLop = 0;
  649. do {
  650. u16 level;
  651. /* Automatic control */
  652. /* Powerup PD2, AGC2 as output, TGC source */
  653. (state->m_FeAgRegAgPwd) &=
  654. ~(FE_AG_REG_AG_PWD_PWD_PD2__M);
  655. (state->m_FeAgRegAgPwd) |=
  656. FE_AG_REG_AG_PWD_PWD_PD2_DISABLE;
  657. status = Write16(state, FE_AG_REG_AG_PWD__A, (state->m_FeAgRegAgPwd), 0x0000);
  658. if (status < 0)
  659. break;
  660. status = Read16(state, FE_AG_REG_AG_MODE_LOP__A, &AgModeLop, 0x0000);
  661. if (status < 0)
  662. break;
  663. AgModeLop &= (~(FE_AG_REG_AG_MODE_LOP_MODE_5__M |
  664. FE_AG_REG_AG_MODE_LOP_MODE_E__M));
  665. AgModeLop |= (FE_AG_REG_AG_MODE_LOP_MODE_5_STATIC |
  666. FE_AG_REG_AG_MODE_LOP_MODE_E_DYNAMIC);
  667. status = Write16(state, FE_AG_REG_AG_MODE_LOP__A, AgModeLop, 0x0000);
  668. if (status < 0)
  669. break;
  670. /* Settle level */
  671. level = (((cfg->settleLevel) >> 4) &
  672. FE_AG_REG_TGC_SET_LVL__M);
  673. status = Write16(state, FE_AG_REG_TGC_SET_LVL__A, level, 0x0000);
  674. if (status < 0)
  675. break;
  676. /* Min/max: don't care */
  677. /* Speed: TODO */
  678. /* enable AGC2 pin */
  679. {
  680. u16 FeAgRegAgAgcSio = 0;
  681. status = Read16(state, FE_AG_REG_AG_AGC_SIO__A, &FeAgRegAgAgcSio, 0x0000);
  682. if (status < 0)
  683. break;
  684. FeAgRegAgAgcSio &=
  685. ~(FE_AG_REG_AG_AGC_SIO_AGC_SIO_2__M);
  686. FeAgRegAgAgcSio |=
  687. FE_AG_REG_AG_AGC_SIO_AGC_SIO_2_OUTPUT;
  688. status = Write16(state, FE_AG_REG_AG_AGC_SIO__A, FeAgRegAgAgcSio, 0x0000);
  689. if (status < 0)
  690. break;
  691. }
  692. } while (0);
  693. } else {
  694. u16 AgModeLop = 0;
  695. do {
  696. /* No RF AGC control */
  697. /* Powerdown PD2, AGC2 as output, WRI source */
  698. (state->m_FeAgRegAgPwd) &=
  699. ~(FE_AG_REG_AG_PWD_PWD_PD2__M);
  700. (state->m_FeAgRegAgPwd) |=
  701. FE_AG_REG_AG_PWD_PWD_PD2_ENABLE;
  702. status = Write16(state, FE_AG_REG_AG_PWD__A, (state->m_FeAgRegAgPwd), 0x0000);
  703. if (status < 0)
  704. break;
  705. status = Read16(state, FE_AG_REG_AG_MODE_LOP__A, &AgModeLop, 0x0000);
  706. if (status < 0)
  707. break;
  708. AgModeLop &= (~(FE_AG_REG_AG_MODE_LOP_MODE_5__M |
  709. FE_AG_REG_AG_MODE_LOP_MODE_E__M));
  710. AgModeLop |= (FE_AG_REG_AG_MODE_LOP_MODE_5_STATIC |
  711. FE_AG_REG_AG_MODE_LOP_MODE_E_STATIC);
  712. status = Write16(state, FE_AG_REG_AG_MODE_LOP__A, AgModeLop, 0x0000);
  713. if (status < 0)
  714. break;
  715. /* set FeAgRegAgAgcSio AGC2 (RF) as input */
  716. {
  717. u16 FeAgRegAgAgcSio = 0;
  718. status = Read16(state, FE_AG_REG_AG_AGC_SIO__A, &FeAgRegAgAgcSio, 0x0000);
  719. if (status < 0)
  720. break;
  721. FeAgRegAgAgcSio &=
  722. ~(FE_AG_REG_AG_AGC_SIO_AGC_SIO_2__M);
  723. FeAgRegAgAgcSio |=
  724. FE_AG_REG_AG_AGC_SIO_AGC_SIO_2_INPUT;
  725. status = Write16(state, FE_AG_REG_AG_AGC_SIO__A, FeAgRegAgAgcSio, 0x0000);
  726. if (status < 0)
  727. break;
  728. }
  729. } while (0);
  730. }
  731. return status;
  732. }
  733. static int ReadIFAgc(struct drxd_state *state, u32 * pValue)
  734. {
  735. int status = 0;
  736. *pValue = 0;
  737. if (state->if_agc_cfg.ctrlMode != AGC_CTRL_OFF) {
  738. u16 Value;
  739. status = Read16(state, FE_AG_REG_GC1_AGC_DAT__A, &Value, 0);
  740. Value &= FE_AG_REG_GC1_AGC_DAT__M;
  741. if (status >= 0) {
  742. /* 3.3V
  743. |
  744. R1
  745. |
  746. Vin - R3 - * -- Vout
  747. |
  748. R2
  749. |
  750. GND
  751. */
  752. u32 R1 = state->if_agc_cfg.R1;
  753. u32 R2 = state->if_agc_cfg.R2;
  754. u32 R3 = state->if_agc_cfg.R3;
  755. u32 Vmax, Rpar, Vmin, Vout;
  756. if (R2 == 0 && (R1 == 0 || R3 == 0))
  757. return 0;
  758. Vmax = (3300 * R2) / (R1 + R2);
  759. Rpar = (R2 * R3) / (R3 + R2);
  760. Vmin = (3300 * Rpar) / (R1 + Rpar);
  761. Vout = Vmin + ((Vmax - Vmin) * Value) / 1024;
  762. *pValue = Vout;
  763. }
  764. }
  765. return status;
  766. }
  767. static int load_firmware(struct drxd_state *state, const char *fw_name)
  768. {
  769. const struct firmware *fw;
  770. if (request_firmware(&fw, fw_name, state->dev) < 0) {
  771. printk(KERN_ERR "drxd: firmware load failure [%s]\n", fw_name);
  772. return -EIO;
  773. }
  774. state->microcode = kmemdup(fw->data, fw->size, GFP_KERNEL);
  775. if (state->microcode == NULL) {
  776. release_firmware(fw);
  777. printk(KERN_ERR "drxd: firmware load failure: no memory\n");
  778. return -ENOMEM;
  779. }
  780. state->microcode_length = fw->size;
  781. release_firmware(fw);
  782. return 0;
  783. }
  784. static int DownloadMicrocode(struct drxd_state *state,
  785. const u8 *pMCImage, u32 Length)
  786. {
  787. u8 *pSrc;
  788. u32 Address;
  789. u16 nBlocks;
  790. u16 BlockSize;
  791. u32 offset = 0;
  792. int i, status = 0;
  793. pSrc = (u8 *) pMCImage;
  794. /* We're not using Flags */
  795. /* Flags = (pSrc[0] << 8) | pSrc[1]; */
  796. pSrc += sizeof(u16);
  797. offset += sizeof(u16);
  798. nBlocks = (pSrc[0] << 8) | pSrc[1];
  799. pSrc += sizeof(u16);
  800. offset += sizeof(u16);
  801. for (i = 0; i < nBlocks; i++) {
  802. Address = (pSrc[0] << 24) | (pSrc[1] << 16) |
  803. (pSrc[2] << 8) | pSrc[3];
  804. pSrc += sizeof(u32);
  805. offset += sizeof(u32);
  806. BlockSize = ((pSrc[0] << 8) | pSrc[1]) * sizeof(u16);
  807. pSrc += sizeof(u16);
  808. offset += sizeof(u16);
  809. /* We're not using Flags */
  810. /* u16 Flags = (pSrc[0] << 8) | pSrc[1]; */
  811. pSrc += sizeof(u16);
  812. offset += sizeof(u16);
  813. /* We're not using BlockCRC */
  814. /* u16 BlockCRC = (pSrc[0] << 8) | pSrc[1]; */
  815. pSrc += sizeof(u16);
  816. offset += sizeof(u16);
  817. status = WriteBlock(state, Address, BlockSize,
  818. pSrc, DRX_I2C_CLEARCRC);
  819. if (status < 0)
  820. break;
  821. pSrc += BlockSize;
  822. offset += BlockSize;
  823. }
  824. return status;
  825. }
  826. static int HI_Command(struct drxd_state *state, u16 cmd, u16 * pResult)
  827. {
  828. u32 nrRetries = 0;
  829. u16 waitCmd;
  830. int status;
  831. status = Write16(state, HI_RA_RAM_SRV_CMD__A, cmd, 0);
  832. if (status < 0)
  833. return status;
  834. do {
  835. nrRetries += 1;
  836. if (nrRetries > DRXD_MAX_RETRIES) {
  837. status = -1;
  838. break;
  839. }
  840. status = Read16(state, HI_RA_RAM_SRV_CMD__A, &waitCmd, 0);
  841. } while (waitCmd != 0);
  842. if (status >= 0)
  843. status = Read16(state, HI_RA_RAM_SRV_RES__A, pResult, 0);
  844. return status;
  845. }
  846. static int HI_CfgCommand(struct drxd_state *state)
  847. {
  848. int status = 0;
  849. mutex_lock(&state->mutex);
  850. Write16(state, HI_RA_RAM_SRV_CFG_KEY__A, HI_RA_RAM_SRV_RST_KEY_ACT, 0);
  851. Write16(state, HI_RA_RAM_SRV_CFG_DIV__A, state->hi_cfg_timing_div, 0);
  852. Write16(state, HI_RA_RAM_SRV_CFG_BDL__A, state->hi_cfg_bridge_delay, 0);
  853. Write16(state, HI_RA_RAM_SRV_CFG_WUP__A, state->hi_cfg_wakeup_key, 0);
  854. Write16(state, HI_RA_RAM_SRV_CFG_ACT__A, state->hi_cfg_ctrl, 0);
  855. Write16(state, HI_RA_RAM_SRV_CFG_KEY__A, HI_RA_RAM_SRV_RST_KEY_ACT, 0);
  856. if ((state->hi_cfg_ctrl & HI_RA_RAM_SRV_CFG_ACT_PWD_EXE) ==
  857. HI_RA_RAM_SRV_CFG_ACT_PWD_EXE)
  858. status = Write16(state, HI_RA_RAM_SRV_CMD__A,
  859. HI_RA_RAM_SRV_CMD_CONFIG, 0);
  860. else
  861. status = HI_Command(state, HI_RA_RAM_SRV_CMD_CONFIG, NULL);
  862. mutex_unlock(&state->mutex);
  863. return status;
  864. }
  865. static int InitHI(struct drxd_state *state)
  866. {
  867. state->hi_cfg_wakeup_key = (state->chip_adr);
  868. /* port/bridge/power down ctrl */
  869. state->hi_cfg_ctrl = HI_RA_RAM_SRV_CFG_ACT_SLV0_ON;
  870. return HI_CfgCommand(state);
  871. }
  872. static int HI_ResetCommand(struct drxd_state *state)
  873. {
  874. int status;
  875. mutex_lock(&state->mutex);
  876. status = Write16(state, HI_RA_RAM_SRV_RST_KEY__A,
  877. HI_RA_RAM_SRV_RST_KEY_ACT, 0);
  878. if (status == 0)
  879. status = HI_Command(state, HI_RA_RAM_SRV_CMD_RESET, NULL);
  880. mutex_unlock(&state->mutex);
  881. msleep(1);
  882. return status;
  883. }
  884. static int DRX_ConfigureI2CBridge(struct drxd_state *state, int bEnableBridge)
  885. {
  886. state->hi_cfg_ctrl &= (~HI_RA_RAM_SRV_CFG_ACT_BRD__M);
  887. if (bEnableBridge)
  888. state->hi_cfg_ctrl |= HI_RA_RAM_SRV_CFG_ACT_BRD_ON;
  889. else
  890. state->hi_cfg_ctrl |= HI_RA_RAM_SRV_CFG_ACT_BRD_OFF;
  891. return HI_CfgCommand(state);
  892. }
  893. #define HI_TR_WRITE 0x9
  894. #define HI_TR_READ 0xA
  895. #define HI_TR_READ_WRITE 0xB
  896. #define HI_TR_BROADCAST 0x4
  897. #if 0
  898. static int AtomicReadBlock(struct drxd_state *state,
  899. u32 Addr, u16 DataSize, u8 *pData, u8 Flags)
  900. {
  901. int status;
  902. int i = 0;
  903. /* Parameter check */
  904. if ((!pData) || ((DataSize & 1) != 0))
  905. return -1;
  906. mutex_lock(&state->mutex);
  907. do {
  908. /* Instruct HI to read n bytes */
  909. /* TODO use proper names forthese egisters */
  910. status = Write16(state, HI_RA_RAM_SRV_CFG_KEY__A, (HI_TR_FUNC_ADDR & 0xFFFF), 0);
  911. if (status < 0)
  912. break;
  913. status = Write16(state, HI_RA_RAM_SRV_CFG_DIV__A, (u16) (Addr >> 16), 0);
  914. if (status < 0)
  915. break;
  916. status = Write16(state, HI_RA_RAM_SRV_CFG_BDL__A, (u16) (Addr & 0xFFFF), 0);
  917. if (status < 0)
  918. break;
  919. status = Write16(state, HI_RA_RAM_SRV_CFG_WUP__A, (u16) ((DataSize / 2) - 1), 0);
  920. if (status < 0)
  921. break;
  922. status = Write16(state, HI_RA_RAM_SRV_CFG_ACT__A, HI_TR_READ, 0);
  923. if (status < 0)
  924. break;
  925. status = HI_Command(state, HI_RA_RAM_SRV_CMD_EXECUTE, 0);
  926. if (status < 0)
  927. break;
  928. } while (0);
  929. if (status >= 0) {
  930. for (i = 0; i < (DataSize / 2); i += 1) {
  931. u16 word;
  932. status = Read16(state, (HI_RA_RAM_USR_BEGIN__A + i),
  933. &word, 0);
  934. if (status < 0)
  935. break;
  936. pData[2 * i] = (u8) (word & 0xFF);
  937. pData[(2 * i) + 1] = (u8) (word >> 8);
  938. }
  939. }
  940. mutex_unlock(&state->mutex);
  941. return status;
  942. }
  943. static int AtomicReadReg32(struct drxd_state *state,
  944. u32 Addr, u32 *pData, u8 Flags)
  945. {
  946. u8 buf[sizeof(u32)];
  947. int status;
  948. if (!pData)
  949. return -1;
  950. status = AtomicReadBlock(state, Addr, sizeof(u32), buf, Flags);
  951. *pData = (((u32) buf[0]) << 0) +
  952. (((u32) buf[1]) << 8) +
  953. (((u32) buf[2]) << 16) + (((u32) buf[3]) << 24);
  954. return status;
  955. }
  956. #endif
  957. static int StopAllProcessors(struct drxd_state *state)
  958. {
  959. return Write16(state, HI_COMM_EXEC__A,
  960. SC_COMM_EXEC_CTL_STOP, DRX_I2C_BROADCAST);
  961. }
  962. static int EnableAndResetMB(struct drxd_state *state)
  963. {
  964. if (state->type_A) {
  965. /* disable? monitor bus observe @ EC_OC */
  966. Write16(state, EC_OC_REG_OC_MON_SIO__A, 0x0000, 0x0000);
  967. }
  968. /* do inverse broadcast, followed by explicit write to HI */
  969. Write16(state, HI_COMM_MB__A, 0x0000, DRX_I2C_BROADCAST);
  970. Write16(state, HI_COMM_MB__A, 0x0000, 0x0000);
  971. return 0;
  972. }
  973. static int InitCC(struct drxd_state *state)
  974. {
  975. if (state->osc_clock_freq == 0 ||
  976. state->osc_clock_freq > 20000 ||
  977. (state->osc_clock_freq % 4000) != 0) {
  978. printk(KERN_ERR "invalid osc frequency %d\n", state->osc_clock_freq);
  979. return -1;
  980. }
  981. Write16(state, CC_REG_OSC_MODE__A, CC_REG_OSC_MODE_M20, 0);
  982. Write16(state, CC_REG_PLL_MODE__A, CC_REG_PLL_MODE_BYPASS_PLL |
  983. CC_REG_PLL_MODE_PUMP_CUR_12, 0);
  984. Write16(state, CC_REG_REF_DIVIDE__A, state->osc_clock_freq / 4000, 0);
  985. Write16(state, CC_REG_PWD_MODE__A, CC_REG_PWD_MODE_DOWN_PLL, 0);
  986. Write16(state, CC_REG_UPDATE__A, CC_REG_UPDATE_KEY, 0);
  987. return 0;
  988. }
  989. static int ResetECOD(struct drxd_state *state)
  990. {
  991. int status = 0;
  992. if (state->type_A)
  993. status = Write16(state, EC_OD_REG_SYNC__A, 0x0664, 0);
  994. else
  995. status = Write16(state, B_EC_OD_REG_SYNC__A, 0x0664, 0);
  996. if (!(status < 0))
  997. status = WriteTable(state, state->m_ResetECRAM);
  998. if (!(status < 0))
  999. status = Write16(state, EC_OD_REG_COMM_EXEC__A, 0x0001, 0);
  1000. return status;
  1001. }
  1002. /* Configure PGA switch */
  1003. static int SetCfgPga(struct drxd_state *state, int pgaSwitch)
  1004. {
  1005. int status;
  1006. u16 AgModeLop = 0;
  1007. u16 AgModeHip = 0;
  1008. do {
  1009. if (pgaSwitch) {
  1010. /* PGA on */
  1011. /* fine gain */
  1012. status = Read16(state, B_FE_AG_REG_AG_MODE_LOP__A, &AgModeLop, 0x0000);
  1013. if (status < 0)
  1014. break;
  1015. AgModeLop &= (~(B_FE_AG_REG_AG_MODE_LOP_MODE_C__M));
  1016. AgModeLop |= B_FE_AG_REG_AG_MODE_LOP_MODE_C_DYNAMIC;
  1017. status = Write16(state, B_FE_AG_REG_AG_MODE_LOP__A, AgModeLop, 0x0000);
  1018. if (status < 0)
  1019. break;
  1020. /* coarse gain */
  1021. status = Read16(state, B_FE_AG_REG_AG_MODE_HIP__A, &AgModeHip, 0x0000);
  1022. if (status < 0)
  1023. break;
  1024. AgModeHip &= (~(B_FE_AG_REG_AG_MODE_HIP_MODE_J__M));
  1025. AgModeHip |= B_FE_AG_REG_AG_MODE_HIP_MODE_J_DYNAMIC;
  1026. status = Write16(state, B_FE_AG_REG_AG_MODE_HIP__A, AgModeHip, 0x0000);
  1027. if (status < 0)
  1028. break;
  1029. /* enable fine and coarse gain, enable AAF,
  1030. no ext resistor */
  1031. status = Write16(state, B_FE_AG_REG_AG_PGA_MODE__A, B_FE_AG_REG_AG_PGA_MODE_PFY_PCY_AFY_REN, 0x0000);
  1032. if (status < 0)
  1033. break;
  1034. } else {
  1035. /* PGA off, bypass */
  1036. /* fine gain */
  1037. status = Read16(state, B_FE_AG_REG_AG_MODE_LOP__A, &AgModeLop, 0x0000);
  1038. if (status < 0)
  1039. break;
  1040. AgModeLop &= (~(B_FE_AG_REG_AG_MODE_LOP_MODE_C__M));
  1041. AgModeLop |= B_FE_AG_REG_AG_MODE_LOP_MODE_C_STATIC;
  1042. status = Write16(state, B_FE_AG_REG_AG_MODE_LOP__A, AgModeLop, 0x0000);
  1043. if (status < 0)
  1044. break;
  1045. /* coarse gain */
  1046. status = Read16(state, B_FE_AG_REG_AG_MODE_HIP__A, &AgModeHip, 0x0000);
  1047. if (status < 0)
  1048. break;
  1049. AgModeHip &= (~(B_FE_AG_REG_AG_MODE_HIP_MODE_J__M));
  1050. AgModeHip |= B_FE_AG_REG_AG_MODE_HIP_MODE_J_STATIC;
  1051. status = Write16(state, B_FE_AG_REG_AG_MODE_HIP__A, AgModeHip, 0x0000);
  1052. if (status < 0)
  1053. break;
  1054. /* disable fine and coarse gain, enable AAF,
  1055. no ext resistor */
  1056. status = Write16(state, B_FE_AG_REG_AG_PGA_MODE__A, B_FE_AG_REG_AG_PGA_MODE_PFN_PCN_AFY_REN, 0x0000);
  1057. if (status < 0)
  1058. break;
  1059. }
  1060. } while (0);
  1061. return status;
  1062. }
  1063. static int InitFE(struct drxd_state *state)
  1064. {
  1065. int status;
  1066. do {
  1067. status = WriteTable(state, state->m_InitFE_1);
  1068. if (status < 0)
  1069. break;
  1070. if (state->type_A) {
  1071. status = Write16(state, FE_AG_REG_AG_PGA_MODE__A,
  1072. FE_AG_REG_AG_PGA_MODE_PFN_PCN_AFY_REN,
  1073. 0);
  1074. } else {
  1075. if (state->PGA)
  1076. status = SetCfgPga(state, 0);
  1077. else
  1078. status =
  1079. Write16(state, B_FE_AG_REG_AG_PGA_MODE__A,
  1080. B_FE_AG_REG_AG_PGA_MODE_PFN_PCN_AFY_REN,
  1081. 0);
  1082. }
  1083. if (status < 0)
  1084. break;
  1085. status = Write16(state, FE_AG_REG_AG_AGC_SIO__A, state->m_FeAgRegAgAgcSio, 0x0000);
  1086. if (status < 0)
  1087. break;
  1088. status = Write16(state, FE_AG_REG_AG_PWD__A, state->m_FeAgRegAgPwd, 0x0000);
  1089. if (status < 0)
  1090. break;
  1091. status = WriteTable(state, state->m_InitFE_2);
  1092. if (status < 0)
  1093. break;
  1094. } while (0);
  1095. return status;
  1096. }
  1097. static int InitFT(struct drxd_state *state)
  1098. {
  1099. /*
  1100. norm OFFSET, MB says =2 voor 8K en =3 voor 2K waarschijnlijk
  1101. SC stuff
  1102. */
  1103. return Write16(state, FT_REG_COMM_EXEC__A, 0x0001, 0x0000);
  1104. }
  1105. static int SC_WaitForReady(struct drxd_state *state)
  1106. {
  1107. u16 curCmd;
  1108. int i;
  1109. for (i = 0; i < DRXD_MAX_RETRIES; i += 1) {
  1110. int status = Read16(state, SC_RA_RAM_CMD__A, &curCmd, 0);
  1111. if (status == 0 || curCmd == 0)
  1112. return status;
  1113. }
  1114. return -1;
  1115. }
  1116. static int SC_SendCommand(struct drxd_state *state, u16 cmd)
  1117. {
  1118. int status = 0;
  1119. u16 errCode;
  1120. Write16(state, SC_RA_RAM_CMD__A, cmd, 0);
  1121. SC_WaitForReady(state);
  1122. Read16(state, SC_RA_RAM_CMD_ADDR__A, &errCode, 0);
  1123. if (errCode == 0xFFFF) {
  1124. printk(KERN_ERR "Command Error\n");
  1125. status = -1;
  1126. }
  1127. return status;
  1128. }
  1129. static int SC_ProcStartCommand(struct drxd_state *state,
  1130. u16 subCmd, u16 param0, u16 param1)
  1131. {
  1132. int status = 0;
  1133. u16 scExec;
  1134. mutex_lock(&state->mutex);
  1135. do {
  1136. Read16(state, SC_COMM_EXEC__A, &scExec, 0);
  1137. if (scExec != 1) {
  1138. status = -1;
  1139. break;
  1140. }
  1141. SC_WaitForReady(state);
  1142. Write16(state, SC_RA_RAM_CMD_ADDR__A, subCmd, 0);
  1143. Write16(state, SC_RA_RAM_PARAM1__A, param1, 0);
  1144. Write16(state, SC_RA_RAM_PARAM0__A, param0, 0);
  1145. SC_SendCommand(state, SC_RA_RAM_CMD_PROC_START);
  1146. } while (0);
  1147. mutex_unlock(&state->mutex);
  1148. return status;
  1149. }
  1150. static int SC_SetPrefParamCommand(struct drxd_state *state,
  1151. u16 subCmd, u16 param0, u16 param1)
  1152. {
  1153. int status;
  1154. mutex_lock(&state->mutex);
  1155. do {
  1156. status = SC_WaitForReady(state);
  1157. if (status < 0)
  1158. break;
  1159. status = Write16(state, SC_RA_RAM_CMD_ADDR__A, subCmd, 0);
  1160. if (status < 0)
  1161. break;
  1162. status = Write16(state, SC_RA_RAM_PARAM1__A, param1, 0);
  1163. if (status < 0)
  1164. break;
  1165. status = Write16(state, SC_RA_RAM_PARAM0__A, param0, 0);
  1166. if (status < 0)
  1167. break;
  1168. status = SC_SendCommand(state, SC_RA_RAM_CMD_SET_PREF_PARAM);
  1169. if (status < 0)
  1170. break;
  1171. } while (0);
  1172. mutex_unlock(&state->mutex);
  1173. return status;
  1174. }
  1175. #if 0
  1176. static int SC_GetOpParamCommand(struct drxd_state *state, u16 * result)
  1177. {
  1178. int status = 0;
  1179. mutex_lock(&state->mutex);
  1180. do {
  1181. status = SC_WaitForReady(state);
  1182. if (status < 0)
  1183. break;
  1184. status = SC_SendCommand(state, SC_RA_RAM_CMD_GET_OP_PARAM);
  1185. if (status < 0)
  1186. break;
  1187. status = Read16(state, SC_RA_RAM_PARAM0__A, result, 0);
  1188. if (status < 0)
  1189. break;
  1190. } while (0);
  1191. mutex_unlock(&state->mutex);
  1192. return status;
  1193. }
  1194. #endif
  1195. static int ConfigureMPEGOutput(struct drxd_state *state, int bEnableOutput)
  1196. {
  1197. int status;
  1198. do {
  1199. u16 EcOcRegIprInvMpg = 0;
  1200. u16 EcOcRegOcModeLop = 0;
  1201. u16 EcOcRegOcModeHip = 0;
  1202. u16 EcOcRegOcMpgSio = 0;
  1203. /*CHK_ERROR(Read16(state, EC_OC_REG_OC_MODE_LOP__A, &EcOcRegOcModeLop, 0)); */
  1204. if (state->operation_mode == OM_DVBT_Diversity_Front) {
  1205. if (bEnableOutput) {
  1206. EcOcRegOcModeHip |=
  1207. B_EC_OC_REG_OC_MODE_HIP_MPG_BUS_SRC_MONITOR;
  1208. } else
  1209. EcOcRegOcMpgSio |= EC_OC_REG_OC_MPG_SIO__M;
  1210. EcOcRegOcModeLop |=
  1211. EC_OC_REG_OC_MODE_LOP_PAR_ENA_DISABLE;
  1212. } else {
  1213. EcOcRegOcModeLop = state->m_EcOcRegOcModeLop;
  1214. if (bEnableOutput)
  1215. EcOcRegOcMpgSio &= (~(EC_OC_REG_OC_MPG_SIO__M));
  1216. else
  1217. EcOcRegOcMpgSio |= EC_OC_REG_OC_MPG_SIO__M;
  1218. /* Don't Insert RS Byte */
  1219. if (state->insert_rs_byte) {
  1220. EcOcRegOcModeLop &=
  1221. (~(EC_OC_REG_OC_MODE_LOP_PAR_ENA__M));
  1222. EcOcRegOcModeHip &=
  1223. (~EC_OC_REG_OC_MODE_HIP_MPG_PAR_VAL__M);
  1224. EcOcRegOcModeHip |=
  1225. EC_OC_REG_OC_MODE_HIP_MPG_PAR_VAL_ENABLE;
  1226. } else {
  1227. EcOcRegOcModeLop |=
  1228. EC_OC_REG_OC_MODE_LOP_PAR_ENA_DISABLE;
  1229. EcOcRegOcModeHip &=
  1230. (~EC_OC_REG_OC_MODE_HIP_MPG_PAR_VAL__M);
  1231. EcOcRegOcModeHip |=
  1232. EC_OC_REG_OC_MODE_HIP_MPG_PAR_VAL_DISABLE;
  1233. }
  1234. /* Mode = Parallel */
  1235. if (state->enable_parallel)
  1236. EcOcRegOcModeLop &=
  1237. (~(EC_OC_REG_OC_MODE_LOP_MPG_TRM_MDE__M));
  1238. else
  1239. EcOcRegOcModeLop |=
  1240. EC_OC_REG_OC_MODE_LOP_MPG_TRM_MDE_SERIAL;
  1241. }
  1242. /* Invert Data */
  1243. /* EcOcRegIprInvMpg |= 0x00FF; */
  1244. EcOcRegIprInvMpg &= (~(0x00FF));
  1245. /* Invert Error ( we don't use the pin ) */
  1246. /* EcOcRegIprInvMpg |= 0x0100; */
  1247. EcOcRegIprInvMpg &= (~(0x0100));
  1248. /* Invert Start ( we don't use the pin ) */
  1249. /* EcOcRegIprInvMpg |= 0x0200; */
  1250. EcOcRegIprInvMpg &= (~(0x0200));
  1251. /* Invert Valid ( we don't use the pin ) */
  1252. /* EcOcRegIprInvMpg |= 0x0400; */
  1253. EcOcRegIprInvMpg &= (~(0x0400));
  1254. /* Invert Clock */
  1255. /* EcOcRegIprInvMpg |= 0x0800; */
  1256. EcOcRegIprInvMpg &= (~(0x0800));
  1257. /* EcOcRegOcModeLop =0x05; */
  1258. status = Write16(state, EC_OC_REG_IPR_INV_MPG__A, EcOcRegIprInvMpg, 0);
  1259. if (status < 0)
  1260. break;
  1261. status = Write16(state, EC_OC_REG_OC_MODE_LOP__A, EcOcRegOcModeLop, 0);
  1262. if (status < 0)
  1263. break;
  1264. status = Write16(state, EC_OC_REG_OC_MODE_HIP__A, EcOcRegOcModeHip, 0x0000);
  1265. if (status < 0)
  1266. break;
  1267. status = Write16(state, EC_OC_REG_OC_MPG_SIO__A, EcOcRegOcMpgSio, 0);
  1268. if (status < 0)
  1269. break;
  1270. } while (0);
  1271. return status;
  1272. }
  1273. static int SetDeviceTypeId(struct drxd_state *state)
  1274. {
  1275. int status = 0;
  1276. u16 deviceId = 0;
  1277. do {
  1278. status = Read16(state, CC_REG_JTAGID_L__A, &deviceId, 0);
  1279. if (status < 0)
  1280. break;
  1281. /* TODO: why twice? */
  1282. status = Read16(state, CC_REG_JTAGID_L__A, &deviceId, 0);
  1283. if (status < 0)
  1284. break;
  1285. printk(KERN_INFO "drxd: deviceId = %04x\n", deviceId);
  1286. state->type_A = 0;
  1287. state->PGA = 0;
  1288. state->diversity = 0;
  1289. if (deviceId == 0) { /* on A2 only 3975 available */
  1290. state->type_A = 1;
  1291. printk(KERN_INFO "DRX3975D-A2\n");
  1292. } else {
  1293. deviceId >>= 12;
  1294. printk(KERN_INFO "DRX397%dD-B1\n", deviceId);
  1295. switch (deviceId) {
  1296. case 4:
  1297. state->diversity = 1;
  1298. case 3:
  1299. case 7:
  1300. state->PGA = 1;
  1301. break;
  1302. case 6:
  1303. state->diversity = 1;
  1304. case 5:
  1305. case 8:
  1306. break;
  1307. default:
  1308. status = -1;
  1309. break;
  1310. }
  1311. }
  1312. } while (0);
  1313. if (status < 0)
  1314. return status;
  1315. /* Init Table selection */
  1316. state->m_InitAtomicRead = DRXD_InitAtomicRead;
  1317. state->m_InitSC = DRXD_InitSC;
  1318. state->m_ResetECRAM = DRXD_ResetECRAM;
  1319. if (state->type_A) {
  1320. state->m_ResetCEFR = DRXD_ResetCEFR;
  1321. state->m_InitFE_1 = DRXD_InitFEA2_1;
  1322. state->m_InitFE_2 = DRXD_InitFEA2_2;
  1323. state->m_InitCP = DRXD_InitCPA2;
  1324. state->m_InitCE = DRXD_InitCEA2;
  1325. state->m_InitEQ = DRXD_InitEQA2;
  1326. state->m_InitEC = DRXD_InitECA2;
  1327. if (load_firmware(state, DRX_FW_FILENAME_A2))
  1328. return -EIO;
  1329. } else {
  1330. state->m_ResetCEFR = NULL;
  1331. state->m_InitFE_1 = DRXD_InitFEB1_1;
  1332. state->m_InitFE_2 = DRXD_InitFEB1_2;
  1333. state->m_InitCP = DRXD_InitCPB1;
  1334. state->m_InitCE = DRXD_InitCEB1;
  1335. state->m_InitEQ = DRXD_InitEQB1;
  1336. state->m_InitEC = DRXD_InitECB1;
  1337. if (load_firmware(state, DRX_FW_FILENAME_B1))
  1338. return -EIO;
  1339. }
  1340. if (state->diversity) {
  1341. state->m_InitDiversityFront = DRXD_InitDiversityFront;
  1342. state->m_InitDiversityEnd = DRXD_InitDiversityEnd;
  1343. state->m_DisableDiversity = DRXD_DisableDiversity;
  1344. state->m_StartDiversityFront = DRXD_StartDiversityFront;
  1345. state->m_StartDiversityEnd = DRXD_StartDiversityEnd;
  1346. state->m_DiversityDelay8MHZ = DRXD_DiversityDelay8MHZ;
  1347. state->m_DiversityDelay6MHZ = DRXD_DiversityDelay6MHZ;
  1348. } else {
  1349. state->m_InitDiversityFront = NULL;
  1350. state->m_InitDiversityEnd = NULL;
  1351. state->m_DisableDiversity = NULL;
  1352. state->m_StartDiversityFront = NULL;
  1353. state->m_StartDiversityEnd = NULL;
  1354. state->m_DiversityDelay8MHZ = NULL;
  1355. state->m_DiversityDelay6MHZ = NULL;
  1356. }
  1357. return status;
  1358. }
  1359. static int CorrectSysClockDeviation(struct drxd_state *state)
  1360. {
  1361. int status;
  1362. s32 incr = 0;
  1363. s32 nomincr = 0;
  1364. u32 bandwidth = 0;
  1365. u32 sysClockInHz = 0;
  1366. u32 sysClockFreq = 0; /* in kHz */
  1367. s16 oscClockDeviation;
  1368. s16 Diff;
  1369. do {
  1370. /* Retrieve bandwidth and incr, sanity check */
  1371. /* These accesses should be AtomicReadReg32, but that
  1372. causes trouble (at least for diversity */
  1373. status = Read32(state, LC_RA_RAM_IFINCR_NOM_L__A, ((u32 *) &nomincr), 0);
  1374. if (status < 0)
  1375. break;
  1376. status = Read32(state, FE_IF_REG_INCR0__A, (u32 *) &incr, 0);
  1377. if (status < 0)
  1378. break;
  1379. if (state->type_A) {
  1380. if ((nomincr - incr < -500) || (nomincr - incr > 500))
  1381. break;
  1382. } else {
  1383. if ((nomincr - incr < -2000) || (nomincr - incr > 2000))
  1384. break;
  1385. }
  1386. switch (state->props.bandwidth_hz) {
  1387. case 8000000:
  1388. bandwidth = DRXD_BANDWIDTH_8MHZ_IN_HZ;
  1389. break;
  1390. case 7000000:
  1391. bandwidth = DRXD_BANDWIDTH_7MHZ_IN_HZ;
  1392. break;
  1393. case 6000000:
  1394. bandwidth = DRXD_BANDWIDTH_6MHZ_IN_HZ;
  1395. break;
  1396. default:
  1397. return -1;
  1398. break;
  1399. }
  1400. /* Compute new sysclock value
  1401. sysClockFreq = (((incr + 2^23)*bandwidth)/2^21)/1000 */
  1402. incr += (1 << 23);
  1403. sysClockInHz = MulDiv32(incr, bandwidth, 1 << 21);
  1404. sysClockFreq = (u32) (sysClockInHz / 1000);
  1405. /* rounding */
  1406. if ((sysClockInHz % 1000) > 500)
  1407. sysClockFreq++;
  1408. /* Compute clock deviation in ppm */
  1409. oscClockDeviation = (u16) ((((s32) (sysClockFreq) -
  1410. (s32)
  1411. (state->expected_sys_clock_freq)) *
  1412. 1000000L) /
  1413. (s32)
  1414. (state->expected_sys_clock_freq));
  1415. Diff = oscClockDeviation - state->osc_clock_deviation;
  1416. /*printk(KERN_INFO "sysclockdiff=%d\n", Diff); */
  1417. if (Diff >= -200 && Diff <= 200) {
  1418. state->sys_clock_freq = (u16) sysClockFreq;
  1419. if (oscClockDeviation != state->osc_clock_deviation) {
  1420. if (state->config.osc_deviation) {
  1421. state->config.osc_deviation(state->priv,
  1422. oscClockDeviation,
  1423. 1);
  1424. state->osc_clock_deviation =
  1425. oscClockDeviation;
  1426. }
  1427. }
  1428. /* switch OFF SRMM scan in SC */
  1429. status = Write16(state, SC_RA_RAM_SAMPLE_RATE_COUNT__A, DRXD_OSCDEV_DONT_SCAN, 0);
  1430. if (status < 0)
  1431. break;
  1432. /* overrule FE_IF internal value for
  1433. proper re-locking */
  1434. status = Write16(state, SC_RA_RAM_IF_SAVE__AX, state->current_fe_if_incr, 0);
  1435. if (status < 0)
  1436. break;
  1437. state->cscd_state = CSCD_SAVED;
  1438. }
  1439. } while (0);
  1440. return status;
  1441. }
  1442. static int DRX_Stop(struct drxd_state *state)
  1443. {
  1444. int status;
  1445. if (state->drxd_state != DRXD_STARTED)
  1446. return 0;
  1447. do {
  1448. if (state->cscd_state != CSCD_SAVED) {
  1449. u32 lock;
  1450. status = DRX_GetLockStatus(state, &lock);
  1451. if (status < 0)
  1452. break;
  1453. }
  1454. status = StopOC(state);
  1455. if (status < 0)
  1456. break;
  1457. state->drxd_state = DRXD_STOPPED;
  1458. status = ConfigureMPEGOutput(state, 0);
  1459. if (status < 0)
  1460. break;
  1461. if (state->type_A) {
  1462. /* Stop relevant processors off the device */
  1463. status = Write16(state, EC_OD_REG_COMM_EXEC__A, 0x0000, 0x0000);
  1464. if (status < 0)
  1465. break;
  1466. status = Write16(state, SC_COMM_EXEC__A, SC_COMM_EXEC_CTL_STOP, 0);
  1467. if (status < 0)
  1468. break;
  1469. status = Write16(state, LC_COMM_EXEC__A, SC_COMM_EXEC_CTL_STOP, 0);
  1470. if (status < 0)
  1471. break;
  1472. } else {
  1473. /* Stop all processors except HI & CC & FE */
  1474. status = Write16(state, B_SC_COMM_EXEC__A, SC_COMM_EXEC_CTL_STOP, 0);
  1475. if (status < 0)
  1476. break;
  1477. status = Write16(state, B_LC_COMM_EXEC__A, SC_COMM_EXEC_CTL_STOP, 0);
  1478. if (status < 0)
  1479. break;
  1480. status = Write16(state, B_FT_COMM_EXEC__A, SC_COMM_EXEC_CTL_STOP, 0);
  1481. if (status < 0)
  1482. break;
  1483. status = Write16(state, B_CP_COMM_EXEC__A, SC_COMM_EXEC_CTL_STOP, 0);
  1484. if (status < 0)
  1485. break;
  1486. status = Write16(state, B_CE_COMM_EXEC__A, SC_COMM_EXEC_CTL_STOP, 0);
  1487. if (status < 0)
  1488. break;
  1489. status = Write16(state, B_EQ_COMM_EXEC__A, SC_COMM_EXEC_CTL_STOP, 0);
  1490. if (status < 0)
  1491. break;
  1492. status = Write16(state, EC_OD_REG_COMM_EXEC__A, 0x0000, 0);
  1493. if (status < 0)
  1494. break;
  1495. }
  1496. } while (0);
  1497. return status;
  1498. }
  1499. #if 0 /* Currently unused */
  1500. static int SetOperationMode(struct drxd_state *state, int oMode)
  1501. {
  1502. int status;
  1503. do {
  1504. if (state->drxd_state != DRXD_STOPPED) {
  1505. status = -1;
  1506. break;
  1507. }
  1508. if (oMode == state->operation_mode) {
  1509. status = 0;
  1510. break;
  1511. }
  1512. if (oMode != OM_Default && !state->diversity) {
  1513. status = -1;
  1514. break;
  1515. }
  1516. switch (oMode) {
  1517. case OM_DVBT_Diversity_Front:
  1518. status = WriteTable(state, state->m_InitDiversityFront);
  1519. break;
  1520. case OM_DVBT_Diversity_End:
  1521. status = WriteTable(state, state->m_InitDiversityEnd);
  1522. break;
  1523. case OM_Default:
  1524. /* We need to check how to
  1525. get DRXD out of diversity */
  1526. default:
  1527. status = WriteTable(state, state->m_DisableDiversity);
  1528. break;
  1529. }
  1530. } while (0);
  1531. if (!status)
  1532. state->operation_mode = oMode;
  1533. return status;
  1534. }
  1535. #endif
  1536. static int StartDiversity(struct drxd_state *state)
  1537. {
  1538. int status = 0;
  1539. u16 rcControl;
  1540. do {
  1541. if (state->operation_mode == OM_DVBT_Diversity_Front) {
  1542. status = WriteTable(state, state->m_StartDiversityFront);
  1543. if (status < 0)
  1544. break;
  1545. } else if (state->operation_mode == OM_DVBT_Diversity_End) {
  1546. status = WriteTable(state, state->m_StartDiversityEnd);
  1547. if (status < 0)
  1548. break;
  1549. if (state->props.bandwidth_hz == 8000000) {
  1550. status = WriteTable(state, state->m_DiversityDelay8MHZ);
  1551. if (status < 0)
  1552. break;
  1553. } else {
  1554. status = WriteTable(state, state->m_DiversityDelay6MHZ);
  1555. if (status < 0)
  1556. break;
  1557. }
  1558. status = Read16(state, B_EQ_REG_RC_SEL_CAR__A, &rcControl, 0);
  1559. if (status < 0)
  1560. break;
  1561. rcControl &= ~(B_EQ_REG_RC_SEL_CAR_FFTMODE__M);
  1562. rcControl |= B_EQ_REG_RC_SEL_CAR_DIV_ON |
  1563. /* combining enabled */
  1564. B_EQ_REG_RC_SEL_CAR_MEAS_A_CC |
  1565. B_EQ_REG_RC_SEL_CAR_PASS_A_CC |
  1566. B_EQ_REG_RC_SEL_CAR_LOCAL_A_CC;
  1567. status = Write16(state, B_EQ_REG_RC_SEL_CAR__A, rcControl, 0);
  1568. if (status < 0)
  1569. break;
  1570. }
  1571. } while (0);
  1572. return status;
  1573. }
  1574. static int SetFrequencyShift(struct drxd_state *state,
  1575. u32 offsetFreq, int channelMirrored)
  1576. {
  1577. int negativeShift = (state->tuner_mirrors == channelMirrored);
  1578. /* Handle all mirroring
  1579. *
  1580. * Note: ADC mirroring (aliasing) is implictly handled by limiting
  1581. * feFsRegAddInc to 28 bits below
  1582. * (if the result before masking is more than 28 bits, this means
  1583. * that the ADC is mirroring.
  1584. * The masking is in fact the aliasing of the ADC)
  1585. *
  1586. */
  1587. /* Compute register value, unsigned computation */
  1588. state->fe_fs_add_incr = MulDiv32(state->intermediate_freq +
  1589. offsetFreq,
  1590. 1 << 28, state->sys_clock_freq);
  1591. /* Remove integer part */
  1592. state->fe_fs_add_incr &= 0x0FFFFFFFL;
  1593. if (negativeShift)
  1594. state->fe_fs_add_incr = ((1 << 28) - state->fe_fs_add_incr);
  1595. /* Save the frequency shift without tunerOffset compensation
  1596. for CtrlGetChannel. */
  1597. state->org_fe_fs_add_incr = MulDiv32(state->intermediate_freq,
  1598. 1 << 28, state->sys_clock_freq);
  1599. /* Remove integer part */
  1600. state->org_fe_fs_add_incr &= 0x0FFFFFFFL;
  1601. if (negativeShift)
  1602. state->org_fe_fs_add_incr = ((1L << 28) -
  1603. state->org_fe_fs_add_incr);
  1604. return Write32(state, FE_FS_REG_ADD_INC_LOP__A,
  1605. state->fe_fs_add_incr, 0);
  1606. }
  1607. static int SetCfgNoiseCalibration(struct drxd_state *state,
  1608. struct SNoiseCal *noiseCal)
  1609. {
  1610. u16 beOptEna;
  1611. int status = 0;
  1612. do {
  1613. status = Read16(state, SC_RA_RAM_BE_OPT_ENA__A, &beOptEna, 0);
  1614. if (status < 0)
  1615. break;
  1616. if (noiseCal->cpOpt) {
  1617. beOptEna |= (1 << SC_RA_RAM_BE_OPT_ENA_CP_OPT);
  1618. } else {
  1619. beOptEna &= ~(1 << SC_RA_RAM_BE_OPT_ENA_CP_OPT);
  1620. status = Write16(state, CP_REG_AC_NEXP_OFFS__A, noiseCal->cpNexpOfs, 0);
  1621. if (status < 0)
  1622. break;
  1623. }
  1624. status = Write16(state, SC_RA_RAM_BE_OPT_ENA__A, beOptEna, 0);
  1625. if (status < 0)
  1626. break;
  1627. if (!state->type_A) {
  1628. status = Write16(state, B_SC_RA_RAM_CO_TD_CAL_2K__A, noiseCal->tdCal2k, 0);
  1629. if (status < 0)
  1630. break;
  1631. status = Write16(state, B_SC_RA_RAM_CO_TD_CAL_8K__A, noiseCal->tdCal8k, 0);
  1632. if (status < 0)
  1633. break;
  1634. }
  1635. } while (0);
  1636. return status;
  1637. }
  1638. static int DRX_Start(struct drxd_state *state, s32 off)
  1639. {
  1640. struct dtv_frontend_properties *p = &state->props;
  1641. int status;
  1642. u16 transmissionParams = 0;
  1643. u16 operationMode = 0;
  1644. u16 qpskTdTpsPwr = 0;
  1645. u16 qam16TdTpsPwr = 0;
  1646. u16 qam64TdTpsPwr = 0;
  1647. u32 feIfIncr = 0;
  1648. u32 bandwidth = 0;
  1649. int mirrorFreqSpect;
  1650. u16 qpskSnCeGain = 0;
  1651. u16 qam16SnCeGain = 0;
  1652. u16 qam64SnCeGain = 0;
  1653. u16 qpskIsGainMan = 0;
  1654. u16 qam16IsGainMan = 0;
  1655. u16 qam64IsGainMan = 0;
  1656. u16 qpskIsGainExp = 0;
  1657. u16 qam16IsGainExp = 0;
  1658. u16 qam64IsGainExp = 0;
  1659. u16 bandwidthParam = 0;
  1660. if (off < 0)
  1661. off = (off - 500) / 1000;
  1662. else
  1663. off = (off + 500) / 1000;
  1664. do {
  1665. if (state->drxd_state != DRXD_STOPPED)
  1666. return -1;
  1667. status = ResetECOD(state);
  1668. if (status < 0)
  1669. break;
  1670. if (state->type_A) {
  1671. status = InitSC(state);
  1672. if (status < 0)
  1673. break;
  1674. } else {
  1675. status = InitFT(state);
  1676. if (status < 0)
  1677. break;
  1678. status = InitCP(state);
  1679. if (status < 0)
  1680. break;
  1681. status = InitCE(state);
  1682. if (status < 0)
  1683. break;
  1684. status = InitEQ(state);
  1685. if (status < 0)
  1686. break;
  1687. status = InitSC(state);
  1688. if (status < 0)
  1689. break;
  1690. }
  1691. /* Restore current IF & RF AGC settings */
  1692. status = SetCfgIfAgc(state, &state->if_agc_cfg);
  1693. if (status < 0)
  1694. break;
  1695. status = SetCfgRfAgc(state, &state->rf_agc_cfg);
  1696. if (status < 0)
  1697. break;
  1698. mirrorFreqSpect = (state->props.inversion == INVERSION_ON);
  1699. switch (p->transmission_mode) {
  1700. default: /* Not set, detect it automatically */
  1701. operationMode |= SC_RA_RAM_OP_AUTO_MODE__M;
  1702. /* fall through , try first guess DRX_FFTMODE_8K */
  1703. case TRANSMISSION_MODE_8K:
  1704. transmissionParams |= SC_RA_RAM_OP_PARAM_MODE_8K;
  1705. if (state->type_A) {
  1706. status = Write16(state, EC_SB_REG_TR_MODE__A, EC_SB_REG_TR_MODE_8K, 0x0000);
  1707. if (status < 0)
  1708. break;
  1709. qpskSnCeGain = 99;
  1710. qam16SnCeGain = 83;
  1711. qam64SnCeGain = 67;
  1712. }
  1713. break;
  1714. case TRANSMISSION_MODE_2K:
  1715. transmissionParams |= SC_RA_RAM_OP_PARAM_MODE_2K;
  1716. if (state->type_A) {
  1717. status = Write16(state, EC_SB_REG_TR_MODE__A, EC_SB_REG_TR_MODE_2K, 0x0000);
  1718. if (status < 0)
  1719. break;
  1720. qpskSnCeGain = 97;
  1721. qam16SnCeGain = 71;
  1722. qam64SnCeGain = 65;
  1723. }
  1724. break;
  1725. }
  1726. switch (p->guard_interval) {
  1727. case GUARD_INTERVAL_1_4:
  1728. transmissionParams |= SC_RA_RAM_OP_PARAM_GUARD_4;
  1729. break;
  1730. case GUARD_INTERVAL_1_8:
  1731. transmissionParams |= SC_RA_RAM_OP_PARAM_GUARD_8;
  1732. break;
  1733. case GUARD_INTERVAL_1_16:
  1734. transmissionParams |= SC_RA_RAM_OP_PARAM_GUARD_16;
  1735. break;
  1736. case GUARD_INTERVAL_1_32:
  1737. transmissionParams |= SC_RA_RAM_OP_PARAM_GUARD_32;
  1738. break;
  1739. default: /* Not set, detect it automatically */
  1740. operationMode |= SC_RA_RAM_OP_AUTO_GUARD__M;
  1741. /* try first guess 1/4 */
  1742. transmissionParams |= SC_RA_RAM_OP_PARAM_GUARD_4;
  1743. break;
  1744. }
  1745. switch (p->hierarchy) {
  1746. case HIERARCHY_1:
  1747. transmissionParams |= SC_RA_RAM_OP_PARAM_HIER_A1;
  1748. if (state->type_A) {
  1749. status = Write16(state, EQ_REG_OT_ALPHA__A, 0x0001, 0x0000);
  1750. if (status < 0)
  1751. break;
  1752. status = Write16(state, EC_SB_REG_ALPHA__A, 0x0001, 0x0000);
  1753. if (status < 0)
  1754. break;
  1755. qpskTdTpsPwr = EQ_TD_TPS_PWR_UNKNOWN;
  1756. qam16TdTpsPwr = EQ_TD_TPS_PWR_QAM16_ALPHA1;
  1757. qam64TdTpsPwr = EQ_TD_TPS_PWR_QAM64_ALPHA1;
  1758. qpskIsGainMan =
  1759. SC_RA_RAM_EQ_IS_GAIN_UNKNOWN_MAN__PRE;
  1760. qam16IsGainMan =
  1761. SC_RA_RAM_EQ_IS_GAIN_16QAM_MAN__PRE;
  1762. qam64IsGainMan =
  1763. SC_RA_RAM_EQ_IS_GAIN_64QAM_MAN__PRE;
  1764. qpskIsGainExp =
  1765. SC_RA_RAM_EQ_IS_GAIN_UNKNOWN_EXP__PRE;
  1766. qam16IsGainExp =
  1767. SC_RA_RAM_EQ_IS_GAIN_16QAM_EXP__PRE;
  1768. qam64IsGainExp =
  1769. SC_RA_RAM_EQ_IS_GAIN_64QAM_EXP__PRE;
  1770. }
  1771. break;
  1772. case HIERARCHY_2:
  1773. transmissionParams |= SC_RA_RAM_OP_PARAM_HIER_A2;
  1774. if (state->type_A) {
  1775. status = Write16(state, EQ_REG_OT_ALPHA__A, 0x0002, 0x0000);
  1776. if (status < 0)
  1777. break;
  1778. status = Write16(state, EC_SB_REG_ALPHA__A, 0x0002, 0x0000);
  1779. if (status < 0)
  1780. break;
  1781. qpskTdTpsPwr = EQ_TD_TPS_PWR_UNKNOWN;
  1782. qam16TdTpsPwr = EQ_TD_TPS_PWR_QAM16_ALPHA2;
  1783. qam64TdTpsPwr = EQ_TD_TPS_PWR_QAM64_ALPHA2;
  1784. qpskIsGainMan =
  1785. SC_RA_RAM_EQ_IS_GAIN_UNKNOWN_MAN__PRE;
  1786. qam16IsGainMan =
  1787. SC_RA_RAM_EQ_IS_GAIN_16QAM_A2_MAN__PRE;
  1788. qam64IsGainMan =
  1789. SC_RA_RAM_EQ_IS_GAIN_64QAM_A2_MAN__PRE;
  1790. qpskIsGainExp =
  1791. SC_RA_RAM_EQ_IS_GAIN_UNKNOWN_EXP__PRE;
  1792. qam16IsGainExp =
  1793. SC_RA_RAM_EQ_IS_GAIN_16QAM_A2_EXP__PRE;
  1794. qam64IsGainExp =
  1795. SC_RA_RAM_EQ_IS_GAIN_64QAM_A2_EXP__PRE;
  1796. }
  1797. break;
  1798. case HIERARCHY_4:
  1799. transmissionParams |= SC_RA_RAM_OP_PARAM_HIER_A4;
  1800. if (state->type_A) {
  1801. status = Write16(state, EQ_REG_OT_ALPHA__A, 0x0003, 0x0000);
  1802. if (status < 0)
  1803. break;
  1804. status = Write16(state, EC_SB_REG_ALPHA__A, 0x0003, 0x0000);
  1805. if (status < 0)
  1806. break;
  1807. qpskTdTpsPwr = EQ_TD_TPS_PWR_UNKNOWN;
  1808. qam16TdTpsPwr = EQ_TD_TPS_PWR_QAM16_ALPHA4;
  1809. qam64TdTpsPwr = EQ_TD_TPS_PWR_QAM64_ALPHA4;
  1810. qpskIsGainMan =
  1811. SC_RA_RAM_EQ_IS_GAIN_UNKNOWN_MAN__PRE;
  1812. qam16IsGainMan =
  1813. SC_RA_RAM_EQ_IS_GAIN_16QAM_A4_MAN__PRE;
  1814. qam64IsGainMan =
  1815. SC_RA_RAM_EQ_IS_GAIN_64QAM_A4_MAN__PRE;
  1816. qpskIsGainExp =
  1817. SC_RA_RAM_EQ_IS_GAIN_UNKNOWN_EXP__PRE;
  1818. qam16IsGainExp =
  1819. SC_RA_RAM_EQ_IS_GAIN_16QAM_A4_EXP__PRE;
  1820. qam64IsGainExp =
  1821. SC_RA_RAM_EQ_IS_GAIN_64QAM_A4_EXP__PRE;
  1822. }
  1823. break;
  1824. case HIERARCHY_AUTO:
  1825. default:
  1826. /* Not set, detect it automatically, start with none */
  1827. operationMode |= SC_RA_RAM_OP_AUTO_HIER__M;
  1828. transmissionParams |= SC_RA_RAM_OP_PARAM_HIER_NO;
  1829. if (state->type_A) {
  1830. status = Write16(state, EQ_REG_OT_ALPHA__A, 0x0000, 0x0000);
  1831. if (status < 0)
  1832. break;
  1833. status = Write16(state, EC_SB_REG_ALPHA__A, 0x0000, 0x0000);
  1834. if (status < 0)
  1835. break;
  1836. qpskTdTpsPwr = EQ_TD_TPS_PWR_QPSK;
  1837. qam16TdTpsPwr = EQ_TD_TPS_PWR_QAM16_ALPHAN;
  1838. qam64TdTpsPwr = EQ_TD_TPS_PWR_QAM64_ALPHAN;
  1839. qpskIsGainMan =
  1840. SC_RA_RAM_EQ_IS_GAIN_QPSK_MAN__PRE;
  1841. qam16IsGainMan =
  1842. SC_RA_RAM_EQ_IS_GAIN_16QAM_MAN__PRE;
  1843. qam64IsGainMan =
  1844. SC_RA_RAM_EQ_IS_GAIN_64QAM_MAN__PRE;
  1845. qpskIsGainExp =
  1846. SC_RA_RAM_EQ_IS_GAIN_QPSK_EXP__PRE;
  1847. qam16IsGainExp =
  1848. SC_RA_RAM_EQ_IS_GAIN_16QAM_EXP__PRE;
  1849. qam64IsGainExp =
  1850. SC_RA_RAM_EQ_IS_GAIN_64QAM_EXP__PRE;
  1851. }
  1852. break;
  1853. }
  1854. status = status;
  1855. if (status < 0)
  1856. break;
  1857. switch (p->modulation) {
  1858. default:
  1859. operationMode |= SC_RA_RAM_OP_AUTO_CONST__M;
  1860. /* fall through , try first guess
  1861. DRX_CONSTELLATION_QAM64 */
  1862. case QAM_64:
  1863. transmissionParams |= SC_RA_RAM_OP_PARAM_CONST_QAM64;
  1864. if (state->type_A) {
  1865. status = Write16(state, EQ_REG_OT_CONST__A, 0x0002, 0x0000);
  1866. if (status < 0)
  1867. break;
  1868. status = Write16(state, EC_SB_REG_CONST__A, EC_SB_REG_CONST_64QAM, 0x0000);
  1869. if (status < 0)
  1870. break;
  1871. status = Write16(state, EC_SB_REG_SCALE_MSB__A, 0x0020, 0x0000);
  1872. if (status < 0)
  1873. break;
  1874. status = Write16(state, EC_SB_REG_SCALE_BIT2__A, 0x0008, 0x0000);
  1875. if (status < 0)
  1876. break;
  1877. status = Write16(state, EC_SB_REG_SCALE_LSB__A, 0x0002, 0x0000);
  1878. if (status < 0)
  1879. break;
  1880. status = Write16(state, EQ_REG_TD_TPS_PWR_OFS__A, qam64TdTpsPwr, 0x0000);
  1881. if (status < 0)
  1882. break;
  1883. status = Write16(state, EQ_REG_SN_CEGAIN__A, qam64SnCeGain, 0x0000);
  1884. if (status < 0)
  1885. break;
  1886. status = Write16(state, EQ_REG_IS_GAIN_MAN__A, qam64IsGainMan, 0x0000);
  1887. if (status < 0)
  1888. break;
  1889. status = Write16(state, EQ_REG_IS_GAIN_EXP__A, qam64IsGainExp, 0x0000);
  1890. if (status < 0)
  1891. break;
  1892. }
  1893. break;
  1894. case QPSK:
  1895. transmissionParams |= SC_RA_RAM_OP_PARAM_CONST_QPSK;
  1896. if (state->type_A) {
  1897. status = Write16(state, EQ_REG_OT_CONST__A, 0x0000, 0x0000);
  1898. if (status < 0)
  1899. break;
  1900. status = Write16(state, EC_SB_REG_CONST__A, EC_SB_REG_CONST_QPSK, 0x0000);
  1901. if (status < 0)
  1902. break;
  1903. status = Write16(state, EC_SB_REG_SCALE_MSB__A, 0x0010, 0x0000);
  1904. if (status < 0)
  1905. break;
  1906. status = Write16(state, EC_SB_REG_SCALE_BIT2__A, 0x0000, 0x0000);
  1907. if (status < 0)
  1908. break;
  1909. status = Write16(state, EC_SB_REG_SCALE_LSB__A, 0x0000, 0x0000);
  1910. if (status < 0)
  1911. break;
  1912. status = Write16(state, EQ_REG_TD_TPS_PWR_OFS__A, qpskTdTpsPwr, 0x0000);
  1913. if (status < 0)
  1914. break;
  1915. status = Write16(state, EQ_REG_SN_CEGAIN__A, qpskSnCeGain, 0x0000);
  1916. if (status < 0)
  1917. break;
  1918. status = Write16(state, EQ_REG_IS_GAIN_MAN__A, qpskIsGainMan, 0x0000);
  1919. if (status < 0)
  1920. break;
  1921. status = Write16(state, EQ_REG_IS_GAIN_EXP__A, qpskIsGainExp, 0x0000);
  1922. if (status < 0)
  1923. break;
  1924. }
  1925. break;
  1926. case QAM_16:
  1927. transmissionParams |= SC_RA_RAM_OP_PARAM_CONST_QAM16;
  1928. if (state->type_A) {
  1929. status = Write16(state, EQ_REG_OT_CONST__A, 0x0001, 0x0000);
  1930. if (status < 0)
  1931. break;
  1932. status = Write16(state, EC_SB_REG_CONST__A, EC_SB_REG_CONST_16QAM, 0x0000);
  1933. if (status < 0)
  1934. break;
  1935. status = Write16(state, EC_SB_REG_SCALE_MSB__A, 0x0010, 0x0000);
  1936. if (status < 0)
  1937. break;
  1938. status = Write16(state, EC_SB_REG_SCALE_BIT2__A, 0x0004, 0x0000);
  1939. if (status < 0)
  1940. break;
  1941. status = Write16(state, EC_SB_REG_SCALE_LSB__A, 0x0000, 0x0000);
  1942. if (status < 0)
  1943. break;
  1944. status = Write16(state, EQ_REG_TD_TPS_PWR_OFS__A, qam16TdTpsPwr, 0x0000);
  1945. if (status < 0)
  1946. break;
  1947. status = Write16(state, EQ_REG_SN_CEGAIN__A, qam16SnCeGain, 0x0000);
  1948. if (status < 0)
  1949. break;
  1950. status = Write16(state, EQ_REG_IS_GAIN_MAN__A, qam16IsGainMan, 0x0000);
  1951. if (status < 0)
  1952. break;
  1953. status = Write16(state, EQ_REG_IS_GAIN_EXP__A, qam16IsGainExp, 0x0000);
  1954. if (status < 0)
  1955. break;
  1956. }
  1957. break;
  1958. }
  1959. status = status;
  1960. if (status < 0)
  1961. break;
  1962. switch (DRX_CHANNEL_HIGH) {
  1963. default:
  1964. case DRX_CHANNEL_AUTO:
  1965. case DRX_CHANNEL_LOW:
  1966. transmissionParams |= SC_RA_RAM_OP_PARAM_PRIO_LO;
  1967. status = Write16(state, EC_SB_REG_PRIOR__A, EC_SB_REG_PRIOR_LO, 0x0000);
  1968. if (status < 0)
  1969. break;
  1970. break;
  1971. case DRX_CHANNEL_HIGH:
  1972. transmissionParams |= SC_RA_RAM_OP_PARAM_PRIO_HI;
  1973. status = Write16(state, EC_SB_REG_PRIOR__A, EC_SB_REG_PRIOR_HI, 0x0000);
  1974. if (status < 0)
  1975. break;
  1976. break;
  1977. }
  1978. switch (p->code_rate_HP) {
  1979. case FEC_1_2:
  1980. transmissionParams |= SC_RA_RAM_OP_PARAM_RATE_1_2;
  1981. if (state->type_A) {
  1982. status = Write16(state, EC_VD_REG_SET_CODERATE__A, EC_VD_REG_SET_CODERATE_C1_2, 0x0000);
  1983. if (status < 0)
  1984. break;
  1985. }
  1986. break;
  1987. default:
  1988. operationMode |= SC_RA_RAM_OP_AUTO_RATE__M;
  1989. case FEC_2_3:
  1990. transmissionParams |= SC_RA_RAM_OP_PARAM_RATE_2_3;
  1991. if (state->type_A) {
  1992. status = Write16(state, EC_VD_REG_SET_CODERATE__A, EC_VD_REG_SET_CODERATE_C2_3, 0x0000);
  1993. if (status < 0)
  1994. break;
  1995. }
  1996. break;
  1997. case FEC_3_4:
  1998. transmissionParams |= SC_RA_RAM_OP_PARAM_RATE_3_4;
  1999. if (state->type_A) {
  2000. status = Write16(state, EC_VD_REG_SET_CODERATE__A, EC_VD_REG_SET_CODERATE_C3_4, 0x0000);
  2001. if (status < 0)
  2002. break;
  2003. }
  2004. break;
  2005. case FEC_5_6:
  2006. transmissionParams |= SC_RA_RAM_OP_PARAM_RATE_5_6;
  2007. if (state->type_A) {
  2008. status = Write16(state, EC_VD_REG_SET_CODERATE__A, EC_VD_REG_SET_CODERATE_C5_6, 0x0000);
  2009. if (status < 0)
  2010. break;
  2011. }
  2012. break;
  2013. case FEC_7_8:
  2014. transmissionParams |= SC_RA_RAM_OP_PARAM_RATE_7_8;
  2015. if (state->type_A) {
  2016. status = Write16(state, EC_VD_REG_SET_CODERATE__A, EC_VD_REG_SET_CODERATE_C7_8, 0x0000);
  2017. if (status < 0)
  2018. break;
  2019. }
  2020. break;
  2021. }
  2022. status = status;
  2023. if (status < 0)
  2024. break;
  2025. /* First determine real bandwidth (Hz) */
  2026. /* Also set delay for impulse noise cruncher (only A2) */
  2027. /* Also set parameters for EC_OC fix, note
  2028. EC_OC_REG_TMD_HIL_MAR is changed
  2029. by SC for fix for some 8K,1/8 guard but is restored by
  2030. InitEC and ResetEC
  2031. functions */
  2032. switch (p->bandwidth_hz) {
  2033. case 0:
  2034. p->bandwidth_hz = 8000000;
  2035. /* fall through */
  2036. case 8000000:
  2037. /* (64/7)*(8/8)*1000000 */
  2038. bandwidth = DRXD_BANDWIDTH_8MHZ_IN_HZ;
  2039. bandwidthParam = 0;
  2040. status = Write16(state,
  2041. FE_AG_REG_IND_DEL__A, 50, 0x0000);
  2042. break;
  2043. case 7000000:
  2044. /* (64/7)*(7/8)*1000000 */
  2045. bandwidth = DRXD_BANDWIDTH_7MHZ_IN_HZ;
  2046. bandwidthParam = 0x4807; /*binary:0100 1000 0000 0111 */
  2047. status = Write16(state,
  2048. FE_AG_REG_IND_DEL__A, 59, 0x0000);
  2049. break;
  2050. case 6000000:
  2051. /* (64/7)*(6/8)*1000000 */
  2052. bandwidth = DRXD_BANDWIDTH_6MHZ_IN_HZ;
  2053. bandwidthParam = 0x0F07; /*binary: 0000 1111 0000 0111 */
  2054. status = Write16(state,
  2055. FE_AG_REG_IND_DEL__A, 71, 0x0000);
  2056. break;
  2057. default:
  2058. status = -EINVAL;
  2059. }
  2060. if (status < 0)
  2061. break;
  2062. status = Write16(state, SC_RA_RAM_BAND__A, bandwidthParam, 0x0000);
  2063. if (status < 0)
  2064. break;
  2065. {
  2066. u16 sc_config;
  2067. status = Read16(state, SC_RA_RAM_CONFIG__A, &sc_config, 0);
  2068. if (status < 0)
  2069. break;
  2070. /* enable SLAVE mode in 2k 1/32 to
  2071. prevent timing change glitches */
  2072. if ((p->transmission_mode == TRANSMISSION_MODE_2K) &&
  2073. (p->guard_interval == GUARD_INTERVAL_1_32)) {
  2074. /* enable slave */
  2075. sc_config |= SC_RA_RAM_CONFIG_SLAVE__M;
  2076. } else {
  2077. /* disable slave */
  2078. sc_config &= ~SC_RA_RAM_CONFIG_SLAVE__M;
  2079. }
  2080. status = Write16(state, SC_RA_RAM_CONFIG__A, sc_config, 0);
  2081. if (status < 0)
  2082. break;
  2083. }
  2084. status = SetCfgNoiseCalibration(state, &state->noise_cal);
  2085. if (status < 0)
  2086. break;
  2087. if (state->cscd_state == CSCD_INIT) {
  2088. /* switch on SRMM scan in SC */
  2089. status = Write16(state, SC_RA_RAM_SAMPLE_RATE_COUNT__A, DRXD_OSCDEV_DO_SCAN, 0x0000);
  2090. if (status < 0)
  2091. break;
  2092. /* CHK_ERROR(Write16(SC_RA_RAM_SAMPLE_RATE_STEP__A, DRXD_OSCDEV_STEP, 0x0000));*/
  2093. state->cscd_state = CSCD_SET;
  2094. }
  2095. /* Now compute FE_IF_REG_INCR */
  2096. /*((( SysFreq/BandWidth)/2)/2) -1) * 2^23) =>
  2097. ((SysFreq / BandWidth) * (2^21) ) - (2^23) */
  2098. feIfIncr = MulDiv32(state->sys_clock_freq * 1000,
  2099. (1ULL << 21), bandwidth) - (1 << 23);
  2100. status = Write16(state, FE_IF_REG_INCR0__A, (u16) (feIfIncr & FE_IF_REG_INCR0__M), 0x0000);
  2101. if (status < 0)
  2102. break;
  2103. status = Write16(state, FE_IF_REG_INCR1__A, (u16) ((feIfIncr >> FE_IF_REG_INCR0__W) & FE_IF_REG_INCR1__M), 0x0000);
  2104. if (status < 0)
  2105. break;
  2106. /* Bandwidth setting done */
  2107. /* Mirror & frequency offset */
  2108. SetFrequencyShift(state, off, mirrorFreqSpect);
  2109. /* Start SC, write channel settings to SC */
  2110. /* Enable SC after setting all other parameters */
  2111. status = Write16(state, SC_COMM_STATE__A, 0, 0x0000);
  2112. if (status < 0)
  2113. break;
  2114. status = Write16(state, SC_COMM_EXEC__A, 1, 0x0000);
  2115. if (status < 0)
  2116. break;
  2117. /* Write SC parameter registers, operation mode */
  2118. #if 1
  2119. operationMode = (SC_RA_RAM_OP_AUTO_MODE__M |
  2120. SC_RA_RAM_OP_AUTO_GUARD__M |
  2121. SC_RA_RAM_OP_AUTO_CONST__M |
  2122. SC_RA_RAM_OP_AUTO_HIER__M |
  2123. SC_RA_RAM_OP_AUTO_RATE__M);
  2124. #endif
  2125. status = SC_SetPrefParamCommand(state, 0x0000, transmissionParams, operationMode);
  2126. if (status < 0)
  2127. break;
  2128. /* Start correct processes to get in lock */
  2129. status = SC_ProcStartCommand(state, SC_RA_RAM_PROC_LOCKTRACK, SC_RA_RAM_SW_EVENT_RUN_NMASK__M, SC_RA_RAM_LOCKTRACK_MIN);
  2130. if (status < 0)
  2131. break;
  2132. status = StartOC(state);
  2133. if (status < 0)
  2134. break;
  2135. if (state->operation_mode != OM_Default) {
  2136. status = StartDiversity(state);
  2137. if (status < 0)
  2138. break;
  2139. }
  2140. state->drxd_state = DRXD_STARTED;
  2141. } while (0);
  2142. return status;
  2143. }
  2144. static int CDRXD(struct drxd_state *state, u32 IntermediateFrequency)
  2145. {
  2146. u32 ulRfAgcOutputLevel = 0xffffffff;
  2147. u32 ulRfAgcSettleLevel = 528; /* Optimum value for MT2060 */
  2148. u32 ulRfAgcMinLevel = 0; /* Currently unused */
  2149. u32 ulRfAgcMaxLevel = DRXD_FE_CTRL_MAX; /* Currently unused */
  2150. u32 ulRfAgcSpeed = 0; /* Currently unused */
  2151. u32 ulRfAgcMode = 0; /*2; Off */
  2152. u32 ulRfAgcR1 = 820;
  2153. u32 ulRfAgcR2 = 2200;
  2154. u32 ulRfAgcR3 = 150;
  2155. u32 ulIfAgcMode = 0; /* Auto */
  2156. u32 ulIfAgcOutputLevel = 0xffffffff;
  2157. u32 ulIfAgcSettleLevel = 0xffffffff;
  2158. u32 ulIfAgcMinLevel = 0xffffffff;
  2159. u32 ulIfAgcMaxLevel = 0xffffffff;
  2160. u32 ulIfAgcSpeed = 0xffffffff;
  2161. u32 ulIfAgcR1 = 820;
  2162. u32 ulIfAgcR2 = 2200;
  2163. u32 ulIfAgcR3 = 150;
  2164. u32 ulClock = state->config.clock;
  2165. u32 ulSerialMode = 0;
  2166. u32 ulEcOcRegOcModeLop = 4; /* Dynamic DTO source */
  2167. u32 ulHiI2cDelay = HI_I2C_DELAY;
  2168. u32 ulHiI2cBridgeDelay = HI_I2C_BRIDGE_DELAY;
  2169. u32 ulHiI2cPatch = 0;
  2170. u32 ulEnvironment = APPENV_PORTABLE;
  2171. u32 ulEnvironmentDiversity = APPENV_MOBILE;
  2172. u32 ulIFFilter = IFFILTER_SAW;
  2173. state->if_agc_cfg.ctrlMode = AGC_CTRL_AUTO;
  2174. state->if_agc_cfg.outputLevel = 0;
  2175. state->if_agc_cfg.settleLevel = 140;
  2176. state->if_agc_cfg.minOutputLevel = 0;
  2177. state->if_agc_cfg.maxOutputLevel = 1023;
  2178. state->if_agc_cfg.speed = 904;
  2179. if (ulIfAgcMode == 1 && ulIfAgcOutputLevel <= DRXD_FE_CTRL_MAX) {
  2180. state->if_agc_cfg.ctrlMode = AGC_CTRL_USER;
  2181. state->if_agc_cfg.outputLevel = (u16) (ulIfAgcOutputLevel);
  2182. }
  2183. if (ulIfAgcMode == 0 &&
  2184. ulIfAgcSettleLevel <= DRXD_FE_CTRL_MAX &&
  2185. ulIfAgcMinLevel <= DRXD_FE_CTRL_MAX &&
  2186. ulIfAgcMaxLevel <= DRXD_FE_CTRL_MAX &&
  2187. ulIfAgcSpeed <= DRXD_FE_CTRL_MAX) {
  2188. state->if_agc_cfg.ctrlMode = AGC_CTRL_AUTO;
  2189. state->if_agc_cfg.settleLevel = (u16) (ulIfAgcSettleLevel);
  2190. state->if_agc_cfg.minOutputLevel = (u16) (ulIfAgcMinLevel);
  2191. state->if_agc_cfg.maxOutputLevel = (u16) (ulIfAgcMaxLevel);
  2192. state->if_agc_cfg.speed = (u16) (ulIfAgcSpeed);
  2193. }
  2194. state->if_agc_cfg.R1 = (u16) (ulIfAgcR1);
  2195. state->if_agc_cfg.R2 = (u16) (ulIfAgcR2);
  2196. state->if_agc_cfg.R3 = (u16) (ulIfAgcR3);
  2197. state->rf_agc_cfg.R1 = (u16) (ulRfAgcR1);
  2198. state->rf_agc_cfg.R2 = (u16) (ulRfAgcR2);
  2199. state->rf_agc_cfg.R3 = (u16) (ulRfAgcR3);
  2200. state->rf_agc_cfg.ctrlMode = AGC_CTRL_AUTO;
  2201. /* rest of the RFAgcCfg structure currently unused */
  2202. if (ulRfAgcMode == 1 && ulRfAgcOutputLevel <= DRXD_FE_CTRL_MAX) {
  2203. state->rf_agc_cfg.ctrlMode = AGC_CTRL_USER;
  2204. state->rf_agc_cfg.outputLevel = (u16) (ulRfAgcOutputLevel);
  2205. }
  2206. if (ulRfAgcMode == 0 &&
  2207. ulRfAgcSettleLevel <= DRXD_FE_CTRL_MAX &&
  2208. ulRfAgcMinLevel <= DRXD_FE_CTRL_MAX &&
  2209. ulRfAgcMaxLevel <= DRXD_FE_CTRL_MAX &&
  2210. ulRfAgcSpeed <= DRXD_FE_CTRL_MAX) {
  2211. state->rf_agc_cfg.ctrlMode = AGC_CTRL_AUTO;
  2212. state->rf_agc_cfg.settleLevel = (u16) (ulRfAgcSettleLevel);
  2213. state->rf_agc_cfg.minOutputLevel = (u16) (ulRfAgcMinLevel);
  2214. state->rf_agc_cfg.maxOutputLevel = (u16) (ulRfAgcMaxLevel);
  2215. state->rf_agc_cfg.speed = (u16) (ulRfAgcSpeed);
  2216. }
  2217. if (ulRfAgcMode == 2)
  2218. state->rf_agc_cfg.ctrlMode = AGC_CTRL_OFF;
  2219. if (ulEnvironment <= 2)
  2220. state->app_env_default = (enum app_env)
  2221. (ulEnvironment);
  2222. if (ulEnvironmentDiversity <= 2)
  2223. state->app_env_diversity = (enum app_env)
  2224. (ulEnvironmentDiversity);
  2225. if (ulIFFilter == IFFILTER_DISCRETE) {
  2226. /* discrete filter */
  2227. state->noise_cal.cpOpt = 0;
  2228. state->noise_cal.cpNexpOfs = 40;
  2229. state->noise_cal.tdCal2k = -40;
  2230. state->noise_cal.tdCal8k = -24;
  2231. } else {
  2232. /* SAW filter */
  2233. state->noise_cal.cpOpt = 1;
  2234. state->noise_cal.cpNexpOfs = 0;
  2235. state->noise_cal.tdCal2k = -21;
  2236. state->noise_cal.tdCal8k = -24;
  2237. }
  2238. state->m_EcOcRegOcModeLop = (u16) (ulEcOcRegOcModeLop);
  2239. state->chip_adr = (state->config.demod_address << 1) | 1;
  2240. switch (ulHiI2cPatch) {
  2241. case 1:
  2242. state->m_HiI2cPatch = DRXD_HiI2cPatch_1;
  2243. break;
  2244. case 3:
  2245. state->m_HiI2cPatch = DRXD_HiI2cPatch_3;
  2246. break;
  2247. default:
  2248. state->m_HiI2cPatch = NULL;
  2249. }
  2250. /* modify tuner and clock attributes */
  2251. state->intermediate_freq = (u16) (IntermediateFrequency / 1000);
  2252. /* expected system clock frequency in kHz */
  2253. state->expected_sys_clock_freq = 48000;
  2254. /* real system clock frequency in kHz */
  2255. state->sys_clock_freq = 48000;
  2256. state->osc_clock_freq = (u16) ulClock;
  2257. state->osc_clock_deviation = 0;
  2258. state->cscd_state = CSCD_INIT;
  2259. state->drxd_state = DRXD_UNINITIALIZED;
  2260. state->PGA = 0;
  2261. state->type_A = 0;
  2262. state->tuner_mirrors = 0;
  2263. /* modify MPEG output attributes */
  2264. state->insert_rs_byte = state->config.insert_rs_byte;
  2265. state->enable_parallel = (ulSerialMode != 1);
  2266. /* Timing div, 250ns/Psys */
  2267. /* Timing div, = ( delay (nano seconds) * sysclk (kHz) )/ 1000 */
  2268. state->hi_cfg_timing_div = (u16) ((state->sys_clock_freq / 1000) *
  2269. ulHiI2cDelay) / 1000;
  2270. /* Bridge delay, uses oscilator clock */
  2271. /* Delay = ( delay (nano seconds) * oscclk (kHz) )/ 1000 */
  2272. state->hi_cfg_bridge_delay = (u16) ((state->osc_clock_freq / 1000) *
  2273. ulHiI2cBridgeDelay) / 1000;
  2274. state->m_FeAgRegAgPwd = DRXD_DEF_AG_PWD_CONSUMER;
  2275. /* state->m_FeAgRegAgPwd = DRXD_DEF_AG_PWD_PRO; */
  2276. state->m_FeAgRegAgAgcSio = DRXD_DEF_AG_AGC_SIO;
  2277. return 0;
  2278. }
  2279. static int DRXD_init(struct drxd_state *state, const u8 *fw, u32 fw_size)
  2280. {
  2281. int status = 0;
  2282. u32 driverVersion;
  2283. if (state->init_done)
  2284. return 0;
  2285. CDRXD(state, state->config.IF ? state->config.IF : 36000000);
  2286. do {
  2287. state->operation_mode = OM_Default;
  2288. status = SetDeviceTypeId(state);
  2289. if (status < 0)
  2290. break;
  2291. /* Apply I2c address patch to B1 */
  2292. if (!state->type_A && state->m_HiI2cPatch != NULL) {
  2293. status = WriteTable(state, state->m_HiI2cPatch);
  2294. if (status < 0)
  2295. break;
  2296. }
  2297. if (state->type_A) {
  2298. /* HI firmware patch for UIO readout,
  2299. avoid clearing of result register */
  2300. status = Write16(state, 0x43012D, 0x047f, 0);
  2301. if (status < 0)
  2302. break;
  2303. }
  2304. status = HI_ResetCommand(state);
  2305. if (status < 0)
  2306. break;
  2307. status = StopAllProcessors(state);
  2308. if (status < 0)
  2309. break;
  2310. status = InitCC(state);
  2311. if (status < 0)
  2312. break;
  2313. state->osc_clock_deviation = 0;
  2314. if (state->config.osc_deviation)
  2315. state->osc_clock_deviation =
  2316. state->config.osc_deviation(state->priv, 0, 0);
  2317. {
  2318. /* Handle clock deviation */
  2319. s32 devB;
  2320. s32 devA = (s32) (state->osc_clock_deviation) *
  2321. (s32) (state->expected_sys_clock_freq);
  2322. /* deviation in kHz */
  2323. s32 deviation = (devA / (1000000L));
  2324. /* rounding, signed */
  2325. if (devA > 0)
  2326. devB = (2);
  2327. else
  2328. devB = (-2);
  2329. if ((devB * (devA % 1000000L) > 1000000L)) {
  2330. /* add +1 or -1 */
  2331. deviation += (devB / 2);
  2332. }
  2333. state->sys_clock_freq =
  2334. (u16) ((state->expected_sys_clock_freq) +
  2335. deviation);
  2336. }
  2337. status = InitHI(state);
  2338. if (status < 0)
  2339. break;
  2340. status = InitAtomicRead(state);
  2341. if (status < 0)
  2342. break;
  2343. status = EnableAndResetMB(state);
  2344. if (status < 0)
  2345. break;
  2346. if (state->type_A) {
  2347. status = ResetCEFR(state);
  2348. if (status < 0)
  2349. break;
  2350. }
  2351. if (fw) {
  2352. status = DownloadMicrocode(state, fw, fw_size);
  2353. if (status < 0)
  2354. break;
  2355. } else {
  2356. status = DownloadMicrocode(state, state->microcode, state->microcode_length);
  2357. if (status < 0)
  2358. break;
  2359. }
  2360. if (state->PGA) {
  2361. state->m_FeAgRegAgPwd = DRXD_DEF_AG_PWD_PRO;
  2362. SetCfgPga(state, 0); /* PGA = 0 dB */
  2363. } else {
  2364. state->m_FeAgRegAgPwd = DRXD_DEF_AG_PWD_CONSUMER;
  2365. }
  2366. state->m_FeAgRegAgAgcSio = DRXD_DEF_AG_AGC_SIO;
  2367. status = InitFE(state);
  2368. if (status < 0)
  2369. break;
  2370. status = InitFT(state);
  2371. if (status < 0)
  2372. break;
  2373. status = InitCP(state);
  2374. if (status < 0)
  2375. break;
  2376. status = InitCE(state);
  2377. if (status < 0)
  2378. break;
  2379. status = InitEQ(state);
  2380. if (status < 0)
  2381. break;
  2382. status = InitEC(state);
  2383. if (status < 0)
  2384. break;
  2385. status = InitSC(state);
  2386. if (status < 0)
  2387. break;
  2388. status = SetCfgIfAgc(state, &state->if_agc_cfg);
  2389. if (status < 0)
  2390. break;
  2391. status = SetCfgRfAgc(state, &state->rf_agc_cfg);
  2392. if (status < 0)
  2393. break;
  2394. state->cscd_state = CSCD_INIT;
  2395. status = Write16(state, SC_COMM_EXEC__A, SC_COMM_EXEC_CTL_STOP, 0);
  2396. if (status < 0)
  2397. break;
  2398. status = Write16(state, LC_COMM_EXEC__A, SC_COMM_EXEC_CTL_STOP, 0);
  2399. if (status < 0)
  2400. break;
  2401. driverVersion = (((VERSION_MAJOR / 10) << 4) +
  2402. (VERSION_MAJOR % 10)) << 24;
  2403. driverVersion += (((VERSION_MINOR / 10) << 4) +
  2404. (VERSION_MINOR % 10)) << 16;
  2405. driverVersion += ((VERSION_PATCH / 1000) << 12) +
  2406. ((VERSION_PATCH / 100) << 8) +
  2407. ((VERSION_PATCH / 10) << 4) + (VERSION_PATCH % 10);
  2408. status = Write32(state, SC_RA_RAM_DRIVER_VERSION__AX, driverVersion, 0);
  2409. if (status < 0)
  2410. break;
  2411. status = StopOC(state);
  2412. if (status < 0)
  2413. break;
  2414. state->drxd_state = DRXD_STOPPED;
  2415. state->init_done = 1;
  2416. status = 0;
  2417. } while (0);
  2418. return status;
  2419. }
  2420. static int DRXD_status(struct drxd_state *state, u32 *pLockStatus)
  2421. {
  2422. DRX_GetLockStatus(state, pLockStatus);
  2423. /*if (*pLockStatus&DRX_LOCK_MPEG) */
  2424. if (*pLockStatus & DRX_LOCK_FEC) {
  2425. ConfigureMPEGOutput(state, 1);
  2426. /* Get status again, in case we have MPEG lock now */
  2427. /*DRX_GetLockStatus(state, pLockStatus); */
  2428. }
  2429. return 0;
  2430. }
  2431. /****************************************************************************/
  2432. /****************************************************************************/
  2433. /****************************************************************************/
  2434. static int drxd_read_signal_strength(struct dvb_frontend *fe, u16 * strength)
  2435. {
  2436. struct drxd_state *state = fe->demodulator_priv;
  2437. u32 value;
  2438. int res;
  2439. res = ReadIFAgc(state, &value);
  2440. if (res < 0)
  2441. *strength = 0;
  2442. else
  2443. *strength = 0xffff - (value << 4);
  2444. return 0;
  2445. }
  2446. static int drxd_read_status(struct dvb_frontend *fe, enum fe_status *status)
  2447. {
  2448. struct drxd_state *state = fe->demodulator_priv;
  2449. u32 lock;
  2450. DRXD_status(state, &lock);
  2451. *status = 0;
  2452. /* No MPEG lock in V255 firmware, bug ? */
  2453. #if 1
  2454. if (lock & DRX_LOCK_MPEG)
  2455. *status |= FE_HAS_LOCK;
  2456. #else
  2457. if (lock & DRX_LOCK_FEC)
  2458. *status |= FE_HAS_LOCK;
  2459. #endif
  2460. if (lock & DRX_LOCK_FEC)
  2461. *status |= FE_HAS_VITERBI | FE_HAS_SYNC;
  2462. if (lock & DRX_LOCK_DEMOD)
  2463. *status |= FE_HAS_CARRIER | FE_HAS_SIGNAL;
  2464. return 0;
  2465. }
  2466. static int drxd_init(struct dvb_frontend *fe)
  2467. {
  2468. struct drxd_state *state = fe->demodulator_priv;
  2469. return DRXD_init(state, NULL, 0);
  2470. }
  2471. static int drxd_config_i2c(struct dvb_frontend *fe, int onoff)
  2472. {
  2473. struct drxd_state *state = fe->demodulator_priv;
  2474. if (state->config.disable_i2c_gate_ctrl == 1)
  2475. return 0;
  2476. return DRX_ConfigureI2CBridge(state, onoff);
  2477. }
  2478. static int drxd_get_tune_settings(struct dvb_frontend *fe,
  2479. struct dvb_frontend_tune_settings *sets)
  2480. {
  2481. sets->min_delay_ms = 10000;
  2482. sets->max_drift = 0;
  2483. sets->step_size = 0;
  2484. return 0;
  2485. }
  2486. static int drxd_read_ber(struct dvb_frontend *fe, u32 * ber)
  2487. {
  2488. *ber = 0;
  2489. return 0;
  2490. }
  2491. static int drxd_read_snr(struct dvb_frontend *fe, u16 * snr)
  2492. {
  2493. *snr = 0;
  2494. return 0;
  2495. }
  2496. static int drxd_read_ucblocks(struct dvb_frontend *fe, u32 * ucblocks)
  2497. {
  2498. *ucblocks = 0;
  2499. return 0;
  2500. }
  2501. static int drxd_sleep(struct dvb_frontend *fe)
  2502. {
  2503. struct drxd_state *state = fe->demodulator_priv;
  2504. ConfigureMPEGOutput(state, 0);
  2505. return 0;
  2506. }
  2507. static int drxd_i2c_gate_ctrl(struct dvb_frontend *fe, int enable)
  2508. {
  2509. return drxd_config_i2c(fe, enable);
  2510. }
  2511. static int drxd_set_frontend(struct dvb_frontend *fe)
  2512. {
  2513. struct dtv_frontend_properties *p = &fe->dtv_property_cache;
  2514. struct drxd_state *state = fe->demodulator_priv;
  2515. s32 off = 0;
  2516. state->props = *p;
  2517. DRX_Stop(state);
  2518. if (fe->ops.tuner_ops.set_params) {
  2519. fe->ops.tuner_ops.set_params(fe);
  2520. if (fe->ops.i2c_gate_ctrl)
  2521. fe->ops.i2c_gate_ctrl(fe, 0);
  2522. }
  2523. msleep(200);
  2524. return DRX_Start(state, off);
  2525. }
  2526. static void drxd_release(struct dvb_frontend *fe)
  2527. {
  2528. struct drxd_state *state = fe->demodulator_priv;
  2529. kfree(state);
  2530. }
  2531. static struct dvb_frontend_ops drxd_ops = {
  2532. .delsys = { SYS_DVBT},
  2533. .info = {
  2534. .name = "Micronas DRXD DVB-T",
  2535. .frequency_min = 47125000,
  2536. .frequency_max = 855250000,
  2537. .frequency_stepsize = 166667,
  2538. .frequency_tolerance = 0,
  2539. .caps = FE_CAN_FEC_1_2 | FE_CAN_FEC_2_3 |
  2540. FE_CAN_FEC_3_4 | FE_CAN_FEC_5_6 | FE_CAN_FEC_7_8 |
  2541. FE_CAN_FEC_AUTO |
  2542. FE_CAN_QAM_16 | FE_CAN_QAM_64 |
  2543. FE_CAN_QAM_AUTO |
  2544. FE_CAN_TRANSMISSION_MODE_AUTO |
  2545. FE_CAN_GUARD_INTERVAL_AUTO |
  2546. FE_CAN_HIERARCHY_AUTO | FE_CAN_RECOVER | FE_CAN_MUTE_TS},
  2547. .release = drxd_release,
  2548. .init = drxd_init,
  2549. .sleep = drxd_sleep,
  2550. .i2c_gate_ctrl = drxd_i2c_gate_ctrl,
  2551. .set_frontend = drxd_set_frontend,
  2552. .get_tune_settings = drxd_get_tune_settings,
  2553. .read_status = drxd_read_status,
  2554. .read_ber = drxd_read_ber,
  2555. .read_signal_strength = drxd_read_signal_strength,
  2556. .read_snr = drxd_read_snr,
  2557. .read_ucblocks = drxd_read_ucblocks,
  2558. };
  2559. struct dvb_frontend *drxd_attach(const struct drxd_config *config,
  2560. void *priv, struct i2c_adapter *i2c,
  2561. struct device *dev)
  2562. {
  2563. struct drxd_state *state = NULL;
  2564. state = kzalloc(sizeof(*state), GFP_KERNEL);
  2565. if (!state)
  2566. return NULL;
  2567. state->ops = drxd_ops;
  2568. state->dev = dev;
  2569. state->config = *config;
  2570. state->i2c = i2c;
  2571. state->priv = priv;
  2572. mutex_init(&state->mutex);
  2573. if (Read16(state, 0, NULL, 0) < 0)
  2574. goto error;
  2575. state->frontend.ops = drxd_ops;
  2576. state->frontend.demodulator_priv = state;
  2577. ConfigureMPEGOutput(state, 0);
  2578. /* add few initialization to allow gate control */
  2579. CDRXD(state, state->config.IF ? state->config.IF : 36000000);
  2580. InitHI(state);
  2581. return &state->frontend;
  2582. error:
  2583. printk(KERN_ERR "drxd: not found\n");
  2584. kfree(state);
  2585. return NULL;
  2586. }
  2587. EXPORT_SYMBOL(drxd_attach);
  2588. MODULE_DESCRIPTION("DRXD driver");
  2589. MODULE_AUTHOR("Micronas");
  2590. MODULE_LICENSE("GPL");