lgdt3306a.c 52 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145
  1. /*
  2. * Support for LGDT3306A - 8VSB/QAM-B
  3. *
  4. * Copyright (C) 2013 Fred Richter <frichter@hauppauge.com>
  5. * - driver structure based on lgdt3305.[ch] by Michael Krufky
  6. * - code based on LG3306_V0.35 API by LG Electronics Inc.
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. */
  18. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  19. #include <asm/div64.h>
  20. #include <linux/dvb/frontend.h>
  21. #include "dvb_math.h"
  22. #include "lgdt3306a.h"
  23. static int debug;
  24. module_param(debug, int, 0644);
  25. MODULE_PARM_DESC(debug, "set debug level (info=1, reg=2 (or-able))");
  26. #define DBG_INFO 1
  27. #define DBG_REG 2
  28. #define DBG_DUMP 4 /* FGR - comment out to remove dump code */
  29. #define lg_debug(fmt, arg...) \
  30. printk(KERN_DEBUG pr_fmt(fmt), ## arg)
  31. #define dbg_info(fmt, arg...) \
  32. do { \
  33. if (debug & DBG_INFO) \
  34. lg_debug(fmt, ## arg); \
  35. } while (0)
  36. #define dbg_reg(fmt, arg...) \
  37. do { \
  38. if (debug & DBG_REG) \
  39. lg_debug(fmt, ## arg); \
  40. } while (0)
  41. #define lg_chkerr(ret) \
  42. ({ \
  43. int __ret; \
  44. __ret = (ret < 0); \
  45. if (__ret) \
  46. pr_err("error %d on line %d\n", ret, __LINE__); \
  47. __ret; \
  48. })
  49. struct lgdt3306a_state {
  50. struct i2c_adapter *i2c_adap;
  51. const struct lgdt3306a_config *cfg;
  52. struct dvb_frontend frontend;
  53. enum fe_modulation current_modulation;
  54. u32 current_frequency;
  55. u32 snr;
  56. };
  57. /*
  58. * LG3306A Register Usage
  59. * (LG does not really name the registers, so this code does not either)
  60. *
  61. * 0000 -> 00FF Common control and status
  62. * 1000 -> 10FF Synchronizer control and status
  63. * 1F00 -> 1FFF Smart Antenna control and status
  64. * 2100 -> 21FF VSB Equalizer control and status
  65. * 2800 -> 28FF QAM Equalizer control and status
  66. * 3000 -> 30FF FEC control and status
  67. */
  68. enum lgdt3306a_lock_status {
  69. LG3306_UNLOCK = 0x00,
  70. LG3306_LOCK = 0x01,
  71. LG3306_UNKNOWN_LOCK = 0xff
  72. };
  73. enum lgdt3306a_neverlock_status {
  74. LG3306_NL_INIT = 0x00,
  75. LG3306_NL_PROCESS = 0x01,
  76. LG3306_NL_LOCK = 0x02,
  77. LG3306_NL_FAIL = 0x03,
  78. LG3306_NL_UNKNOWN = 0xff
  79. };
  80. enum lgdt3306a_modulation {
  81. LG3306_VSB = 0x00,
  82. LG3306_QAM64 = 0x01,
  83. LG3306_QAM256 = 0x02,
  84. LG3306_UNKNOWN_MODE = 0xff
  85. };
  86. enum lgdt3306a_lock_check {
  87. LG3306_SYNC_LOCK,
  88. LG3306_FEC_LOCK,
  89. LG3306_TR_LOCK,
  90. LG3306_AGC_LOCK,
  91. };
  92. #ifdef DBG_DUMP
  93. static void lgdt3306a_DumpAllRegs(struct lgdt3306a_state *state);
  94. static void lgdt3306a_DumpRegs(struct lgdt3306a_state *state);
  95. #endif
  96. static int lgdt3306a_write_reg(struct lgdt3306a_state *state, u16 reg, u8 val)
  97. {
  98. int ret;
  99. u8 buf[] = { reg >> 8, reg & 0xff, val };
  100. struct i2c_msg msg = {
  101. .addr = state->cfg->i2c_addr, .flags = 0,
  102. .buf = buf, .len = 3,
  103. };
  104. dbg_reg("reg: 0x%04x, val: 0x%02x\n", reg, val);
  105. ret = i2c_transfer(state->i2c_adap, &msg, 1);
  106. if (ret != 1) {
  107. pr_err("error (addr %02x %02x <- %02x, err = %i)\n",
  108. msg.buf[0], msg.buf[1], msg.buf[2], ret);
  109. if (ret < 0)
  110. return ret;
  111. else
  112. return -EREMOTEIO;
  113. }
  114. return 0;
  115. }
  116. static int lgdt3306a_read_reg(struct lgdt3306a_state *state, u16 reg, u8 *val)
  117. {
  118. int ret;
  119. u8 reg_buf[] = { reg >> 8, reg & 0xff };
  120. struct i2c_msg msg[] = {
  121. { .addr = state->cfg->i2c_addr,
  122. .flags = 0, .buf = reg_buf, .len = 2 },
  123. { .addr = state->cfg->i2c_addr,
  124. .flags = I2C_M_RD, .buf = val, .len = 1 },
  125. };
  126. ret = i2c_transfer(state->i2c_adap, msg, 2);
  127. if (ret != 2) {
  128. pr_err("error (addr %02x reg %04x error (ret == %i)\n",
  129. state->cfg->i2c_addr, reg, ret);
  130. if (ret < 0)
  131. return ret;
  132. else
  133. return -EREMOTEIO;
  134. }
  135. dbg_reg("reg: 0x%04x, val: 0x%02x\n", reg, *val);
  136. return 0;
  137. }
  138. #define read_reg(state, reg) \
  139. ({ \
  140. u8 __val; \
  141. int ret = lgdt3306a_read_reg(state, reg, &__val); \
  142. if (lg_chkerr(ret)) \
  143. __val = 0; \
  144. __val; \
  145. })
  146. static int lgdt3306a_set_reg_bit(struct lgdt3306a_state *state,
  147. u16 reg, int bit, int onoff)
  148. {
  149. u8 val;
  150. int ret;
  151. dbg_reg("reg: 0x%04x, bit: %d, level: %d\n", reg, bit, onoff);
  152. ret = lgdt3306a_read_reg(state, reg, &val);
  153. if (lg_chkerr(ret))
  154. goto fail;
  155. val &= ~(1 << bit);
  156. val |= (onoff & 1) << bit;
  157. ret = lgdt3306a_write_reg(state, reg, val);
  158. lg_chkerr(ret);
  159. fail:
  160. return ret;
  161. }
  162. /* ------------------------------------------------------------------------ */
  163. static int lgdt3306a_soft_reset(struct lgdt3306a_state *state)
  164. {
  165. int ret;
  166. dbg_info("\n");
  167. ret = lgdt3306a_set_reg_bit(state, 0x0000, 7, 0);
  168. if (lg_chkerr(ret))
  169. goto fail;
  170. msleep(20);
  171. ret = lgdt3306a_set_reg_bit(state, 0x0000, 7, 1);
  172. lg_chkerr(ret);
  173. fail:
  174. return ret;
  175. }
  176. static int lgdt3306a_mpeg_mode(struct lgdt3306a_state *state,
  177. enum lgdt3306a_mpeg_mode mode)
  178. {
  179. u8 val;
  180. int ret;
  181. dbg_info("(%d)\n", mode);
  182. /* transport packet format - TPSENB=0x80 */
  183. ret = lgdt3306a_set_reg_bit(state, 0x0071, 7,
  184. mode == LGDT3306A_MPEG_PARALLEL ? 1 : 0);
  185. if (lg_chkerr(ret))
  186. goto fail;
  187. /*
  188. * start of packet signal duration
  189. * TPSSOPBITEN=0x40; 0=byte duration, 1=bit duration
  190. */
  191. ret = lgdt3306a_set_reg_bit(state, 0x0071, 6, 0);
  192. if (lg_chkerr(ret))
  193. goto fail;
  194. ret = lgdt3306a_read_reg(state, 0x0070, &val);
  195. if (lg_chkerr(ret))
  196. goto fail;
  197. val |= 0x10; /* TPCLKSUPB=0x10 */
  198. if (mode == LGDT3306A_MPEG_PARALLEL)
  199. val &= ~0x10;
  200. ret = lgdt3306a_write_reg(state, 0x0070, val);
  201. lg_chkerr(ret);
  202. fail:
  203. return ret;
  204. }
  205. static int lgdt3306a_mpeg_mode_polarity(struct lgdt3306a_state *state,
  206. enum lgdt3306a_tp_clock_edge edge,
  207. enum lgdt3306a_tp_valid_polarity valid)
  208. {
  209. u8 val;
  210. int ret;
  211. dbg_info("edge=%d, valid=%d\n", edge, valid);
  212. ret = lgdt3306a_read_reg(state, 0x0070, &val);
  213. if (lg_chkerr(ret))
  214. goto fail;
  215. val &= ~0x06; /* TPCLKPOL=0x04, TPVALPOL=0x02 */
  216. if (edge == LGDT3306A_TPCLK_RISING_EDGE)
  217. val |= 0x04;
  218. if (valid == LGDT3306A_TP_VALID_HIGH)
  219. val |= 0x02;
  220. ret = lgdt3306a_write_reg(state, 0x0070, val);
  221. lg_chkerr(ret);
  222. fail:
  223. return ret;
  224. }
  225. static int lgdt3306a_mpeg_tristate(struct lgdt3306a_state *state,
  226. int mode)
  227. {
  228. u8 val;
  229. int ret;
  230. dbg_info("(%d)\n", mode);
  231. if (mode) {
  232. ret = lgdt3306a_read_reg(state, 0x0070, &val);
  233. if (lg_chkerr(ret))
  234. goto fail;
  235. /*
  236. * Tristate bus; TPOUTEN=0x80, TPCLKOUTEN=0x20,
  237. * TPDATAOUTEN=0x08
  238. */
  239. val &= ~0xa8;
  240. ret = lgdt3306a_write_reg(state, 0x0070, val);
  241. if (lg_chkerr(ret))
  242. goto fail;
  243. /* AGCIFOUTENB=0x40; 1=Disable IFAGC pin */
  244. ret = lgdt3306a_set_reg_bit(state, 0x0003, 6, 1);
  245. if (lg_chkerr(ret))
  246. goto fail;
  247. } else {
  248. /* enable IFAGC pin */
  249. ret = lgdt3306a_set_reg_bit(state, 0x0003, 6, 0);
  250. if (lg_chkerr(ret))
  251. goto fail;
  252. ret = lgdt3306a_read_reg(state, 0x0070, &val);
  253. if (lg_chkerr(ret))
  254. goto fail;
  255. val |= 0xa8; /* enable bus */
  256. ret = lgdt3306a_write_reg(state, 0x0070, val);
  257. if (lg_chkerr(ret))
  258. goto fail;
  259. }
  260. fail:
  261. return ret;
  262. }
  263. static int lgdt3306a_ts_bus_ctrl(struct dvb_frontend *fe, int acquire)
  264. {
  265. struct lgdt3306a_state *state = fe->demodulator_priv;
  266. dbg_info("acquire=%d\n", acquire);
  267. return lgdt3306a_mpeg_tristate(state, acquire ? 0 : 1);
  268. }
  269. static int lgdt3306a_power(struct lgdt3306a_state *state,
  270. int mode)
  271. {
  272. int ret;
  273. dbg_info("(%d)\n", mode);
  274. if (mode == 0) {
  275. /* into reset */
  276. ret = lgdt3306a_set_reg_bit(state, 0x0000, 7, 0);
  277. if (lg_chkerr(ret))
  278. goto fail;
  279. /* power down */
  280. ret = lgdt3306a_set_reg_bit(state, 0x0000, 0, 0);
  281. if (lg_chkerr(ret))
  282. goto fail;
  283. } else {
  284. /* out of reset */
  285. ret = lgdt3306a_set_reg_bit(state, 0x0000, 7, 1);
  286. if (lg_chkerr(ret))
  287. goto fail;
  288. /* power up */
  289. ret = lgdt3306a_set_reg_bit(state, 0x0000, 0, 1);
  290. if (lg_chkerr(ret))
  291. goto fail;
  292. }
  293. #ifdef DBG_DUMP
  294. lgdt3306a_DumpAllRegs(state);
  295. #endif
  296. fail:
  297. return ret;
  298. }
  299. static int lgdt3306a_set_vsb(struct lgdt3306a_state *state)
  300. {
  301. u8 val;
  302. int ret;
  303. dbg_info("\n");
  304. /* 0. Spectrum inversion detection manual; spectrum inverted */
  305. ret = lgdt3306a_read_reg(state, 0x0002, &val);
  306. val &= 0xf7; /* SPECINVAUTO Off */
  307. val |= 0x04; /* SPECINV On */
  308. ret = lgdt3306a_write_reg(state, 0x0002, val);
  309. if (lg_chkerr(ret))
  310. goto fail;
  311. /* 1. Selection of standard mode(0x08=QAM, 0x80=VSB) */
  312. ret = lgdt3306a_write_reg(state, 0x0008, 0x80);
  313. if (lg_chkerr(ret))
  314. goto fail;
  315. /* 2. Bandwidth mode for VSB(6MHz) */
  316. ret = lgdt3306a_read_reg(state, 0x0009, &val);
  317. val &= 0xe3;
  318. val |= 0x0c; /* STDOPDETTMODE[2:0]=3 */
  319. ret = lgdt3306a_write_reg(state, 0x0009, val);
  320. if (lg_chkerr(ret))
  321. goto fail;
  322. /* 3. QAM mode detection mode(None) */
  323. ret = lgdt3306a_read_reg(state, 0x0009, &val);
  324. val &= 0xfc; /* STDOPDETCMODE[1:0]=0 */
  325. ret = lgdt3306a_write_reg(state, 0x0009, val);
  326. if (lg_chkerr(ret))
  327. goto fail;
  328. /* 4. ADC sampling frequency rate(2x sampling) */
  329. ret = lgdt3306a_read_reg(state, 0x000d, &val);
  330. val &= 0xbf; /* SAMPLING4XFEN=0 */
  331. ret = lgdt3306a_write_reg(state, 0x000d, val);
  332. if (lg_chkerr(ret))
  333. goto fail;
  334. #if 0
  335. /* FGR - disable any AICC filtering, testing only */
  336. ret = lgdt3306a_write_reg(state, 0x0024, 0x00);
  337. if (lg_chkerr(ret))
  338. goto fail;
  339. /* AICCFIXFREQ0 NT N-1(Video rejection) */
  340. ret = lgdt3306a_write_reg(state, 0x002e, 0x00);
  341. ret = lgdt3306a_write_reg(state, 0x002f, 0x00);
  342. ret = lgdt3306a_write_reg(state, 0x0030, 0x00);
  343. /* AICCFIXFREQ1 NT N-1(Audio rejection) */
  344. ret = lgdt3306a_write_reg(state, 0x002b, 0x00);
  345. ret = lgdt3306a_write_reg(state, 0x002c, 0x00);
  346. ret = lgdt3306a_write_reg(state, 0x002d, 0x00);
  347. /* AICCFIXFREQ2 NT Co-Channel(Video rejection) */
  348. ret = lgdt3306a_write_reg(state, 0x0028, 0x00);
  349. ret = lgdt3306a_write_reg(state, 0x0029, 0x00);
  350. ret = lgdt3306a_write_reg(state, 0x002a, 0x00);
  351. /* AICCFIXFREQ3 NT Co-Channel(Audio rejection) */
  352. ret = lgdt3306a_write_reg(state, 0x0025, 0x00);
  353. ret = lgdt3306a_write_reg(state, 0x0026, 0x00);
  354. ret = lgdt3306a_write_reg(state, 0x0027, 0x00);
  355. #else
  356. /* FGR - this works well for HVR-1955,1975 */
  357. /* 5. AICCOPMODE NT N-1 Adj. */
  358. ret = lgdt3306a_write_reg(state, 0x0024, 0x5A);
  359. if (lg_chkerr(ret))
  360. goto fail;
  361. /* AICCFIXFREQ0 NT N-1(Video rejection) */
  362. ret = lgdt3306a_write_reg(state, 0x002e, 0x5A);
  363. ret = lgdt3306a_write_reg(state, 0x002f, 0x00);
  364. ret = lgdt3306a_write_reg(state, 0x0030, 0x00);
  365. /* AICCFIXFREQ1 NT N-1(Audio rejection) */
  366. ret = lgdt3306a_write_reg(state, 0x002b, 0x36);
  367. ret = lgdt3306a_write_reg(state, 0x002c, 0x00);
  368. ret = lgdt3306a_write_reg(state, 0x002d, 0x00);
  369. /* AICCFIXFREQ2 NT Co-Channel(Video rejection) */
  370. ret = lgdt3306a_write_reg(state, 0x0028, 0x2A);
  371. ret = lgdt3306a_write_reg(state, 0x0029, 0x00);
  372. ret = lgdt3306a_write_reg(state, 0x002a, 0x00);
  373. /* AICCFIXFREQ3 NT Co-Channel(Audio rejection) */
  374. ret = lgdt3306a_write_reg(state, 0x0025, 0x06);
  375. ret = lgdt3306a_write_reg(state, 0x0026, 0x00);
  376. ret = lgdt3306a_write_reg(state, 0x0027, 0x00);
  377. #endif
  378. ret = lgdt3306a_read_reg(state, 0x001e, &val);
  379. val &= 0x0f;
  380. val |= 0xa0;
  381. ret = lgdt3306a_write_reg(state, 0x001e, val);
  382. ret = lgdt3306a_write_reg(state, 0x0022, 0x08);
  383. ret = lgdt3306a_write_reg(state, 0x0023, 0xFF);
  384. ret = lgdt3306a_read_reg(state, 0x211f, &val);
  385. val &= 0xef;
  386. ret = lgdt3306a_write_reg(state, 0x211f, val);
  387. ret = lgdt3306a_write_reg(state, 0x2173, 0x01);
  388. ret = lgdt3306a_read_reg(state, 0x1061, &val);
  389. val &= 0xf8;
  390. val |= 0x04;
  391. ret = lgdt3306a_write_reg(state, 0x1061, val);
  392. ret = lgdt3306a_read_reg(state, 0x103d, &val);
  393. val &= 0xcf;
  394. ret = lgdt3306a_write_reg(state, 0x103d, val);
  395. ret = lgdt3306a_write_reg(state, 0x2122, 0x40);
  396. ret = lgdt3306a_read_reg(state, 0x2141, &val);
  397. val &= 0x3f;
  398. ret = lgdt3306a_write_reg(state, 0x2141, val);
  399. ret = lgdt3306a_read_reg(state, 0x2135, &val);
  400. val &= 0x0f;
  401. val |= 0x70;
  402. ret = lgdt3306a_write_reg(state, 0x2135, val);
  403. ret = lgdt3306a_read_reg(state, 0x0003, &val);
  404. val &= 0xf7;
  405. ret = lgdt3306a_write_reg(state, 0x0003, val);
  406. ret = lgdt3306a_read_reg(state, 0x001c, &val);
  407. val &= 0x7f;
  408. ret = lgdt3306a_write_reg(state, 0x001c, val);
  409. /* 6. EQ step size */
  410. ret = lgdt3306a_read_reg(state, 0x2179, &val);
  411. val &= 0xf8;
  412. ret = lgdt3306a_write_reg(state, 0x2179, val);
  413. ret = lgdt3306a_read_reg(state, 0x217a, &val);
  414. val &= 0xf8;
  415. ret = lgdt3306a_write_reg(state, 0x217a, val);
  416. /* 7. Reset */
  417. ret = lgdt3306a_soft_reset(state);
  418. if (lg_chkerr(ret))
  419. goto fail;
  420. dbg_info("complete\n");
  421. fail:
  422. return ret;
  423. }
  424. static int lgdt3306a_set_qam(struct lgdt3306a_state *state, int modulation)
  425. {
  426. u8 val;
  427. int ret;
  428. dbg_info("modulation=%d\n", modulation);
  429. /* 1. Selection of standard mode(0x08=QAM, 0x80=VSB) */
  430. ret = lgdt3306a_write_reg(state, 0x0008, 0x08);
  431. if (lg_chkerr(ret))
  432. goto fail;
  433. /* 1a. Spectrum inversion detection to Auto */
  434. ret = lgdt3306a_read_reg(state, 0x0002, &val);
  435. val &= 0xfb; /* SPECINV Off */
  436. val |= 0x08; /* SPECINVAUTO On */
  437. ret = lgdt3306a_write_reg(state, 0x0002, val);
  438. if (lg_chkerr(ret))
  439. goto fail;
  440. /* 2. Bandwidth mode for QAM */
  441. ret = lgdt3306a_read_reg(state, 0x0009, &val);
  442. val &= 0xe3; /* STDOPDETTMODE[2:0]=0 VSB Off */
  443. ret = lgdt3306a_write_reg(state, 0x0009, val);
  444. if (lg_chkerr(ret))
  445. goto fail;
  446. /* 3. : 64QAM/256QAM detection(manual, auto) */
  447. ret = lgdt3306a_read_reg(state, 0x0009, &val);
  448. val &= 0xfc;
  449. val |= 0x02; /* STDOPDETCMODE[1:0]=1=Manual 2=Auto */
  450. ret = lgdt3306a_write_reg(state, 0x0009, val);
  451. if (lg_chkerr(ret))
  452. goto fail;
  453. /* 3a. : 64QAM/256QAM selection for manual */
  454. ret = lgdt3306a_read_reg(state, 0x101a, &val);
  455. val &= 0xf8;
  456. if (modulation == QAM_64)
  457. val |= 0x02; /* QMDQMODE[2:0]=2=QAM64 */
  458. else
  459. val |= 0x04; /* QMDQMODE[2:0]=4=QAM256 */
  460. ret = lgdt3306a_write_reg(state, 0x101a, val);
  461. if (lg_chkerr(ret))
  462. goto fail;
  463. /* 4. ADC sampling frequency rate(4x sampling) */
  464. ret = lgdt3306a_read_reg(state, 0x000d, &val);
  465. val &= 0xbf;
  466. val |= 0x40; /* SAMPLING4XFEN=1 */
  467. ret = lgdt3306a_write_reg(state, 0x000d, val);
  468. if (lg_chkerr(ret))
  469. goto fail;
  470. /* 5. No AICC operation in QAM mode */
  471. ret = lgdt3306a_read_reg(state, 0x0024, &val);
  472. val &= 0x00;
  473. ret = lgdt3306a_write_reg(state, 0x0024, val);
  474. if (lg_chkerr(ret))
  475. goto fail;
  476. /* 6. Reset */
  477. ret = lgdt3306a_soft_reset(state);
  478. if (lg_chkerr(ret))
  479. goto fail;
  480. dbg_info("complete\n");
  481. fail:
  482. return ret;
  483. }
  484. static int lgdt3306a_set_modulation(struct lgdt3306a_state *state,
  485. struct dtv_frontend_properties *p)
  486. {
  487. int ret;
  488. dbg_info("\n");
  489. switch (p->modulation) {
  490. case VSB_8:
  491. ret = lgdt3306a_set_vsb(state);
  492. break;
  493. case QAM_64:
  494. ret = lgdt3306a_set_qam(state, QAM_64);
  495. break;
  496. case QAM_256:
  497. ret = lgdt3306a_set_qam(state, QAM_256);
  498. break;
  499. default:
  500. return -EINVAL;
  501. }
  502. if (lg_chkerr(ret))
  503. goto fail;
  504. state->current_modulation = p->modulation;
  505. fail:
  506. return ret;
  507. }
  508. /* ------------------------------------------------------------------------ */
  509. static int lgdt3306a_agc_setup(struct lgdt3306a_state *state,
  510. struct dtv_frontend_properties *p)
  511. {
  512. /* TODO: anything we want to do here??? */
  513. dbg_info("\n");
  514. switch (p->modulation) {
  515. case VSB_8:
  516. break;
  517. case QAM_64:
  518. case QAM_256:
  519. break;
  520. default:
  521. return -EINVAL;
  522. }
  523. return 0;
  524. }
  525. /* ------------------------------------------------------------------------ */
  526. static int lgdt3306a_set_inversion(struct lgdt3306a_state *state,
  527. int inversion)
  528. {
  529. int ret;
  530. dbg_info("(%d)\n", inversion);
  531. ret = lgdt3306a_set_reg_bit(state, 0x0002, 2, inversion ? 1 : 0);
  532. return ret;
  533. }
  534. static int lgdt3306a_set_inversion_auto(struct lgdt3306a_state *state,
  535. int enabled)
  536. {
  537. int ret;
  538. dbg_info("(%d)\n", enabled);
  539. /* 0=Manual 1=Auto(QAM only) - SPECINVAUTO=0x04 */
  540. ret = lgdt3306a_set_reg_bit(state, 0x0002, 3, enabled);
  541. return ret;
  542. }
  543. static int lgdt3306a_spectral_inversion(struct lgdt3306a_state *state,
  544. struct dtv_frontend_properties *p,
  545. int inversion)
  546. {
  547. int ret = 0;
  548. dbg_info("(%d)\n", inversion);
  549. #if 0
  550. /*
  551. * FGR - spectral_inversion defaults already set for VSB and QAM;
  552. * can enable later if desired
  553. */
  554. ret = lgdt3306a_set_inversion(state, inversion);
  555. switch (p->modulation) {
  556. case VSB_8:
  557. /* Manual only for VSB */
  558. ret = lgdt3306a_set_inversion_auto(state, 0);
  559. break;
  560. case QAM_64:
  561. case QAM_256:
  562. /* Auto ok for QAM */
  563. ret = lgdt3306a_set_inversion_auto(state, 1);
  564. break;
  565. default:
  566. ret = -EINVAL;
  567. }
  568. #endif
  569. return ret;
  570. }
  571. static int lgdt3306a_set_if(struct lgdt3306a_state *state,
  572. struct dtv_frontend_properties *p)
  573. {
  574. int ret;
  575. u16 if_freq_khz;
  576. u8 nco1, nco2;
  577. switch (p->modulation) {
  578. case VSB_8:
  579. if_freq_khz = state->cfg->vsb_if_khz;
  580. break;
  581. case QAM_64:
  582. case QAM_256:
  583. if_freq_khz = state->cfg->qam_if_khz;
  584. break;
  585. default:
  586. return -EINVAL;
  587. }
  588. switch (if_freq_khz) {
  589. default:
  590. pr_warn("IF=%d KHz is not supportted, 3250 assumed\n",
  591. if_freq_khz);
  592. /* fallthrough */
  593. case 3250: /* 3.25Mhz */
  594. nco1 = 0x34;
  595. nco2 = 0x00;
  596. break;
  597. case 3500: /* 3.50Mhz */
  598. nco1 = 0x38;
  599. nco2 = 0x00;
  600. break;
  601. case 4000: /* 4.00Mhz */
  602. nco1 = 0x40;
  603. nco2 = 0x00;
  604. break;
  605. case 5000: /* 5.00Mhz */
  606. nco1 = 0x50;
  607. nco2 = 0x00;
  608. break;
  609. case 5380: /* 5.38Mhz */
  610. nco1 = 0x56;
  611. nco2 = 0x14;
  612. break;
  613. }
  614. ret = lgdt3306a_write_reg(state, 0x0010, nco1);
  615. if (ret)
  616. return ret;
  617. ret = lgdt3306a_write_reg(state, 0x0011, nco2);
  618. if (ret)
  619. return ret;
  620. dbg_info("if_freq=%d KHz->[%04x]\n", if_freq_khz, nco1<<8 | nco2);
  621. return 0;
  622. }
  623. /* ------------------------------------------------------------------------ */
  624. static int lgdt3306a_i2c_gate_ctrl(struct dvb_frontend *fe, int enable)
  625. {
  626. struct lgdt3306a_state *state = fe->demodulator_priv;
  627. if (state->cfg->deny_i2c_rptr) {
  628. dbg_info("deny_i2c_rptr=%d\n", state->cfg->deny_i2c_rptr);
  629. return 0;
  630. }
  631. dbg_info("(%d)\n", enable);
  632. /* NI2CRPTEN=0x80 */
  633. return lgdt3306a_set_reg_bit(state, 0x0002, 7, enable ? 0 : 1);
  634. }
  635. static int lgdt3306a_sleep(struct lgdt3306a_state *state)
  636. {
  637. int ret;
  638. dbg_info("\n");
  639. state->current_frequency = -1; /* force re-tune, when we wake */
  640. ret = lgdt3306a_mpeg_tristate(state, 1); /* disable data bus */
  641. if (lg_chkerr(ret))
  642. goto fail;
  643. ret = lgdt3306a_power(state, 0); /* power down */
  644. lg_chkerr(ret);
  645. fail:
  646. return 0;
  647. }
  648. static int lgdt3306a_fe_sleep(struct dvb_frontend *fe)
  649. {
  650. struct lgdt3306a_state *state = fe->demodulator_priv;
  651. return lgdt3306a_sleep(state);
  652. }
  653. static int lgdt3306a_init(struct dvb_frontend *fe)
  654. {
  655. struct lgdt3306a_state *state = fe->demodulator_priv;
  656. u8 val;
  657. int ret;
  658. dbg_info("\n");
  659. /* 1. Normal operation mode */
  660. ret = lgdt3306a_set_reg_bit(state, 0x0001, 0, 1); /* SIMFASTENB=0x01 */
  661. if (lg_chkerr(ret))
  662. goto fail;
  663. /* 2. Spectrum inversion auto detection (Not valid for VSB) */
  664. ret = lgdt3306a_set_inversion_auto(state, 0);
  665. if (lg_chkerr(ret))
  666. goto fail;
  667. /* 3. Spectrum inversion(According to the tuner configuration) */
  668. ret = lgdt3306a_set_inversion(state, 1);
  669. if (lg_chkerr(ret))
  670. goto fail;
  671. /* 4. Peak-to-peak voltage of ADC input signal */
  672. /* ADCSEL1V=0x80=1Vpp; 0x00=2Vpp */
  673. ret = lgdt3306a_set_reg_bit(state, 0x0004, 7, 1);
  674. if (lg_chkerr(ret))
  675. goto fail;
  676. /* 5. ADC output data capture clock phase */
  677. /* 0=same phase as ADC clock */
  678. ret = lgdt3306a_set_reg_bit(state, 0x0004, 2, 0);
  679. if (lg_chkerr(ret))
  680. goto fail;
  681. /* 5a. ADC sampling clock source */
  682. /* ADCCLKPLLSEL=0x08; 0=use ext clock, not PLL */
  683. ret = lgdt3306a_set_reg_bit(state, 0x0004, 3, 0);
  684. if (lg_chkerr(ret))
  685. goto fail;
  686. /* 6. Automatic PLL set */
  687. /* PLLSETAUTO=0x40; 0=off */
  688. ret = lgdt3306a_set_reg_bit(state, 0x0005, 6, 0);
  689. if (lg_chkerr(ret))
  690. goto fail;
  691. if (state->cfg->xtalMHz == 24) { /* 24MHz */
  692. /* 7. Frequency for PLL output(0x2564 for 192MHz for 24MHz) */
  693. ret = lgdt3306a_read_reg(state, 0x0005, &val);
  694. if (lg_chkerr(ret))
  695. goto fail;
  696. val &= 0xc0;
  697. val |= 0x25;
  698. ret = lgdt3306a_write_reg(state, 0x0005, val);
  699. if (lg_chkerr(ret))
  700. goto fail;
  701. ret = lgdt3306a_write_reg(state, 0x0006, 0x64);
  702. if (lg_chkerr(ret))
  703. goto fail;
  704. /* 8. ADC sampling frequency(0x180000 for 24MHz sampling) */
  705. ret = lgdt3306a_read_reg(state, 0x000d, &val);
  706. if (lg_chkerr(ret))
  707. goto fail;
  708. val &= 0xc0;
  709. val |= 0x18;
  710. ret = lgdt3306a_write_reg(state, 0x000d, val);
  711. if (lg_chkerr(ret))
  712. goto fail;
  713. } else if (state->cfg->xtalMHz == 25) { /* 25MHz */
  714. /* 7. Frequency for PLL output */
  715. ret = lgdt3306a_read_reg(state, 0x0005, &val);
  716. if (lg_chkerr(ret))
  717. goto fail;
  718. val &= 0xc0;
  719. val |= 0x25;
  720. ret = lgdt3306a_write_reg(state, 0x0005, val);
  721. if (lg_chkerr(ret))
  722. goto fail;
  723. ret = lgdt3306a_write_reg(state, 0x0006, 0x64);
  724. if (lg_chkerr(ret))
  725. goto fail;
  726. /* 8. ADC sampling frequency(0x190000 for 25MHz sampling) */
  727. ret = lgdt3306a_read_reg(state, 0x000d, &val);
  728. if (lg_chkerr(ret))
  729. goto fail;
  730. val &= 0xc0;
  731. val |= 0x19;
  732. ret = lgdt3306a_write_reg(state, 0x000d, val);
  733. if (lg_chkerr(ret))
  734. goto fail;
  735. } else {
  736. pr_err("Bad xtalMHz=%d\n", state->cfg->xtalMHz);
  737. }
  738. #if 0
  739. ret = lgdt3306a_write_reg(state, 0x000e, 0x00);
  740. ret = lgdt3306a_write_reg(state, 0x000f, 0x00);
  741. #endif
  742. /* 9. Center frequency of input signal of ADC */
  743. ret = lgdt3306a_write_reg(state, 0x0010, 0x34); /* 3.25MHz */
  744. ret = lgdt3306a_write_reg(state, 0x0011, 0x00);
  745. /* 10. Fixed gain error value */
  746. ret = lgdt3306a_write_reg(state, 0x0014, 0); /* gain error=0 */
  747. /* 10a. VSB TR BW gear shift initial step */
  748. ret = lgdt3306a_read_reg(state, 0x103c, &val);
  749. val &= 0x0f;
  750. val |= 0x20; /* SAMGSAUTOSTL_V[3:0] = 2 */
  751. ret = lgdt3306a_write_reg(state, 0x103c, val);
  752. /* 10b. Timing offset calibration in low temperature for VSB */
  753. ret = lgdt3306a_read_reg(state, 0x103d, &val);
  754. val &= 0xfc;
  755. val |= 0x03;
  756. ret = lgdt3306a_write_reg(state, 0x103d, val);
  757. /* 10c. Timing offset calibration in low temperature for QAM */
  758. ret = lgdt3306a_read_reg(state, 0x1036, &val);
  759. val &= 0xf0;
  760. val |= 0x0c;
  761. ret = lgdt3306a_write_reg(state, 0x1036, val);
  762. /* 11. Using the imaginary part of CIR in CIR loading */
  763. ret = lgdt3306a_read_reg(state, 0x211f, &val);
  764. val &= 0xef; /* do not use imaginary of CIR */
  765. ret = lgdt3306a_write_reg(state, 0x211f, val);
  766. /* 12. Control of no signal detector function */
  767. ret = lgdt3306a_read_reg(state, 0x2849, &val);
  768. val &= 0xef; /* NOUSENOSIGDET=0, enable no signal detector */
  769. ret = lgdt3306a_write_reg(state, 0x2849, val);
  770. /* FGR - put demod in some known mode */
  771. ret = lgdt3306a_set_vsb(state);
  772. /* 13. TP stream format */
  773. ret = lgdt3306a_mpeg_mode(state, state->cfg->mpeg_mode);
  774. /* 14. disable output buses */
  775. ret = lgdt3306a_mpeg_tristate(state, 1);
  776. /* 15. Sleep (in reset) */
  777. ret = lgdt3306a_sleep(state);
  778. lg_chkerr(ret);
  779. fail:
  780. return ret;
  781. }
  782. static int lgdt3306a_set_parameters(struct dvb_frontend *fe)
  783. {
  784. struct dtv_frontend_properties *p = &fe->dtv_property_cache;
  785. struct lgdt3306a_state *state = fe->demodulator_priv;
  786. int ret;
  787. dbg_info("(%d, %d)\n", p->frequency, p->modulation);
  788. if (state->current_frequency == p->frequency &&
  789. state->current_modulation == p->modulation) {
  790. dbg_info(" (already set, skipping ...)\n");
  791. return 0;
  792. }
  793. state->current_frequency = -1;
  794. state->current_modulation = -1;
  795. ret = lgdt3306a_power(state, 1); /* power up */
  796. if (lg_chkerr(ret))
  797. goto fail;
  798. if (fe->ops.tuner_ops.set_params) {
  799. ret = fe->ops.tuner_ops.set_params(fe);
  800. if (fe->ops.i2c_gate_ctrl)
  801. fe->ops.i2c_gate_ctrl(fe, 0);
  802. #if 0
  803. if (lg_chkerr(ret))
  804. goto fail;
  805. state->current_frequency = p->frequency;
  806. #endif
  807. }
  808. ret = lgdt3306a_set_modulation(state, p);
  809. if (lg_chkerr(ret))
  810. goto fail;
  811. ret = lgdt3306a_agc_setup(state, p);
  812. if (lg_chkerr(ret))
  813. goto fail;
  814. ret = lgdt3306a_set_if(state, p);
  815. if (lg_chkerr(ret))
  816. goto fail;
  817. ret = lgdt3306a_spectral_inversion(state, p,
  818. state->cfg->spectral_inversion ? 1 : 0);
  819. if (lg_chkerr(ret))
  820. goto fail;
  821. ret = lgdt3306a_mpeg_mode(state, state->cfg->mpeg_mode);
  822. if (lg_chkerr(ret))
  823. goto fail;
  824. ret = lgdt3306a_mpeg_mode_polarity(state,
  825. state->cfg->tpclk_edge,
  826. state->cfg->tpvalid_polarity);
  827. if (lg_chkerr(ret))
  828. goto fail;
  829. ret = lgdt3306a_mpeg_tristate(state, 0); /* enable data bus */
  830. if (lg_chkerr(ret))
  831. goto fail;
  832. ret = lgdt3306a_soft_reset(state);
  833. if (lg_chkerr(ret))
  834. goto fail;
  835. #ifdef DBG_DUMP
  836. lgdt3306a_DumpAllRegs(state);
  837. #endif
  838. state->current_frequency = p->frequency;
  839. fail:
  840. return ret;
  841. }
  842. static int lgdt3306a_get_frontend(struct dvb_frontend *fe)
  843. {
  844. struct lgdt3306a_state *state = fe->demodulator_priv;
  845. struct dtv_frontend_properties *p = &fe->dtv_property_cache;
  846. dbg_info("(%u, %d)\n",
  847. state->current_frequency, state->current_modulation);
  848. p->modulation = state->current_modulation;
  849. p->frequency = state->current_frequency;
  850. return 0;
  851. }
  852. static enum dvbfe_algo lgdt3306a_get_frontend_algo(struct dvb_frontend *fe)
  853. {
  854. #if 1
  855. return DVBFE_ALGO_CUSTOM;
  856. #else
  857. return DVBFE_ALGO_HW;
  858. #endif
  859. }
  860. /* ------------------------------------------------------------------------ */
  861. static int lgdt3306a_monitor_vsb(struct lgdt3306a_state *state)
  862. {
  863. u8 val;
  864. int ret;
  865. u8 snrRef, maxPowerMan, nCombDet;
  866. u16 fbDlyCir;
  867. ret = lgdt3306a_read_reg(state, 0x21a1, &val);
  868. if (ret)
  869. return ret;
  870. snrRef = val & 0x3f;
  871. ret = lgdt3306a_read_reg(state, 0x2185, &maxPowerMan);
  872. if (ret)
  873. return ret;
  874. ret = lgdt3306a_read_reg(state, 0x2191, &val);
  875. if (ret)
  876. return ret;
  877. nCombDet = (val & 0x80) >> 7;
  878. ret = lgdt3306a_read_reg(state, 0x2180, &val);
  879. if (ret)
  880. return ret;
  881. fbDlyCir = (val & 0x03) << 8;
  882. ret = lgdt3306a_read_reg(state, 0x2181, &val);
  883. if (ret)
  884. return ret;
  885. fbDlyCir |= val;
  886. dbg_info("snrRef=%d maxPowerMan=0x%x nCombDet=%d fbDlyCir=0x%x\n",
  887. snrRef, maxPowerMan, nCombDet, fbDlyCir);
  888. /* Carrier offset sub loop bandwidth */
  889. ret = lgdt3306a_read_reg(state, 0x1061, &val);
  890. if (ret)
  891. return ret;
  892. val &= 0xf8;
  893. if ((snrRef > 18) && (maxPowerMan > 0x68)
  894. && (nCombDet == 0x01)
  895. && ((fbDlyCir == 0x03FF) || (fbDlyCir < 0x6C))) {
  896. /* SNR is over 18dB and no ghosting */
  897. val |= 0x00; /* final bandwidth = 0 */
  898. } else {
  899. val |= 0x04; /* final bandwidth = 4 */
  900. }
  901. ret = lgdt3306a_write_reg(state, 0x1061, val);
  902. if (ret)
  903. return ret;
  904. /* Adjust Notch Filter */
  905. ret = lgdt3306a_read_reg(state, 0x0024, &val);
  906. if (ret)
  907. return ret;
  908. val &= 0x0f;
  909. if (nCombDet == 0) { /* Turn on the Notch Filter */
  910. val |= 0x50;
  911. }
  912. ret = lgdt3306a_write_reg(state, 0x0024, val);
  913. if (ret)
  914. return ret;
  915. /* VSB Timing Recovery output normalization */
  916. ret = lgdt3306a_read_reg(state, 0x103d, &val);
  917. if (ret)
  918. return ret;
  919. val &= 0xcf;
  920. val |= 0x20;
  921. ret = lgdt3306a_write_reg(state, 0x103d, val);
  922. return ret;
  923. }
  924. static enum lgdt3306a_modulation
  925. lgdt3306a_check_oper_mode(struct lgdt3306a_state *state)
  926. {
  927. u8 val = 0;
  928. int ret;
  929. ret = lgdt3306a_read_reg(state, 0x0081, &val);
  930. if (ret)
  931. goto err;
  932. if (val & 0x80) {
  933. dbg_info("VSB\n");
  934. return LG3306_VSB;
  935. }
  936. if (val & 0x08) {
  937. ret = lgdt3306a_read_reg(state, 0x00a6, &val);
  938. if (ret)
  939. goto err;
  940. val = val >> 2;
  941. if (val & 0x01) {
  942. dbg_info("QAM256\n");
  943. return LG3306_QAM256;
  944. }
  945. dbg_info("QAM64\n");
  946. return LG3306_QAM64;
  947. }
  948. err:
  949. pr_warn("UNKNOWN\n");
  950. return LG3306_UNKNOWN_MODE;
  951. }
  952. static enum lgdt3306a_lock_status
  953. lgdt3306a_check_lock_status(struct lgdt3306a_state *state,
  954. enum lgdt3306a_lock_check whatLock)
  955. {
  956. u8 val = 0;
  957. int ret;
  958. enum lgdt3306a_modulation modeOper;
  959. enum lgdt3306a_lock_status lockStatus;
  960. modeOper = LG3306_UNKNOWN_MODE;
  961. switch (whatLock) {
  962. case LG3306_SYNC_LOCK:
  963. {
  964. ret = lgdt3306a_read_reg(state, 0x00a6, &val);
  965. if (ret)
  966. return ret;
  967. if ((val & 0x80) == 0x80)
  968. lockStatus = LG3306_LOCK;
  969. else
  970. lockStatus = LG3306_UNLOCK;
  971. dbg_info("SYNC_LOCK=%x\n", lockStatus);
  972. break;
  973. }
  974. case LG3306_AGC_LOCK:
  975. {
  976. ret = lgdt3306a_read_reg(state, 0x0080, &val);
  977. if (ret)
  978. return ret;
  979. if ((val & 0x40) == 0x40)
  980. lockStatus = LG3306_LOCK;
  981. else
  982. lockStatus = LG3306_UNLOCK;
  983. dbg_info("AGC_LOCK=%x\n", lockStatus);
  984. break;
  985. }
  986. case LG3306_TR_LOCK:
  987. {
  988. modeOper = lgdt3306a_check_oper_mode(state);
  989. if ((modeOper == LG3306_QAM64) || (modeOper == LG3306_QAM256)) {
  990. ret = lgdt3306a_read_reg(state, 0x1094, &val);
  991. if (ret)
  992. return ret;
  993. if ((val & 0x80) == 0x80)
  994. lockStatus = LG3306_LOCK;
  995. else
  996. lockStatus = LG3306_UNLOCK;
  997. } else
  998. lockStatus = LG3306_UNKNOWN_LOCK;
  999. dbg_info("TR_LOCK=%x\n", lockStatus);
  1000. break;
  1001. }
  1002. case LG3306_FEC_LOCK:
  1003. {
  1004. modeOper = lgdt3306a_check_oper_mode(state);
  1005. if ((modeOper == LG3306_QAM64) || (modeOper == LG3306_QAM256)) {
  1006. ret = lgdt3306a_read_reg(state, 0x0080, &val);
  1007. if (ret)
  1008. return ret;
  1009. if ((val & 0x10) == 0x10)
  1010. lockStatus = LG3306_LOCK;
  1011. else
  1012. lockStatus = LG3306_UNLOCK;
  1013. } else
  1014. lockStatus = LG3306_UNKNOWN_LOCK;
  1015. dbg_info("FEC_LOCK=%x\n", lockStatus);
  1016. break;
  1017. }
  1018. default:
  1019. lockStatus = LG3306_UNKNOWN_LOCK;
  1020. pr_warn("UNKNOWN whatLock=%d\n", whatLock);
  1021. break;
  1022. }
  1023. return lockStatus;
  1024. }
  1025. static enum lgdt3306a_neverlock_status
  1026. lgdt3306a_check_neverlock_status(struct lgdt3306a_state *state)
  1027. {
  1028. u8 val = 0;
  1029. int ret;
  1030. enum lgdt3306a_neverlock_status lockStatus;
  1031. ret = lgdt3306a_read_reg(state, 0x0080, &val);
  1032. if (ret)
  1033. return ret;
  1034. lockStatus = (enum lgdt3306a_neverlock_status)(val & 0x03);
  1035. dbg_info("NeverLock=%d", lockStatus);
  1036. return lockStatus;
  1037. }
  1038. static int lgdt3306a_pre_monitoring(struct lgdt3306a_state *state)
  1039. {
  1040. u8 val = 0;
  1041. int ret;
  1042. u8 currChDiffACQ, snrRef, mainStrong, aiccrejStatus;
  1043. /* Channel variation */
  1044. ret = lgdt3306a_read_reg(state, 0x21bc, &currChDiffACQ);
  1045. if (ret)
  1046. return ret;
  1047. /* SNR of Frame sync */
  1048. ret = lgdt3306a_read_reg(state, 0x21a1, &val);
  1049. if (ret)
  1050. return ret;
  1051. snrRef = val & 0x3f;
  1052. /* Strong Main CIR */
  1053. ret = lgdt3306a_read_reg(state, 0x2199, &val);
  1054. if (ret)
  1055. return ret;
  1056. mainStrong = (val & 0x40) >> 6;
  1057. ret = lgdt3306a_read_reg(state, 0x0090, &val);
  1058. if (ret)
  1059. return ret;
  1060. aiccrejStatus = (val & 0xf0) >> 4;
  1061. dbg_info("snrRef=%d mainStrong=%d aiccrejStatus=%d currChDiffACQ=0x%x\n",
  1062. snrRef, mainStrong, aiccrejStatus, currChDiffACQ);
  1063. #if 0
  1064. /* Dynamic ghost exists */
  1065. if ((mainStrong == 0) && (currChDiffACQ > 0x70))
  1066. #endif
  1067. if (mainStrong == 0) {
  1068. ret = lgdt3306a_read_reg(state, 0x2135, &val);
  1069. if (ret)
  1070. return ret;
  1071. val &= 0x0f;
  1072. val |= 0xa0;
  1073. ret = lgdt3306a_write_reg(state, 0x2135, val);
  1074. if (ret)
  1075. return ret;
  1076. ret = lgdt3306a_read_reg(state, 0x2141, &val);
  1077. if (ret)
  1078. return ret;
  1079. val &= 0x3f;
  1080. val |= 0x80;
  1081. ret = lgdt3306a_write_reg(state, 0x2141, val);
  1082. if (ret)
  1083. return ret;
  1084. ret = lgdt3306a_write_reg(state, 0x2122, 0x70);
  1085. if (ret)
  1086. return ret;
  1087. } else { /* Weak ghost or static channel */
  1088. ret = lgdt3306a_read_reg(state, 0x2135, &val);
  1089. if (ret)
  1090. return ret;
  1091. val &= 0x0f;
  1092. val |= 0x70;
  1093. ret = lgdt3306a_write_reg(state, 0x2135, val);
  1094. if (ret)
  1095. return ret;
  1096. ret = lgdt3306a_read_reg(state, 0x2141, &val);
  1097. if (ret)
  1098. return ret;
  1099. val &= 0x3f;
  1100. val |= 0x40;
  1101. ret = lgdt3306a_write_reg(state, 0x2141, val);
  1102. if (ret)
  1103. return ret;
  1104. ret = lgdt3306a_write_reg(state, 0x2122, 0x40);
  1105. if (ret)
  1106. return ret;
  1107. }
  1108. return 0;
  1109. }
  1110. static enum lgdt3306a_lock_status
  1111. lgdt3306a_sync_lock_poll(struct lgdt3306a_state *state)
  1112. {
  1113. enum lgdt3306a_lock_status syncLockStatus = LG3306_UNLOCK;
  1114. int i;
  1115. for (i = 0; i < 2; i++) {
  1116. msleep(30);
  1117. syncLockStatus = lgdt3306a_check_lock_status(state,
  1118. LG3306_SYNC_LOCK);
  1119. if (syncLockStatus == LG3306_LOCK) {
  1120. dbg_info("locked(%d)\n", i);
  1121. return LG3306_LOCK;
  1122. }
  1123. }
  1124. dbg_info("not locked\n");
  1125. return LG3306_UNLOCK;
  1126. }
  1127. static enum lgdt3306a_lock_status
  1128. lgdt3306a_fec_lock_poll(struct lgdt3306a_state *state)
  1129. {
  1130. enum lgdt3306a_lock_status FECLockStatus = LG3306_UNLOCK;
  1131. int i;
  1132. for (i = 0; i < 2; i++) {
  1133. msleep(30);
  1134. FECLockStatus = lgdt3306a_check_lock_status(state,
  1135. LG3306_FEC_LOCK);
  1136. if (FECLockStatus == LG3306_LOCK) {
  1137. dbg_info("locked(%d)\n", i);
  1138. return FECLockStatus;
  1139. }
  1140. }
  1141. dbg_info("not locked\n");
  1142. return FECLockStatus;
  1143. }
  1144. static enum lgdt3306a_neverlock_status
  1145. lgdt3306a_neverlock_poll(struct lgdt3306a_state *state)
  1146. {
  1147. enum lgdt3306a_neverlock_status NLLockStatus = LG3306_NL_FAIL;
  1148. int i;
  1149. for (i = 0; i < 5; i++) {
  1150. msleep(30);
  1151. NLLockStatus = lgdt3306a_check_neverlock_status(state);
  1152. if (NLLockStatus == LG3306_NL_LOCK) {
  1153. dbg_info("NL_LOCK(%d)\n", i);
  1154. return NLLockStatus;
  1155. }
  1156. }
  1157. dbg_info("NLLockStatus=%d\n", NLLockStatus);
  1158. return NLLockStatus;
  1159. }
  1160. static u8 lgdt3306a_get_packet_error(struct lgdt3306a_state *state)
  1161. {
  1162. u8 val;
  1163. int ret;
  1164. ret = lgdt3306a_read_reg(state, 0x00fa, &val);
  1165. if (ret)
  1166. return ret;
  1167. return val;
  1168. }
  1169. static const u32 valx_x10[] = {
  1170. 10, 11, 13, 15, 17, 20, 25, 33, 41, 50, 59, 73, 87, 100
  1171. };
  1172. static const u32 log10x_x1000[] = {
  1173. 0, 41, 114, 176, 230, 301, 398, 518, 613, 699, 771, 863, 939, 1000
  1174. };
  1175. static u32 log10_x1000(u32 x)
  1176. {
  1177. u32 diff_val, step_val, step_log10;
  1178. u32 log_val = 0;
  1179. u32 i;
  1180. if (x <= 0)
  1181. return -1000000; /* signal error */
  1182. if (x == 10)
  1183. return 0; /* log(1)=0 */
  1184. if (x < 10) {
  1185. while (x < 10) {
  1186. x = x * 10;
  1187. log_val--;
  1188. }
  1189. } else { /* x > 10 */
  1190. while (x >= 100) {
  1191. x = x / 10;
  1192. log_val++;
  1193. }
  1194. }
  1195. log_val *= 1000;
  1196. if (x == 10) /* was our input an exact multiple of 10 */
  1197. return log_val; /* don't need to interpolate */
  1198. /* find our place on the log curve */
  1199. for (i = 1; i < ARRAY_SIZE(valx_x10); i++) {
  1200. if (valx_x10[i] >= x)
  1201. break;
  1202. }
  1203. if (i == ARRAY_SIZE(valx_x10))
  1204. return log_val + log10x_x1000[i - 1];
  1205. diff_val = x - valx_x10[i-1];
  1206. step_val = valx_x10[i] - valx_x10[i - 1];
  1207. step_log10 = log10x_x1000[i] - log10x_x1000[i - 1];
  1208. /* do a linear interpolation to get in-between values */
  1209. return log_val + log10x_x1000[i - 1] +
  1210. ((diff_val*step_log10) / step_val);
  1211. }
  1212. static u32 lgdt3306a_calculate_snr_x100(struct lgdt3306a_state *state)
  1213. {
  1214. u32 mse; /* Mean-Square Error */
  1215. u32 pwr; /* Constelation power */
  1216. u32 snr_x100;
  1217. mse = (read_reg(state, 0x00ec) << 8) |
  1218. (read_reg(state, 0x00ed));
  1219. pwr = (read_reg(state, 0x00e8) << 8) |
  1220. (read_reg(state, 0x00e9));
  1221. if (mse == 0) /* no signal */
  1222. return 0;
  1223. snr_x100 = log10_x1000((pwr * 10000) / mse) - 3000;
  1224. dbg_info("mse=%u, pwr=%u, snr_x100=%d\n", mse, pwr, snr_x100);
  1225. return snr_x100;
  1226. }
  1227. static enum lgdt3306a_lock_status
  1228. lgdt3306a_vsb_lock_poll(struct lgdt3306a_state *state)
  1229. {
  1230. int ret;
  1231. u8 cnt = 0;
  1232. u8 packet_error;
  1233. u32 snr;
  1234. for (cnt = 0; cnt < 10; cnt++) {
  1235. if (lgdt3306a_sync_lock_poll(state) == LG3306_UNLOCK) {
  1236. dbg_info("no sync lock!\n");
  1237. return LG3306_UNLOCK;
  1238. }
  1239. msleep(20);
  1240. ret = lgdt3306a_pre_monitoring(state);
  1241. if (ret)
  1242. break;
  1243. packet_error = lgdt3306a_get_packet_error(state);
  1244. snr = lgdt3306a_calculate_snr_x100(state);
  1245. dbg_info("cnt=%d errors=%d snr=%d\n", cnt, packet_error, snr);
  1246. if ((snr >= 1500) && (packet_error < 0xff))
  1247. return LG3306_LOCK;
  1248. }
  1249. dbg_info("not locked!\n");
  1250. return LG3306_UNLOCK;
  1251. }
  1252. static enum lgdt3306a_lock_status
  1253. lgdt3306a_qam_lock_poll(struct lgdt3306a_state *state)
  1254. {
  1255. u8 cnt;
  1256. u8 packet_error;
  1257. u32 snr;
  1258. for (cnt = 0; cnt < 10; cnt++) {
  1259. if (lgdt3306a_fec_lock_poll(state) == LG3306_UNLOCK) {
  1260. dbg_info("no fec lock!\n");
  1261. return LG3306_UNLOCK;
  1262. }
  1263. msleep(20);
  1264. packet_error = lgdt3306a_get_packet_error(state);
  1265. snr = lgdt3306a_calculate_snr_x100(state);
  1266. dbg_info("cnt=%d errors=%d snr=%d\n", cnt, packet_error, snr);
  1267. if ((snr >= 1500) && (packet_error < 0xff))
  1268. return LG3306_LOCK;
  1269. }
  1270. dbg_info("not locked!\n");
  1271. return LG3306_UNLOCK;
  1272. }
  1273. static int lgdt3306a_read_status(struct dvb_frontend *fe,
  1274. enum fe_status *status)
  1275. {
  1276. struct lgdt3306a_state *state = fe->demodulator_priv;
  1277. u16 strength = 0;
  1278. int ret = 0;
  1279. if (fe->ops.tuner_ops.get_rf_strength) {
  1280. ret = fe->ops.tuner_ops.get_rf_strength(fe, &strength);
  1281. if (ret == 0)
  1282. dbg_info("strength=%d\n", strength);
  1283. else
  1284. dbg_info("fe->ops.tuner_ops.get_rf_strength() failed\n");
  1285. }
  1286. *status = 0;
  1287. if (lgdt3306a_neverlock_poll(state) == LG3306_NL_LOCK) {
  1288. *status |= FE_HAS_SIGNAL;
  1289. *status |= FE_HAS_CARRIER;
  1290. switch (state->current_modulation) {
  1291. case QAM_256:
  1292. case QAM_64:
  1293. if (lgdt3306a_qam_lock_poll(state) == LG3306_LOCK) {
  1294. *status |= FE_HAS_VITERBI;
  1295. *status |= FE_HAS_SYNC;
  1296. *status |= FE_HAS_LOCK;
  1297. }
  1298. break;
  1299. case VSB_8:
  1300. if (lgdt3306a_vsb_lock_poll(state) == LG3306_LOCK) {
  1301. *status |= FE_HAS_VITERBI;
  1302. *status |= FE_HAS_SYNC;
  1303. *status |= FE_HAS_LOCK;
  1304. ret = lgdt3306a_monitor_vsb(state);
  1305. }
  1306. break;
  1307. default:
  1308. ret = -EINVAL;
  1309. }
  1310. }
  1311. return ret;
  1312. }
  1313. static int lgdt3306a_read_snr(struct dvb_frontend *fe, u16 *snr)
  1314. {
  1315. struct lgdt3306a_state *state = fe->demodulator_priv;
  1316. state->snr = lgdt3306a_calculate_snr_x100(state);
  1317. /* report SNR in dB * 10 */
  1318. *snr = state->snr/10;
  1319. return 0;
  1320. }
  1321. static int lgdt3306a_read_signal_strength(struct dvb_frontend *fe,
  1322. u16 *strength)
  1323. {
  1324. /*
  1325. * Calculate some sort of "strength" from SNR
  1326. */
  1327. struct lgdt3306a_state *state = fe->demodulator_priv;
  1328. u16 snr; /* snr_x10 */
  1329. int ret;
  1330. u32 ref_snr; /* snr*100 */
  1331. u32 str;
  1332. *strength = 0;
  1333. switch (state->current_modulation) {
  1334. case VSB_8:
  1335. ref_snr = 1600; /* 16dB */
  1336. break;
  1337. case QAM_64:
  1338. ref_snr = 2200; /* 22dB */
  1339. break;
  1340. case QAM_256:
  1341. ref_snr = 2800; /* 28dB */
  1342. break;
  1343. default:
  1344. return -EINVAL;
  1345. }
  1346. ret = fe->ops.read_snr(fe, &snr);
  1347. if (lg_chkerr(ret))
  1348. goto fail;
  1349. if (state->snr <= (ref_snr - 100))
  1350. str = 0;
  1351. else if (state->snr <= ref_snr)
  1352. str = (0xffff * 65) / 100; /* 65% */
  1353. else {
  1354. str = state->snr - ref_snr;
  1355. str /= 50;
  1356. str += 78; /* 78%-100% */
  1357. if (str > 100)
  1358. str = 100;
  1359. str = (0xffff * str) / 100;
  1360. }
  1361. *strength = (u16)str;
  1362. dbg_info("strength=%u\n", *strength);
  1363. fail:
  1364. return ret;
  1365. }
  1366. /* ------------------------------------------------------------------------ */
  1367. static int lgdt3306a_read_ber(struct dvb_frontend *fe, u32 *ber)
  1368. {
  1369. struct lgdt3306a_state *state = fe->demodulator_priv;
  1370. u32 tmp;
  1371. *ber = 0;
  1372. #if 1
  1373. /* FGR - FIXME - I don't know what value is expected by dvb_core
  1374. * what is the scale of the value?? */
  1375. tmp = read_reg(state, 0x00fc); /* NBERVALUE[24-31] */
  1376. tmp = (tmp << 8) | read_reg(state, 0x00fd); /* NBERVALUE[16-23] */
  1377. tmp = (tmp << 8) | read_reg(state, 0x00fe); /* NBERVALUE[8-15] */
  1378. tmp = (tmp << 8) | read_reg(state, 0x00ff); /* NBERVALUE[0-7] */
  1379. *ber = tmp;
  1380. dbg_info("ber=%u\n", tmp);
  1381. #endif
  1382. return 0;
  1383. }
  1384. static int lgdt3306a_read_ucblocks(struct dvb_frontend *fe, u32 *ucblocks)
  1385. {
  1386. struct lgdt3306a_state *state = fe->demodulator_priv;
  1387. *ucblocks = 0;
  1388. #if 1
  1389. /* FGR - FIXME - I don't know what value is expected by dvb_core
  1390. * what happens when value wraps? */
  1391. *ucblocks = read_reg(state, 0x00f4); /* TPIFTPERRCNT[0-7] */
  1392. dbg_info("ucblocks=%u\n", *ucblocks);
  1393. #endif
  1394. return 0;
  1395. }
  1396. static int lgdt3306a_tune(struct dvb_frontend *fe, bool re_tune,
  1397. unsigned int mode_flags, unsigned int *delay,
  1398. enum fe_status *status)
  1399. {
  1400. int ret = 0;
  1401. struct lgdt3306a_state *state = fe->demodulator_priv;
  1402. dbg_info("re_tune=%u\n", re_tune);
  1403. if (re_tune) {
  1404. state->current_frequency = -1; /* force re-tune */
  1405. ret = lgdt3306a_set_parameters(fe);
  1406. if (ret != 0)
  1407. return ret;
  1408. }
  1409. *delay = 125;
  1410. ret = lgdt3306a_read_status(fe, status);
  1411. return ret;
  1412. }
  1413. static int lgdt3306a_get_tune_settings(struct dvb_frontend *fe,
  1414. struct dvb_frontend_tune_settings
  1415. *fe_tune_settings)
  1416. {
  1417. fe_tune_settings->min_delay_ms = 100;
  1418. dbg_info("\n");
  1419. return 0;
  1420. }
  1421. static int lgdt3306a_search(struct dvb_frontend *fe)
  1422. {
  1423. enum fe_status status = 0;
  1424. int i, ret;
  1425. /* set frontend */
  1426. ret = lgdt3306a_set_parameters(fe);
  1427. if (ret)
  1428. goto error;
  1429. /* wait frontend lock */
  1430. for (i = 20; i > 0; i--) {
  1431. dbg_info(": loop=%d\n", i);
  1432. msleep(50);
  1433. ret = lgdt3306a_read_status(fe, &status);
  1434. if (ret)
  1435. goto error;
  1436. if (status & FE_HAS_LOCK)
  1437. break;
  1438. }
  1439. /* check if we have a valid signal */
  1440. if (status & FE_HAS_LOCK)
  1441. return DVBFE_ALGO_SEARCH_SUCCESS;
  1442. else
  1443. return DVBFE_ALGO_SEARCH_AGAIN;
  1444. error:
  1445. dbg_info("failed (%d)\n", ret);
  1446. return DVBFE_ALGO_SEARCH_ERROR;
  1447. }
  1448. static void lgdt3306a_release(struct dvb_frontend *fe)
  1449. {
  1450. struct lgdt3306a_state *state = fe->demodulator_priv;
  1451. dbg_info("\n");
  1452. kfree(state);
  1453. }
  1454. static struct dvb_frontend_ops lgdt3306a_ops;
  1455. struct dvb_frontend *lgdt3306a_attach(const struct lgdt3306a_config *config,
  1456. struct i2c_adapter *i2c_adap)
  1457. {
  1458. struct lgdt3306a_state *state = NULL;
  1459. int ret;
  1460. u8 val;
  1461. dbg_info("(%d-%04x)\n",
  1462. i2c_adap ? i2c_adapter_id(i2c_adap) : 0,
  1463. config ? config->i2c_addr : 0);
  1464. state = kzalloc(sizeof(struct lgdt3306a_state), GFP_KERNEL);
  1465. if (state == NULL)
  1466. goto fail;
  1467. state->cfg = config;
  1468. state->i2c_adap = i2c_adap;
  1469. memcpy(&state->frontend.ops, &lgdt3306a_ops,
  1470. sizeof(struct dvb_frontend_ops));
  1471. state->frontend.demodulator_priv = state;
  1472. /* verify that we're talking to a lg3306a */
  1473. /* FGR - NOTE - there is no obvious ChipId to check; we check
  1474. * some "known" bits after reset, but it's still just a guess */
  1475. ret = lgdt3306a_read_reg(state, 0x0000, &val);
  1476. if (lg_chkerr(ret))
  1477. goto fail;
  1478. if ((val & 0x74) != 0x74) {
  1479. pr_warn("expected 0x74, got 0x%x\n", (val & 0x74));
  1480. #if 0
  1481. /* FIXME - re-enable when we know this is right */
  1482. goto fail;
  1483. #endif
  1484. }
  1485. ret = lgdt3306a_read_reg(state, 0x0001, &val);
  1486. if (lg_chkerr(ret))
  1487. goto fail;
  1488. if ((val & 0xf6) != 0xc6) {
  1489. pr_warn("expected 0xc6, got 0x%x\n", (val & 0xf6));
  1490. #if 0
  1491. /* FIXME - re-enable when we know this is right */
  1492. goto fail;
  1493. #endif
  1494. }
  1495. ret = lgdt3306a_read_reg(state, 0x0002, &val);
  1496. if (lg_chkerr(ret))
  1497. goto fail;
  1498. if ((val & 0x73) != 0x03) {
  1499. pr_warn("expected 0x03, got 0x%x\n", (val & 0x73));
  1500. #if 0
  1501. /* FIXME - re-enable when we know this is right */
  1502. goto fail;
  1503. #endif
  1504. }
  1505. state->current_frequency = -1;
  1506. state->current_modulation = -1;
  1507. lgdt3306a_sleep(state);
  1508. return &state->frontend;
  1509. fail:
  1510. pr_warn("unable to detect LGDT3306A hardware\n");
  1511. kfree(state);
  1512. return NULL;
  1513. }
  1514. EXPORT_SYMBOL(lgdt3306a_attach);
  1515. #ifdef DBG_DUMP
  1516. static const short regtab[] = {
  1517. 0x0000, /* SOFTRSTB 1'b1 1'b1 1'b1 ADCPDB 1'b1 PLLPDB GBBPDB 11111111 */
  1518. 0x0001, /* 1'b1 1'b1 1'b0 1'b0 AUTORPTRS */
  1519. 0x0002, /* NI2CRPTEN 1'b0 1'b0 1'b0 SPECINVAUT */
  1520. 0x0003, /* AGCRFOUT */
  1521. 0x0004, /* ADCSEL1V ADCCNT ADCCNF ADCCNS ADCCLKPLL */
  1522. 0x0005, /* PLLINDIVSE */
  1523. 0x0006, /* PLLCTRL[7:0] 11100001 */
  1524. 0x0007, /* SYSINITWAITTIME[7:0] (msec) 00001000 */
  1525. 0x0008, /* STDOPMODE[7:0] 10000000 */
  1526. 0x0009, /* 1'b0 1'b0 1'b0 STDOPDETTMODE[2:0] STDOPDETCMODE[1:0] 00011110 */
  1527. 0x000a, /* DAFTEN 1'b1 x x SCSYSLOCK */
  1528. 0x000b, /* SCSYSLOCKCHKTIME[7:0] (10msec) 01100100 */
  1529. 0x000d, /* x SAMPLING4 */
  1530. 0x000e, /* SAMFREQ[15:8] 00000000 */
  1531. 0x000f, /* SAMFREQ[7:0] 00000000 */
  1532. 0x0010, /* IFFREQ[15:8] 01100000 */
  1533. 0x0011, /* IFFREQ[7:0] 00000000 */
  1534. 0x0012, /* AGCEN AGCREFMO */
  1535. 0x0013, /* AGCRFFIXB AGCIFFIXB AGCLOCKDETRNGSEL[1:0] 1'b1 1'b0 1'b0 1'b0 11101000 */
  1536. 0x0014, /* AGCFIXVALUE[7:0] 01111111 */
  1537. 0x0015, /* AGCREF[15:8] 00001010 */
  1538. 0x0016, /* AGCREF[7:0] 11100100 */
  1539. 0x0017, /* AGCDELAY[7:0] 00100000 */
  1540. 0x0018, /* AGCRFBW[3:0] AGCIFBW[3:0] 10001000 */
  1541. 0x0019, /* AGCUDOUTMODE[1:0] AGCUDCTRLLEN[1:0] AGCUDCTRL */
  1542. 0x001c, /* 1'b1 PFEN MFEN AICCVSYNC */
  1543. 0x001d, /* 1'b0 1'b1 1'b0 1'b1 AICCVSYNC */
  1544. 0x001e, /* AICCALPHA[3:0] 1'b1 1'b0 1'b1 1'b0 01111010 */
  1545. 0x001f, /* AICCDETTH[19:16] AICCOFFTH[19:16] 00000000 */
  1546. 0x0020, /* AICCDETTH[15:8] 01111100 */
  1547. 0x0021, /* AICCDETTH[7:0] 00000000 */
  1548. 0x0022, /* AICCOFFTH[15:8] 00000101 */
  1549. 0x0023, /* AICCOFFTH[7:0] 11100000 */
  1550. 0x0024, /* AICCOPMODE3[1:0] AICCOPMODE2[1:0] AICCOPMODE1[1:0] AICCOPMODE0[1:0] 00000000 */
  1551. 0x0025, /* AICCFIXFREQ3[23:16] 00000000 */
  1552. 0x0026, /* AICCFIXFREQ3[15:8] 00000000 */
  1553. 0x0027, /* AICCFIXFREQ3[7:0] 00000000 */
  1554. 0x0028, /* AICCFIXFREQ2[23:16] 00000000 */
  1555. 0x0029, /* AICCFIXFREQ2[15:8] 00000000 */
  1556. 0x002a, /* AICCFIXFREQ2[7:0] 00000000 */
  1557. 0x002b, /* AICCFIXFREQ1[23:16] 00000000 */
  1558. 0x002c, /* AICCFIXFREQ1[15:8] 00000000 */
  1559. 0x002d, /* AICCFIXFREQ1[7:0] 00000000 */
  1560. 0x002e, /* AICCFIXFREQ0[23:16] 00000000 */
  1561. 0x002f, /* AICCFIXFREQ0[15:8] 00000000 */
  1562. 0x0030, /* AICCFIXFREQ0[7:0] 00000000 */
  1563. 0x0031, /* 1'b0 1'b1 1'b0 1'b0 x DAGC1STER */
  1564. 0x0032, /* DAGC1STEN DAGC1STER */
  1565. 0x0033, /* DAGC1STREF[15:8] 00001010 */
  1566. 0x0034, /* DAGC1STREF[7:0] 11100100 */
  1567. 0x0035, /* DAGC2NDE */
  1568. 0x0036, /* DAGC2NDREF[15:8] 00001010 */
  1569. 0x0037, /* DAGC2NDREF[7:0] 10000000 */
  1570. 0x0038, /* DAGC2NDLOCKDETRNGSEL[1:0] */
  1571. 0x003d, /* 1'b1 SAMGEARS */
  1572. 0x0040, /* SAMLFGMA */
  1573. 0x0041, /* SAMLFBWM */
  1574. 0x0044, /* 1'b1 CRGEARSHE */
  1575. 0x0045, /* CRLFGMAN */
  1576. 0x0046, /* CFLFBWMA */
  1577. 0x0047, /* CRLFGMAN */
  1578. 0x0048, /* x x x x CRLFGSTEP_VS[3:0] xxxx1001 */
  1579. 0x0049, /* CRLFBWMA */
  1580. 0x004a, /* CRLFBWMA */
  1581. 0x0050, /* 1'b0 1'b1 1'b1 1'b0 MSECALCDA */
  1582. 0x0070, /* TPOUTEN TPIFEN TPCLKOUTE */
  1583. 0x0071, /* TPSENB TPSSOPBITE */
  1584. 0x0073, /* TP47HINS x x CHBERINT PERMODE[1:0] PERINT[1:0] 1xx11100 */
  1585. 0x0075, /* x x x x x IQSWAPCTRL[2:0] xxxxx000 */
  1586. 0x0076, /* NBERCON NBERST NBERPOL NBERWSYN */
  1587. 0x0077, /* x NBERLOSTTH[2:0] NBERACQTH[3:0] x0000000 */
  1588. 0x0078, /* NBERPOLY[31:24] 00000000 */
  1589. 0x0079, /* NBERPOLY[23:16] 00000000 */
  1590. 0x007a, /* NBERPOLY[15:8] 00000000 */
  1591. 0x007b, /* NBERPOLY[7:0] 00000000 */
  1592. 0x007c, /* NBERPED[31:24] 00000000 */
  1593. 0x007d, /* NBERPED[23:16] 00000000 */
  1594. 0x007e, /* NBERPED[15:8] 00000000 */
  1595. 0x007f, /* NBERPED[7:0] 00000000 */
  1596. 0x0080, /* x AGCLOCK DAGCLOCK SYSLOCK x x NEVERLOCK[1:0] */
  1597. 0x0085, /* SPECINVST */
  1598. 0x0088, /* SYSLOCKTIME[15:8] */
  1599. 0x0089, /* SYSLOCKTIME[7:0] */
  1600. 0x008c, /* FECLOCKTIME[15:8] */
  1601. 0x008d, /* FECLOCKTIME[7:0] */
  1602. 0x008e, /* AGCACCOUT[15:8] */
  1603. 0x008f, /* AGCACCOUT[7:0] */
  1604. 0x0090, /* AICCREJSTATUS[3:0] AICCREJBUSY[3:0] */
  1605. 0x0091, /* AICCVSYNC */
  1606. 0x009c, /* CARRFREQOFFSET[15:8] */
  1607. 0x009d, /* CARRFREQOFFSET[7:0] */
  1608. 0x00a1, /* SAMFREQOFFSET[23:16] */
  1609. 0x00a2, /* SAMFREQOFFSET[15:8] */
  1610. 0x00a3, /* SAMFREQOFFSET[7:0] */
  1611. 0x00a6, /* SYNCLOCK SYNCLOCKH */
  1612. #if 0 /* covered elsewhere */
  1613. 0x00e8, /* CONSTPWR[15:8] */
  1614. 0x00e9, /* CONSTPWR[7:0] */
  1615. 0x00ea, /* BMSE[15:8] */
  1616. 0x00eb, /* BMSE[7:0] */
  1617. 0x00ec, /* MSE[15:8] */
  1618. 0x00ed, /* MSE[7:0] */
  1619. 0x00ee, /* CONSTI[7:0] */
  1620. 0x00ef, /* CONSTQ[7:0] */
  1621. #endif
  1622. 0x00f4, /* TPIFTPERRCNT[7:0] */
  1623. 0x00f5, /* TPCORREC */
  1624. 0x00f6, /* VBBER[15:8] */
  1625. 0x00f7, /* VBBER[7:0] */
  1626. 0x00f8, /* VABER[15:8] */
  1627. 0x00f9, /* VABER[7:0] */
  1628. 0x00fa, /* TPERRCNT[7:0] */
  1629. 0x00fb, /* NBERLOCK x x x x x x x */
  1630. 0x00fc, /* NBERVALUE[31:24] */
  1631. 0x00fd, /* NBERVALUE[23:16] */
  1632. 0x00fe, /* NBERVALUE[15:8] */
  1633. 0x00ff, /* NBERVALUE[7:0] */
  1634. 0x1000, /* 1'b0 WODAGCOU */
  1635. 0x1005, /* x x 1'b1 1'b1 x SRD_Q_QM */
  1636. 0x1009, /* SRDWAITTIME[7:0] (10msec) 00100011 */
  1637. 0x100a, /* SRDWAITTIME_CQS[7:0] (msec) 01100100 */
  1638. 0x101a, /* x 1'b1 1'b0 1'b0 x QMDQAMMODE[2:0] x100x010 */
  1639. 0x1036, /* 1'b0 1'b1 1'b0 1'b0 SAMGSEND_CQS[3:0] 01001110 */
  1640. 0x103c, /* SAMGSAUTOSTL_V[3:0] SAMGSAUTOEDL_V[3:0] 01000110 */
  1641. 0x103d, /* 1'b1 1'b1 SAMCNORMBP_V[1:0] 1'b0 1'b0 SAMMODESEL_V[1:0] 11100001 */
  1642. 0x103f, /* SAMZTEDSE */
  1643. 0x105d, /* EQSTATUSE */
  1644. 0x105f, /* x PMAPG2_V[2:0] x DMAPG2_V[2:0] x001x011 */
  1645. 0x1060, /* 1'b1 EQSTATUSE */
  1646. 0x1061, /* CRMAPBWSTL_V[3:0] CRMAPBWEDL_V[3:0] 00000100 */
  1647. 0x1065, /* 1'b0 x CRMODE_V[1:0] 1'b1 x 1'b1 x 0x111x1x */
  1648. 0x1066, /* 1'b0 1'b0 1'b1 1'b0 1'b1 PNBOOSTSE */
  1649. 0x1068, /* CREPHNGAIN2_V[3:0] CREPHNPBW_V[3:0] 10010001 */
  1650. 0x106e, /* x x x x x CREPHNEN_ */
  1651. 0x106f, /* CREPHNTH_V[7:0] 00010101 */
  1652. 0x1072, /* CRSWEEPN */
  1653. 0x1073, /* CRPGAIN_V[3:0] x x 1'b1 1'b1 1001xx11 */
  1654. 0x1074, /* CRPBW_V[3:0] x x 1'b1 1'b1 0001xx11 */
  1655. 0x1080, /* DAFTSTATUS[1:0] x x x x x x */
  1656. 0x1081, /* SRDSTATUS[1:0] x x x x x SRDLOCK */
  1657. 0x10a9, /* EQSTATUS_CQS[1:0] x x x x x x */
  1658. 0x10b7, /* EQSTATUS_V[1:0] x x x x x x */
  1659. #if 0 /* SMART_ANT */
  1660. 0x1f00, /* MODEDETE */
  1661. 0x1f01, /* x x x x x x x SFNRST xxxxxxx0 */
  1662. 0x1f03, /* NUMOFANT[7:0] 10000000 */
  1663. 0x1f04, /* x SELMASK[6:0] x0000000 */
  1664. 0x1f05, /* x SETMASK[6:0] x0000000 */
  1665. 0x1f06, /* x TXDATA[6:0] x0000000 */
  1666. 0x1f07, /* x CHNUMBER[6:0] x0000000 */
  1667. 0x1f09, /* AGCTIME[23:16] 10011000 */
  1668. 0x1f0a, /* AGCTIME[15:8] 10010110 */
  1669. 0x1f0b, /* AGCTIME[7:0] 10000000 */
  1670. 0x1f0c, /* ANTTIME[31:24] 00000000 */
  1671. 0x1f0d, /* ANTTIME[23:16] 00000011 */
  1672. 0x1f0e, /* ANTTIME[15:8] 10010000 */
  1673. 0x1f0f, /* ANTTIME[7:0] 10010000 */
  1674. 0x1f11, /* SYNCTIME[23:16] 10011000 */
  1675. 0x1f12, /* SYNCTIME[15:8] 10010110 */
  1676. 0x1f13, /* SYNCTIME[7:0] 10000000 */
  1677. 0x1f14, /* SNRTIME[31:24] 00000001 */
  1678. 0x1f15, /* SNRTIME[23:16] 01111101 */
  1679. 0x1f16, /* SNRTIME[15:8] 01111000 */
  1680. 0x1f17, /* SNRTIME[7:0] 01000000 */
  1681. 0x1f19, /* FECTIME[23:16] 00000000 */
  1682. 0x1f1a, /* FECTIME[15:8] 01110010 */
  1683. 0x1f1b, /* FECTIME[7:0] 01110000 */
  1684. 0x1f1d, /* FECTHD[7:0] 00000011 */
  1685. 0x1f1f, /* SNRTHD[23:16] 00001000 */
  1686. 0x1f20, /* SNRTHD[15:8] 01111111 */
  1687. 0x1f21, /* SNRTHD[7:0] 10000101 */
  1688. 0x1f80, /* IRQFLG x x SFSDRFLG MODEBFLG SAVEFLG SCANFLG TRACKFLG */
  1689. 0x1f81, /* x SYNCCON SNRCON FECCON x STDBUSY SYNCRST AGCFZCO */
  1690. 0x1f82, /* x x x SCANOPCD[4:0] */
  1691. 0x1f83, /* x x x x MAINOPCD[3:0] */
  1692. 0x1f84, /* x x RXDATA[13:8] */
  1693. 0x1f85, /* RXDATA[7:0] */
  1694. 0x1f86, /* x x SDTDATA[13:8] */
  1695. 0x1f87, /* SDTDATA[7:0] */
  1696. 0x1f89, /* ANTSNR[23:16] */
  1697. 0x1f8a, /* ANTSNR[15:8] */
  1698. 0x1f8b, /* ANTSNR[7:0] */
  1699. 0x1f8c, /* x x x x ANTFEC[13:8] */
  1700. 0x1f8d, /* ANTFEC[7:0] */
  1701. 0x1f8e, /* MAXCNT[7:0] */
  1702. 0x1f8f, /* SCANCNT[7:0] */
  1703. 0x1f91, /* MAXPW[23:16] */
  1704. 0x1f92, /* MAXPW[15:8] */
  1705. 0x1f93, /* MAXPW[7:0] */
  1706. 0x1f95, /* CURPWMSE[23:16] */
  1707. 0x1f96, /* CURPWMSE[15:8] */
  1708. 0x1f97, /* CURPWMSE[7:0] */
  1709. #endif /* SMART_ANT */
  1710. 0x211f, /* 1'b1 1'b1 1'b1 CIRQEN x x 1'b0 1'b0 1111xx00 */
  1711. 0x212a, /* EQAUTOST */
  1712. 0x2122, /* CHFAST[7:0] 01100000 */
  1713. 0x212b, /* FFFSTEP_V[3:0] x FBFSTEP_V[2:0] 0001x001 */
  1714. 0x212c, /* PHDEROTBWSEL[3:0] 1'b1 1'b1 1'b1 1'b0 10001110 */
  1715. 0x212d, /* 1'b1 1'b1 1'b1 1'b1 x x TPIFLOCKS */
  1716. 0x2135, /* DYNTRACKFDEQ[3:0] x 1'b0 1'b0 1'b0 1010x000 */
  1717. 0x2141, /* TRMODE[1:0] 1'b1 1'b1 1'b0 1'b1 1'b1 1'b1 01110111 */
  1718. 0x2162, /* AICCCTRLE */
  1719. 0x2173, /* PHNCNFCNT[7:0] 00000100 */
  1720. 0x2179, /* 1'b0 1'b0 1'b0 1'b1 x BADSINGLEDYNTRACKFBF[2:0] 0001x001 */
  1721. 0x217a, /* 1'b0 1'b0 1'b0 1'b1 x BADSLOWSINGLEDYNTRACKFBF[2:0] 0001x001 */
  1722. 0x217e, /* CNFCNTTPIF[7:0] 00001000 */
  1723. 0x217f, /* TPERRCNTTPIF[7:0] 00000001 */
  1724. 0x2180, /* x x x x x x FBDLYCIR[9:8] */
  1725. 0x2181, /* FBDLYCIR[7:0] */
  1726. 0x2185, /* MAXPWRMAIN[7:0] */
  1727. 0x2191, /* NCOMBDET x x x x x x x */
  1728. 0x2199, /* x MAINSTRON */
  1729. 0x219a, /* FFFEQSTEPOUT_V[3:0] FBFSTEPOUT_V[2:0] */
  1730. 0x21a1, /* x x SNRREF[5:0] */
  1731. 0x2845, /* 1'b0 1'b1 x x FFFSTEP_CQS[1:0] FFFCENTERTAP[1:0] 01xx1110 */
  1732. 0x2846, /* 1'b0 x 1'b0 1'b1 FBFSTEP_CQS[1:0] 1'b1 1'b0 0x011110 */
  1733. 0x2847, /* ENNOSIGDE */
  1734. 0x2849, /* 1'b1 1'b1 NOUSENOSI */
  1735. 0x284a, /* EQINITWAITTIME[7:0] 01100100 */
  1736. 0x3000, /* 1'b1 1'b1 1'b1 x x x 1'b0 RPTRSTM */
  1737. 0x3001, /* RPTRSTWAITTIME[7:0] (100msec) 00110010 */
  1738. 0x3031, /* FRAMELOC */
  1739. 0x3032, /* 1'b1 1'b0 1'b0 1'b0 x x FRAMELOCKMODE_CQS[1:0] 1000xx11 */
  1740. 0x30a9, /* VDLOCK_Q FRAMELOCK */
  1741. 0x30aa, /* MPEGLOCK */
  1742. };
  1743. #define numDumpRegs (sizeof(regtab)/sizeof(regtab[0]))
  1744. static u8 regval1[numDumpRegs] = {0, };
  1745. static u8 regval2[numDumpRegs] = {0, };
  1746. static void lgdt3306a_DumpAllRegs(struct lgdt3306a_state *state)
  1747. {
  1748. memset(regval2, 0xff, sizeof(regval2));
  1749. lgdt3306a_DumpRegs(state);
  1750. }
  1751. static void lgdt3306a_DumpRegs(struct lgdt3306a_state *state)
  1752. {
  1753. int i;
  1754. int sav_debug = debug;
  1755. if ((debug & DBG_DUMP) == 0)
  1756. return;
  1757. debug &= ~DBG_REG; /* suppress DBG_REG during reg dump */
  1758. lg_debug("\n");
  1759. for (i = 0; i < numDumpRegs; i++) {
  1760. lgdt3306a_read_reg(state, regtab[i], &regval1[i]);
  1761. if (regval1[i] != regval2[i]) {
  1762. lg_debug(" %04X = %02X\n", regtab[i], regval1[i]);
  1763. regval2[i] = regval1[i];
  1764. }
  1765. }
  1766. debug = sav_debug;
  1767. }
  1768. #endif /* DBG_DUMP */
  1769. static struct dvb_frontend_ops lgdt3306a_ops = {
  1770. .delsys = { SYS_ATSC, SYS_DVBC_ANNEX_B },
  1771. .info = {
  1772. .name = "LG Electronics LGDT3306A VSB/QAM Frontend",
  1773. .frequency_min = 54000000,
  1774. .frequency_max = 858000000,
  1775. .frequency_stepsize = 62500,
  1776. .caps = FE_CAN_QAM_64 | FE_CAN_QAM_256 | FE_CAN_8VSB
  1777. },
  1778. .i2c_gate_ctrl = lgdt3306a_i2c_gate_ctrl,
  1779. .init = lgdt3306a_init,
  1780. .sleep = lgdt3306a_fe_sleep,
  1781. /* if this is set, it overrides the default swzigzag */
  1782. .tune = lgdt3306a_tune,
  1783. .set_frontend = lgdt3306a_set_parameters,
  1784. .get_frontend = lgdt3306a_get_frontend,
  1785. .get_frontend_algo = lgdt3306a_get_frontend_algo,
  1786. .get_tune_settings = lgdt3306a_get_tune_settings,
  1787. .read_status = lgdt3306a_read_status,
  1788. .read_ber = lgdt3306a_read_ber,
  1789. .read_signal_strength = lgdt3306a_read_signal_strength,
  1790. .read_snr = lgdt3306a_read_snr,
  1791. .read_ucblocks = lgdt3306a_read_ucblocks,
  1792. .release = lgdt3306a_release,
  1793. .ts_bus_ctrl = lgdt3306a_ts_bus_ctrl,
  1794. .search = lgdt3306a_search,
  1795. };
  1796. MODULE_DESCRIPTION("LG Electronics LGDT3306A ATSC/QAM-B Demodulator Driver");
  1797. MODULE_AUTHOR("Fred Richter <frichter@hauppauge.com>");
  1798. MODULE_LICENSE("GPL");
  1799. MODULE_VERSION("0.2");