stb0899_drv.c 53 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010
  1. /*
  2. STB0899 Multistandard Frontend driver
  3. Copyright (C) Manu Abraham (abraham.manu@gmail.com)
  4. Copyright (C) ST Microelectronics
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program; if not, write to the Free Software
  15. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  16. */
  17. #include <linux/init.h>
  18. #include <linux/kernel.h>
  19. #include <linux/module.h>
  20. #include <linux/string.h>
  21. #include <linux/dvb/frontend.h>
  22. #include "dvb_frontend.h"
  23. #include "stb0899_drv.h"
  24. #include "stb0899_priv.h"
  25. #include "stb0899_reg.h"
  26. static unsigned int verbose = 1;
  27. module_param(verbose, int, 0644);
  28. /* C/N in dB/10, NIRM/NIRL */
  29. static const struct stb0899_tab stb0899_cn_tab[] = {
  30. { 200, 2600 },
  31. { 190, 2700 },
  32. { 180, 2860 },
  33. { 170, 3020 },
  34. { 160, 3210 },
  35. { 150, 3440 },
  36. { 140, 3710 },
  37. { 130, 4010 },
  38. { 120, 4360 },
  39. { 110, 4740 },
  40. { 100, 5190 },
  41. { 90, 5670 },
  42. { 80, 6200 },
  43. { 70, 6770 },
  44. { 60, 7360 },
  45. { 50, 7970 },
  46. { 40, 8250 },
  47. { 30, 9000 },
  48. { 20, 9450 },
  49. { 15, 9600 },
  50. };
  51. /* DVB-S AGCIQ_VALUE vs. signal level in dBm/10.
  52. * As measured, connected to a modulator.
  53. * -8.0 to -50.0 dBm directly connected,
  54. * -52.0 to -74.8 with extra attenuation.
  55. * Cut-off to AGCIQ_VALUE = 0x80 below -74.8dBm.
  56. * Crude linear extrapolation below -84.8dBm and above -8.0dBm.
  57. */
  58. static const struct stb0899_tab stb0899_dvbsrf_tab[] = {
  59. { -950, -128 },
  60. { -748, -94 },
  61. { -745, -92 },
  62. { -735, -90 },
  63. { -720, -87 },
  64. { -670, -77 },
  65. { -640, -70 },
  66. { -610, -62 },
  67. { -600, -60 },
  68. { -590, -56 },
  69. { -560, -41 },
  70. { -540, -25 },
  71. { -530, -17 },
  72. { -520, -11 },
  73. { -500, 1 },
  74. { -490, 6 },
  75. { -480, 10 },
  76. { -440, 22 },
  77. { -420, 27 },
  78. { -400, 31 },
  79. { -380, 34 },
  80. { -340, 40 },
  81. { -320, 43 },
  82. { -280, 48 },
  83. { -250, 52 },
  84. { -230, 55 },
  85. { -180, 61 },
  86. { -140, 66 },
  87. { -90, 73 },
  88. { -80, 74 },
  89. { 500, 127 }
  90. };
  91. /* DVB-S2 IF_AGC_GAIN vs. signal level in dBm/10.
  92. * As measured, connected to a modulator.
  93. * -8.0 to -50.1 dBm directly connected,
  94. * -53.0 to -76.6 with extra attenuation.
  95. * Cut-off to IF_AGC_GAIN = 0x3fff below -76.6dBm.
  96. * Crude linear extrapolation below -76.6dBm and above -8.0dBm.
  97. */
  98. static const struct stb0899_tab stb0899_dvbs2rf_tab[] = {
  99. { 700, 0 },
  100. { -80, 3217 },
  101. { -150, 3893 },
  102. { -190, 4217 },
  103. { -240, 4621 },
  104. { -280, 4945 },
  105. { -320, 5273 },
  106. { -350, 5545 },
  107. { -370, 5741 },
  108. { -410, 6147 },
  109. { -450, 6671 },
  110. { -490, 7413 },
  111. { -501, 7665 },
  112. { -530, 8767 },
  113. { -560, 10219 },
  114. { -580, 10939 },
  115. { -590, 11518 },
  116. { -600, 11723 },
  117. { -650, 12659 },
  118. { -690, 13219 },
  119. { -730, 13645 },
  120. { -750, 13909 },
  121. { -766, 14153 },
  122. { -999, 16383 }
  123. };
  124. /* DVB-S2 Es/N0 quant in dB/100 vs read value * 100*/
  125. struct stb0899_tab stb0899_quant_tab[] = {
  126. { 0, 0 },
  127. { 0, 100 },
  128. { 600, 200 },
  129. { 950, 299 },
  130. { 1200, 398 },
  131. { 1400, 501 },
  132. { 1560, 603 },
  133. { 1690, 700 },
  134. { 1810, 804 },
  135. { 1910, 902 },
  136. { 2000, 1000 },
  137. { 2080, 1096 },
  138. { 2160, 1202 },
  139. { 2230, 1303 },
  140. { 2350, 1496 },
  141. { 2410, 1603 },
  142. { 2460, 1698 },
  143. { 2510, 1799 },
  144. { 2600, 1995 },
  145. { 2650, 2113 },
  146. { 2690, 2213 },
  147. { 2720, 2291 },
  148. { 2760, 2399 },
  149. { 2800, 2512 },
  150. { 2860, 2692 },
  151. { 2930, 2917 },
  152. { 2960, 3020 },
  153. { 3010, 3199 },
  154. { 3040, 3311 },
  155. { 3060, 3388 },
  156. { 3120, 3631 },
  157. { 3190, 3936 },
  158. { 3400, 5012 },
  159. { 3610, 6383 },
  160. { 3800, 7943 },
  161. { 4210, 12735 },
  162. { 4500, 17783 },
  163. { 4690, 22131 },
  164. { 4810, 25410 }
  165. };
  166. /* DVB-S2 Es/N0 estimate in dB/100 vs read value */
  167. struct stb0899_tab stb0899_est_tab[] = {
  168. { 0, 0 },
  169. { 0, 1 },
  170. { 301, 2 },
  171. { 1204, 16 },
  172. { 1806, 64 },
  173. { 2408, 256 },
  174. { 2709, 512 },
  175. { 3010, 1023 },
  176. { 3311, 2046 },
  177. { 3612, 4093 },
  178. { 3823, 6653 },
  179. { 3913, 8185 },
  180. { 4010, 10233 },
  181. { 4107, 12794 },
  182. { 4214, 16368 },
  183. { 4266, 18450 },
  184. { 4311, 20464 },
  185. { 4353, 22542 },
  186. { 4391, 24604 },
  187. { 4425, 26607 },
  188. { 4457, 28642 },
  189. { 4487, 30690 },
  190. { 4515, 32734 },
  191. { 4612, 40926 },
  192. { 4692, 49204 },
  193. { 4816, 65464 },
  194. { 4913, 81846 },
  195. { 4993, 98401 },
  196. { 5060, 114815 },
  197. { 5118, 131220 },
  198. { 5200, 158489 },
  199. { 5300, 199526 },
  200. { 5400, 251189 },
  201. { 5500, 316228 },
  202. { 5600, 398107 },
  203. { 5720, 524807 },
  204. { 5721, 526017 },
  205. };
  206. int _stb0899_read_reg(struct stb0899_state *state, unsigned int reg)
  207. {
  208. int ret;
  209. u8 b0[] = { reg >> 8, reg & 0xff };
  210. u8 buf;
  211. struct i2c_msg msg[] = {
  212. {
  213. .addr = state->config->demod_address,
  214. .flags = 0,
  215. .buf = b0,
  216. .len = 2
  217. },{
  218. .addr = state->config->demod_address,
  219. .flags = I2C_M_RD,
  220. .buf = &buf,
  221. .len = 1
  222. }
  223. };
  224. ret = i2c_transfer(state->i2c, msg, 2);
  225. if (ret != 2) {
  226. if (ret != -ERESTARTSYS)
  227. dprintk(verbose, FE_ERROR, 1,
  228. "Read error, Reg=[0x%02x], Status=%d",
  229. reg, ret);
  230. return ret < 0 ? ret : -EREMOTEIO;
  231. }
  232. if (unlikely(verbose >= FE_DEBUGREG))
  233. dprintk(verbose, FE_ERROR, 1, "Reg=[0x%02x], data=%02x",
  234. reg, buf);
  235. return (unsigned int)buf;
  236. }
  237. int stb0899_read_reg(struct stb0899_state *state, unsigned int reg)
  238. {
  239. int result;
  240. result = _stb0899_read_reg(state, reg);
  241. /*
  242. * Bug ID 9:
  243. * access to 0xf2xx/0xf6xx
  244. * must be followed by read from 0xf2ff/0xf6ff.
  245. */
  246. if ((reg != 0xf2ff) && (reg != 0xf6ff) &&
  247. (((reg & 0xff00) == 0xf200) || ((reg & 0xff00) == 0xf600)))
  248. _stb0899_read_reg(state, (reg | 0x00ff));
  249. return result;
  250. }
  251. u32 _stb0899_read_s2reg(struct stb0899_state *state,
  252. u32 stb0899_i2cdev,
  253. u32 stb0899_base_addr,
  254. u16 stb0899_reg_offset)
  255. {
  256. int status;
  257. u32 data;
  258. u8 buf[7] = { 0 };
  259. u16 tmpaddr;
  260. u8 buf_0[] = {
  261. GETBYTE(stb0899_i2cdev, BYTE1), /* 0xf3 S2 Base Address (MSB) */
  262. GETBYTE(stb0899_i2cdev, BYTE0), /* 0xfc S2 Base Address (LSB) */
  263. GETBYTE(stb0899_base_addr, BYTE0), /* 0x00 Base Address (LSB) */
  264. GETBYTE(stb0899_base_addr, BYTE1), /* 0x04 Base Address (LSB) */
  265. GETBYTE(stb0899_base_addr, BYTE2), /* 0x00 Base Address (MSB) */
  266. GETBYTE(stb0899_base_addr, BYTE3), /* 0x00 Base Address (MSB) */
  267. };
  268. u8 buf_1[] = {
  269. 0x00, /* 0xf3 Reg Offset */
  270. 0x00, /* 0x44 Reg Offset */
  271. };
  272. struct i2c_msg msg_0 = {
  273. .addr = state->config->demod_address,
  274. .flags = 0,
  275. .buf = buf_0,
  276. .len = 6
  277. };
  278. struct i2c_msg msg_1 = {
  279. .addr = state->config->demod_address,
  280. .flags = 0,
  281. .buf = buf_1,
  282. .len = 2
  283. };
  284. struct i2c_msg msg_r = {
  285. .addr = state->config->demod_address,
  286. .flags = I2C_M_RD,
  287. .buf = buf,
  288. .len = 4
  289. };
  290. tmpaddr = stb0899_reg_offset & 0xff00;
  291. if (!(stb0899_reg_offset & 0x8))
  292. tmpaddr = stb0899_reg_offset | 0x20;
  293. buf_1[0] = GETBYTE(tmpaddr, BYTE1);
  294. buf_1[1] = GETBYTE(tmpaddr, BYTE0);
  295. status = i2c_transfer(state->i2c, &msg_0, 1);
  296. if (status < 1) {
  297. if (status != -ERESTARTSYS)
  298. printk(KERN_ERR "%s ERR(1), Device=[0x%04x], Base address=[0x%08x], Offset=[0x%04x], Status=%d\n",
  299. __func__, stb0899_i2cdev, stb0899_base_addr, stb0899_reg_offset, status);
  300. goto err;
  301. }
  302. /* Dummy */
  303. status = i2c_transfer(state->i2c, &msg_1, 1);
  304. if (status < 1)
  305. goto err;
  306. status = i2c_transfer(state->i2c, &msg_r, 1);
  307. if (status < 1)
  308. goto err;
  309. buf_1[0] = GETBYTE(stb0899_reg_offset, BYTE1);
  310. buf_1[1] = GETBYTE(stb0899_reg_offset, BYTE0);
  311. /* Actual */
  312. status = i2c_transfer(state->i2c, &msg_1, 1);
  313. if (status < 1) {
  314. if (status != -ERESTARTSYS)
  315. printk(KERN_ERR "%s ERR(2), Device=[0x%04x], Base address=[0x%08x], Offset=[0x%04x], Status=%d\n",
  316. __func__, stb0899_i2cdev, stb0899_base_addr, stb0899_reg_offset, status);
  317. goto err;
  318. }
  319. status = i2c_transfer(state->i2c, &msg_r, 1);
  320. if (status < 1) {
  321. if (status != -ERESTARTSYS)
  322. printk(KERN_ERR "%s ERR(3), Device=[0x%04x], Base address=[0x%08x], Offset=[0x%04x], Status=%d\n",
  323. __func__, stb0899_i2cdev, stb0899_base_addr, stb0899_reg_offset, status);
  324. return status < 0 ? status : -EREMOTEIO;
  325. }
  326. data = MAKEWORD32(buf[3], buf[2], buf[1], buf[0]);
  327. if (unlikely(state->verbose >= FE_DEBUGREG))
  328. printk(KERN_DEBUG "%s Device=[0x%04x], Base address=[0x%08x], Offset=[0x%04x], Data=[0x%08x]\n",
  329. __func__, stb0899_i2cdev, stb0899_base_addr, stb0899_reg_offset, data);
  330. return data;
  331. err:
  332. return status < 0 ? status : -EREMOTEIO;
  333. }
  334. int stb0899_write_s2reg(struct stb0899_state *state,
  335. u32 stb0899_i2cdev,
  336. u32 stb0899_base_addr,
  337. u16 stb0899_reg_offset,
  338. u32 stb0899_data)
  339. {
  340. int status;
  341. /* Base Address Setup */
  342. u8 buf_0[] = {
  343. GETBYTE(stb0899_i2cdev, BYTE1), /* 0xf3 S2 Base Address (MSB) */
  344. GETBYTE(stb0899_i2cdev, BYTE0), /* 0xfc S2 Base Address (LSB) */
  345. GETBYTE(stb0899_base_addr, BYTE0), /* 0x00 Base Address (LSB) */
  346. GETBYTE(stb0899_base_addr, BYTE1), /* 0x04 Base Address (LSB) */
  347. GETBYTE(stb0899_base_addr, BYTE2), /* 0x00 Base Address (MSB) */
  348. GETBYTE(stb0899_base_addr, BYTE3), /* 0x00 Base Address (MSB) */
  349. };
  350. u8 buf_1[] = {
  351. 0x00, /* 0xf3 Reg Offset */
  352. 0x00, /* 0x44 Reg Offset */
  353. 0x00, /* data */
  354. 0x00, /* data */
  355. 0x00, /* data */
  356. 0x00, /* data */
  357. };
  358. struct i2c_msg msg_0 = {
  359. .addr = state->config->demod_address,
  360. .flags = 0,
  361. .buf = buf_0,
  362. .len = 6
  363. };
  364. struct i2c_msg msg_1 = {
  365. .addr = state->config->demod_address,
  366. .flags = 0,
  367. .buf = buf_1,
  368. .len = 6
  369. };
  370. buf_1[0] = GETBYTE(stb0899_reg_offset, BYTE1);
  371. buf_1[1] = GETBYTE(stb0899_reg_offset, BYTE0);
  372. buf_1[2] = GETBYTE(stb0899_data, BYTE0);
  373. buf_1[3] = GETBYTE(stb0899_data, BYTE1);
  374. buf_1[4] = GETBYTE(stb0899_data, BYTE2);
  375. buf_1[5] = GETBYTE(stb0899_data, BYTE3);
  376. if (unlikely(state->verbose >= FE_DEBUGREG))
  377. printk(KERN_DEBUG "%s Device=[0x%04x], Base Address=[0x%08x], Offset=[0x%04x], Data=[0x%08x]\n",
  378. __func__, stb0899_i2cdev, stb0899_base_addr, stb0899_reg_offset, stb0899_data);
  379. status = i2c_transfer(state->i2c, &msg_0, 1);
  380. if (unlikely(status < 1)) {
  381. if (status != -ERESTARTSYS)
  382. printk(KERN_ERR "%s ERR (1), Device=[0x%04x], Base Address=[0x%08x], Offset=[0x%04x], Data=[0x%08x], status=%d\n",
  383. __func__, stb0899_i2cdev, stb0899_base_addr, stb0899_reg_offset, stb0899_data, status);
  384. goto err;
  385. }
  386. status = i2c_transfer(state->i2c, &msg_1, 1);
  387. if (unlikely(status < 1)) {
  388. if (status != -ERESTARTSYS)
  389. printk(KERN_ERR "%s ERR (2), Device=[0x%04x], Base Address=[0x%08x], Offset=[0x%04x], Data=[0x%08x], status=%d\n",
  390. __func__, stb0899_i2cdev, stb0899_base_addr, stb0899_reg_offset, stb0899_data, status);
  391. return status < 0 ? status : -EREMOTEIO;
  392. }
  393. return 0;
  394. err:
  395. return status < 0 ? status : -EREMOTEIO;
  396. }
  397. int stb0899_read_regs(struct stb0899_state *state, unsigned int reg, u8 *buf, u32 count)
  398. {
  399. int status;
  400. u8 b0[] = { reg >> 8, reg & 0xff };
  401. struct i2c_msg msg[] = {
  402. {
  403. .addr = state->config->demod_address,
  404. .flags = 0,
  405. .buf = b0,
  406. .len = 2
  407. },{
  408. .addr = state->config->demod_address,
  409. .flags = I2C_M_RD,
  410. .buf = buf,
  411. .len = count
  412. }
  413. };
  414. status = i2c_transfer(state->i2c, msg, 2);
  415. if (status != 2) {
  416. if (status != -ERESTARTSYS)
  417. printk(KERN_ERR "%s Read error, Reg=[0x%04x], Count=%u, Status=%d\n",
  418. __func__, reg, count, status);
  419. goto err;
  420. }
  421. /*
  422. * Bug ID 9:
  423. * access to 0xf2xx/0xf6xx
  424. * must be followed by read from 0xf2ff/0xf6ff.
  425. */
  426. if ((reg != 0xf2ff) && (reg != 0xf6ff) &&
  427. (((reg & 0xff00) == 0xf200) || ((reg & 0xff00) == 0xf600)))
  428. _stb0899_read_reg(state, (reg | 0x00ff));
  429. if (unlikely(state->verbose >= FE_DEBUGREG)) {
  430. int i;
  431. printk(KERN_DEBUG "%s [0x%04x]:", __func__, reg);
  432. for (i = 0; i < count; i++) {
  433. printk(" %02x", buf[i]);
  434. }
  435. printk("\n");
  436. }
  437. return 0;
  438. err:
  439. return status < 0 ? status : -EREMOTEIO;
  440. }
  441. int stb0899_write_regs(struct stb0899_state *state, unsigned int reg, u8 *data, u32 count)
  442. {
  443. int ret;
  444. u8 buf[2 + count];
  445. struct i2c_msg i2c_msg = {
  446. .addr = state->config->demod_address,
  447. .flags = 0,
  448. .buf = buf,
  449. .len = 2 + count
  450. };
  451. buf[0] = reg >> 8;
  452. buf[1] = reg & 0xff;
  453. memcpy(&buf[2], data, count);
  454. if (unlikely(state->verbose >= FE_DEBUGREG)) {
  455. int i;
  456. printk(KERN_DEBUG "%s [0x%04x]:", __func__, reg);
  457. for (i = 0; i < count; i++)
  458. printk(" %02x", data[i]);
  459. printk("\n");
  460. }
  461. ret = i2c_transfer(state->i2c, &i2c_msg, 1);
  462. /*
  463. * Bug ID 9:
  464. * access to 0xf2xx/0xf6xx
  465. * must be followed by read from 0xf2ff/0xf6ff.
  466. */
  467. if ((((reg & 0xff00) == 0xf200) || ((reg & 0xff00) == 0xf600)))
  468. stb0899_read_reg(state, (reg | 0x00ff));
  469. if (ret != 1) {
  470. if (ret != -ERESTARTSYS)
  471. dprintk(verbose, FE_ERROR, 1, "Reg=[0x%04x], Data=[0x%02x ...], Count=%u, Status=%d",
  472. reg, data[0], count, ret);
  473. return ret < 0 ? ret : -EREMOTEIO;
  474. }
  475. return 0;
  476. }
  477. int stb0899_write_reg(struct stb0899_state *state, unsigned int reg, u8 data)
  478. {
  479. return stb0899_write_regs(state, reg, &data, 1);
  480. }
  481. /*
  482. * stb0899_get_mclk
  483. * Get STB0899 master clock frequency
  484. * ExtClk: external clock frequency (Hz)
  485. */
  486. static u32 stb0899_get_mclk(struct stb0899_state *state)
  487. {
  488. u32 mclk = 0, div = 0;
  489. div = stb0899_read_reg(state, STB0899_NCOARSE);
  490. mclk = (div + 1) * state->config->xtal_freq / 6;
  491. dprintk(verbose, FE_DEBUG, 1, "div=%d, mclk=%d", div, mclk);
  492. return mclk;
  493. }
  494. /*
  495. * stb0899_set_mclk
  496. * Set STB0899 master Clock frequency
  497. * Mclk: demodulator master clock
  498. * ExtClk: external clock frequency (Hz)
  499. */
  500. static void stb0899_set_mclk(struct stb0899_state *state, u32 Mclk)
  501. {
  502. struct stb0899_internal *internal = &state->internal;
  503. u8 mdiv = 0;
  504. dprintk(verbose, FE_DEBUG, 1, "state->config=%p", state->config);
  505. mdiv = ((6 * Mclk) / state->config->xtal_freq) - 1;
  506. dprintk(verbose, FE_DEBUG, 1, "mdiv=%d", mdiv);
  507. stb0899_write_reg(state, STB0899_NCOARSE, mdiv);
  508. internal->master_clk = stb0899_get_mclk(state);
  509. dprintk(verbose, FE_DEBUG, 1, "MasterCLOCK=%d", internal->master_clk);
  510. }
  511. static int stb0899_postproc(struct stb0899_state *state, u8 ctl, int enable)
  512. {
  513. struct stb0899_config *config = state->config;
  514. const struct stb0899_postproc *postproc = config->postproc;
  515. /* post process event */
  516. if (postproc) {
  517. if (enable) {
  518. if (postproc[ctl].level == STB0899_GPIOPULLUP)
  519. stb0899_write_reg(state, postproc[ctl].gpio, 0x02);
  520. else
  521. stb0899_write_reg(state, postproc[ctl].gpio, 0x82);
  522. } else {
  523. if (postproc[ctl].level == STB0899_GPIOPULLUP)
  524. stb0899_write_reg(state, postproc[ctl].gpio, 0x82);
  525. else
  526. stb0899_write_reg(state, postproc[ctl].gpio, 0x02);
  527. }
  528. }
  529. return 0;
  530. }
  531. static void stb0899_release(struct dvb_frontend *fe)
  532. {
  533. struct stb0899_state *state = fe->demodulator_priv;
  534. dprintk(verbose, FE_DEBUG, 1, "Release Frontend");
  535. /* post process event */
  536. stb0899_postproc(state, STB0899_POSTPROC_GPIO_POWER, 0);
  537. kfree(state);
  538. }
  539. /*
  540. * stb0899_get_alpha
  541. * return: rolloff
  542. */
  543. static int stb0899_get_alpha(struct stb0899_state *state)
  544. {
  545. u8 mode_coeff;
  546. mode_coeff = stb0899_read_reg(state, STB0899_DEMOD);
  547. if (STB0899_GETFIELD(MODECOEFF, mode_coeff) == 1)
  548. return 20;
  549. else
  550. return 35;
  551. }
  552. /*
  553. * stb0899_init_calc
  554. */
  555. static void stb0899_init_calc(struct stb0899_state *state)
  556. {
  557. struct stb0899_internal *internal = &state->internal;
  558. int master_clk;
  559. u8 agc[2];
  560. u8 agc1cn;
  561. u32 reg;
  562. /* Read registers (in burst mode) */
  563. agc1cn = stb0899_read_reg(state, STB0899_AGC1CN);
  564. stb0899_read_regs(state, STB0899_AGC1REF, agc, 2); /* AGC1R and AGC2O */
  565. /* Initial calculations */
  566. master_clk = stb0899_get_mclk(state);
  567. internal->t_agc1 = 0;
  568. internal->t_agc2 = 0;
  569. internal->master_clk = master_clk;
  570. internal->mclk = master_clk / 65536L;
  571. internal->rolloff = stb0899_get_alpha(state);
  572. /* DVBS2 Initial calculations */
  573. /* Set AGC value to the middle */
  574. internal->agc_gain = 8154;
  575. reg = STB0899_READ_S2REG(STB0899_S2DEMOD, IF_AGC_CNTRL);
  576. STB0899_SETFIELD_VAL(IF_GAIN_INIT, reg, internal->agc_gain);
  577. stb0899_write_s2reg(state, STB0899_S2DEMOD, STB0899_BASE_IF_AGC_CNTRL, STB0899_OFF0_IF_AGC_CNTRL, reg);
  578. reg = STB0899_READ_S2REG(STB0899_S2DEMOD, RRC_ALPHA);
  579. internal->rrc_alpha = STB0899_GETFIELD(RRC_ALPHA, reg);
  580. internal->center_freq = 0;
  581. internal->av_frame_coarse = 10;
  582. internal->av_frame_fine = 20;
  583. internal->step_size = 2;
  584. /*
  585. if ((pParams->SpectralInv == FE_IQ_NORMAL) || (pParams->SpectralInv == FE_IQ_AUTO))
  586. pParams->IQLocked = 0;
  587. else
  588. pParams->IQLocked = 1;
  589. */
  590. }
  591. static int stb0899_wait_diseqc_fifo_empty(struct stb0899_state *state, int timeout)
  592. {
  593. u8 reg = 0;
  594. unsigned long start = jiffies;
  595. while (1) {
  596. reg = stb0899_read_reg(state, STB0899_DISSTATUS);
  597. if (!STB0899_GETFIELD(FIFOFULL, reg))
  598. break;
  599. if ((jiffies - start) > timeout) {
  600. dprintk(verbose, FE_ERROR, 1, "timed out !!");
  601. return -ETIMEDOUT;
  602. }
  603. }
  604. return 0;
  605. }
  606. static int stb0899_send_diseqc_msg(struct dvb_frontend *fe, struct dvb_diseqc_master_cmd *cmd)
  607. {
  608. struct stb0899_state *state = fe->demodulator_priv;
  609. u8 reg, i;
  610. if (cmd->msg_len > 8)
  611. return -EINVAL;
  612. /* enable FIFO precharge */
  613. reg = stb0899_read_reg(state, STB0899_DISCNTRL1);
  614. STB0899_SETFIELD_VAL(DISPRECHARGE, reg, 1);
  615. stb0899_write_reg(state, STB0899_DISCNTRL1, reg);
  616. for (i = 0; i < cmd->msg_len; i++) {
  617. /* wait for FIFO empty */
  618. if (stb0899_wait_diseqc_fifo_empty(state, 10) < 0)
  619. return -ETIMEDOUT;
  620. stb0899_write_reg(state, STB0899_DISFIFO, cmd->msg[i]);
  621. }
  622. reg = stb0899_read_reg(state, STB0899_DISCNTRL1);
  623. STB0899_SETFIELD_VAL(DISPRECHARGE, reg, 0);
  624. stb0899_write_reg(state, STB0899_DISCNTRL1, reg);
  625. return 0;
  626. }
  627. static int stb0899_wait_diseqc_rxidle(struct stb0899_state *state, int timeout)
  628. {
  629. u8 reg = 0;
  630. unsigned long start = jiffies;
  631. while (!STB0899_GETFIELD(RXEND, reg)) {
  632. reg = stb0899_read_reg(state, STB0899_DISRX_ST0);
  633. if (jiffies - start > timeout) {
  634. dprintk(verbose, FE_ERROR, 1, "timed out!!");
  635. return -ETIMEDOUT;
  636. }
  637. msleep(10);
  638. }
  639. return 0;
  640. }
  641. static int stb0899_recv_slave_reply(struct dvb_frontend *fe, struct dvb_diseqc_slave_reply *reply)
  642. {
  643. struct stb0899_state *state = fe->demodulator_priv;
  644. u8 reg, length = 0, i;
  645. int result;
  646. if (stb0899_wait_diseqc_rxidle(state, 100) < 0)
  647. return -ETIMEDOUT;
  648. reg = stb0899_read_reg(state, STB0899_DISRX_ST0);
  649. if (STB0899_GETFIELD(RXEND, reg)) {
  650. reg = stb0899_read_reg(state, STB0899_DISRX_ST1);
  651. length = STB0899_GETFIELD(FIFOBYTENBR, reg);
  652. if (length > sizeof (reply->msg)) {
  653. result = -EOVERFLOW;
  654. goto exit;
  655. }
  656. reply->msg_len = length;
  657. /* extract data */
  658. for (i = 0; i < length; i++)
  659. reply->msg[i] = stb0899_read_reg(state, STB0899_DISFIFO);
  660. }
  661. return 0;
  662. exit:
  663. return result;
  664. }
  665. static int stb0899_wait_diseqc_txidle(struct stb0899_state *state, int timeout)
  666. {
  667. u8 reg = 0;
  668. unsigned long start = jiffies;
  669. while (!STB0899_GETFIELD(TXIDLE, reg)) {
  670. reg = stb0899_read_reg(state, STB0899_DISSTATUS);
  671. if (jiffies - start > timeout) {
  672. dprintk(verbose, FE_ERROR, 1, "timed out!!");
  673. return -ETIMEDOUT;
  674. }
  675. msleep(10);
  676. }
  677. return 0;
  678. }
  679. static int stb0899_send_diseqc_burst(struct dvb_frontend *fe, fe_sec_mini_cmd_t burst)
  680. {
  681. struct stb0899_state *state = fe->demodulator_priv;
  682. u8 reg, old_state;
  683. /* wait for diseqc idle */
  684. if (stb0899_wait_diseqc_txidle(state, 100) < 0)
  685. return -ETIMEDOUT;
  686. reg = stb0899_read_reg(state, STB0899_DISCNTRL1);
  687. old_state = reg;
  688. /* set to burst mode */
  689. STB0899_SETFIELD_VAL(DISEQCMODE, reg, 0x02);
  690. STB0899_SETFIELD_VAL(DISPRECHARGE, reg, 0x01);
  691. stb0899_write_reg(state, STB0899_DISCNTRL1, reg);
  692. switch (burst) {
  693. case SEC_MINI_A:
  694. /* unmodulated */
  695. stb0899_write_reg(state, STB0899_DISFIFO, 0x00);
  696. break;
  697. case SEC_MINI_B:
  698. /* modulated */
  699. stb0899_write_reg(state, STB0899_DISFIFO, 0xff);
  700. break;
  701. }
  702. reg = stb0899_read_reg(state, STB0899_DISCNTRL1);
  703. STB0899_SETFIELD_VAL(DISPRECHARGE, reg, 0x00);
  704. stb0899_write_reg(state, STB0899_DISCNTRL1, reg);
  705. /* wait for diseqc idle */
  706. if (stb0899_wait_diseqc_txidle(state, 100) < 0)
  707. return -ETIMEDOUT;
  708. /* restore state */
  709. stb0899_write_reg(state, STB0899_DISCNTRL1, old_state);
  710. return 0;
  711. }
  712. static int stb0899_diseqc_init(struct stb0899_state *state)
  713. {
  714. struct dvb_diseqc_master_cmd tx_data;
  715. struct dvb_diseqc_slave_reply rx_data;
  716. u8 f22_tx, f22_rx, reg;
  717. u32 mclk, tx_freq = 22000, count = 0, i;
  718. u32 trial = 0; /* try max = 2 (try 20khz and 17.5 khz) */
  719. u32 ret_1 = 0; /* 20 Khz status */
  720. u32 ret_2 = 0; /* 17.5 Khz status */
  721. tx_data.msg[0] = 0xe2;
  722. tx_data.msg_len = 3;
  723. reg = stb0899_read_reg(state, STB0899_DISCNTRL2);
  724. STB0899_SETFIELD_VAL(ONECHIP_TRX, reg, 0);
  725. stb0899_write_reg(state, STB0899_DISCNTRL2, reg);
  726. /* disable Tx spy */
  727. reg = stb0899_read_reg(state, STB0899_DISCNTRL1);
  728. STB0899_SETFIELD_VAL(DISEQCRESET, reg, 1);
  729. stb0899_write_reg(state, STB0899_DISCNTRL1, reg);
  730. reg = stb0899_read_reg(state, STB0899_DISCNTRL1);
  731. STB0899_SETFIELD_VAL(DISEQCRESET, reg, 0);
  732. stb0899_write_reg(state, STB0899_DISCNTRL1, reg);
  733. mclk = stb0899_get_mclk(state);
  734. f22_tx = mclk / (tx_freq * 32);
  735. stb0899_write_reg(state, STB0899_DISF22, f22_tx); /* DiSEqC Tx freq */
  736. state->rx_freq = 20000;
  737. f22_rx = mclk / (state->rx_freq * 32);
  738. return 0;
  739. }
  740. static int stb0899_sleep(struct dvb_frontend *fe)
  741. {
  742. struct stb0899_state *state = fe->demodulator_priv;
  743. u8 reg;
  744. dprintk(verbose, FE_DEBUG, 1, "Going to Sleep .. (Really tired .. :-))");
  745. /* post process event */
  746. stb0899_postproc(state, STB0899_POSTPROC_GPIO_POWER, 0);
  747. return 0;
  748. }
  749. static int stb0899_wakeup(struct dvb_frontend *fe)
  750. {
  751. int rc;
  752. struct stb0899_state *state = fe->demodulator_priv;
  753. if ((rc = stb0899_write_reg(state, STB0899_SYNTCTRL, STB0899_SELOSCI)))
  754. return rc;
  755. /* Activate all clocks; DVB-S2 registers are inaccessible otherwise. */
  756. if ((rc = stb0899_write_reg(state, STB0899_STOPCLK1, 0x00)))
  757. return rc;
  758. if ((rc = stb0899_write_reg(state, STB0899_STOPCLK2, 0x00)))
  759. return rc;
  760. /* post process event */
  761. stb0899_postproc(state, STB0899_POSTPROC_GPIO_POWER, 1);
  762. return 0;
  763. }
  764. static int stb0899_init(struct dvb_frontend *fe)
  765. {
  766. int i;
  767. struct stb0899_state *state = fe->demodulator_priv;
  768. struct stb0899_config *config = state->config;
  769. dprintk(verbose, FE_DEBUG, 1, "Initializing STB0899 ... ");
  770. // mutex_init(&state->search_lock);
  771. /* init device */
  772. dprintk(verbose, FE_DEBUG, 1, "init device");
  773. for (i = 0; config->init_dev[i].address != 0xffff; i++)
  774. stb0899_write_reg(state, config->init_dev[i].address, config->init_dev[i].data);
  775. dprintk(verbose, FE_DEBUG, 1, "init S2 demod");
  776. /* init S2 demod */
  777. for (i = 0; config->init_s2_demod[i].offset != 0xffff; i++)
  778. stb0899_write_s2reg(state, STB0899_S2DEMOD,
  779. config->init_s2_demod[i].base_address,
  780. config->init_s2_demod[i].offset,
  781. config->init_s2_demod[i].data);
  782. dprintk(verbose, FE_DEBUG, 1, "init S1 demod");
  783. /* init S1 demod */
  784. for (i = 0; config->init_s1_demod[i].address != 0xffff; i++)
  785. stb0899_write_reg(state, config->init_s1_demod[i].address, config->init_s1_demod[i].data);
  786. dprintk(verbose, FE_DEBUG, 1, "init S2 FEC");
  787. /* init S2 fec */
  788. for (i = 0; config->init_s2_fec[i].offset != 0xffff; i++)
  789. stb0899_write_s2reg(state, STB0899_S2FEC,
  790. config->init_s2_fec[i].base_address,
  791. config->init_s2_fec[i].offset,
  792. config->init_s2_fec[i].data);
  793. dprintk(verbose, FE_DEBUG, 1, "init TST");
  794. /* init test */
  795. for (i = 0; config->init_tst[i].address != 0xffff; i++)
  796. stb0899_write_reg(state, config->init_tst[i].address, config->init_tst[i].data);
  797. stb0899_init_calc(state);
  798. stb0899_diseqc_init(state);
  799. return 0;
  800. }
  801. static int stb0899_table_lookup(const struct stb0899_tab *tab, int max, int val)
  802. {
  803. int res = 0;
  804. int min = 0, med;
  805. if (val < tab[min].read)
  806. res = tab[min].real;
  807. else if (val >= tab[max].read)
  808. res = tab[max].real;
  809. else {
  810. while ((max - min) > 1) {
  811. med = (max + min) / 2;
  812. if (val >= tab[min].read && val < tab[med].read)
  813. max = med;
  814. else
  815. min = med;
  816. }
  817. res = ((val - tab[min].read) *
  818. (tab[max].real - tab[min].real) /
  819. (tab[max].read - tab[min].read)) +
  820. tab[min].real;
  821. }
  822. return res;
  823. }
  824. static int stb0899_read_signal_strength(struct dvb_frontend *fe, u16 *strength)
  825. {
  826. struct stb0899_state *state = fe->demodulator_priv;
  827. struct stb0899_internal *internal = &state->internal;
  828. int val;
  829. u32 reg;
  830. switch (state->delsys) {
  831. case DVBFE_DELSYS_DVBS:
  832. case DVBFE_DELSYS_DSS:
  833. if (internal->lock) {
  834. reg = stb0899_read_reg(state, STB0899_VSTATUS);
  835. if (STB0899_GETFIELD(VSTATUS_LOCKEDVIT, reg)) {
  836. reg = stb0899_read_reg(state, STB0899_AGCIQIN);
  837. val = (s32)(s8)STB0899_GETFIELD(AGCIQVALUE, reg);
  838. *strength = stb0899_table_lookup(stb0899_dvbsrf_tab, ARRAY_SIZE(stb0899_dvbsrf_tab) - 1, val);
  839. *strength += 750;
  840. dprintk(verbose, FE_DEBUG, 1, "AGCIQVALUE = 0x%02x, C = %d * 0.1 dBm",
  841. val & 0xff, *strength);
  842. }
  843. }
  844. break;
  845. case DVBFE_DELSYS_DVBS2:
  846. if (internal->lock) {
  847. reg = STB0899_READ_S2REG(STB0899_DEMOD, IF_AGC_GAIN);
  848. val = STB0899_GETFIELD(IF_AGC_GAIN, reg);
  849. *strength = stb0899_table_lookup(stb0899_dvbs2rf_tab, ARRAY_SIZE(stb0899_dvbs2rf_tab) - 1, val);
  850. *strength += 750;
  851. dprintk(verbose, FE_DEBUG, 1, "IF_AGC_GAIN = 0x%04x, C = %d * 0.1 dBm",
  852. val & 0x3fff, *strength);
  853. }
  854. break;
  855. default:
  856. dprintk(verbose, FE_DEBUG, 1, "Unsupported delivery system");
  857. return -EINVAL;
  858. }
  859. return 0;
  860. }
  861. static int stb0899_read_snr(struct dvb_frontend *fe, u16 *snr)
  862. {
  863. struct stb0899_state *state = fe->demodulator_priv;
  864. struct stb0899_internal *internal = &state->internal;
  865. unsigned int val, quant, quantn = -1, est, estn = -1;
  866. u8 buf[2];
  867. u32 reg;
  868. reg = stb0899_read_reg(state, STB0899_VSTATUS);
  869. switch (state->delsys) {
  870. case DVBFE_DELSYS_DVBS:
  871. case DVBFE_DELSYS_DSS:
  872. if (internal->lock) {
  873. if (STB0899_GETFIELD(VSTATUS_LOCKEDVIT, reg)) {
  874. stb0899_read_regs(state, STB0899_NIRM, buf, 2);
  875. val = MAKEWORD16(buf[0], buf[1]);
  876. *snr = stb0899_table_lookup(stb0899_cn_tab, ARRAY_SIZE(stb0899_cn_tab) - 1, val);
  877. dprintk(verbose, FE_DEBUG, 1, "NIR = 0x%02x%02x = %u, C/N = %d * 0.1 dBm\n",
  878. buf[0], buf[1], val, *snr);
  879. }
  880. }
  881. break;
  882. case DVBFE_DELSYS_DVBS2:
  883. if (internal->lock) {
  884. reg = STB0899_READ_S2REG(STB0899_S2DEMOD, UWP_CNTRL1);
  885. quant = STB0899_GETFIELD(UWP_ESN0_QUANT, reg);
  886. reg = STB0899_READ_S2REG(STB0899_S2DEMOD, UWP_STAT2);
  887. est = STB0899_GETFIELD(ESN0_EST, reg);
  888. if (est == 1)
  889. val = 301; /* C/N = 30.1 dB */
  890. else if (est == 2)
  891. val = 270; /* C/N = 27.0 dB */
  892. else {
  893. /* quantn = 100 * log(quant^2) */
  894. quantn = stb0899_table_lookup(stb0899_quant_tab, ARRAY_SIZE(stb0899_quant_tab) - 1, quant * 100);
  895. /* estn = 100 * log(est) */
  896. estn = stb0899_table_lookup(stb0899_est_tab, ARRAY_SIZE(stb0899_est_tab) - 1, est);
  897. /* snr(dBm/10) = -10*(log(est)-log(quant^2)) => snr(dBm/10) = (100*log(quant^2)-100*log(est))/10 */
  898. val = (quantn - estn) / 10;
  899. }
  900. *snr = val;
  901. dprintk(verbose, FE_DEBUG, 1, "Es/N0 quant = %d (%d) estimate = %u (%d), C/N = %d * 0.1 dBm",
  902. quant, quantn, est, estn, val);
  903. }
  904. break;
  905. default:
  906. dprintk(verbose, FE_DEBUG, 1, "Unsupported delivery system");
  907. return -EINVAL;
  908. }
  909. return 0;
  910. }
  911. static int stb0899_read_status(struct dvb_frontend *fe, enum fe_status *status)
  912. {
  913. struct stb0899_state *state = fe->demodulator_priv;
  914. struct stb0899_internal *internal = &state->internal;
  915. u8 reg;
  916. *status = 0;
  917. switch (state->delsys) {
  918. case DVBFE_DELSYS_DVBS:
  919. case DVBFE_DELSYS_DSS:
  920. dprintk(state->verbose, FE_DEBUG, 1, "Delivery system DVB-S/DSS");
  921. if (internal->lock) {
  922. reg = stb0899_read_reg(state, STB0899_VSTATUS);
  923. if (STB0899_GETFIELD(VSTATUS_LOCKEDVIT, reg)) {
  924. dprintk(state->verbose, FE_DEBUG, 1, "--------> FE_HAS_CARRIER | FE_HAS_LOCK");
  925. *status |= FE_HAS_CARRIER | FE_HAS_LOCK;
  926. reg = stb0899_read_reg(state, STB0899_PLPARM);
  927. if (STB0899_GETFIELD(VITCURPUN, reg)) {
  928. dprintk(state->verbose, FE_DEBUG, 1, "--------> FE_HAS_VITERBI | FE_HAS_SYNC");
  929. *status |= FE_HAS_VITERBI | FE_HAS_SYNC;
  930. /* post process event */
  931. stb0899_postproc(state, STB0899_POSTPROC_GPIO_LOCK, 1);
  932. }
  933. }
  934. }
  935. break;
  936. case DVBFE_DELSYS_DVBS2:
  937. dprintk(state->verbose, FE_DEBUG, 1, "Delivery system DVB-S2");
  938. if (internal->lock) {
  939. reg = STB0899_READ_S2REG(STB0899_S2DEMOD, DMD_STAT2);
  940. if (STB0899_GETFIELD(UWP_LOCK, reg) && STB0899_GETFIELD(CSM_LOCK, reg)) {
  941. *status |= FE_HAS_CARRIER;
  942. dprintk(state->verbose, FE_DEBUG, 1,
  943. "UWP & CSM Lock ! ---> DVB-S2 FE_HAS_CARRIER");
  944. reg = stb0899_read_reg(state, STB0899_CFGPDELSTATUS1);
  945. if (STB0899_GETFIELD(CFGPDELSTATUS_LOCK, reg)) {
  946. *status |= FE_HAS_LOCK;
  947. dprintk(state->verbose, FE_DEBUG, 1,
  948. "Packet Delineator Locked ! -----> DVB-S2 FE_HAS_LOCK");
  949. }
  950. if (STB0899_GETFIELD(CONTINUOUS_STREAM, reg)) {
  951. *status |= FE_HAS_VITERBI;
  952. dprintk(state->verbose, FE_DEBUG, 1,
  953. "Packet Delineator found VITERBI ! -----> DVB-S2 FE_HAS_VITERBI");
  954. }
  955. if (STB0899_GETFIELD(ACCEPTED_STREAM, reg)) {
  956. *status |= FE_HAS_SYNC;
  957. dprintk(state->verbose, FE_DEBUG, 1,
  958. "Packet Delineator found SYNC ! -----> DVB-S2 FE_HAS_SYNC");
  959. /* post process event */
  960. stb0899_postproc(state, STB0899_POSTPROC_GPIO_LOCK, 1);
  961. }
  962. }
  963. }
  964. break;
  965. default:
  966. dprintk(verbose, FE_DEBUG, 1, "Unsupported delivery system");
  967. return -EINVAL;
  968. }
  969. return 0;
  970. }
  971. /*
  972. * stb0899_get_error
  973. * viterbi error for DVB-S/DSS
  974. * packet error for DVB-S2
  975. * Bit Error Rate or Packet Error Rate * 10 ^ 7
  976. */
  977. static int stb0899_read_ber(struct dvb_frontend *fe, u32 *ber)
  978. {
  979. struct stb0899_state *state = fe->demodulator_priv;
  980. struct stb0899_internal *internal = &state->internal;
  981. u8 lsb, msb;
  982. u32 i;
  983. *ber = 0;
  984. switch (state->delsys) {
  985. case DVBFE_DELSYS_DVBS:
  986. case DVBFE_DELSYS_DSS:
  987. if (internal->lock) {
  988. /* average 5 BER values */
  989. for (i = 0; i < 5; i++) {
  990. msleep(100);
  991. lsb = stb0899_read_reg(state, STB0899_ECNT1L);
  992. msb = stb0899_read_reg(state, STB0899_ECNT1M);
  993. *ber += MAKEWORD16(msb, lsb);
  994. }
  995. *ber /= 5;
  996. /* Viterbi Check */
  997. if (STB0899_GETFIELD(VSTATUS_PRFVIT, internal->v_status)) {
  998. /* Error Rate */
  999. *ber *= 9766;
  1000. /* ber = ber * 10 ^ 7 */
  1001. *ber /= (-1 + (1 << (2 * STB0899_GETFIELD(NOE, internal->err_ctrl))));
  1002. *ber /= 8;
  1003. }
  1004. }
  1005. break;
  1006. case DVBFE_DELSYS_DVBS2:
  1007. if (internal->lock) {
  1008. /* Average 5 PER values */
  1009. for (i = 0; i < 5; i++) {
  1010. msleep(100);
  1011. lsb = stb0899_read_reg(state, STB0899_ECNT1L);
  1012. msb = stb0899_read_reg(state, STB0899_ECNT1M);
  1013. *ber += MAKEWORD16(msb, lsb);
  1014. }
  1015. /* ber = ber * 10 ^ 7 */
  1016. *ber *= 10000000;
  1017. *ber /= (-1 + (1 << (4 + 2 * STB0899_GETFIELD(NOE, internal->err_ctrl))));
  1018. }
  1019. break;
  1020. default:
  1021. dprintk(verbose, FE_DEBUG, 1, "Unsupported delivery system");
  1022. return -EINVAL;
  1023. }
  1024. return 0;
  1025. }
  1026. static int stb0899_set_voltage(struct dvb_frontend *fe, fe_sec_voltage_t voltage)
  1027. {
  1028. struct stb0899_state *state = fe->demodulator_priv;
  1029. switch (voltage) {
  1030. case SEC_VOLTAGE_13:
  1031. stb0899_write_reg(state, STB0899_GPIO00CFG, 0x82);
  1032. stb0899_write_reg(state, STB0899_GPIO01CFG, 0x02);
  1033. stb0899_write_reg(state, STB0899_GPIO02CFG, 0x00);
  1034. break;
  1035. case SEC_VOLTAGE_18:
  1036. stb0899_write_reg(state, STB0899_GPIO00CFG, 0x02);
  1037. stb0899_write_reg(state, STB0899_GPIO01CFG, 0x02);
  1038. stb0899_write_reg(state, STB0899_GPIO02CFG, 0x82);
  1039. break;
  1040. case SEC_VOLTAGE_OFF:
  1041. stb0899_write_reg(state, STB0899_GPIO00CFG, 0x82);
  1042. stb0899_write_reg(state, STB0899_GPIO01CFG, 0x82);
  1043. stb0899_write_reg(state, STB0899_GPIO02CFG, 0x82);
  1044. break;
  1045. default:
  1046. return -EINVAL;
  1047. }
  1048. return 0;
  1049. }
  1050. static int stb0899_set_tone(struct dvb_frontend *fe, fe_sec_tone_mode_t tone)
  1051. {
  1052. struct stb0899_state *state = fe->demodulator_priv;
  1053. struct stb0899_internal *internal = &state->internal;
  1054. u8 div, reg;
  1055. /* wait for diseqc idle */
  1056. if (stb0899_wait_diseqc_txidle(state, 100) < 0)
  1057. return -ETIMEDOUT;
  1058. switch (tone) {
  1059. case SEC_TONE_ON:
  1060. div = (internal->master_clk / 100) / 5632;
  1061. div = (div + 5) / 10;
  1062. stb0899_write_reg(state, STB0899_DISEQCOCFG, 0x66);
  1063. reg = stb0899_read_reg(state, STB0899_ACRPRESC);
  1064. STB0899_SETFIELD_VAL(ACRPRESC, reg, 0x03);
  1065. stb0899_write_reg(state, STB0899_ACRPRESC, reg);
  1066. stb0899_write_reg(state, STB0899_ACRDIV1, div);
  1067. break;
  1068. case SEC_TONE_OFF:
  1069. stb0899_write_reg(state, STB0899_DISEQCOCFG, 0x20);
  1070. break;
  1071. default:
  1072. return -EINVAL;
  1073. }
  1074. return 0;
  1075. }
  1076. int stb0899_i2c_gate_ctrl(struct dvb_frontend *fe, int enable)
  1077. {
  1078. int i2c_stat;
  1079. struct stb0899_state *state = fe->demodulator_priv;
  1080. i2c_stat = stb0899_read_reg(state, STB0899_I2CRPT);
  1081. if (i2c_stat < 0)
  1082. goto err;
  1083. if (enable) {
  1084. dprintk(state->verbose, FE_DEBUG, 1, "Enabling I2C Repeater ...");
  1085. i2c_stat |= STB0899_I2CTON;
  1086. if (stb0899_write_reg(state, STB0899_I2CRPT, i2c_stat) < 0)
  1087. goto err;
  1088. } else {
  1089. dprintk(state->verbose, FE_DEBUG, 1, "Disabling I2C Repeater ...");
  1090. i2c_stat &= ~STB0899_I2CTON;
  1091. if (stb0899_write_reg(state, STB0899_I2CRPT, i2c_stat) < 0)
  1092. goto err;
  1093. }
  1094. return 0;
  1095. err:
  1096. dprintk(state->verbose, FE_ERROR, 1, "I2C Repeater control failed");
  1097. return -EREMOTEIO;
  1098. }
  1099. static inline void CONVERT32(u32 x, char *str)
  1100. {
  1101. *str++ = (x >> 24) & 0xff;
  1102. *str++ = (x >> 16) & 0xff;
  1103. *str++ = (x >> 8) & 0xff;
  1104. *str++ = (x >> 0) & 0xff;
  1105. *str = '\0';
  1106. }
  1107. int stb0899_get_dev_id(struct stb0899_state *state)
  1108. {
  1109. u8 chip_id, release;
  1110. u16 id;
  1111. u32 demod_ver = 0, fec_ver = 0;
  1112. char demod_str[5] = { 0 };
  1113. char fec_str[5] = { 0 };
  1114. id = stb0899_read_reg(state, STB0899_DEV_ID);
  1115. dprintk(state->verbose, FE_DEBUG, 1, "ID reg=[0x%02x]", id);
  1116. chip_id = STB0899_GETFIELD(CHIP_ID, id);
  1117. release = STB0899_GETFIELD(CHIP_REL, id);
  1118. dprintk(state->verbose, FE_ERROR, 1, "Device ID=[%d], Release=[%d]",
  1119. chip_id, release);
  1120. CONVERT32(STB0899_READ_S2REG(STB0899_S2DEMOD, DMD_CORE_ID), (char *)&demod_str);
  1121. demod_ver = STB0899_READ_S2REG(STB0899_S2DEMOD, DMD_VERSION_ID);
  1122. dprintk(state->verbose, FE_ERROR, 1, "Demodulator Core ID=[%s], Version=[%d]", (char *) &demod_str, demod_ver);
  1123. CONVERT32(STB0899_READ_S2REG(STB0899_S2FEC, FEC_CORE_ID_REG), (char *)&fec_str);
  1124. fec_ver = STB0899_READ_S2REG(STB0899_S2FEC, FEC_VER_ID_REG);
  1125. if (! (chip_id > 0)) {
  1126. dprintk(state->verbose, FE_ERROR, 1, "couldn't find a STB 0899");
  1127. return -ENODEV;
  1128. }
  1129. dprintk(state->verbose, FE_ERROR, 1, "FEC Core ID=[%s], Version=[%d]", (char*) &fec_str, fec_ver);
  1130. return 0;
  1131. }
  1132. static const struct dvbfe_info dvbs_info = {
  1133. .name = "STB0899 DVB-S",
  1134. .delsys = {
  1135. .dvbs.modulation = DVBFE_MOD_QPSK,
  1136. .dvbs.fec = DVBFE_FEC_1_2 | DVBFE_FEC_2_3 |
  1137. DVBFE_FEC_3_4 | DVBFE_FEC_5_6 |
  1138. DVBFE_FEC_6_7
  1139. },
  1140. .frequency_min = 950000,
  1141. .frequency_max = 2150000,
  1142. .frequency_step = 0,
  1143. .symbol_rate_min = 1000000,
  1144. .symbol_rate_max = 45000000,
  1145. .symbol_rate_tolerance = 0
  1146. };
  1147. static const struct dvbfe_info dss_info = {
  1148. .name = "STB0899 DSS",
  1149. .delsys = {
  1150. .dss.modulation = DVBFE_MOD_BPSK | DVBFE_MOD_QPSK,
  1151. .dss.fec = DVBFE_FEC_1_2 | DVBFE_FEC_2_3 |
  1152. DVBFE_FEC_3_4 | DVBFE_FEC_5_6 |
  1153. DVBFE_FEC_6_7
  1154. },
  1155. .frequency_min = 950000,
  1156. .frequency_max = 2150000,
  1157. .frequency_step = 0,
  1158. .symbol_rate_min = 1000000,
  1159. .symbol_rate_max = 45000000,
  1160. .symbol_rate_tolerance = 0
  1161. };
  1162. static const struct dvbfe_info dvbs2_info = {
  1163. .name = "STB0899 DVB-S2",
  1164. .delsys = {
  1165. .dvbs2.modulation = DVBFE_MOD_QPSK | DVBFE_MOD_8PSK |
  1166. DVBFE_MOD_16APSK | DVBFE_MOD_32APSK,
  1167. .dvbs2.fec = DVBFE_FEC_1_4 | DVBFE_FEC_1_3 |
  1168. DVBFE_FEC_2_5 | DVBFE_FEC_1_2 |
  1169. DVBFE_FEC_3_5 | DVBFE_FEC_2_3 |
  1170. DVBFE_FEC_3_4 | DVBFE_FEC_4_5 |
  1171. DVBFE_FEC_5_6 | DVBFE_FEC_8_9 |
  1172. DVBFE_FEC_9_10,
  1173. },
  1174. .frequency_min = 950000,
  1175. .frequency_max = 2150000,
  1176. .frequency_step = 0,
  1177. .symbol_rate_min = 1000000,
  1178. .symbol_rate_max = 45000000,
  1179. .symbol_rate_tolerance = 0
  1180. };
  1181. static int stb0899_get_info(struct dvb_frontend *fe, struct dvbfe_info *fe_info)
  1182. {
  1183. struct stb0899_state *state = fe->demodulator_priv;
  1184. dprintk(verbose, FE_DEBUG, 1, "Get Info");
  1185. switch (state->delsys) {
  1186. case DVBFE_DELSYS_DVBS:
  1187. dprintk(verbose, FE_ERROR, 1, "Querying DVB-S info");
  1188. memcpy(fe_info, &dvbs_info, sizeof (struct dvbfe_info));
  1189. break;
  1190. case DVBFE_DELSYS_DSS:
  1191. dprintk(verbose, FE_ERROR, 1, "Querying DSS info");
  1192. memcpy(fe_info, &dss_info, sizeof (struct dvbfe_info));
  1193. break;
  1194. case DVBFE_DELSYS_DVBS2:
  1195. dprintk(verbose, FE_ERROR, 1, "Querying DVB-S2 info");
  1196. memcpy(fe_info, &dvbs2_info, sizeof (struct dvbfe_info));
  1197. break;
  1198. default:
  1199. dprintk(verbose, FE_ERROR, 1, "Unsupported delivery system");
  1200. return -EINVAL;
  1201. }
  1202. dprintk(verbose, FE_DEBUG, 1, "delivery system=%d", state->delsys);
  1203. return 0;
  1204. }
  1205. static int stb0899_get_delsys(struct dvb_frontend *fe, enum dvbfe_delsys *fe_delsys)
  1206. {
  1207. *fe_delsys = DVBFE_DELSYS_DVBS | DVBFE_DELSYS_DSS | DVBFE_DELSYS_DVBS2;
  1208. return 0;
  1209. }
  1210. static void stb0899_set_delivery(struct stb0899_state *state)
  1211. {
  1212. u8 reg;
  1213. u8 stop_clk[2];
  1214. stop_clk[0] = stb0899_read_reg(state, STB0899_STOPCLK1);
  1215. stop_clk[1] = stb0899_read_reg(state, STB0899_STOPCLK2);
  1216. switch (state->delsys) {
  1217. case DVBFE_DELSYS_DVBS:
  1218. dprintk(verbose, FE_DEBUG, 1, "Delivery System -- DVB-S");
  1219. /* FECM/Viterbi ON */
  1220. reg = stb0899_read_reg(state, STB0899_FECM);
  1221. STB0899_SETFIELD_VAL(FECM_RSVD0, reg, 0);
  1222. STB0899_SETFIELD_VAL(FECM_VITERBI_ON, reg, 1);
  1223. stb0899_write_reg(state, STB0899_FECM, reg);
  1224. stb0899_write_reg(state, STB0899_RSULC, 0xb1);
  1225. stb0899_write_reg(state, STB0899_TSULC, 0x40);
  1226. stb0899_write_reg(state, STB0899_RSLLC, 0x42);
  1227. stb0899_write_reg(state, STB0899_TSLPL, 0x12);
  1228. reg = stb0899_read_reg(state, STB0899_TSTRES);
  1229. STB0899_SETFIELD_VAL(FRESLDPC, reg, 1);
  1230. stb0899_write_reg(state, STB0899_TSTRES, reg);
  1231. STB0899_SETFIELD_VAL(STOP_CHK8PSK, stop_clk[0], 1);
  1232. STB0899_SETFIELD_VAL(STOP_CKFEC108, stop_clk[0], 1);
  1233. STB0899_SETFIELD_VAL(STOP_CKFEC216, stop_clk[0], 1);
  1234. STB0899_SETFIELD_VAL(STOP_CKPKDLIN108, stop_clk[1], 1);
  1235. STB0899_SETFIELD_VAL(STOP_CKPKDLIN216, stop_clk[1], 1);
  1236. STB0899_SETFIELD_VAL(STOP_CKINTBUF216, stop_clk[0], 1);
  1237. STB0899_SETFIELD_VAL(STOP_CKCORE216, stop_clk[0], 0);
  1238. STB0899_SETFIELD_VAL(STOP_CKS2DMD108, stop_clk[1], 1);
  1239. break;
  1240. case DVBFE_DELSYS_DVBS2:
  1241. /* FECM/Viterbi OFF */
  1242. reg = stb0899_read_reg(state, STB0899_FECM);
  1243. STB0899_SETFIELD_VAL(FECM_RSVD0, reg, 0);
  1244. STB0899_SETFIELD_VAL(FECM_VITERBI_ON, reg, 0);
  1245. stb0899_write_reg(state, STB0899_FECM, reg);
  1246. stb0899_write_reg(state, STB0899_RSULC, 0xb1);
  1247. stb0899_write_reg(state, STB0899_TSULC, 0x42);
  1248. stb0899_write_reg(state, STB0899_RSLLC, 0x40);
  1249. stb0899_write_reg(state, STB0899_TSLPL, 0x02);
  1250. reg = stb0899_read_reg(state, STB0899_TSTRES);
  1251. STB0899_SETFIELD_VAL(FRESLDPC, reg, 0);
  1252. stb0899_write_reg(state, STB0899_TSTRES, reg);
  1253. STB0899_SETFIELD_VAL(STOP_CHK8PSK, stop_clk[0], 1);
  1254. STB0899_SETFIELD_VAL(STOP_CKFEC108, stop_clk[0], 0);
  1255. STB0899_SETFIELD_VAL(STOP_CKFEC216, stop_clk[0], 0);
  1256. STB0899_SETFIELD_VAL(STOP_CKPKDLIN108, stop_clk[1], 0);
  1257. STB0899_SETFIELD_VAL(STOP_CKPKDLIN216, stop_clk[1], 0);
  1258. STB0899_SETFIELD_VAL(STOP_CKINTBUF216, stop_clk[0], 0);
  1259. STB0899_SETFIELD_VAL(STOP_CKCORE216, stop_clk[0], 0);
  1260. STB0899_SETFIELD_VAL(STOP_CKS2DMD108, stop_clk[1], 0);
  1261. break;
  1262. case DVBFE_DELSYS_DSS:
  1263. /* FECM/Viterbi ON */
  1264. reg = stb0899_read_reg(state, STB0899_FECM);
  1265. STB0899_SETFIELD_VAL(FECM_RSVD0, reg, 1);
  1266. STB0899_SETFIELD_VAL(FECM_VITERBI_ON, reg, 1);
  1267. stb0899_write_reg(state, STB0899_FECM, reg);
  1268. stb0899_write_reg(state, STB0899_RSULC, 0xa1);
  1269. stb0899_write_reg(state, STB0899_TSULC, 0x61);
  1270. stb0899_write_reg(state, STB0899_RSLLC, 0x42);
  1271. reg = stb0899_read_reg(state, STB0899_TSTRES);
  1272. STB0899_SETFIELD_VAL(FRESLDPC, reg, 1);
  1273. stb0899_write_reg(state, STB0899_TSTRES, reg);
  1274. STB0899_SETFIELD_VAL(STOP_CHK8PSK, stop_clk[0], 1);
  1275. STB0899_SETFIELD_VAL(STOP_CKFEC108, stop_clk[0], 1);
  1276. STB0899_SETFIELD_VAL(STOP_CKFEC216, stop_clk[0], 1);
  1277. STB0899_SETFIELD_VAL(STOP_CKPKDLIN108, stop_clk[1], 1);
  1278. STB0899_SETFIELD_VAL(STOP_CKPKDLIN216, stop_clk[1], 1);
  1279. STB0899_SETFIELD_VAL(STOP_CKCORE216, stop_clk[0], 0);
  1280. STB0899_SETFIELD_VAL(STOP_CKS2DMD108, stop_clk[1], 1);
  1281. break;
  1282. default:
  1283. dprintk(verbose, FE_ERROR, 1, "Unsupported delivery system");
  1284. break;
  1285. }
  1286. STB0899_SETFIELD_VAL(STOP_CKADCI108, stop_clk[0], 0);
  1287. stb0899_write_regs(state, STB0899_STOPCLK1, stop_clk, 2);
  1288. }
  1289. /*
  1290. * stb0899_set_iterations
  1291. * set the LDPC iteration scale function
  1292. */
  1293. static void stb0899_set_iterations(struct stb0899_state *state)
  1294. {
  1295. struct stb0899_internal *internal = &state->internal;
  1296. struct stb0899_config *config = state->config;
  1297. s32 iter_scale;
  1298. u32 reg;
  1299. iter_scale = 17 * (internal->master_clk / 1000);
  1300. iter_scale += 410000;
  1301. iter_scale /= (internal->srate / 1000000);
  1302. iter_scale /= 1000;
  1303. if (iter_scale > config->ldpc_max_iter)
  1304. iter_scale = config->ldpc_max_iter;
  1305. reg = STB0899_READ_S2REG(STB0899_S2DEMOD, MAX_ITER);
  1306. STB0899_SETFIELD_VAL(MAX_ITERATIONS, reg, iter_scale);
  1307. stb0899_write_s2reg(state, STB0899_S2DEMOD, STB0899_BASE_MAX_ITER, STB0899_OFF0_MAX_ITER, reg);
  1308. }
  1309. static enum dvbfe_search stb0899_search(struct dvb_frontend *fe, struct dvbfe_params *params)
  1310. {
  1311. struct stb0899_state *state = fe->demodulator_priv;
  1312. struct stb0899_params *i_params = &state->params;
  1313. struct stb0899_internal *internal = &state->internal;
  1314. struct stb0899_config *config = state->config;
  1315. u32 SearchRange, gain;
  1316. i_params->freq = params->frequency;
  1317. switch (state->delsys) {
  1318. case DVBFE_DELSYS_DVBS:
  1319. dprintk(verbose, FE_ERROR, 1, "set DVB-S params");
  1320. i_params->srate = params->delsys.dvbs.symbol_rate;
  1321. break;
  1322. case DVBFE_DELSYS_DSS:
  1323. dprintk(verbose, FE_ERROR, 1, "set DSS params");
  1324. i_params->srate = params->delsys.dss.symbol_rate;
  1325. break;
  1326. case DVBFE_DELSYS_DVBS2:
  1327. dprintk(verbose, FE_ERROR, 1, "set DVB-S2 params");
  1328. i_params->srate = params->delsys.dvbs2.symbol_rate;
  1329. break;
  1330. default:
  1331. dprintk(verbose, FE_ERROR, 1, "Unsupported delivery system");
  1332. return -EINVAL;
  1333. }
  1334. dprintk(verbose, FE_DEBUG, 1, "delivery system=%d", state->delsys);
  1335. SearchRange = 10000000;
  1336. dprintk(verbose, FE_DEBUG, 1, "Frequency=%d, Srate=%d", i_params->freq, i_params->srate);
  1337. /* checking Search Range is meaningless for a fixed 3 Mhz */
  1338. if (INRANGE(i_params->srate, 1000000, 45000000)) {
  1339. dprintk(verbose, FE_DEBUG, 1, "Parameters IN RANGE");
  1340. stb0899_set_delivery(state);
  1341. if (state->config->tuner_set_rfsiggain) {
  1342. if (internal->srate > 15000000)
  1343. gain = 8; /* 15Mb < srate < 45Mb, gain = 8dB */
  1344. else if (internal->srate > 5000000)
  1345. gain = 12; /* 5Mb < srate < 15Mb, gain = 12dB */
  1346. else
  1347. gain = 14; /* 1Mb < srate < 5Mb, gain = 14db */
  1348. state->config->tuner_set_rfsiggain(fe, gain);
  1349. }
  1350. if (i_params->srate <= 5000000)
  1351. stb0899_set_mclk(state, config->lo_clk);
  1352. else
  1353. stb0899_set_mclk(state, config->hi_clk);
  1354. switch (state->delsys) {
  1355. case DVBFE_DELSYS_DVBS:
  1356. case DVBFE_DELSYS_DSS:
  1357. dprintk(verbose, FE_DEBUG, 1, "DVB-S delivery system");
  1358. internal->freq = i_params->freq;
  1359. internal->srate = i_params->srate;
  1360. /*
  1361. * search = user search range +
  1362. * 500Khz +
  1363. * 2 * Tuner_step_size +
  1364. * 10% of the symbol rate
  1365. */
  1366. internal->srch_range = SearchRange + 1500000 + (i_params->srate / 5);
  1367. internal->derot_percent = 30;
  1368. /* What to do for tuners having no bandwidth setup ? */
  1369. /* enable tuner I/O */
  1370. stb0899_i2c_gate_ctrl(&state->frontend, 1);
  1371. if (state->config->tuner_set_bandwidth)
  1372. state->config->tuner_set_bandwidth(fe, (13 * (stb0899_carr_width(state) + SearchRange)) / 10);
  1373. if (state->config->tuner_get_bandwidth)
  1374. state->config->tuner_get_bandwidth(fe, &internal->tuner_bw);
  1375. /* disable tuner I/O */
  1376. stb0899_i2c_gate_ctrl(&state->frontend, 0);
  1377. /* Set DVB-S1 AGC */
  1378. stb0899_write_reg(state, STB0899_AGCRFCFG, 0x11);
  1379. /* Run the search algorithm */
  1380. dprintk(verbose, FE_DEBUG, 1, "running DVB-S search algo ..");
  1381. if (stb0899_dvbs_algo(state) == RANGEOK) {
  1382. internal->lock = 1;
  1383. dprintk(verbose, FE_DEBUG, 1,
  1384. "-------------------------------------> DVB-S LOCK !");
  1385. // stb0899_write_reg(state, STB0899_ERRCTRL1, 0x3d); /* Viterbi Errors */
  1386. // internal->v_status = stb0899_read_reg(state, STB0899_VSTATUS);
  1387. // internal->err_ctrl = stb0899_read_reg(state, STB0899_ERRCTRL1);
  1388. // dprintk(verbose, FE_DEBUG, 1, "VSTATUS=0x%02x", internal->v_status);
  1389. // dprintk(verbose, FE_DEBUG, 1, "ERR_CTRL=0x%02x", internal->err_ctrl);
  1390. return DVBFE_ALGO_SEARCH_SUCCESS;
  1391. } else {
  1392. internal->lock = 0;
  1393. return DVBFE_ALGO_SEARCH_FAILED;
  1394. }
  1395. break;
  1396. case DVBFE_DELSYS_DVBS2:
  1397. internal->freq = i_params->freq;
  1398. internal->srate = i_params->srate;
  1399. internal->srch_range = SearchRange;
  1400. /* enable tuner I/O */
  1401. stb0899_i2c_gate_ctrl(&state->frontend, 1);
  1402. if (state->config->tuner_set_bandwidth)
  1403. state->config->tuner_set_bandwidth(fe, (stb0899_carr_width(state) + SearchRange));
  1404. if (state->config->tuner_get_bandwidth)
  1405. state->config->tuner_get_bandwidth(fe, &internal->tuner_bw);
  1406. /* disable tuner I/O */
  1407. stb0899_i2c_gate_ctrl(&state->frontend, 0);
  1408. // pParams->SpectralInv = pSearch->IQ_Inversion;
  1409. /* Set DVB-S2 AGC */
  1410. stb0899_write_reg(state, STB0899_AGCRFCFG, 0x1c);
  1411. /* Set IterScale =f(MCLK,SYMB) */
  1412. stb0899_set_iterations(state);
  1413. /* Run the search algorithm */
  1414. dprintk(verbose, FE_DEBUG, 1, "running DVB-S2 search algo ..");
  1415. if (stb0899_dvbs2_algo(state) == DVBS2_FEC_LOCK) {
  1416. internal->lock = 1;
  1417. dprintk(verbose, FE_DEBUG, 1,
  1418. "-------------------------------------> DVB-S2 LOCK !");
  1419. // stb0899_write_reg(state, STB0899_ERRCTRL1, 0xb6); /* Packet Errors */
  1420. // internal->v_status = stb0899_read_reg(state, STB0899_VSTATUS);
  1421. // internal->err_ctrl = stb0899_read_reg(state, STB0899_ERRCTRL1);
  1422. return DVBFE_ALGO_SEARCH_SUCCESS;
  1423. } else {
  1424. internal->lock = 0;
  1425. return DVBFE_ALGO_SEARCH_FAILED;
  1426. }
  1427. break;
  1428. default:
  1429. dprintk(verbose, FE_ERROR, 1, "Unsupported delivery system");
  1430. return DVBFE_ALGO_SEARCH_INVALID;
  1431. }
  1432. }
  1433. return DVBFE_ALGO_SEARCH_ERROR;
  1434. }
  1435. static enum stb0899_status stb0899_track_carrier(struct stb0899_state *state)
  1436. {
  1437. u8 reg;
  1438. reg = stb0899_read_reg(state, STB0899_DSTATUS);
  1439. dprintk(verbose, FE_DEBUG, 1, "--------------------> STB0899_DSTATUS=[0x%02x]", reg);
  1440. if (STB0899_GETFIELD(CARRIER_FOUND, reg)) {
  1441. dprintk(verbose, FE_DEBUG, 1, "-------------> CARRIEROK !");
  1442. return CARRIEROK;
  1443. } else {
  1444. dprintk(verbose, FE_DEBUG, 1, "-------------> NOCARRIER !");
  1445. return NOCARRIER;
  1446. }
  1447. return NOCARRIER;
  1448. }
  1449. static enum stb0899_status stb0899_get_ifagc(struct stb0899_state *state)
  1450. {
  1451. u8 reg;
  1452. reg = STB0899_READ_S2REG(STB0899_S2DEMOD, DMD_STATUS);
  1453. dprintk(verbose, FE_DEBUG, 1, "DMD_STATUS=[0x%02x]", reg);
  1454. if (STB0899_GETFIELD(IF_AGC_LOCK, reg)) {
  1455. dprintk(verbose, FE_DEBUG, 1, "------------->IF AGC LOCKED !");
  1456. return AGC1OK;
  1457. } else {
  1458. dprintk(verbose, FE_DEBUG, 1, "------------->IF AGC LOCK LOST !");
  1459. return NOAGC1;
  1460. }
  1461. return NOAGC1;
  1462. }
  1463. static int stb0899_get_s1fec(struct stb0899_internal *internal, enum dvbfe_fec *fec)
  1464. {
  1465. switch (internal->fecrate) {
  1466. case STB0899_FEC_1_2:
  1467. *fec = DVBFE_FEC_1_2;
  1468. break;
  1469. case STB0899_FEC_2_3:
  1470. *fec = DVBFE_FEC_2_3;
  1471. break;
  1472. case STB0899_FEC_3_4:
  1473. *fec = DVBFE_FEC_3_4;
  1474. break;
  1475. case STB0899_FEC_5_6:
  1476. *fec = DVBFE_FEC_5_6;
  1477. break;
  1478. case STB0899_FEC_6_7:
  1479. *fec = DVBFE_FEC_6_7;
  1480. break;
  1481. case STB0899_FEC_7_8:
  1482. *fec = DVBFE_FEC_7_8;
  1483. break;
  1484. default:
  1485. return -EINVAL;
  1486. }
  1487. return 0;
  1488. }
  1489. static int stb0899_get_modcod(struct stb0899_internal *internal, struct dvbs2_params *params)
  1490. {
  1491. switch (internal->modcod) {
  1492. case STB0899_DUMMY_PLF:
  1493. params->modulation = DVBFE_MOD_NONE;
  1494. params->fec = DVBFE_FEC_NONE;
  1495. break;
  1496. case STB0899_QPSK_14:
  1497. params->modulation = DVBFE_MOD_QPSK;
  1498. params->fec = DVBFE_FEC_1_4;
  1499. break;
  1500. case STB0899_QPSK_13:
  1501. params->modulation = DVBFE_MOD_QPSK;
  1502. params->fec = DVBFE_FEC_1_3;
  1503. break;
  1504. case STB0899_QPSK_25:
  1505. params->modulation = DVBFE_MOD_QPSK;
  1506. params->fec = DVBFE_FEC_2_5;
  1507. break;
  1508. case STB0899_QPSK_12:
  1509. params->modulation = DVBFE_MOD_QPSK;
  1510. params->fec = DVBFE_FEC_1_2;
  1511. break;
  1512. case STB0899_QPSK_35:
  1513. params->modulation = DVBFE_MOD_QPSK;
  1514. params->fec = DVBFE_FEC_3_5;
  1515. break;
  1516. case STB0899_QPSK_23:
  1517. params->modulation = DVBFE_MOD_QPSK;
  1518. params->fec = DVBFE_FEC_2_3;
  1519. break;
  1520. case STB0899_QPSK_34:
  1521. params->modulation = DVBFE_MOD_QPSK;
  1522. params->fec = DVBFE_FEC_3_4;
  1523. break;
  1524. case STB0899_QPSK_45:
  1525. params->modulation = DVBFE_MOD_QPSK;
  1526. params->fec = DVBFE_FEC_4_5;
  1527. break;
  1528. case STB0899_QPSK_56:
  1529. params->modulation = DVBFE_MOD_QPSK;
  1530. params->fec = DVBFE_FEC_5_6;
  1531. break;
  1532. case STB0899_QPSK_89:
  1533. params->modulation = DVBFE_MOD_QPSK;
  1534. params->fec = DVBFE_FEC_8_9;
  1535. break;
  1536. case STB0899_QPSK_910:
  1537. params->modulation = DVBFE_MOD_QPSK;
  1538. params->fec = DVBFE_FEC_9_10;
  1539. break;
  1540. case STB0899_8PSK_35:
  1541. params->modulation = DVBFE_MOD_8PSK;
  1542. params->fec = DVBFE_FEC_3_5;
  1543. break;
  1544. case STB0899_8PSK_23:
  1545. params->modulation = DVBFE_MOD_8PSK;
  1546. params->fec = DVBFE_FEC_2_3;
  1547. break;
  1548. case STB0899_8PSK_34:
  1549. params->modulation = DVBFE_MOD_8PSK;
  1550. params->fec = DVBFE_FEC_3_4;
  1551. break;
  1552. case STB0899_8PSK_56:
  1553. params->modulation = DVBFE_MOD_8PSK;
  1554. params->fec = DVBFE_FEC_5_6;
  1555. break;
  1556. case STB0899_8PSK_89:
  1557. params->modulation = DVBFE_MOD_8PSK;
  1558. params->fec = DVBFE_FEC_8_9;
  1559. break;
  1560. case STB0899_8PSK_910:
  1561. params->modulation = DVBFE_MOD_8PSK;
  1562. params->fec = DVBFE_FEC_9_10;
  1563. break;
  1564. case STB0899_16APSK_23:
  1565. params->modulation = DVBFE_MOD_16APSK;
  1566. params->fec = DVBFE_FEC_2_3;
  1567. break;
  1568. case STB0899_16APSK_34:
  1569. params->modulation = DVBFE_MOD_16APSK;
  1570. params->fec = DVBFE_FEC_3_4;
  1571. break;
  1572. case STB0899_16APSK_45:
  1573. params->modulation = DVBFE_MOD_16APSK;
  1574. params->fec = DVBFE_FEC_4_5;
  1575. break;
  1576. case STB0899_16APSK_56:
  1577. params->modulation = DVBFE_MOD_16APSK;
  1578. params->fec = DVBFE_FEC_5_6;
  1579. break;
  1580. case STB0899_16APSK_89:
  1581. params->modulation = DVBFE_MOD_16APSK;
  1582. params->fec = DVBFE_FEC_8_9;
  1583. break;
  1584. case STB0899_16APSK_910:
  1585. params->modulation = DVBFE_MOD_16APSK;
  1586. params->fec = DVBFE_FEC_9_10;
  1587. break;
  1588. case STB0899_32APSK_34:
  1589. params->modulation = DVBFE_MOD_32APSK;
  1590. params->fec = DVBFE_FEC_3_4;
  1591. break;
  1592. case STB0899_32APSK_45:
  1593. params->modulation = DVBFE_MOD_32APSK;
  1594. params->fec = DVBFE_FEC_4_5;
  1595. break;
  1596. case STB0899_32APSK_56:
  1597. params->modulation = DVBFE_MOD_32APSK;
  1598. params->fec = DVBFE_FEC_5_6;
  1599. break;
  1600. case STB0899_32APSK_89:
  1601. params->modulation = DVBFE_MOD_32APSK;
  1602. params->fec = DVBFE_FEC_8_9;
  1603. break;
  1604. case STB0899_32APSK_910:
  1605. params->modulation = DVBFE_MOD_32APSK;
  1606. params->fec = DVBFE_FEC_9_10;
  1607. break;
  1608. default:
  1609. return -EINVAL;
  1610. }
  1611. return 0;
  1612. }
  1613. /*
  1614. * stb0899_track
  1615. * periodically check the signal level against a specified
  1616. * threshold level and perform derotator centering.
  1617. * called once we have a lock from a succesful search
  1618. * event.
  1619. *
  1620. * Will be called periodically called to maintain the
  1621. * lock.
  1622. *
  1623. * Will be used to get parameters as well as info from
  1624. * the decoded baseband header
  1625. *
  1626. * Once a new lock has established, the internal state
  1627. * frequency (internal->freq) is updated
  1628. */
  1629. static int stb0899_track(struct dvb_frontend *fe, struct dvbfe_params *params, int *delay)
  1630. {
  1631. u32 lock_lost;
  1632. struct stb0899_state *state = fe->demodulator_priv;
  1633. struct stb0899_internal *internal = &state->internal;
  1634. *delay = HZ/10;
  1635. return 0;
  1636. }
  1637. static int stb0899_get_params(struct dvb_frontend *fe, struct dvbfe_params *params)
  1638. {
  1639. struct stb0899_state *state = fe->demodulator_priv;
  1640. struct stb0899_internal *internal = &state->internal;
  1641. params->frequency = internal->freq;
  1642. params->inversion = internal->inversion;
  1643. params->delivery = state->delsys;
  1644. switch (state->delsys) {
  1645. case DVBFE_DELSYS_DVBS:
  1646. dprintk(verbose, FE_DEBUG, 1, "Get DVB-S params");
  1647. params->delsys.dvbs.symbol_rate = internal->srate;
  1648. params->delsys.dvbs.modulation = DVBFE_MOD_QPSK;
  1649. break;
  1650. case DVBFE_DELSYS_DSS:
  1651. dprintk(verbose, FE_DEBUG, 1, "Get DSS params");
  1652. params->delsys.dss.symbol_rate = internal->srate;
  1653. params->delsys.dss.modulation = DVBFE_MOD_QPSK;
  1654. break;
  1655. case DVBFE_DELSYS_DVBS2:
  1656. dprintk(verbose, FE_DEBUG, 1, "Get DVB-S2 params");
  1657. params->delsys.dvbs2.symbol_rate = internal->srate;
  1658. break;
  1659. default:
  1660. dprintk(verbose, FE_ERROR, 1, "Unsupported delivery system");
  1661. return -EINVAL;
  1662. }
  1663. return 0;
  1664. }
  1665. static enum dvbfe_algo stb0899_frontend_algo(struct dvb_frontend *fe)
  1666. {
  1667. return DVBFE_ALGO_CUSTOM;
  1668. }
  1669. static int stb0899_set_delsys(struct dvb_frontend *fe, enum dvbfe_delsys delsys)
  1670. {
  1671. struct stb0899_state *state = fe->demodulator_priv;
  1672. state->delsys = delsys;
  1673. return 0;
  1674. }
  1675. static struct dvb_frontend_ops stb0899_ops = {
  1676. .info = {
  1677. .name = "STB0899 Multistandard",
  1678. },
  1679. .release = stb0899_release,
  1680. .init = stb0899_init,
  1681. .sleep = stb0899_sleep,
  1682. // .wakeup = stb0899_wakeup,
  1683. .i2c_gate_ctrl = stb0899_i2c_gate_ctrl,
  1684. .get_info = stb0899_get_info,
  1685. .get_delsys = stb0899_get_delsys,
  1686. .set_delsys = stb0899_set_delsys,
  1687. .get_frontend_algo = stb0899_frontend_algo,
  1688. .search = stb0899_search,
  1689. .track = stb0899_track,
  1690. .get_params = stb0899_get_params,
  1691. .read_status = stb0899_read_status,
  1692. .read_snr = stb0899_read_snr,
  1693. .read_signal_strength = stb0899_read_signal_strength,
  1694. .read_status = stb0899_read_status,
  1695. .read_ber = stb0899_read_ber,
  1696. .set_voltage = stb0899_set_voltage,
  1697. .set_tone = stb0899_set_tone,
  1698. .diseqc_send_master_cmd = stb0899_send_diseqc_msg,
  1699. .diseqc_recv_slave_reply = stb0899_recv_slave_reply,
  1700. .diseqc_send_burst = stb0899_send_diseqc_burst,
  1701. };
  1702. struct dvb_frontend *stb0899_attach(struct stb0899_config *config, struct i2c_adapter *i2c)
  1703. {
  1704. struct stb0899_state *state = NULL;
  1705. enum stb0899_inversion inversion;
  1706. state = kzalloc(sizeof (struct stb0899_state), GFP_KERNEL);
  1707. if (state == NULL)
  1708. goto error;
  1709. inversion = config->inversion;
  1710. state->verbose = verbose;
  1711. state->config = config;
  1712. state->i2c = i2c;
  1713. state->frontend.ops = stb0899_ops;
  1714. state->frontend.demodulator_priv = state;
  1715. state->internal.inversion = inversion;
  1716. stb0899_wakeup(&state->frontend);
  1717. if (stb0899_get_dev_id(state) == -ENODEV) {
  1718. printk("%s: Exiting .. !\n", __func__);
  1719. goto error;
  1720. }
  1721. printk("%s: Attaching STB0899 \n", __func__);
  1722. return &state->frontend;
  1723. error:
  1724. kfree(state);
  1725. return NULL;
  1726. }
  1727. EXPORT_SYMBOL(stb0899_attach);
  1728. MODULE_PARM_DESC(verbose, "Set Verbosity level");
  1729. MODULE_AUTHOR("Manu Abraham");
  1730. MODULE_DESCRIPTION("STB0899 Multi-Std frontend");
  1731. MODULE_LICENSE("GPL");