stv0900_core.c 50 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987
  1. /*
  2. * stv0900_core.c
  3. *
  4. * Driver for ST STV0900 satellite demodulator IC.
  5. *
  6. * Copyright (C) ST Microelectronics.
  7. * Copyright (C) 2009 NetUP Inc.
  8. * Copyright (C) 2009 Igor M. Liplianin <liplianin@netup.ru>
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License as published by
  12. * the Free Software Foundation; either version 2 of the License, or
  13. * (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. *
  19. * GNU General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU General Public License
  22. * along with this program; if not, write to the Free Software
  23. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  24. */
  25. #include <linux/kernel.h>
  26. #include <linux/module.h>
  27. #include <linux/string.h>
  28. #include <linux/slab.h>
  29. #include <linux/i2c.h>
  30. #include "stv0900.h"
  31. #include "stv0900_reg.h"
  32. #include "stv0900_priv.h"
  33. #include "stv0900_init.h"
  34. int stvdebug = 1;
  35. module_param_named(debug, stvdebug, int, 0644);
  36. /* internal params node */
  37. struct stv0900_inode {
  38. /* pointer for internal params, one for each pair of demods */
  39. struct stv0900_internal *internal;
  40. struct stv0900_inode *next_inode;
  41. };
  42. /* first internal params */
  43. static struct stv0900_inode *stv0900_first_inode;
  44. /* find chip by i2c adapter and i2c address */
  45. static struct stv0900_inode *find_inode(struct i2c_adapter *i2c_adap,
  46. u8 i2c_addr)
  47. {
  48. struct stv0900_inode *temp_chip = stv0900_first_inode;
  49. if (temp_chip != NULL) {
  50. /*
  51. Search of the last stv0900 chip or
  52. find it by i2c adapter and i2c address */
  53. while ((temp_chip != NULL) &&
  54. ((temp_chip->internal->i2c_adap != i2c_adap) ||
  55. (temp_chip->internal->i2c_addr != i2c_addr)))
  56. temp_chip = temp_chip->next_inode;
  57. }
  58. return temp_chip;
  59. }
  60. /* deallocating chip */
  61. static void remove_inode(struct stv0900_internal *internal)
  62. {
  63. struct stv0900_inode *prev_node = stv0900_first_inode;
  64. struct stv0900_inode *del_node = find_inode(internal->i2c_adap,
  65. internal->i2c_addr);
  66. if (del_node != NULL) {
  67. if (del_node == stv0900_first_inode) {
  68. stv0900_first_inode = del_node->next_inode;
  69. } else {
  70. while (prev_node->next_inode != del_node)
  71. prev_node = prev_node->next_inode;
  72. if (del_node->next_inode == NULL)
  73. prev_node->next_inode = NULL;
  74. else
  75. prev_node->next_inode =
  76. prev_node->next_inode->next_inode;
  77. }
  78. kfree(del_node);
  79. }
  80. }
  81. /* allocating new chip */
  82. static struct stv0900_inode *append_internal(struct stv0900_internal *internal)
  83. {
  84. struct stv0900_inode *new_node = stv0900_first_inode;
  85. if (new_node == NULL) {
  86. new_node = kmalloc(sizeof(struct stv0900_inode), GFP_KERNEL);
  87. stv0900_first_inode = new_node;
  88. } else {
  89. while (new_node->next_inode != NULL)
  90. new_node = new_node->next_inode;
  91. new_node->next_inode = kmalloc(sizeof(struct stv0900_inode),
  92. GFP_KERNEL);
  93. if (new_node->next_inode != NULL)
  94. new_node = new_node->next_inode;
  95. else
  96. new_node = NULL;
  97. }
  98. if (new_node != NULL) {
  99. new_node->internal = internal;
  100. new_node->next_inode = NULL;
  101. }
  102. return new_node;
  103. }
  104. s32 ge2comp(s32 a, s32 width)
  105. {
  106. if (width == 32)
  107. return a;
  108. else
  109. return (a >= (1 << (width - 1))) ? (a - (1 << width)) : a;
  110. }
  111. void stv0900_write_reg(struct stv0900_internal *intp, u16 reg_addr,
  112. u8 reg_data)
  113. {
  114. u8 data[3];
  115. int ret;
  116. struct i2c_msg i2cmsg = {
  117. .addr = intp->i2c_addr,
  118. .flags = 0,
  119. .len = 3,
  120. .buf = data,
  121. };
  122. data[0] = MSB(reg_addr);
  123. data[1] = LSB(reg_addr);
  124. data[2] = reg_data;
  125. ret = i2c_transfer(intp->i2c_adap, &i2cmsg, 1);
  126. if (ret != 1)
  127. dprintk("%s: i2c error %d\n", __func__, ret);
  128. }
  129. u8 stv0900_read_reg(struct stv0900_internal *intp, u16 reg)
  130. {
  131. int ret;
  132. u8 b0[] = { MSB(reg), LSB(reg) };
  133. u8 buf = 0;
  134. struct i2c_msg msg[] = {
  135. {
  136. .addr = intp->i2c_addr,
  137. .flags = 0,
  138. .buf = b0,
  139. .len = 2,
  140. }, {
  141. .addr = intp->i2c_addr,
  142. .flags = I2C_M_RD,
  143. .buf = &buf,
  144. .len = 1,
  145. },
  146. };
  147. ret = i2c_transfer(intp->i2c_adap, msg, 2);
  148. if (ret != 2)
  149. dprintk("%s: i2c error %d, reg[0x%02x]\n",
  150. __func__, ret, reg);
  151. return buf;
  152. }
  153. static void extract_mask_pos(u32 label, u8 *mask, u8 *pos)
  154. {
  155. u8 position = 0, i = 0;
  156. (*mask) = label & 0xff;
  157. while ((position == 0) && (i < 8)) {
  158. position = ((*mask) >> i) & 0x01;
  159. i++;
  160. }
  161. (*pos) = (i - 1);
  162. }
  163. void stv0900_write_bits(struct stv0900_internal *intp, u32 label, u8 val)
  164. {
  165. u8 reg, mask, pos;
  166. reg = stv0900_read_reg(intp, (label >> 16) & 0xffff);
  167. extract_mask_pos(label, &mask, &pos);
  168. val = mask & (val << pos);
  169. reg = (reg & (~mask)) | val;
  170. stv0900_write_reg(intp, (label >> 16) & 0xffff, reg);
  171. }
  172. u8 stv0900_get_bits(struct stv0900_internal *intp, u32 label)
  173. {
  174. u8 val = 0xff;
  175. u8 mask, pos;
  176. extract_mask_pos(label, &mask, &pos);
  177. val = stv0900_read_reg(intp, label >> 16);
  178. val = (val & mask) >> pos;
  179. return val;
  180. }
  181. static enum fe_stv0900_error stv0900_initialize(struct stv0900_internal *intp)
  182. {
  183. s32 i;
  184. if (intp == NULL)
  185. return STV0900_INVALID_HANDLE;
  186. intp->chip_id = stv0900_read_reg(intp, R0900_MID);
  187. if (intp->errs != STV0900_NO_ERROR)
  188. return intp->errs;
  189. /*Startup sequence*/
  190. stv0900_write_reg(intp, R0900_P1_DMDISTATE, 0x5c);
  191. stv0900_write_reg(intp, R0900_P2_DMDISTATE, 0x5c);
  192. msleep(3);
  193. stv0900_write_reg(intp, R0900_P1_TNRCFG, 0x6c);
  194. stv0900_write_reg(intp, R0900_P2_TNRCFG, 0x6f);
  195. stv0900_write_reg(intp, R0900_P1_I2CRPT, 0x20);
  196. stv0900_write_reg(intp, R0900_P2_I2CRPT, 0x20);
  197. stv0900_write_reg(intp, R0900_NCOARSE, 0x13);
  198. msleep(3);
  199. stv0900_write_reg(intp, R0900_I2CCFG, 0x08);
  200. switch (intp->clkmode) {
  201. case 0:
  202. case 2:
  203. stv0900_write_reg(intp, R0900_SYNTCTRL, 0x20
  204. | intp->clkmode);
  205. break;
  206. default:
  207. /* preserve SELOSCI bit */
  208. i = 0x02 & stv0900_read_reg(intp, R0900_SYNTCTRL);
  209. stv0900_write_reg(intp, R0900_SYNTCTRL, 0x20 | i);
  210. break;
  211. }
  212. msleep(3);
  213. for (i = 0; i < 181; i++)
  214. stv0900_write_reg(intp, STV0900_InitVal[i][0],
  215. STV0900_InitVal[i][1]);
  216. if (stv0900_read_reg(intp, R0900_MID) >= 0x20) {
  217. stv0900_write_reg(intp, R0900_TSGENERAL, 0x0c);
  218. for (i = 0; i < 32; i++)
  219. stv0900_write_reg(intp, STV0900_Cut20_AddOnVal[i][0],
  220. STV0900_Cut20_AddOnVal[i][1]);
  221. }
  222. stv0900_write_reg(intp, R0900_P1_FSPYCFG, 0x6c);
  223. stv0900_write_reg(intp, R0900_P2_FSPYCFG, 0x6c);
  224. stv0900_write_reg(intp, R0900_P1_PDELCTRL2, 0x01);
  225. stv0900_write_reg(intp, R0900_P2_PDELCTRL2, 0x21);
  226. stv0900_write_reg(intp, R0900_P1_PDELCTRL3, 0x20);
  227. stv0900_write_reg(intp, R0900_P2_PDELCTRL3, 0x20);
  228. stv0900_write_reg(intp, R0900_TSTRES0, 0x80);
  229. stv0900_write_reg(intp, R0900_TSTRES0, 0x00);
  230. return STV0900_NO_ERROR;
  231. }
  232. static u32 stv0900_get_mclk_freq(struct stv0900_internal *intp, u32 ext_clk)
  233. {
  234. u32 mclk = 90000000, div = 0, ad_div = 0;
  235. div = stv0900_get_bits(intp, F0900_M_DIV);
  236. ad_div = ((stv0900_get_bits(intp, F0900_SELX1RATIO) == 1) ? 4 : 6);
  237. mclk = (div + 1) * ext_clk / ad_div;
  238. dprintk("%s: Calculated Mclk = %d\n", __func__, mclk);
  239. return mclk;
  240. }
  241. static enum fe_stv0900_error stv0900_set_mclk(struct stv0900_internal *intp, u32 mclk)
  242. {
  243. u32 m_div, clk_sel;
  244. dprintk("%s: Mclk set to %d, Quartz = %d\n", __func__, mclk,
  245. intp->quartz);
  246. if (intp == NULL)
  247. return STV0900_INVALID_HANDLE;
  248. if (intp->errs)
  249. return STV0900_I2C_ERROR;
  250. clk_sel = ((stv0900_get_bits(intp, F0900_SELX1RATIO) == 1) ? 4 : 6);
  251. m_div = ((clk_sel * mclk) / intp->quartz) - 1;
  252. stv0900_write_bits(intp, F0900_M_DIV, m_div);
  253. intp->mclk = stv0900_get_mclk_freq(intp,
  254. intp->quartz);
  255. /*Set the DiseqC frequency to 22KHz */
  256. /*
  257. Formula:
  258. DiseqC_TX_Freq= MasterClock/(32*F22TX_Reg)
  259. DiseqC_RX_Freq= MasterClock/(32*F22RX_Reg)
  260. */
  261. m_div = intp->mclk / 704000;
  262. stv0900_write_reg(intp, R0900_P1_F22TX, m_div);
  263. stv0900_write_reg(intp, R0900_P1_F22RX, m_div);
  264. stv0900_write_reg(intp, R0900_P2_F22TX, m_div);
  265. stv0900_write_reg(intp, R0900_P2_F22RX, m_div);
  266. if ((intp->errs))
  267. return STV0900_I2C_ERROR;
  268. return STV0900_NO_ERROR;
  269. }
  270. static u32 stv0900_get_err_count(struct stv0900_internal *intp, int cntr,
  271. enum fe_stv0900_demod_num demod)
  272. {
  273. u32 lsb, msb, hsb, err_val;
  274. switch (cntr) {
  275. case 0:
  276. default:
  277. hsb = stv0900_get_bits(intp, ERR_CNT12);
  278. msb = stv0900_get_bits(intp, ERR_CNT11);
  279. lsb = stv0900_get_bits(intp, ERR_CNT10);
  280. break;
  281. case 1:
  282. hsb = stv0900_get_bits(intp, ERR_CNT22);
  283. msb = stv0900_get_bits(intp, ERR_CNT21);
  284. lsb = stv0900_get_bits(intp, ERR_CNT20);
  285. break;
  286. }
  287. err_val = (hsb << 16) + (msb << 8) + (lsb);
  288. return err_val;
  289. }
  290. static int stv0900_i2c_gate_ctrl(struct dvb_frontend *fe, int enable)
  291. {
  292. struct stv0900_state *state = fe->demodulator_priv;
  293. struct stv0900_internal *intp = state->internal;
  294. enum fe_stv0900_demod_num demod = state->demod;
  295. stv0900_write_bits(intp, I2CT_ON, enable);
  296. return 0;
  297. }
  298. static void stv0900_set_ts_parallel_serial(struct stv0900_internal *intp,
  299. enum fe_stv0900_clock_type path1_ts,
  300. enum fe_stv0900_clock_type path2_ts)
  301. {
  302. dprintk("%s\n", __func__);
  303. if (intp->chip_id >= 0x20) {
  304. switch (path1_ts) {
  305. case STV0900_PARALLEL_PUNCT_CLOCK:
  306. case STV0900_DVBCI_CLOCK:
  307. switch (path2_ts) {
  308. case STV0900_SERIAL_PUNCT_CLOCK:
  309. case STV0900_SERIAL_CONT_CLOCK:
  310. default:
  311. stv0900_write_reg(intp, R0900_TSGENERAL,
  312. 0x00);
  313. break;
  314. case STV0900_PARALLEL_PUNCT_CLOCK:
  315. case STV0900_DVBCI_CLOCK:
  316. stv0900_write_reg(intp, R0900_TSGENERAL,
  317. 0x06);
  318. stv0900_write_bits(intp,
  319. F0900_P1_TSFIFO_MANSPEED, 3);
  320. stv0900_write_bits(intp,
  321. F0900_P2_TSFIFO_MANSPEED, 0);
  322. stv0900_write_reg(intp,
  323. R0900_P1_TSSPEED, 0x14);
  324. stv0900_write_reg(intp,
  325. R0900_P2_TSSPEED, 0x28);
  326. break;
  327. }
  328. break;
  329. case STV0900_SERIAL_PUNCT_CLOCK:
  330. case STV0900_SERIAL_CONT_CLOCK:
  331. default:
  332. switch (path2_ts) {
  333. case STV0900_SERIAL_PUNCT_CLOCK:
  334. case STV0900_SERIAL_CONT_CLOCK:
  335. default:
  336. stv0900_write_reg(intp,
  337. R0900_TSGENERAL, 0x0C);
  338. break;
  339. case STV0900_PARALLEL_PUNCT_CLOCK:
  340. case STV0900_DVBCI_CLOCK:
  341. stv0900_write_reg(intp,
  342. R0900_TSGENERAL, 0x0A);
  343. dprintk("%s: 0x0a\n", __func__);
  344. break;
  345. }
  346. break;
  347. }
  348. } else {
  349. switch (path1_ts) {
  350. case STV0900_PARALLEL_PUNCT_CLOCK:
  351. case STV0900_DVBCI_CLOCK:
  352. switch (path2_ts) {
  353. case STV0900_SERIAL_PUNCT_CLOCK:
  354. case STV0900_SERIAL_CONT_CLOCK:
  355. default:
  356. stv0900_write_reg(intp, R0900_TSGENERAL1X,
  357. 0x10);
  358. break;
  359. case STV0900_PARALLEL_PUNCT_CLOCK:
  360. case STV0900_DVBCI_CLOCK:
  361. stv0900_write_reg(intp, R0900_TSGENERAL1X,
  362. 0x16);
  363. stv0900_write_bits(intp,
  364. F0900_P1_TSFIFO_MANSPEED, 3);
  365. stv0900_write_bits(intp,
  366. F0900_P2_TSFIFO_MANSPEED, 0);
  367. stv0900_write_reg(intp, R0900_P1_TSSPEED,
  368. 0x14);
  369. stv0900_write_reg(intp, R0900_P2_TSSPEED,
  370. 0x28);
  371. break;
  372. }
  373. break;
  374. case STV0900_SERIAL_PUNCT_CLOCK:
  375. case STV0900_SERIAL_CONT_CLOCK:
  376. default:
  377. switch (path2_ts) {
  378. case STV0900_SERIAL_PUNCT_CLOCK:
  379. case STV0900_SERIAL_CONT_CLOCK:
  380. default:
  381. stv0900_write_reg(intp, R0900_TSGENERAL1X,
  382. 0x14);
  383. break;
  384. case STV0900_PARALLEL_PUNCT_CLOCK:
  385. case STV0900_DVBCI_CLOCK:
  386. stv0900_write_reg(intp, R0900_TSGENERAL1X,
  387. 0x12);
  388. dprintk("%s: 0x12\n", __func__);
  389. break;
  390. }
  391. break;
  392. }
  393. }
  394. switch (path1_ts) {
  395. case STV0900_PARALLEL_PUNCT_CLOCK:
  396. stv0900_write_bits(intp, F0900_P1_TSFIFO_SERIAL, 0x00);
  397. stv0900_write_bits(intp, F0900_P1_TSFIFO_DVBCI, 0x00);
  398. break;
  399. case STV0900_DVBCI_CLOCK:
  400. stv0900_write_bits(intp, F0900_P1_TSFIFO_SERIAL, 0x00);
  401. stv0900_write_bits(intp, F0900_P1_TSFIFO_DVBCI, 0x01);
  402. break;
  403. case STV0900_SERIAL_PUNCT_CLOCK:
  404. stv0900_write_bits(intp, F0900_P1_TSFIFO_SERIAL, 0x01);
  405. stv0900_write_bits(intp, F0900_P1_TSFIFO_DVBCI, 0x00);
  406. break;
  407. case STV0900_SERIAL_CONT_CLOCK:
  408. stv0900_write_bits(intp, F0900_P1_TSFIFO_SERIAL, 0x01);
  409. stv0900_write_bits(intp, F0900_P1_TSFIFO_DVBCI, 0x01);
  410. break;
  411. default:
  412. break;
  413. }
  414. switch (path2_ts) {
  415. case STV0900_PARALLEL_PUNCT_CLOCK:
  416. stv0900_write_bits(intp, F0900_P2_TSFIFO_SERIAL, 0x00);
  417. stv0900_write_bits(intp, F0900_P2_TSFIFO_DVBCI, 0x00);
  418. break;
  419. case STV0900_DVBCI_CLOCK:
  420. stv0900_write_bits(intp, F0900_P2_TSFIFO_SERIAL, 0x00);
  421. stv0900_write_bits(intp, F0900_P2_TSFIFO_DVBCI, 0x01);
  422. break;
  423. case STV0900_SERIAL_PUNCT_CLOCK:
  424. stv0900_write_bits(intp, F0900_P2_TSFIFO_SERIAL, 0x01);
  425. stv0900_write_bits(intp, F0900_P2_TSFIFO_DVBCI, 0x00);
  426. break;
  427. case STV0900_SERIAL_CONT_CLOCK:
  428. stv0900_write_bits(intp, F0900_P2_TSFIFO_SERIAL, 0x01);
  429. stv0900_write_bits(intp, F0900_P2_TSFIFO_DVBCI, 0x01);
  430. break;
  431. default:
  432. break;
  433. }
  434. stv0900_write_bits(intp, F0900_P2_RST_HWARE, 1);
  435. stv0900_write_bits(intp, F0900_P2_RST_HWARE, 0);
  436. stv0900_write_bits(intp, F0900_P1_RST_HWARE, 1);
  437. stv0900_write_bits(intp, F0900_P1_RST_HWARE, 0);
  438. }
  439. void stv0900_set_tuner(struct dvb_frontend *fe, u32 frequency,
  440. u32 bandwidth)
  441. {
  442. struct dvb_frontend_ops *frontend_ops = NULL;
  443. struct dvb_tuner_ops *tuner_ops = NULL;
  444. if (&fe->ops)
  445. frontend_ops = &fe->ops;
  446. if (&frontend_ops->tuner_ops)
  447. tuner_ops = &frontend_ops->tuner_ops;
  448. if (tuner_ops->set_frequency) {
  449. if ((tuner_ops->set_frequency(fe, frequency)) < 0)
  450. dprintk("%s: Invalid parameter\n", __func__);
  451. else
  452. dprintk("%s: Frequency=%d\n", __func__, frequency);
  453. }
  454. if (tuner_ops->set_bandwidth) {
  455. if ((tuner_ops->set_bandwidth(fe, bandwidth)) < 0)
  456. dprintk("%s: Invalid parameter\n", __func__);
  457. else
  458. dprintk("%s: Bandwidth=%d\n", __func__, bandwidth);
  459. }
  460. }
  461. void stv0900_set_bandwidth(struct dvb_frontend *fe, u32 bandwidth)
  462. {
  463. struct dvb_frontend_ops *frontend_ops = NULL;
  464. struct dvb_tuner_ops *tuner_ops = NULL;
  465. if (&fe->ops)
  466. frontend_ops = &fe->ops;
  467. if (&frontend_ops->tuner_ops)
  468. tuner_ops = &frontend_ops->tuner_ops;
  469. if (tuner_ops->set_bandwidth) {
  470. if ((tuner_ops->set_bandwidth(fe, bandwidth)) < 0)
  471. dprintk("%s: Invalid parameter\n", __func__);
  472. else
  473. dprintk("%s: Bandwidth=%d\n", __func__, bandwidth);
  474. }
  475. }
  476. u32 stv0900_get_freq_auto(struct stv0900_internal *intp, int demod)
  477. {
  478. u32 freq, round;
  479. /* Formulat :
  480. Tuner_Frequency(MHz) = Regs / 64
  481. Tuner_granularity(MHz) = Regs / 2048
  482. real_Tuner_Frequency = Tuner_Frequency(MHz) - Tuner_granularity(MHz)
  483. */
  484. freq = (stv0900_get_bits(intp, TUN_RFFREQ2) << 10) +
  485. (stv0900_get_bits(intp, TUN_RFFREQ1) << 2) +
  486. stv0900_get_bits(intp, TUN_RFFREQ0);
  487. freq = (freq * 1000) / 64;
  488. round = (stv0900_get_bits(intp, TUN_RFRESTE1) >> 2) +
  489. stv0900_get_bits(intp, TUN_RFRESTE0);
  490. round = (round * 1000) / 2048;
  491. return freq + round;
  492. }
  493. void stv0900_set_tuner_auto(struct stv0900_internal *intp, u32 Frequency,
  494. u32 Bandwidth, int demod)
  495. {
  496. u32 tunerFrequency;
  497. /* Formulat:
  498. Tuner_frequency_reg= Frequency(MHz)*64
  499. */
  500. tunerFrequency = (Frequency * 64) / 1000;
  501. stv0900_write_bits(intp, TUN_RFFREQ2, (tunerFrequency >> 10));
  502. stv0900_write_bits(intp, TUN_RFFREQ1, (tunerFrequency >> 2) & 0xff);
  503. stv0900_write_bits(intp, TUN_RFFREQ0, (tunerFrequency & 0x03));
  504. /* Low Pass Filter = BW /2 (MHz)*/
  505. stv0900_write_bits(intp, TUN_BW, Bandwidth / 2000000);
  506. /* Tuner Write trig */
  507. stv0900_write_reg(intp, TNRLD, 1);
  508. }
  509. static s32 stv0900_get_rf_level(struct stv0900_internal *intp,
  510. const struct stv0900_table *lookup,
  511. enum fe_stv0900_demod_num demod)
  512. {
  513. s32 agc_gain = 0,
  514. imin,
  515. imax,
  516. i,
  517. rf_lvl = 0;
  518. dprintk("%s\n", __func__);
  519. if ((lookup == NULL) || (lookup->size <= 0))
  520. return 0;
  521. agc_gain = MAKEWORD(stv0900_get_bits(intp, AGCIQ_VALUE1),
  522. stv0900_get_bits(intp, AGCIQ_VALUE0));
  523. imin = 0;
  524. imax = lookup->size - 1;
  525. if (INRANGE(lookup->table[imin].regval, agc_gain,
  526. lookup->table[imax].regval)) {
  527. while ((imax - imin) > 1) {
  528. i = (imax + imin) >> 1;
  529. if (INRANGE(lookup->table[imin].regval,
  530. agc_gain,
  531. lookup->table[i].regval))
  532. imax = i;
  533. else
  534. imin = i;
  535. }
  536. rf_lvl = (s32)agc_gain - lookup->table[imin].regval;
  537. rf_lvl *= (lookup->table[imax].realval -
  538. lookup->table[imin].realval);
  539. rf_lvl /= (lookup->table[imax].regval -
  540. lookup->table[imin].regval);
  541. rf_lvl += lookup->table[imin].realval;
  542. } else if (agc_gain > lookup->table[0].regval)
  543. rf_lvl = 5;
  544. else if (agc_gain < lookup->table[lookup->size-1].regval)
  545. rf_lvl = -100;
  546. dprintk("%s: RFLevel = %d\n", __func__, rf_lvl);
  547. return rf_lvl;
  548. }
  549. static int stv0900_read_signal_strength(struct dvb_frontend *fe, u16 *strength)
  550. {
  551. struct stv0900_state *state = fe->demodulator_priv;
  552. struct stv0900_internal *internal = state->internal;
  553. s32 rflevel = stv0900_get_rf_level(internal, &stv0900_rf,
  554. state->demod);
  555. rflevel = (rflevel + 100) * (65535 / 70);
  556. if (rflevel < 0)
  557. rflevel = 0;
  558. if (rflevel > 65535)
  559. rflevel = 65535;
  560. *strength = rflevel;
  561. return 0;
  562. }
  563. static s32 stv0900_carr_get_quality(struct dvb_frontend *fe,
  564. const struct stv0900_table *lookup)
  565. {
  566. struct stv0900_state *state = fe->demodulator_priv;
  567. struct stv0900_internal *intp = state->internal;
  568. enum fe_stv0900_demod_num demod = state->demod;
  569. s32 c_n = -100,
  570. regval,
  571. imin,
  572. imax,
  573. i,
  574. noise_field1,
  575. noise_field0;
  576. dprintk("%s\n", __func__);
  577. if (stv0900_get_standard(fe, demod) == STV0900_DVBS2_STANDARD) {
  578. noise_field1 = NOSPLHT_NORMED1;
  579. noise_field0 = NOSPLHT_NORMED0;
  580. } else {
  581. noise_field1 = NOSDATAT_NORMED1;
  582. noise_field0 = NOSDATAT_NORMED0;
  583. }
  584. if (stv0900_get_bits(intp, LOCK_DEFINITIF)) {
  585. if ((lookup != NULL) && lookup->size) {
  586. regval = 0;
  587. msleep(5);
  588. for (i = 0; i < 16; i++) {
  589. regval += MAKEWORD(stv0900_get_bits(intp,
  590. noise_field1),
  591. stv0900_get_bits(intp,
  592. noise_field0));
  593. msleep(1);
  594. }
  595. regval /= 16;
  596. imin = 0;
  597. imax = lookup->size - 1;
  598. if (INRANGE(lookup->table[imin].regval,
  599. regval,
  600. lookup->table[imax].regval)) {
  601. while ((imax - imin) > 1) {
  602. i = (imax + imin) >> 1;
  603. if (INRANGE(lookup->table[imin].regval,
  604. regval,
  605. lookup->table[i].regval))
  606. imax = i;
  607. else
  608. imin = i;
  609. }
  610. c_n = ((regval - lookup->table[imin].regval)
  611. * (lookup->table[imax].realval
  612. - lookup->table[imin].realval)
  613. / (lookup->table[imax].regval
  614. - lookup->table[imin].regval))
  615. + lookup->table[imin].realval;
  616. } else if (regval < lookup->table[imin].regval)
  617. c_n = 1000;
  618. }
  619. }
  620. return c_n;
  621. }
  622. static int stv0900_read_ucblocks(struct dvb_frontend *fe, u32 * ucblocks)
  623. {
  624. struct stv0900_state *state = fe->demodulator_priv;
  625. struct stv0900_internal *intp = state->internal;
  626. enum fe_stv0900_demod_num demod = state->demod;
  627. u8 err_val1, err_val0;
  628. u32 header_err_val = 0;
  629. *ucblocks = 0x0;
  630. if (stv0900_get_standard(fe, demod) == STV0900_DVBS2_STANDARD) {
  631. /* DVB-S2 delineator errors count */
  632. /* retreiving number for errnous headers */
  633. err_val1 = stv0900_read_reg(intp, BBFCRCKO1);
  634. err_val0 = stv0900_read_reg(intp, BBFCRCKO0);
  635. header_err_val = (err_val1 << 8) | err_val0;
  636. /* retreiving number for errnous packets */
  637. err_val1 = stv0900_read_reg(intp, UPCRCKO1);
  638. err_val0 = stv0900_read_reg(intp, UPCRCKO0);
  639. *ucblocks = (err_val1 << 8) | err_val0;
  640. *ucblocks += header_err_val;
  641. }
  642. return 0;
  643. }
  644. static int stv0900_read_snr(struct dvb_frontend *fe, u16 *snr)
  645. {
  646. s32 snrlcl = stv0900_carr_get_quality(fe,
  647. (const struct stv0900_table *)&stv0900_s2_cn);
  648. snrlcl = (snrlcl + 30) * 384;
  649. if (snrlcl < 0)
  650. snrlcl = 0;
  651. if (snrlcl > 65535)
  652. snrlcl = 65535;
  653. *snr = snrlcl;
  654. return 0;
  655. }
  656. static u32 stv0900_get_ber(struct stv0900_internal *intp,
  657. enum fe_stv0900_demod_num demod)
  658. {
  659. u32 ber = 10000000, i;
  660. s32 demod_state;
  661. demod_state = stv0900_get_bits(intp, HEADER_MODE);
  662. switch (demod_state) {
  663. case STV0900_SEARCH:
  664. case STV0900_PLH_DETECTED:
  665. default:
  666. ber = 10000000;
  667. break;
  668. case STV0900_DVBS_FOUND:
  669. ber = 0;
  670. for (i = 0; i < 5; i++) {
  671. msleep(5);
  672. ber += stv0900_get_err_count(intp, 0, demod);
  673. }
  674. ber /= 5;
  675. if (stv0900_get_bits(intp, PRFVIT)) {
  676. ber *= 9766;
  677. ber = ber >> 13;
  678. }
  679. break;
  680. case STV0900_DVBS2_FOUND:
  681. ber = 0;
  682. for (i = 0; i < 5; i++) {
  683. msleep(5);
  684. ber += stv0900_get_err_count(intp, 0, demod);
  685. }
  686. ber /= 5;
  687. if (stv0900_get_bits(intp, PKTDELIN_LOCK)) {
  688. ber *= 9766;
  689. ber = ber >> 13;
  690. }
  691. break;
  692. }
  693. return ber;
  694. }
  695. static int stv0900_read_ber(struct dvb_frontend *fe, u32 *ber)
  696. {
  697. struct stv0900_state *state = fe->demodulator_priv;
  698. struct stv0900_internal *internal = state->internal;
  699. *ber = stv0900_get_ber(internal, state->demod);
  700. return 0;
  701. }
  702. int stv0900_get_demod_lock(struct stv0900_internal *intp,
  703. enum fe_stv0900_demod_num demod, s32 time_out)
  704. {
  705. s32 timer = 0,
  706. lock = 0;
  707. enum fe_stv0900_search_state dmd_state;
  708. while ((timer < time_out) && (lock == 0)) {
  709. dmd_state = stv0900_get_bits(intp, HEADER_MODE);
  710. dprintk("Demod State = %d\n", dmd_state);
  711. switch (dmd_state) {
  712. case STV0900_SEARCH:
  713. case STV0900_PLH_DETECTED:
  714. default:
  715. lock = 0;
  716. break;
  717. case STV0900_DVBS2_FOUND:
  718. case STV0900_DVBS_FOUND:
  719. lock = stv0900_get_bits(intp, LOCK_DEFINITIF);
  720. break;
  721. }
  722. if (lock == 0)
  723. msleep(10);
  724. timer += 10;
  725. }
  726. if (lock)
  727. dprintk("DEMOD LOCK OK\n");
  728. else
  729. dprintk("DEMOD LOCK FAIL\n");
  730. return lock;
  731. }
  732. void stv0900_stop_all_s2_modcod(struct stv0900_internal *intp,
  733. enum fe_stv0900_demod_num demod)
  734. {
  735. s32 regflist,
  736. i;
  737. dprintk("%s\n", __func__);
  738. regflist = MODCODLST0;
  739. for (i = 0; i < 16; i++)
  740. stv0900_write_reg(intp, regflist + i, 0xff);
  741. }
  742. void stv0900_activate_s2_modcod(struct stv0900_internal *intp,
  743. enum fe_stv0900_demod_num demod)
  744. {
  745. u32 matype,
  746. mod_code,
  747. fmod,
  748. reg_index,
  749. field_index;
  750. dprintk("%s\n", __func__);
  751. if (intp->chip_id <= 0x11) {
  752. msleep(5);
  753. mod_code = stv0900_read_reg(intp, PLHMODCOD);
  754. matype = mod_code & 0x3;
  755. mod_code = (mod_code & 0x7f) >> 2;
  756. reg_index = MODCODLSTF - mod_code / 2;
  757. field_index = mod_code % 2;
  758. switch (matype) {
  759. case 0:
  760. default:
  761. fmod = 14;
  762. break;
  763. case 1:
  764. fmod = 13;
  765. break;
  766. case 2:
  767. fmod = 11;
  768. break;
  769. case 3:
  770. fmod = 7;
  771. break;
  772. }
  773. if ((INRANGE(STV0900_QPSK_12, mod_code, STV0900_8PSK_910))
  774. && (matype <= 1)) {
  775. if (field_index == 0)
  776. stv0900_write_reg(intp, reg_index,
  777. 0xf0 | fmod);
  778. else
  779. stv0900_write_reg(intp, reg_index,
  780. (fmod << 4) | 0xf);
  781. }
  782. } else if (intp->chip_id >= 0x12) {
  783. for (reg_index = 0; reg_index < 7; reg_index++)
  784. stv0900_write_reg(intp, MODCODLST0 + reg_index, 0xff);
  785. stv0900_write_reg(intp, MODCODLSTE, 0xff);
  786. stv0900_write_reg(intp, MODCODLSTF, 0xcf);
  787. for (reg_index = 0; reg_index < 8; reg_index++)
  788. stv0900_write_reg(intp, MODCODLST7 + reg_index, 0xcc);
  789. }
  790. }
  791. void stv0900_activate_s2_modcod_single(struct stv0900_internal *intp,
  792. enum fe_stv0900_demod_num demod)
  793. {
  794. u32 reg_index;
  795. dprintk("%s\n", __func__);
  796. stv0900_write_reg(intp, MODCODLST0, 0xff);
  797. stv0900_write_reg(intp, MODCODLST1, 0xf0);
  798. stv0900_write_reg(intp, MODCODLSTF, 0x0f);
  799. for (reg_index = 0; reg_index < 13; reg_index++)
  800. stv0900_write_reg(intp, MODCODLST2 + reg_index, 0);
  801. }
  802. static enum dvbfe_algo stv0900_frontend_algo(struct dvb_frontend *fe)
  803. {
  804. return DVBFE_ALGO_CUSTOM;
  805. }
  806. static int stb0900_set_property(struct dvb_frontend *fe,
  807. struct dtv_property *tvp)
  808. {
  809. dprintk("%s(..)\n", __func__);
  810. return 0;
  811. }
  812. static int stb0900_get_property(struct dvb_frontend *fe,
  813. struct dtv_property *tvp)
  814. {
  815. dprintk("%s(..)\n", __func__);
  816. return 0;
  817. }
  818. void stv0900_start_search(struct stv0900_internal *intp,
  819. enum fe_stv0900_demod_num demod)
  820. {
  821. u32 freq;
  822. s16 freq_s16 ;
  823. stv0900_write_bits(intp, DEMOD_MODE, 0x1f);
  824. if (intp->chip_id == 0x10)
  825. stv0900_write_reg(intp, CORRELEXP, 0xaa);
  826. if (intp->chip_id < 0x20)
  827. stv0900_write_reg(intp, CARHDR, 0x55);
  828. if (intp->chip_id <= 0x20) {
  829. if (intp->symbol_rate[0] <= 5000000) {
  830. stv0900_write_reg(intp, CARCFG, 0x44);
  831. stv0900_write_reg(intp, CFRUP1, 0x0f);
  832. stv0900_write_reg(intp, CFRUP0, 0xff);
  833. stv0900_write_reg(intp, CFRLOW1, 0xf0);
  834. stv0900_write_reg(intp, CFRLOW0, 0x00);
  835. stv0900_write_reg(intp, RTCS2, 0x68);
  836. } else {
  837. stv0900_write_reg(intp, CARCFG, 0xc4);
  838. stv0900_write_reg(intp, RTCS2, 0x44);
  839. }
  840. } else { /*cut 3.0 above*/
  841. if (intp->symbol_rate[demod] <= 5000000)
  842. stv0900_write_reg(intp, RTCS2, 0x68);
  843. else
  844. stv0900_write_reg(intp, RTCS2, 0x44);
  845. stv0900_write_reg(intp, CARCFG, 0x46);
  846. if (intp->srch_algo[demod] == STV0900_WARM_START) {
  847. freq = 1000 << 16;
  848. freq /= (intp->mclk / 1000);
  849. freq_s16 = (s16)freq;
  850. } else {
  851. freq = (intp->srch_range[demod] / 2000);
  852. if (intp->symbol_rate[demod] <= 5000000)
  853. freq += 80;
  854. else
  855. freq += 600;
  856. freq = freq << 16;
  857. freq /= (intp->mclk / 1000);
  858. freq_s16 = (s16)freq;
  859. }
  860. stv0900_write_bits(intp, CFR_UP1, MSB(freq_s16));
  861. stv0900_write_bits(intp, CFR_UP0, LSB(freq_s16));
  862. freq_s16 *= (-1);
  863. stv0900_write_bits(intp, CFR_LOW1, MSB(freq_s16));
  864. stv0900_write_bits(intp, CFR_LOW0, LSB(freq_s16));
  865. }
  866. stv0900_write_reg(intp, CFRINIT1, 0);
  867. stv0900_write_reg(intp, CFRINIT0, 0);
  868. if (intp->chip_id >= 0x20) {
  869. stv0900_write_reg(intp, EQUALCFG, 0x41);
  870. stv0900_write_reg(intp, FFECFG, 0x41);
  871. if ((intp->srch_standard[demod] == STV0900_SEARCH_DVBS1) ||
  872. (intp->srch_standard[demod] == STV0900_SEARCH_DSS) ||
  873. (intp->srch_standard[demod] == STV0900_AUTO_SEARCH)) {
  874. stv0900_write_reg(intp, VITSCALE,
  875. 0x82);
  876. stv0900_write_reg(intp, VAVSRVIT, 0x0);
  877. }
  878. }
  879. stv0900_write_reg(intp, SFRSTEP, 0x00);
  880. stv0900_write_reg(intp, TMGTHRISE, 0xe0);
  881. stv0900_write_reg(intp, TMGTHFALL, 0xc0);
  882. stv0900_write_bits(intp, SCAN_ENABLE, 0);
  883. stv0900_write_bits(intp, CFR_AUTOSCAN, 0);
  884. stv0900_write_bits(intp, S1S2_SEQUENTIAL, 0);
  885. stv0900_write_reg(intp, RTC, 0x88);
  886. if (intp->chip_id >= 0x20) {
  887. if (intp->symbol_rate[demod] < 2000000) {
  888. if (intp->chip_id <= 0x20)
  889. stv0900_write_reg(intp, CARFREQ, 0x39);
  890. else /*cut 3.0*/
  891. stv0900_write_reg(intp, CARFREQ, 0x89);
  892. stv0900_write_reg(intp, CARHDR, 0x40);
  893. } else if (intp->symbol_rate[demod] < 10000000) {
  894. stv0900_write_reg(intp, CARFREQ, 0x4c);
  895. stv0900_write_reg(intp, CARHDR, 0x20);
  896. } else {
  897. stv0900_write_reg(intp, CARFREQ, 0x4b);
  898. stv0900_write_reg(intp, CARHDR, 0x20);
  899. }
  900. } else {
  901. if (intp->symbol_rate[demod] < 10000000)
  902. stv0900_write_reg(intp, CARFREQ, 0xef);
  903. else
  904. stv0900_write_reg(intp, CARFREQ, 0xed);
  905. }
  906. switch (intp->srch_algo[demod]) {
  907. case STV0900_WARM_START:
  908. stv0900_write_reg(intp, DMDISTATE, 0x1f);
  909. stv0900_write_reg(intp, DMDISTATE, 0x18);
  910. break;
  911. case STV0900_COLD_START:
  912. stv0900_write_reg(intp, DMDISTATE, 0x1f);
  913. stv0900_write_reg(intp, DMDISTATE, 0x15);
  914. break;
  915. default:
  916. break;
  917. }
  918. }
  919. u8 stv0900_get_optim_carr_loop(s32 srate, enum fe_stv0900_modcode modcode,
  920. s32 pilot, u8 chip_id)
  921. {
  922. u8 aclc_value = 0x29;
  923. s32 i;
  924. const struct stv0900_car_loop_optim *cls2, *cllqs2, *cllas2;
  925. dprintk("%s\n", __func__);
  926. if (chip_id <= 0x12) {
  927. cls2 = FE_STV0900_S2CarLoop;
  928. cllqs2 = FE_STV0900_S2LowQPCarLoopCut30;
  929. cllas2 = FE_STV0900_S2APSKCarLoopCut30;
  930. } else if (chip_id == 0x20) {
  931. cls2 = FE_STV0900_S2CarLoopCut20;
  932. cllqs2 = FE_STV0900_S2LowQPCarLoopCut20;
  933. cllas2 = FE_STV0900_S2APSKCarLoopCut20;
  934. } else {
  935. cls2 = FE_STV0900_S2CarLoopCut30;
  936. cllqs2 = FE_STV0900_S2LowQPCarLoopCut30;
  937. cllas2 = FE_STV0900_S2APSKCarLoopCut30;
  938. }
  939. if (modcode < STV0900_QPSK_12) {
  940. i = 0;
  941. while ((i < 3) && (modcode != cllqs2[i].modcode))
  942. i++;
  943. if (i >= 3)
  944. i = 2;
  945. } else {
  946. i = 0;
  947. while ((i < 14) && (modcode != cls2[i].modcode))
  948. i++;
  949. if (i >= 14) {
  950. i = 0;
  951. while ((i < 11) && (modcode != cllas2[i].modcode))
  952. i++;
  953. if (i >= 11)
  954. i = 10;
  955. }
  956. }
  957. if (modcode <= STV0900_QPSK_25) {
  958. if (pilot) {
  959. if (srate <= 3000000)
  960. aclc_value = cllqs2[i].car_loop_pilots_on_2;
  961. else if (srate <= 7000000)
  962. aclc_value = cllqs2[i].car_loop_pilots_on_5;
  963. else if (srate <= 15000000)
  964. aclc_value = cllqs2[i].car_loop_pilots_on_10;
  965. else if (srate <= 25000000)
  966. aclc_value = cllqs2[i].car_loop_pilots_on_20;
  967. else
  968. aclc_value = cllqs2[i].car_loop_pilots_on_30;
  969. } else {
  970. if (srate <= 3000000)
  971. aclc_value = cllqs2[i].car_loop_pilots_off_2;
  972. else if (srate <= 7000000)
  973. aclc_value = cllqs2[i].car_loop_pilots_off_5;
  974. else if (srate <= 15000000)
  975. aclc_value = cllqs2[i].car_loop_pilots_off_10;
  976. else if (srate <= 25000000)
  977. aclc_value = cllqs2[i].car_loop_pilots_off_20;
  978. else
  979. aclc_value = cllqs2[i].car_loop_pilots_off_30;
  980. }
  981. } else if (modcode <= STV0900_8PSK_910) {
  982. if (pilot) {
  983. if (srate <= 3000000)
  984. aclc_value = cls2[i].car_loop_pilots_on_2;
  985. else if (srate <= 7000000)
  986. aclc_value = cls2[i].car_loop_pilots_on_5;
  987. else if (srate <= 15000000)
  988. aclc_value = cls2[i].car_loop_pilots_on_10;
  989. else if (srate <= 25000000)
  990. aclc_value = cls2[i].car_loop_pilots_on_20;
  991. else
  992. aclc_value = cls2[i].car_loop_pilots_on_30;
  993. } else {
  994. if (srate <= 3000000)
  995. aclc_value = cls2[i].car_loop_pilots_off_2;
  996. else if (srate <= 7000000)
  997. aclc_value = cls2[i].car_loop_pilots_off_5;
  998. else if (srate <= 15000000)
  999. aclc_value = cls2[i].car_loop_pilots_off_10;
  1000. else if (srate <= 25000000)
  1001. aclc_value = cls2[i].car_loop_pilots_off_20;
  1002. else
  1003. aclc_value = cls2[i].car_loop_pilots_off_30;
  1004. }
  1005. } else {
  1006. if (srate <= 3000000)
  1007. aclc_value = cllas2[i].car_loop_pilots_on_2;
  1008. else if (srate <= 7000000)
  1009. aclc_value = cllas2[i].car_loop_pilots_on_5;
  1010. else if (srate <= 15000000)
  1011. aclc_value = cllas2[i].car_loop_pilots_on_10;
  1012. else if (srate <= 25000000)
  1013. aclc_value = cllas2[i].car_loop_pilots_on_20;
  1014. else
  1015. aclc_value = cllas2[i].car_loop_pilots_on_30;
  1016. }
  1017. return aclc_value;
  1018. }
  1019. u8 stv0900_get_optim_short_carr_loop(s32 srate,
  1020. enum fe_stv0900_modulation modulation,
  1021. u8 chip_id)
  1022. {
  1023. const struct stv0900_short_frames_car_loop_optim *s2scl;
  1024. const struct stv0900_short_frames_car_loop_optim_vs_mod *s2sclc30;
  1025. s32 mod_index = 0;
  1026. u8 aclc_value = 0x0b;
  1027. dprintk("%s\n", __func__);
  1028. s2scl = FE_STV0900_S2ShortCarLoop;
  1029. s2sclc30 = FE_STV0900_S2ShortCarLoopCut30;
  1030. switch (modulation) {
  1031. case STV0900_QPSK:
  1032. default:
  1033. mod_index = 0;
  1034. break;
  1035. case STV0900_8PSK:
  1036. mod_index = 1;
  1037. break;
  1038. case STV0900_16APSK:
  1039. mod_index = 2;
  1040. break;
  1041. case STV0900_32APSK:
  1042. mod_index = 3;
  1043. break;
  1044. }
  1045. if (chip_id >= 0x30) {
  1046. if (srate <= 3000000)
  1047. aclc_value = s2sclc30[mod_index].car_loop_2;
  1048. else if (srate <= 7000000)
  1049. aclc_value = s2sclc30[mod_index].car_loop_5;
  1050. else if (srate <= 15000000)
  1051. aclc_value = s2sclc30[mod_index].car_loop_10;
  1052. else if (srate <= 25000000)
  1053. aclc_value = s2sclc30[mod_index].car_loop_20;
  1054. else
  1055. aclc_value = s2sclc30[mod_index].car_loop_30;
  1056. } else if (chip_id >= 0x20) {
  1057. if (srate <= 3000000)
  1058. aclc_value = s2scl[mod_index].car_loop_cut20_2;
  1059. else if (srate <= 7000000)
  1060. aclc_value = s2scl[mod_index].car_loop_cut20_5;
  1061. else if (srate <= 15000000)
  1062. aclc_value = s2scl[mod_index].car_loop_cut20_10;
  1063. else if (srate <= 25000000)
  1064. aclc_value = s2scl[mod_index].car_loop_cut20_20;
  1065. else
  1066. aclc_value = s2scl[mod_index].car_loop_cut20_30;
  1067. } else {
  1068. if (srate <= 3000000)
  1069. aclc_value = s2scl[mod_index].car_loop_cut12_2;
  1070. else if (srate <= 7000000)
  1071. aclc_value = s2scl[mod_index].car_loop_cut12_5;
  1072. else if (srate <= 15000000)
  1073. aclc_value = s2scl[mod_index].car_loop_cut12_10;
  1074. else if (srate <= 25000000)
  1075. aclc_value = s2scl[mod_index].car_loop_cut12_20;
  1076. else
  1077. aclc_value = s2scl[mod_index].car_loop_cut12_30;
  1078. }
  1079. return aclc_value;
  1080. }
  1081. static
  1082. enum fe_stv0900_error stv0900_st_dvbs2_single(struct stv0900_internal *intp,
  1083. enum fe_stv0900_demod_mode LDPC_Mode,
  1084. enum fe_stv0900_demod_num demod)
  1085. {
  1086. enum fe_stv0900_error error = STV0900_NO_ERROR;
  1087. s32 reg_ind;
  1088. dprintk("%s\n", __func__);
  1089. switch (LDPC_Mode) {
  1090. case STV0900_DUAL:
  1091. default:
  1092. if ((intp->demod_mode != STV0900_DUAL)
  1093. || (stv0900_get_bits(intp, F0900_DDEMOD) != 1)) {
  1094. stv0900_write_reg(intp, R0900_GENCFG, 0x1d);
  1095. intp->demod_mode = STV0900_DUAL;
  1096. stv0900_write_bits(intp, F0900_FRESFEC, 1);
  1097. stv0900_write_bits(intp, F0900_FRESFEC, 0);
  1098. for (reg_ind = 0; reg_ind < 7; reg_ind++)
  1099. stv0900_write_reg(intp,
  1100. R0900_P1_MODCODLST0 + reg_ind,
  1101. 0xff);
  1102. for (reg_ind = 0; reg_ind < 8; reg_ind++)
  1103. stv0900_write_reg(intp,
  1104. R0900_P1_MODCODLST7 + reg_ind,
  1105. 0xcc);
  1106. stv0900_write_reg(intp, R0900_P1_MODCODLSTE, 0xff);
  1107. stv0900_write_reg(intp, R0900_P1_MODCODLSTF, 0xcf);
  1108. for (reg_ind = 0; reg_ind < 7; reg_ind++)
  1109. stv0900_write_reg(intp,
  1110. R0900_P2_MODCODLST0 + reg_ind,
  1111. 0xff);
  1112. for (reg_ind = 0; reg_ind < 8; reg_ind++)
  1113. stv0900_write_reg(intp,
  1114. R0900_P2_MODCODLST7 + reg_ind,
  1115. 0xcc);
  1116. stv0900_write_reg(intp, R0900_P2_MODCODLSTE, 0xff);
  1117. stv0900_write_reg(intp, R0900_P2_MODCODLSTF, 0xcf);
  1118. }
  1119. break;
  1120. case STV0900_SINGLE:
  1121. if (demod == STV0900_DEMOD_2) {
  1122. stv0900_stop_all_s2_modcod(intp, STV0900_DEMOD_1);
  1123. stv0900_activate_s2_modcod_single(intp,
  1124. STV0900_DEMOD_2);
  1125. stv0900_write_reg(intp, R0900_GENCFG, 0x06);
  1126. } else {
  1127. stv0900_stop_all_s2_modcod(intp, STV0900_DEMOD_2);
  1128. stv0900_activate_s2_modcod_single(intp,
  1129. STV0900_DEMOD_1);
  1130. stv0900_write_reg(intp, R0900_GENCFG, 0x04);
  1131. }
  1132. intp->demod_mode = STV0900_SINGLE;
  1133. stv0900_write_bits(intp, F0900_FRESFEC, 1);
  1134. stv0900_write_bits(intp, F0900_FRESFEC, 0);
  1135. stv0900_write_bits(intp, F0900_P1_ALGOSWRST, 1);
  1136. stv0900_write_bits(intp, F0900_P1_ALGOSWRST, 0);
  1137. stv0900_write_bits(intp, F0900_P2_ALGOSWRST, 1);
  1138. stv0900_write_bits(intp, F0900_P2_ALGOSWRST, 0);
  1139. break;
  1140. }
  1141. return error;
  1142. }
  1143. static enum fe_stv0900_error stv0900_init_internal(struct dvb_frontend *fe,
  1144. struct stv0900_init_params *p_init)
  1145. {
  1146. struct stv0900_state *state = fe->demodulator_priv;
  1147. enum fe_stv0900_error error = STV0900_NO_ERROR;
  1148. enum fe_stv0900_error demodError = STV0900_NO_ERROR;
  1149. struct stv0900_internal *intp = NULL;
  1150. int selosci, i;
  1151. struct stv0900_inode *temp_int = find_inode(state->i2c_adap,
  1152. state->config->demod_address);
  1153. dprintk("%s\n", __func__);
  1154. if ((temp_int != NULL) && (p_init->demod_mode == STV0900_DUAL)) {
  1155. state->internal = temp_int->internal;
  1156. (state->internal->dmds_used)++;
  1157. dprintk("%s: Find Internal Structure!\n", __func__);
  1158. return STV0900_NO_ERROR;
  1159. } else {
  1160. state->internal = kmalloc(sizeof(struct stv0900_internal),
  1161. GFP_KERNEL);
  1162. if (state->internal == NULL)
  1163. return STV0900_INVALID_HANDLE;
  1164. temp_int = append_internal(state->internal);
  1165. if (temp_int == NULL) {
  1166. kfree(state->internal);
  1167. state->internal = NULL;
  1168. return STV0900_INVALID_HANDLE;
  1169. }
  1170. state->internal->dmds_used = 1;
  1171. state->internal->i2c_adap = state->i2c_adap;
  1172. state->internal->i2c_addr = state->config->demod_address;
  1173. state->internal->clkmode = state->config->clkmode;
  1174. state->internal->errs = STV0900_NO_ERROR;
  1175. dprintk("%s: Create New Internal Structure!\n", __func__);
  1176. }
  1177. if (state->internal == NULL) {
  1178. error = STV0900_INVALID_HANDLE;
  1179. return error;
  1180. }
  1181. demodError = stv0900_initialize(state->internal);
  1182. if (demodError == STV0900_NO_ERROR) {
  1183. error = STV0900_NO_ERROR;
  1184. } else {
  1185. if (demodError == STV0900_INVALID_HANDLE)
  1186. error = STV0900_INVALID_HANDLE;
  1187. else
  1188. error = STV0900_I2C_ERROR;
  1189. return error;
  1190. }
  1191. intp = state->internal;
  1192. intp->demod_mode = p_init->demod_mode;
  1193. stv0900_st_dvbs2_single(intp, intp->demod_mode, STV0900_DEMOD_1);
  1194. intp->chip_id = stv0900_read_reg(intp, R0900_MID);
  1195. intp->rolloff = p_init->rolloff;
  1196. intp->quartz = p_init->dmd_ref_clk;
  1197. stv0900_write_bits(intp, F0900_P1_ROLLOFF_CONTROL, p_init->rolloff);
  1198. stv0900_write_bits(intp, F0900_P2_ROLLOFF_CONTROL, p_init->rolloff);
  1199. intp->ts_config = p_init->ts_config;
  1200. if (intp->ts_config == NULL)
  1201. stv0900_set_ts_parallel_serial(intp,
  1202. p_init->path1_ts_clock,
  1203. p_init->path2_ts_clock);
  1204. else {
  1205. for (i = 0; intp->ts_config[i].addr != 0xffff; i++)
  1206. stv0900_write_reg(intp,
  1207. intp->ts_config[i].addr,
  1208. intp->ts_config[i].val);
  1209. stv0900_write_bits(intp, F0900_P2_RST_HWARE, 1);
  1210. stv0900_write_bits(intp, F0900_P2_RST_HWARE, 0);
  1211. stv0900_write_bits(intp, F0900_P1_RST_HWARE, 1);
  1212. stv0900_write_bits(intp, F0900_P1_RST_HWARE, 0);
  1213. }
  1214. intp->tuner_type[0] = p_init->tuner1_type;
  1215. intp->tuner_type[1] = p_init->tuner2_type;
  1216. /* tuner init */
  1217. switch (p_init->tuner1_type) {
  1218. case 3: /*FE_AUTO_STB6100:*/
  1219. stv0900_write_reg(intp, R0900_P1_TNRCFG, 0x3c);
  1220. stv0900_write_reg(intp, R0900_P1_TNRCFG2, 0x86);
  1221. stv0900_write_reg(intp, R0900_P1_TNRCFG3, 0x18);
  1222. stv0900_write_reg(intp, R0900_P1_TNRXTAL, 27); /* 27MHz */
  1223. stv0900_write_reg(intp, R0900_P1_TNRSTEPS, 0x05);
  1224. stv0900_write_reg(intp, R0900_P1_TNRGAIN, 0x17);
  1225. stv0900_write_reg(intp, R0900_P1_TNRADJ, 0x1f);
  1226. stv0900_write_reg(intp, R0900_P1_TNRCTL2, 0x0);
  1227. stv0900_write_bits(intp, F0900_P1_TUN_TYPE, 3);
  1228. break;
  1229. /* case FE_SW_TUNER: */
  1230. default:
  1231. stv0900_write_bits(intp, F0900_P1_TUN_TYPE, 6);
  1232. break;
  1233. }
  1234. stv0900_write_bits(intp, F0900_P1_TUN_MADDRESS, p_init->tun1_maddress);
  1235. switch (p_init->tuner1_adc) {
  1236. case 1:
  1237. stv0900_write_reg(intp, R0900_TSTTNR1, 0x26);
  1238. break;
  1239. default:
  1240. break;
  1241. }
  1242. stv0900_write_reg(intp, R0900_P1_TNRLD, 1); /* hw tuner */
  1243. /* tuner init */
  1244. switch (p_init->tuner2_type) {
  1245. case 3: /*FE_AUTO_STB6100:*/
  1246. stv0900_write_reg(intp, R0900_P2_TNRCFG, 0x3c);
  1247. stv0900_write_reg(intp, R0900_P2_TNRCFG2, 0x86);
  1248. stv0900_write_reg(intp, R0900_P2_TNRCFG3, 0x18);
  1249. stv0900_write_reg(intp, R0900_P2_TNRXTAL, 27); /* 27MHz */
  1250. stv0900_write_reg(intp, R0900_P2_TNRSTEPS, 0x05);
  1251. stv0900_write_reg(intp, R0900_P2_TNRGAIN, 0x17);
  1252. stv0900_write_reg(intp, R0900_P2_TNRADJ, 0x1f);
  1253. stv0900_write_reg(intp, R0900_P2_TNRCTL2, 0x0);
  1254. stv0900_write_bits(intp, F0900_P2_TUN_TYPE, 3);
  1255. break;
  1256. /* case FE_SW_TUNER: */
  1257. default:
  1258. stv0900_write_bits(intp, F0900_P2_TUN_TYPE, 6);
  1259. break;
  1260. }
  1261. stv0900_write_bits(intp, F0900_P2_TUN_MADDRESS, p_init->tun2_maddress);
  1262. switch (p_init->tuner2_adc) {
  1263. case 1:
  1264. stv0900_write_reg(intp, R0900_TSTTNR3, 0x26);
  1265. break;
  1266. default:
  1267. break;
  1268. }
  1269. stv0900_write_reg(intp, R0900_P2_TNRLD, 1); /* hw tuner */
  1270. stv0900_write_bits(intp, F0900_P1_TUN_IQSWAP, p_init->tun1_iq_inv);
  1271. stv0900_write_bits(intp, F0900_P2_TUN_IQSWAP, p_init->tun2_iq_inv);
  1272. stv0900_set_mclk(intp, 135000000);
  1273. msleep(3);
  1274. switch (intp->clkmode) {
  1275. case 0:
  1276. case 2:
  1277. stv0900_write_reg(intp, R0900_SYNTCTRL, 0x20 | intp->clkmode);
  1278. break;
  1279. default:
  1280. selosci = 0x02 & stv0900_read_reg(intp, R0900_SYNTCTRL);
  1281. stv0900_write_reg(intp, R0900_SYNTCTRL, 0x20 | selosci);
  1282. break;
  1283. }
  1284. msleep(3);
  1285. intp->mclk = stv0900_get_mclk_freq(intp, intp->quartz);
  1286. if (intp->errs)
  1287. error = STV0900_I2C_ERROR;
  1288. return error;
  1289. }
  1290. static int stv0900_status(struct stv0900_internal *intp,
  1291. enum fe_stv0900_demod_num demod)
  1292. {
  1293. enum fe_stv0900_search_state demod_state;
  1294. int locked = FALSE;
  1295. u8 tsbitrate0_val, tsbitrate1_val;
  1296. s32 bitrate;
  1297. demod_state = stv0900_get_bits(intp, HEADER_MODE);
  1298. switch (demod_state) {
  1299. case STV0900_SEARCH:
  1300. case STV0900_PLH_DETECTED:
  1301. default:
  1302. locked = FALSE;
  1303. break;
  1304. case STV0900_DVBS2_FOUND:
  1305. locked = stv0900_get_bits(intp, LOCK_DEFINITIF) &&
  1306. stv0900_get_bits(intp, PKTDELIN_LOCK) &&
  1307. stv0900_get_bits(intp, TSFIFO_LINEOK);
  1308. break;
  1309. case STV0900_DVBS_FOUND:
  1310. locked = stv0900_get_bits(intp, LOCK_DEFINITIF) &&
  1311. stv0900_get_bits(intp, LOCKEDVIT) &&
  1312. stv0900_get_bits(intp, TSFIFO_LINEOK);
  1313. break;
  1314. }
  1315. dprintk("%s: locked = %d\n", __func__, locked);
  1316. if (stvdebug) {
  1317. /* Print TS bitrate */
  1318. tsbitrate0_val = stv0900_read_reg(intp, TSBITRATE0);
  1319. tsbitrate1_val = stv0900_read_reg(intp, TSBITRATE1);
  1320. /* Formula Bit rate = Mclk * px_tsfifo_bitrate / 16384 */
  1321. bitrate = (stv0900_get_mclk_freq(intp, intp->quartz)/1000000)
  1322. * (tsbitrate1_val << 8 | tsbitrate0_val);
  1323. bitrate /= 16384;
  1324. dprintk("TS bitrate = %d Mbit/sec \n", bitrate);
  1325. };
  1326. return locked;
  1327. }
  1328. static enum dvbfe_search stv0900_search(struct dvb_frontend *fe,
  1329. struct dvb_frontend_parameters *params)
  1330. {
  1331. struct stv0900_state *state = fe->demodulator_priv;
  1332. struct stv0900_internal *intp = state->internal;
  1333. enum fe_stv0900_demod_num demod = state->demod;
  1334. struct dtv_frontend_properties *c = &fe->dtv_property_cache;
  1335. struct stv0900_search_params p_search;
  1336. struct stv0900_signal_info p_result = intp->result[demod];
  1337. enum fe_stv0900_error error = STV0900_NO_ERROR;
  1338. dprintk("%s: ", __func__);
  1339. if (!(INRANGE(100000, c->symbol_rate, 70000000)))
  1340. return DVBFE_ALGO_SEARCH_FAILED;
  1341. if (state->config->set_ts_params)
  1342. state->config->set_ts_params(fe, 0);
  1343. p_result.locked = FALSE;
  1344. p_search.path = demod;
  1345. p_search.frequency = c->frequency;
  1346. p_search.symbol_rate = c->symbol_rate;
  1347. p_search.search_range = 10000000;
  1348. p_search.fec = STV0900_FEC_UNKNOWN;
  1349. p_search.standard = STV0900_AUTO_SEARCH;
  1350. p_search.iq_inversion = STV0900_IQ_AUTO;
  1351. p_search.search_algo = STV0900_BLIND_SEARCH;
  1352. /* Speeds up DVB-S searching */
  1353. if (c->delivery_system == SYS_DVBS)
  1354. p_search.standard = STV0900_SEARCH_DVBS1;
  1355. intp->srch_standard[demod] = p_search.standard;
  1356. intp->symbol_rate[demod] = p_search.symbol_rate;
  1357. intp->srch_range[demod] = p_search.search_range;
  1358. intp->freq[demod] = p_search.frequency;
  1359. intp->srch_algo[demod] = p_search.search_algo;
  1360. intp->srch_iq_inv[demod] = p_search.iq_inversion;
  1361. intp->fec[demod] = p_search.fec;
  1362. if ((stv0900_algo(fe) == STV0900_RANGEOK) &&
  1363. (intp->errs == STV0900_NO_ERROR)) {
  1364. p_result.locked = intp->result[demod].locked;
  1365. p_result.standard = intp->result[demod].standard;
  1366. p_result.frequency = intp->result[demod].frequency;
  1367. p_result.symbol_rate = intp->result[demod].symbol_rate;
  1368. p_result.fec = intp->result[demod].fec;
  1369. p_result.modcode = intp->result[demod].modcode;
  1370. p_result.pilot = intp->result[demod].pilot;
  1371. p_result.frame_len = intp->result[demod].frame_len;
  1372. p_result.spectrum = intp->result[demod].spectrum;
  1373. p_result.rolloff = intp->result[demod].rolloff;
  1374. p_result.modulation = intp->result[demod].modulation;
  1375. } else {
  1376. p_result.locked = FALSE;
  1377. switch (intp->err[demod]) {
  1378. case STV0900_I2C_ERROR:
  1379. error = STV0900_I2C_ERROR;
  1380. break;
  1381. case STV0900_NO_ERROR:
  1382. default:
  1383. error = STV0900_SEARCH_FAILED;
  1384. break;
  1385. }
  1386. }
  1387. if ((p_result.locked == TRUE) && (error == STV0900_NO_ERROR)) {
  1388. dprintk("Search Success\n");
  1389. return DVBFE_ALGO_SEARCH_SUCCESS;
  1390. } else {
  1391. dprintk("Search Fail\n");
  1392. return DVBFE_ALGO_SEARCH_FAILED;
  1393. }
  1394. }
  1395. static int stv0900_read_status(struct dvb_frontend *fe, enum fe_status *status)
  1396. {
  1397. struct stv0900_state *state = fe->demodulator_priv;
  1398. dprintk("%s: ", __func__);
  1399. if ((stv0900_status(state->internal, state->demod)) == TRUE) {
  1400. dprintk("DEMOD LOCK OK\n");
  1401. *status = FE_HAS_CARRIER
  1402. | FE_HAS_VITERBI
  1403. | FE_HAS_SYNC
  1404. | FE_HAS_LOCK;
  1405. if (state->config->set_lock_led)
  1406. state->config->set_lock_led(fe, 1);
  1407. } else {
  1408. *status = 0;
  1409. if (state->config->set_lock_led)
  1410. state->config->set_lock_led(fe, 0);
  1411. dprintk("DEMOD LOCK FAIL\n");
  1412. }
  1413. return 0;
  1414. }
  1415. static int stv0900_track(struct dvb_frontend *fe,
  1416. struct dvb_frontend_parameters *p)
  1417. {
  1418. return 0;
  1419. }
  1420. static int stv0900_stop_ts(struct dvb_frontend *fe, int stop_ts)
  1421. {
  1422. struct stv0900_state *state = fe->demodulator_priv;
  1423. struct stv0900_internal *intp = state->internal;
  1424. enum fe_stv0900_demod_num demod = state->demod;
  1425. if (stop_ts == TRUE)
  1426. stv0900_write_bits(intp, RST_HWARE, 1);
  1427. else
  1428. stv0900_write_bits(intp, RST_HWARE, 0);
  1429. return 0;
  1430. }
  1431. static int stv0900_diseqc_init(struct dvb_frontend *fe)
  1432. {
  1433. struct stv0900_state *state = fe->demodulator_priv;
  1434. struct stv0900_internal *intp = state->internal;
  1435. enum fe_stv0900_demod_num demod = state->demod;
  1436. stv0900_write_bits(intp, DISTX_MODE, state->config->diseqc_mode);
  1437. stv0900_write_bits(intp, DISEQC_RESET, 1);
  1438. stv0900_write_bits(intp, DISEQC_RESET, 0);
  1439. return 0;
  1440. }
  1441. static int stv0900_init(struct dvb_frontend *fe)
  1442. {
  1443. dprintk("%s\n", __func__);
  1444. stv0900_stop_ts(fe, 1);
  1445. stv0900_diseqc_init(fe);
  1446. return 0;
  1447. }
  1448. static int stv0900_diseqc_send(struct stv0900_internal *intp , u8 *data,
  1449. u32 NbData, enum fe_stv0900_demod_num demod)
  1450. {
  1451. s32 i = 0;
  1452. stv0900_write_bits(intp, DIS_PRECHARGE, 1);
  1453. while (i < NbData) {
  1454. while (stv0900_get_bits(intp, FIFO_FULL))
  1455. ;/* checkpatch complains */
  1456. stv0900_write_reg(intp, DISTXDATA, data[i]);
  1457. i++;
  1458. }
  1459. stv0900_write_bits(intp, DIS_PRECHARGE, 0);
  1460. i = 0;
  1461. while ((stv0900_get_bits(intp, TX_IDLE) != 1) && (i < 10)) {
  1462. msleep(10);
  1463. i++;
  1464. }
  1465. return 0;
  1466. }
  1467. static int stv0900_send_master_cmd(struct dvb_frontend *fe,
  1468. struct dvb_diseqc_master_cmd *cmd)
  1469. {
  1470. struct stv0900_state *state = fe->demodulator_priv;
  1471. return stv0900_diseqc_send(state->internal,
  1472. cmd->msg,
  1473. cmd->msg_len,
  1474. state->demod);
  1475. }
  1476. static int stv0900_send_burst(struct dvb_frontend *fe, fe_sec_mini_cmd_t burst)
  1477. {
  1478. struct stv0900_state *state = fe->demodulator_priv;
  1479. struct stv0900_internal *intp = state->internal;
  1480. enum fe_stv0900_demod_num demod = state->demod;
  1481. u8 data;
  1482. switch (burst) {
  1483. case SEC_MINI_A:
  1484. stv0900_write_bits(intp, DISTX_MODE, 3);/* Unmodulated */
  1485. data = 0x00;
  1486. stv0900_diseqc_send(intp, &data, 1, state->demod);
  1487. break;
  1488. case SEC_MINI_B:
  1489. stv0900_write_bits(intp, DISTX_MODE, 2);/* Modulated */
  1490. data = 0xff;
  1491. stv0900_diseqc_send(intp, &data, 1, state->demod);
  1492. break;
  1493. }
  1494. return 0;
  1495. }
  1496. static int stv0900_recv_slave_reply(struct dvb_frontend *fe,
  1497. struct dvb_diseqc_slave_reply *reply)
  1498. {
  1499. struct stv0900_state *state = fe->demodulator_priv;
  1500. struct stv0900_internal *intp = state->internal;
  1501. enum fe_stv0900_demod_num demod = state->demod;
  1502. s32 i = 0;
  1503. reply->msg_len = 0;
  1504. while ((stv0900_get_bits(intp, RX_END) != 1) && (i < 10)) {
  1505. msleep(10);
  1506. i++;
  1507. }
  1508. if (stv0900_get_bits(intp, RX_END)) {
  1509. reply->msg_len = stv0900_get_bits(intp, FIFO_BYTENBR);
  1510. for (i = 0; i < reply->msg_len; i++)
  1511. reply->msg[i] = stv0900_read_reg(intp, DISRXDATA);
  1512. }
  1513. return 0;
  1514. }
  1515. static int stv0900_set_tone(struct dvb_frontend *fe, fe_sec_tone_mode_t toneoff)
  1516. {
  1517. struct stv0900_state *state = fe->demodulator_priv;
  1518. struct stv0900_internal *intp = state->internal;
  1519. enum fe_stv0900_demod_num demod = state->demod;
  1520. dprintk("%s: %s\n", __func__, ((toneoff == 0) ? "On" : "Off"));
  1521. switch (toneoff) {
  1522. case SEC_TONE_ON:
  1523. /*Set the DiseqC mode to 22Khz _continues_ tone*/
  1524. stv0900_write_bits(intp, DISTX_MODE, 0);
  1525. stv0900_write_bits(intp, DISEQC_RESET, 1);
  1526. /*release DiseqC reset to enable the 22KHz tone*/
  1527. stv0900_write_bits(intp, DISEQC_RESET, 0);
  1528. break;
  1529. case SEC_TONE_OFF:
  1530. /*return diseqc mode to config->diseqc_mode.
  1531. Usually it's without _continues_ tone */
  1532. stv0900_write_bits(intp, DISTX_MODE,
  1533. state->config->diseqc_mode);
  1534. /*maintain the DiseqC reset to disable the 22KHz tone*/
  1535. stv0900_write_bits(intp, DISEQC_RESET, 1);
  1536. stv0900_write_bits(intp, DISEQC_RESET, 0);
  1537. break;
  1538. default:
  1539. return -EINVAL;
  1540. }
  1541. return 0;
  1542. }
  1543. static void stv0900_release(struct dvb_frontend *fe)
  1544. {
  1545. struct stv0900_state *state = fe->demodulator_priv;
  1546. dprintk("%s\n", __func__);
  1547. if (state->config->set_lock_led)
  1548. state->config->set_lock_led(fe, 0);
  1549. if ((--(state->internal->dmds_used)) <= 0) {
  1550. dprintk("%s: Actually removing\n", __func__);
  1551. remove_inode(state->internal);
  1552. kfree(state->internal);
  1553. }
  1554. kfree(state);
  1555. }
  1556. static int stv0900_sleep(struct dvb_frontend *fe)
  1557. {
  1558. struct stv0900_state *state = fe->demodulator_priv;
  1559. dprintk("%s\n", __func__);
  1560. if (state->config->set_lock_led)
  1561. state->config->set_lock_led(fe, 0);
  1562. return 0;
  1563. }
  1564. static int stv0900_get_frontend(struct dvb_frontend *fe,
  1565. struct dvb_frontend_parameters *p)
  1566. {
  1567. struct stv0900_state *state = fe->demodulator_priv;
  1568. struct stv0900_internal *intp = state->internal;
  1569. enum fe_stv0900_demod_num demod = state->demod;
  1570. struct stv0900_signal_info p_result = intp->result[demod];
  1571. p->frequency = p_result.locked ? p_result.frequency : 0;
  1572. p->u.qpsk.symbol_rate = p_result.locked ? p_result.symbol_rate : 0;
  1573. return 0;
  1574. }
  1575. static struct dvb_frontend_ops stv0900_ops = {
  1576. .info = {
  1577. .name = "STV0900 frontend",
  1578. .type = FE_QPSK,
  1579. .frequency_min = 950000,
  1580. .frequency_max = 2150000,
  1581. .frequency_stepsize = 125,
  1582. .frequency_tolerance = 0,
  1583. .symbol_rate_min = 1000000,
  1584. .symbol_rate_max = 45000000,
  1585. .symbol_rate_tolerance = 500,
  1586. .caps = FE_CAN_FEC_1_2 | FE_CAN_FEC_2_3 |
  1587. FE_CAN_FEC_3_4 | FE_CAN_FEC_5_6 |
  1588. FE_CAN_FEC_7_8 | FE_CAN_QPSK |
  1589. FE_CAN_2G_MODULATION |
  1590. FE_CAN_FEC_AUTO
  1591. },
  1592. .release = stv0900_release,
  1593. .init = stv0900_init,
  1594. .get_frontend = stv0900_get_frontend,
  1595. .sleep = stv0900_sleep,
  1596. .get_frontend_algo = stv0900_frontend_algo,
  1597. .i2c_gate_ctrl = stv0900_i2c_gate_ctrl,
  1598. .diseqc_send_master_cmd = stv0900_send_master_cmd,
  1599. .diseqc_send_burst = stv0900_send_burst,
  1600. .diseqc_recv_slave_reply = stv0900_recv_slave_reply,
  1601. .set_tone = stv0900_set_tone,
  1602. .set_property = stb0900_set_property,
  1603. .get_property = stb0900_get_property,
  1604. .search = stv0900_search,
  1605. .track = stv0900_track,
  1606. .read_status = stv0900_read_status,
  1607. .read_ber = stv0900_read_ber,
  1608. .read_signal_strength = stv0900_read_signal_strength,
  1609. .read_snr = stv0900_read_snr,
  1610. .read_ucblocks = stv0900_read_ucblocks,
  1611. };
  1612. struct dvb_frontend *stv0900_attach(const struct stv0900_config *config,
  1613. struct i2c_adapter *i2c,
  1614. int demod)
  1615. {
  1616. struct stv0900_state *state = NULL;
  1617. struct stv0900_init_params init_params;
  1618. enum fe_stv0900_error err_stv0900;
  1619. state = kzalloc(sizeof(struct stv0900_state), GFP_KERNEL);
  1620. if (state == NULL)
  1621. goto error;
  1622. state->demod = demod;
  1623. state->config = config;
  1624. state->i2c_adap = i2c;
  1625. memcpy(&state->frontend.ops, &stv0900_ops,
  1626. sizeof(struct dvb_frontend_ops));
  1627. state->frontend.demodulator_priv = state;
  1628. switch (demod) {
  1629. case 0:
  1630. case 1:
  1631. init_params.dmd_ref_clk = config->xtal;
  1632. init_params.demod_mode = config->demod_mode;
  1633. init_params.rolloff = STV0900_35;
  1634. init_params.path1_ts_clock = config->path1_mode;
  1635. init_params.tun1_maddress = config->tun1_maddress;
  1636. init_params.tun1_iq_inv = STV0900_IQ_NORMAL;
  1637. init_params.tuner1_adc = config->tun1_adc;
  1638. init_params.tuner1_type = config->tun1_type;
  1639. init_params.path2_ts_clock = config->path2_mode;
  1640. init_params.ts_config = config->ts_config_regs;
  1641. init_params.tun2_maddress = config->tun2_maddress;
  1642. init_params.tuner2_adc = config->tun2_adc;
  1643. init_params.tuner2_type = config->tun2_type;
  1644. init_params.tun2_iq_inv = STV0900_IQ_SWAPPED;
  1645. err_stv0900 = stv0900_init_internal(&state->frontend,
  1646. &init_params);
  1647. if (err_stv0900)
  1648. goto error;
  1649. break;
  1650. default:
  1651. goto error;
  1652. break;
  1653. }
  1654. dprintk("%s: Attaching STV0900 demodulator(%d) \n", __func__, demod);
  1655. return &state->frontend;
  1656. error:
  1657. dprintk("%s: Failed to attach STV0900 demodulator(%d) \n",
  1658. __func__, demod);
  1659. kfree(state);
  1660. return NULL;
  1661. }
  1662. EXPORT_SYMBOL(stv0900_attach);
  1663. MODULE_PARM_DESC(debug, "Set debug");
  1664. MODULE_AUTHOR("Igor M. Liplianin");
  1665. MODULE_DESCRIPTION("ST STV0900 frontend");
  1666. MODULE_LICENSE("GPL");