stb0899_drv.c 52 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959
  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 = 5;
  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, size_t 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, size_t 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 = 90000000, 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 void stb0899_release(struct dvb_frontend *fe)
  512. {
  513. struct stb0899_state *state = fe->demodulator_priv;
  514. dprintk(verbose, FE_DEBUG, 1, "Release Frontend");
  515. kfree(state);
  516. }
  517. /*
  518. * stb0899_get_alpha
  519. * return: rolloff
  520. */
  521. static int stb0899_get_alpha(struct stb0899_state *state)
  522. {
  523. u8 mode_coeff;
  524. mode_coeff = stb0899_read_reg(state, STB0899_DEMOD);
  525. if (STB0899_GETFIELD(MODECOEFF, mode_coeff) == 1)
  526. return 20;
  527. else
  528. return 35;
  529. }
  530. /*
  531. * stb0899_init_calc
  532. */
  533. static void stb0899_init_calc(struct stb0899_state *state)
  534. {
  535. struct stb0899_internal *internal = &state->internal;
  536. int master_clk;
  537. u8 agc[1];
  538. u8 agc1cn;
  539. u32 reg;
  540. /* Read registers (in burst mode) */
  541. agc1cn = stb0899_read_reg(state, STB0899_AGC1CN);
  542. stb0899_read_regs(state, STB0899_AGC1REF, agc, 2); /* AGC1R and AGC2O */
  543. /* Initial calculations */
  544. master_clk = stb0899_get_mclk(state);
  545. internal->t_agc1 = 0;
  546. internal->t_agc2 = 0;
  547. internal->master_clk = master_clk;
  548. internal->mclk = master_clk / 65536L;
  549. internal->rolloff = stb0899_get_alpha(state);
  550. /* DVBS2 Initial calculations */
  551. /* Set AGC value to the middle */
  552. internal->agc_gain = 8154;
  553. reg = STB0899_READ_S2REG(STB0899_S2DEMOD, IF_AGC_CNTRL);
  554. STB0899_SETFIELD_VAL(IF_GAIN_INIT, reg, internal->agc_gain);
  555. stb0899_write_s2reg(state, STB0899_S2DEMOD, STB0899_BASE_IF_AGC_CNTRL, STB0899_OFF0_IF_AGC_CNTRL, reg);
  556. reg = STB0899_READ_S2REG(STB0899_S2DEMOD, RRC_ALPHA);
  557. internal->rrc_alpha = STB0899_GETFIELD(RRC_ALPHA, reg);
  558. internal->center_freq = 0;
  559. internal->av_frame_coarse = 10;
  560. internal->av_frame_fine = 20;
  561. internal->step_size = 2;
  562. /*
  563. if ((pParams->SpectralInv == FE_IQ_NORMAL) || (pParams->SpectralInv == FE_IQ_AUTO))
  564. pParams->IQLocked = 0;
  565. else
  566. pParams->IQLocked = 1;
  567. */
  568. }
  569. static int stb0899_wait_diseqc_fifo_empty(struct stb0899_state *state, int timeout)
  570. {
  571. u8 reg = 0;
  572. unsigned long start = jiffies;
  573. while (1) {
  574. reg = stb0899_read_reg(state, STB0899_DISSTATUS);
  575. if (!STB0899_GETFIELD(FIFOFULL, reg))
  576. break;
  577. if ((jiffies - start) > timeout) {
  578. dprintk(verbose, FE_ERROR, 1, "timed out !!");
  579. return -ETIMEDOUT;
  580. }
  581. }
  582. return 0;
  583. }
  584. static int stb0899_send_diseqc_msg(struct dvb_frontend *fe, struct dvb_diseqc_master_cmd *cmd)
  585. {
  586. struct stb0899_state *state = fe->demodulator_priv;
  587. u8 reg, i;
  588. if (cmd->msg_len > 8)
  589. return -EINVAL;
  590. /* enable FIFO precharge */
  591. reg = stb0899_read_reg(state, STB0899_DISCNTRL1);
  592. STB0899_SETFIELD_VAL(DISPRECHARGE, reg, 1);
  593. stb0899_write_reg(state, STB0899_DISCNTRL1, reg);
  594. for (i = 0; i < cmd->msg_len; i++) {
  595. /* wait for FIFO empty */
  596. if (stb0899_wait_diseqc_fifo_empty(state, 10) < 0)
  597. return -ETIMEDOUT;
  598. stb0899_write_reg(state, STB0899_DISFIFO, cmd->msg[i]);
  599. }
  600. reg = stb0899_read_reg(state, STB0899_DISCNTRL1);
  601. STB0899_SETFIELD_VAL(DISPRECHARGE, reg, 0);
  602. stb0899_write_reg(state, STB0899_DISCNTRL1, reg);
  603. return 0;
  604. }
  605. static int stb0899_wait_diseqc_rxidle(struct stb0899_state *state, int timeout)
  606. {
  607. u8 reg = 0;
  608. unsigned long start = jiffies;
  609. while (!STB0899_GETFIELD(RXEND, reg)) {
  610. reg = stb0899_read_reg(state, STB0899_DISRX_ST0);
  611. if (jiffies - start > timeout) {
  612. dprintk(verbose, FE_ERROR, 1, "timed out!!");
  613. return -ETIMEDOUT;
  614. }
  615. msleep(10);
  616. }
  617. return 0;
  618. }
  619. static int stb0899_recv_slave_reply(struct dvb_frontend *fe, struct dvb_diseqc_slave_reply *reply)
  620. {
  621. struct stb0899_state *state = fe->demodulator_priv;
  622. u8 reg, length = 0, i;
  623. int result;
  624. if (stb0899_wait_diseqc_rxidle(state, 100) < 0)
  625. return -ETIMEDOUT;
  626. reg = stb0899_read_reg(state, STB0899_DISRX_ST0);
  627. if (STB0899_GETFIELD(RXEND, reg)) {
  628. reg = stb0899_read_reg(state, STB0899_DISRX_ST1);
  629. length = STB0899_GETFIELD(FIFOBYTENBR, reg);
  630. if (length > sizeof (reply->msg)) {
  631. result = -EOVERFLOW;
  632. goto exit;
  633. }
  634. reply->msg_len = length;
  635. /* extract data */
  636. for (i = 0; i < length; i++)
  637. reply->msg[i] = stb0899_read_reg(state, STB0899_DISFIFO);
  638. }
  639. return 0;
  640. exit:
  641. return result;
  642. }
  643. static int stb0899_wait_diseqc_txidle(struct stb0899_state *state, int timeout)
  644. {
  645. u8 reg = 0;
  646. unsigned long start = jiffies;
  647. while (!STB0899_GETFIELD(TXIDLE, reg)) {
  648. reg = stb0899_read_reg(state, STB0899_DISSTATUS);
  649. if (jiffies - start > timeout) {
  650. dprintk(verbose, FE_ERROR, 1, "timed out!!");
  651. return -ETIMEDOUT;
  652. }
  653. msleep(10);
  654. }
  655. return 0;
  656. }
  657. static int stb0899_send_diseqc_burst(struct dvb_frontend *fe, fe_sec_mini_cmd_t burst)
  658. {
  659. struct stb0899_state *state = fe->demodulator_priv;
  660. u8 reg, old_state;
  661. /* wait for diseqc idle */
  662. if (stb0899_wait_diseqc_txidle(state, 100) < 0)
  663. return -ETIMEDOUT;
  664. reg = stb0899_read_reg(state, STB0899_DISCNTRL1);
  665. old_state = reg;
  666. /* set to burst mode */
  667. STB0899_SETFIELD_VAL(DISEQCMODE, reg, 0x02);
  668. STB0899_SETFIELD_VAL(DISPRECHARGE, reg, 0x01);
  669. stb0899_write_reg(state, STB0899_DISCNTRL1, reg);
  670. switch (burst) {
  671. case SEC_MINI_A:
  672. /* unmodulated */
  673. stb0899_write_reg(state, STB0899_DISFIFO, 0x00);
  674. break;
  675. case SEC_MINI_B:
  676. /* modulated */
  677. stb0899_write_reg(state, STB0899_DISFIFO, 0xff);
  678. break;
  679. }
  680. reg = stb0899_read_reg(state, STB0899_DISCNTRL1);
  681. STB0899_SETFIELD_VAL(DISPRECHARGE, reg, 0x00);
  682. stb0899_write_reg(state, STB0899_DISCNTRL1, reg);
  683. /* wait for diseqc idle */
  684. if (stb0899_wait_diseqc_txidle(state, 100) < 0)
  685. return -ETIMEDOUT;
  686. /* restore state */
  687. stb0899_write_reg(state, STB0899_DISCNTRL1, old_state);
  688. return 0;
  689. }
  690. static int stb0899_diseqc_init(struct stb0899_state *state)
  691. {
  692. struct dvb_diseqc_master_cmd tx_data;
  693. struct dvb_diseqc_slave_reply rx_data;
  694. u8 f22_tx, f22_rx, reg;
  695. u32 mclk, tx_freq = 22000, count = 0, i;
  696. u32 trial = 0; /* try max = 2 (try 20khz and 17.5 khz) */
  697. u32 ret_1 = 0; /* 20 Khz status */
  698. u32 ret_2 = 0; /* 17.5 Khz status */
  699. tx_data.msg[0] = 0xe2;
  700. tx_data.msg_len = 3;
  701. reg = stb0899_read_reg(state, STB0899_DISCNTRL2);
  702. STB0899_SETFIELD_VAL(ONECHIP_TRX, reg, 0);
  703. stb0899_write_reg(state, STB0899_DISCNTRL2, reg);
  704. /* disable Tx spy */
  705. reg = stb0899_read_reg(state, STB0899_DISCNTRL1);
  706. STB0899_SETFIELD_VAL(DISEQCRESET, reg, 1);
  707. stb0899_write_reg(state, STB0899_DISCNTRL1, reg);
  708. reg = stb0899_read_reg(state, STB0899_DISCNTRL1);
  709. STB0899_SETFIELD_VAL(DISEQCRESET, reg, 0);
  710. stb0899_write_reg(state, STB0899_DISCNTRL1, reg);
  711. mclk = stb0899_get_mclk(state);
  712. f22_tx = mclk / (tx_freq * 32);
  713. stb0899_write_reg(state, STB0899_DISF22, f22_tx); /* DiSEqC Tx freq */
  714. state->rx_freq = 20000;
  715. f22_rx = mclk / (state->rx_freq * 32);
  716. return 0;
  717. }
  718. static int stb0899_sleep(struct dvb_frontend *fe)
  719. {
  720. struct stb0899_state *state = fe->demodulator_priv;
  721. u8 reg;
  722. dprintk(verbose, FE_DEBUG, 1, "Going to Sleep .. (Really tired .. :-))");
  723. return 0;
  724. }
  725. static int stb0899_wakeup(struct dvb_frontend *fe)
  726. {
  727. int rc;
  728. struct stb0899_state *state = fe->demodulator_priv;
  729. if ((rc = stb0899_write_reg(state, STB0899_SYNTCTRL, STB0899_SELOSCI)))
  730. return rc;
  731. /* Activate all clocks; DVB-S2 registers are inaccessible otherwise. */
  732. if ((rc = stb0899_write_reg(state, STB0899_STOPCLK1, 0x00)))
  733. return rc;
  734. if ((rc = stb0899_write_reg(state, STB0899_STOPCLK2, 0x00)))
  735. return rc;
  736. return 0;
  737. }
  738. static int stb0899_init(struct dvb_frontend *fe)
  739. {
  740. int i;
  741. struct stb0899_state *state = fe->demodulator_priv;
  742. struct stb0899_config *config = state->config;
  743. dprintk(verbose, FE_DEBUG, 1, "Initializing STB0899 ... ");
  744. // mutex_init(&state->search_lock);
  745. /* init device */
  746. dprintk(verbose, FE_DEBUG, 1, "init device");
  747. for (i = 0; config->init_dev[i].address != 0xffff; i++)
  748. stb0899_write_reg(state, config->init_dev[i].address, config->init_dev[i].data);
  749. dprintk(verbose, FE_DEBUG, 1, "init S2 demod");
  750. /* init S2 demod */
  751. for (i = 0; config->init_s2_demod[i].offset != 0xffff; i++)
  752. stb0899_write_s2reg(state, STB0899_S2DEMOD,
  753. config->init_s2_demod[i].base_address,
  754. config->init_s2_demod[i].offset,
  755. config->init_s2_demod[i].data);
  756. dprintk(verbose, FE_DEBUG, 1, "init S1 demod");
  757. /* init S1 demod */
  758. for (i = 0; config->init_s1_demod[i].address != 0xffff; i++)
  759. stb0899_write_reg(state, config->init_s1_demod[i].address, config->init_s1_demod[i].data);
  760. dprintk(verbose, FE_DEBUG, 1, "init S2 FEC");
  761. /* init S2 fec */
  762. for (i = 0; config->init_s2_fec[i].offset != 0xffff; i++)
  763. stb0899_write_s2reg(state, STB0899_S2FEC,
  764. config->init_s2_fec[i].base_address,
  765. config->init_s2_fec[i].offset,
  766. config->init_s2_fec[i].data);
  767. dprintk(verbose, FE_DEBUG, 1, "init TST");
  768. /* init test */
  769. for (i = 0; config->init_tst[i].address != 0xffff; i++)
  770. stb0899_write_reg(state, config->init_tst[i].address, config->init_tst[i].data);
  771. stb0899_init_calc(state);
  772. stb0899_diseqc_init(state);
  773. return 0;
  774. }
  775. static int stb0899_table_lookup(const struct stb0899_tab *tab, int max, int val)
  776. {
  777. int res = 0;
  778. int min = 0, med;
  779. if (val < tab[min].read)
  780. res = tab[min].real;
  781. else if (val >= tab[max].read)
  782. res = tab[max].real;
  783. else {
  784. while ((max - min) > 1) {
  785. med = (max + min) / 2;
  786. if (val >= tab[min].read && val < tab[med].read)
  787. max = med;
  788. else
  789. min = med;
  790. }
  791. res = ((val - tab[min].read) *
  792. (tab[max].real - tab[min].real) /
  793. (tab[max].read - tab[min].read)) +
  794. tab[min].real;
  795. }
  796. return res;
  797. }
  798. static int stb0899_read_signal_strength(struct dvb_frontend *fe, u16 *strength)
  799. {
  800. struct stb0899_state *state = fe->demodulator_priv;
  801. struct stb0899_internal *internal = &state->internal;
  802. int val;
  803. u32 reg;
  804. switch (state->delsys) {
  805. case DVBFE_DELSYS_DVBS:
  806. case DVBFE_DELSYS_DSS:
  807. if (internal->lock) {
  808. reg = stb0899_read_reg(state, STB0899_VSTATUS);
  809. if (STB0899_GETFIELD(VSTATUS_LOCKEDVIT, reg)) {
  810. reg = stb0899_read_reg(state, STB0899_AGCIQIN);
  811. val = (s32)(s8)STB0899_GETFIELD(AGCIQVALUE, reg);
  812. *strength = stb0899_table_lookup(stb0899_dvbsrf_tab, ARRAY_SIZE(stb0899_dvbsrf_tab) - 1, val);
  813. *strength += 750;
  814. dprintk(verbose, FE_DEBUG, 1, "AGCIQVALUE = 0x%02x, C = %d * 0.1 dBm",
  815. val & 0xff, *strength);
  816. }
  817. }
  818. break;
  819. case DVBFE_DELSYS_DVBS2:
  820. if (internal->lock) {
  821. reg = STB0899_READ_S2REG(STB0899_DEMOD, IF_AGC_GAIN);
  822. val = STB0899_GETFIELD(IF_AGC_GAIN, reg);
  823. *strength = stb0899_table_lookup(stb0899_dvbs2rf_tab, ARRAY_SIZE(stb0899_dvbs2rf_tab) - 1, val);
  824. *strength += 750;
  825. dprintk(verbose, FE_DEBUG, 1, "IF_AGC_GAIN = 0x%04x, C = %d * 0.1 dBm",
  826. val & 0x3fff, *strength);
  827. }
  828. break;
  829. default:
  830. dprintk(verbose, FE_DEBUG, 1, "Unsupported delivery system");
  831. return -EINVAL;
  832. }
  833. return 0;
  834. }
  835. static int stb0899_read_snr(struct dvb_frontend *fe, u16 *snr)
  836. {
  837. struct stb0899_state *state = fe->demodulator_priv;
  838. struct stb0899_internal *internal = &state->internal;
  839. unsigned int val, quant, quantn = -1, est, estn = -1;
  840. u8 buf[2];
  841. u32 reg;
  842. reg = stb0899_read_reg(state, STB0899_VSTATUS);
  843. switch (state->delsys) {
  844. case DVBFE_DELSYS_DVBS:
  845. case DVBFE_DELSYS_DSS:
  846. if (internal->lock) {
  847. if (STB0899_GETFIELD(VSTATUS_LOCKEDVIT, reg)) {
  848. stb0899_read_regs(state, STB0899_NIRM, buf, 2);
  849. val = MAKEWORD16(buf[0], buf[1]);
  850. *snr = stb0899_table_lookup(stb0899_cn_tab, ARRAY_SIZE(stb0899_cn_tab) - 1, val);
  851. dprintk(verbose, FE_DEBUG, 1, "NIR = 0x%02x%02x = %u, C/N = %d * 0.1 dBm\n",
  852. buf[0], buf[1], val, *snr);
  853. }
  854. }
  855. break;
  856. case DVBFE_DELSYS_DVBS2:
  857. if (internal->lock) {
  858. reg = STB0899_READ_S2REG(STB0899_S2DEMOD, UWP_CNTRL1);
  859. quant = STB0899_GETFIELD(UWP_ESN0_QUANT, reg);
  860. reg = STB0899_READ_S2REG(STB0899_S2DEMOD, UWP_STAT2);
  861. est = STB0899_GETFIELD(ESN0_EST, reg);
  862. if (est == 1)
  863. val = 301; /* C/N = 30.1 dB */
  864. else if (est == 2)
  865. val = 270; /* C/N = 27.0 dB */
  866. else {
  867. /* quantn = 100 * log(quant^2) */
  868. quantn = stb0899_table_lookup(stb0899_quant_tab, ARRAY_SIZE(stb0899_quant_tab) - 1, quant * 100);
  869. /* estn = 100 * log(est) */
  870. estn = stb0899_table_lookup(stb0899_est_tab, ARRAY_SIZE(stb0899_est_tab) - 1, est);
  871. /* snr(dBm/10) = -10*(log(est)-log(quant^2)) => snr(dBm/10) = (100*log(quant^2)-100*log(est))/10 */
  872. val = (quantn - estn) / 10;
  873. }
  874. *snr = val;
  875. dprintk(verbose, FE_DEBUG, 1, "Es/N0 quant = %d (%d) estimate = %u (%d), C/N = %d * 0.1 dBm",
  876. quant, quantn, est, estn, val);
  877. }
  878. break;
  879. default:
  880. dprintk(verbose, FE_DEBUG, 1, "Unsupported delivery system");
  881. return -EINVAL;
  882. }
  883. return 0;
  884. }
  885. static int stb0899_read_status(struct dvb_frontend *fe, enum fe_status *status)
  886. {
  887. struct stb0899_state *state = fe->demodulator_priv;
  888. struct stb0899_internal *internal = &state->internal;
  889. u8 reg;
  890. *status = 0;
  891. switch (state->delsys) {
  892. case DVBFE_DELSYS_DVBS:
  893. case DVBFE_DELSYS_DSS:
  894. dprintk(state->verbose, FE_DEBUG, 1, "Delivery system DVB-S/DSS");
  895. if (internal->lock) {
  896. reg = stb0899_read_reg(state, STB0899_VSTATUS);
  897. if (STB0899_GETFIELD(VSTATUS_LOCKEDVIT, reg)) {
  898. dprintk(state->verbose, FE_DEBUG, 1, "--------> FE_HAS_CARRIER | FE_HAS_LOCK");
  899. *status |= FE_HAS_CARRIER | FE_HAS_LOCK;
  900. reg = stb0899_read_reg(state, STB0899_PLPARM);
  901. if (STB0899_GETFIELD(VITCURPUN, reg)) {
  902. dprintk(state->verbose, FE_DEBUG, 1, "--------> FE_HAS_VITERBI | FE_HAS_SYNC");
  903. *status |= FE_HAS_VITERBI | FE_HAS_SYNC;
  904. }
  905. }
  906. }
  907. break;
  908. case DVBFE_DELSYS_DVBS2:
  909. dprintk(state->verbose, FE_DEBUG, 1, "Delivery system DVB-S2");
  910. if (internal->lock) {
  911. reg = STB0899_READ_S2REG(STB0899_S2DEMOD, DMD_STAT2);
  912. if (STB0899_GETFIELD(UWP_LOCK, reg) && STB0899_GETFIELD(CSM_LOCK, reg)) {
  913. *status |= FE_HAS_CARRIER;
  914. dprintk(state->verbose, FE_DEBUG, 1,
  915. "UWP & CSM Lock ! ---> DVB-S2 FE_HAS_CARRIER");
  916. reg = stb0899_read_reg(state, STB0899_CFGPDELSTATUS1);
  917. if (STB0899_GETFIELD(CFGPDELSTATUS_LOCK, reg)) {
  918. *status |= FE_HAS_LOCK;
  919. dprintk(state->verbose, FE_DEBUG, 1,
  920. "Packet Delineator Locked ! -----> DVB-S2 FE_HAS_LOCK");
  921. }
  922. if (STB0899_GETFIELD(CONTINUOUS_STREAM, reg)) {
  923. *status |= FE_HAS_VITERBI;
  924. dprintk(state->verbose, FE_DEBUG, 1,
  925. "Packet Delineator found VITERBI ! -----> DVB-S2 FE_HAS_VITERBI");
  926. }
  927. if (STB0899_GETFIELD(ACCEPTED_STREAM, reg)) {
  928. *status |= FE_HAS_SYNC;
  929. dprintk(state->verbose, FE_DEBUG, 1,
  930. "Packet Delineator found SYNC ! -----> DVB-S2 FE_HAS_SYNC");
  931. }
  932. }
  933. }
  934. break;
  935. default:
  936. dprintk(verbose, FE_DEBUG, 1, "Unsupported delivery system");
  937. return -EINVAL;
  938. }
  939. return 0;
  940. }
  941. /*
  942. * stb0899_get_error
  943. * viterbi error for DVB-S/DSS
  944. * packet error for DVB-S2
  945. * Bit Error Rate or Packet Error Rate * 10 ^ 7
  946. */
  947. static int stb0899_read_ber(struct dvb_frontend *fe, u32 *ber)
  948. {
  949. struct stb0899_state *state = fe->demodulator_priv;
  950. struct stb0899_internal *internal = &state->internal;
  951. u8 lsb, msb;
  952. u32 i;
  953. *ber = 0;
  954. switch (state->delsys) {
  955. case DVBFE_DELSYS_DVBS:
  956. case DVBFE_DELSYS_DSS:
  957. if (internal->lock) {
  958. /* average 5 BER values */
  959. for (i = 0; i < 5; i++) {
  960. msleep(100);
  961. lsb = stb0899_read_reg(state, STB0899_ECNT1L);
  962. msb = stb0899_read_reg(state, STB0899_ECNT1M);
  963. *ber += MAKEWORD16(msb, lsb);
  964. }
  965. *ber /= 5;
  966. /* Viterbi Check */
  967. if (STB0899_GETFIELD(VSTATUS_PRFVIT, internal->v_status)) {
  968. /* Error Rate */
  969. *ber *= 9766;
  970. /* ber = ber * 10 ^ 7 */
  971. *ber /= (-1 + (1 << (2 * STB0899_GETFIELD(NOE, internal->err_ctrl))));
  972. *ber /= 8;
  973. }
  974. }
  975. break;
  976. case DVBFE_DELSYS_DVBS2:
  977. if (internal->lock) {
  978. /* Average 5 PER values */
  979. for (i = 0; i < 5; i++) {
  980. msleep(100);
  981. lsb = stb0899_read_reg(state, STB0899_ECNT1L);
  982. msb = stb0899_read_reg(state, STB0899_ECNT1M);
  983. *ber += MAKEWORD16(msb, lsb);
  984. }
  985. /* ber = ber * 10 ^ 7 */
  986. *ber *= 10000000;
  987. *ber /= (-1 + (1 << (4 + 2 * STB0899_GETFIELD(NOE, internal->err_ctrl))));
  988. }
  989. break;
  990. default:
  991. dprintk(verbose, FE_DEBUG, 1, "Unsupported delivery system");
  992. return -EINVAL;
  993. }
  994. return 0;
  995. }
  996. static int stb0899_set_voltage(struct dvb_frontend *fe, fe_sec_voltage_t voltage)
  997. {
  998. struct stb0899_state *state = fe->demodulator_priv;
  999. switch (voltage) {
  1000. case SEC_VOLTAGE_13:
  1001. stb0899_write_reg(state, STB0899_GPIO00CFG, 0x82);
  1002. stb0899_write_reg(state, STB0899_GPIO01CFG, 0x02);
  1003. stb0899_write_reg(state, STB0899_GPIO02CFG, 0x00);
  1004. break;
  1005. case SEC_VOLTAGE_18:
  1006. stb0899_write_reg(state, STB0899_GPIO00CFG, 0x02);
  1007. stb0899_write_reg(state, STB0899_GPIO01CFG, 0x02);
  1008. stb0899_write_reg(state, STB0899_GPIO02CFG, 0x82);
  1009. break;
  1010. case SEC_VOLTAGE_OFF:
  1011. stb0899_write_reg(state, STB0899_GPIO00CFG, 0x82);
  1012. stb0899_write_reg(state, STB0899_GPIO01CFG, 0x82);
  1013. stb0899_write_reg(state, STB0899_GPIO02CFG, 0x82);
  1014. break;
  1015. default:
  1016. return -EINVAL;
  1017. }
  1018. return 0;
  1019. }
  1020. static int stb0899_set_tone(struct dvb_frontend *fe, fe_sec_tone_mode_t tone)
  1021. {
  1022. struct stb0899_state *state = fe->demodulator_priv;
  1023. struct stb0899_internal *internal = &state->internal;
  1024. u8 div, reg;
  1025. /* wait for diseqc idle */
  1026. if (stb0899_wait_diseqc_txidle(state, 100) < 0)
  1027. return -ETIMEDOUT;
  1028. switch (tone) {
  1029. case SEC_TONE_ON:
  1030. div = (internal->master_clk / 100) / 5632;
  1031. div = (div + 5) / 10;
  1032. stb0899_write_reg(state, STB0899_DISEQCOCFG, 0x66);
  1033. reg = stb0899_read_reg(state, STB0899_ACRPRESC);
  1034. STB0899_SETFIELD_VAL(ACRPRESC, reg, 0x03);
  1035. stb0899_write_reg(state, STB0899_ACRPRESC, reg);
  1036. stb0899_write_reg(state, STB0899_ACRDIV1, div);
  1037. break;
  1038. case SEC_TONE_OFF:
  1039. stb0899_write_reg(state, STB0899_DISEQCOCFG, 0x20);
  1040. break;
  1041. default:
  1042. return -EINVAL;
  1043. }
  1044. return 0;
  1045. }
  1046. static int stb0899_i2c_gate_ctrl(struct dvb_frontend *fe, int enable)
  1047. {
  1048. int i2c_stat;
  1049. struct stb0899_state *state = fe->demodulator_priv;
  1050. i2c_stat = stb0899_read_reg(state, STB0899_I2CRPT);
  1051. if (i2c_stat < 0)
  1052. goto err;
  1053. if (enable) {
  1054. dprintk(state->verbose, FE_DEBUG, 1, "Enabling I2C Repeater ...");
  1055. i2c_stat |= STB0899_I2CTON;
  1056. if (stb0899_write_reg(state, STB0899_I2CRPT, i2c_stat) < 0)
  1057. goto err;
  1058. }
  1059. return 0;
  1060. err:
  1061. dprintk(state->verbose, FE_ERROR, 1, "I2C Repeater enable failed");
  1062. return -EREMOTEIO;
  1063. }
  1064. static inline void CONVERT32(u32 x, char *str)
  1065. {
  1066. *str++ = (x >> 24) & 0xff;
  1067. *str++ = (x >> 16) & 0xff;
  1068. *str++ = (x >> 8) & 0xff;
  1069. *str++ = (x >> 0) & 0xff;
  1070. *str = '\0';
  1071. }
  1072. int stb0899_get_dev_id(struct stb0899_state *state)
  1073. {
  1074. u8 chip_id, release;
  1075. u16 id;
  1076. u32 demod_ver = 0, fec_ver = 0;
  1077. char demod_str[4] = { 0 };
  1078. char fec_str[4] = { 0 };
  1079. id = stb0899_read_reg(state, STB0899_DEV_ID);
  1080. dprintk(state->verbose, FE_DEBUG, 1, "ID reg=[0x%02x]", id);
  1081. chip_id = STB0899_GETFIELD(CHIP_ID, id);
  1082. release = STB0899_GETFIELD(CHIP_REL, id);
  1083. dprintk(state->verbose, FE_ERROR, 1, "Device ID=[%d], Release=[%d]",
  1084. chip_id, release);
  1085. CONVERT32(STB0899_READ_S2REG(STB0899_S2DEMOD, DMD_CORE_ID), (char *)&demod_str);
  1086. demod_ver = STB0899_READ_S2REG(STB0899_S2DEMOD, DMD_VERSION_ID);
  1087. dprintk(state->verbose, FE_ERROR, 1, "Demodulator Core ID=[%s], Version=[%d]", (char *) &demod_str, demod_ver);
  1088. CONVERT32(STB0899_READ_S2REG(STB0899_S2FEC, FEC_CORE_ID_REG), (char *)&fec_str);
  1089. fec_ver = STB0899_READ_S2REG(STB0899_S2FEC, FEC_VER_ID_REG);
  1090. if (! (chip_id > 0)) {
  1091. dprintk(state->verbose, FE_ERROR, 1, "couldn't find a STB 0899");
  1092. return -ENODEV;
  1093. }
  1094. dprintk(state->verbose, FE_ERROR, 1, "FEC Core ID=[%s], Version=[%d]", (char*) &fec_str, fec_ver);
  1095. return 0;
  1096. }
  1097. static const struct dvbfe_info dvbs_info = {
  1098. .name = "STB0899 DVB-S",
  1099. .delivery = DVBFE_DELSYS_DVBS,
  1100. .delsys = {
  1101. .dvbs.modulation = DVBFE_MOD_QPSK,
  1102. .dvbs.fec = DVBFE_FEC_1_2 | DVBFE_FEC_2_3 |
  1103. DVBFE_FEC_3_4 | DVBFE_FEC_5_6 |
  1104. DVBFE_FEC_6_7
  1105. },
  1106. .frequency_min = 950000,
  1107. .frequency_max = 2150000,
  1108. .frequency_step = 0,
  1109. .symbol_rate_min = 1000000,
  1110. .symbol_rate_max = 45000000,
  1111. .symbol_rate_tolerance = 0
  1112. };
  1113. static const struct dvbfe_info dss_info = {
  1114. .name = "STB0899 DSS",
  1115. .delivery = DVBFE_DELSYS_DSS,
  1116. .delsys = {
  1117. .dss.modulation = DVBFE_MOD_BPSK | DVBFE_MOD_QPSK,
  1118. .dss.fec = DVBFE_FEC_1_2 | DVBFE_FEC_2_3 |
  1119. DVBFE_FEC_3_4 | DVBFE_FEC_5_6 |
  1120. DVBFE_FEC_6_7
  1121. },
  1122. .frequency_min = 950000,
  1123. .frequency_max = 2150000,
  1124. .frequency_step = 0,
  1125. .symbol_rate_min = 1000000,
  1126. .symbol_rate_max = 45000000,
  1127. .symbol_rate_tolerance = 0
  1128. };
  1129. static const struct dvbfe_info dvbs2_info = {
  1130. .name = "STB0899 DVB-S2",
  1131. .delivery = DVBFE_DELSYS_DVBS2,
  1132. .delsys = {
  1133. .dvbs2.modulation = DVBFE_MOD_QPSK | DVBFE_MOD_8PSK |
  1134. DVBFE_MOD_16APSK | DVBFE_MOD_32APSK,
  1135. .dvbs2.fec = DVBFE_FEC_1_4 | DVBFE_FEC_1_3 |
  1136. DVBFE_FEC_2_5 | DVBFE_FEC_1_2 |
  1137. DVBFE_FEC_3_5 | DVBFE_FEC_2_3 |
  1138. DVBFE_FEC_3_4 | DVBFE_FEC_4_5 |
  1139. DVBFE_FEC_5_6 | DVBFE_FEC_8_9 |
  1140. DVBFE_FEC_9_10,
  1141. },
  1142. .frequency_min = 950000,
  1143. .frequency_max = 2150000,
  1144. .frequency_step = 0,
  1145. .symbol_rate_min = 1000000,
  1146. .symbol_rate_max = 45000000,
  1147. .symbol_rate_tolerance = 0
  1148. };
  1149. static int stb0899_get_info(struct dvb_frontend *fe, struct dvbfe_info *fe_info)
  1150. {
  1151. struct stb0899_state *state = fe->demodulator_priv;
  1152. dprintk(verbose, FE_DEBUG, 1, "Get Info");
  1153. state->delsys = fe_info->delivery;
  1154. switch (state->delsys) {
  1155. case DVBFE_DELSYS_DVBS:
  1156. dprintk(verbose, FE_ERROR, 1, "Querying DVB-S info");
  1157. memcpy(fe_info, &dvbs_info, sizeof (struct dvbfe_info));
  1158. break;
  1159. case DVBFE_DELSYS_DSS:
  1160. dprintk(verbose, FE_ERROR, 1, "Querying DSS info");
  1161. memcpy(fe_info, &dss_info, sizeof (struct dvbfe_info));
  1162. break;
  1163. case DVBFE_DELSYS_DVBS2:
  1164. dprintk(verbose, FE_ERROR, 1, "Querying DVB-S2 info");
  1165. memcpy(fe_info, &dvbs2_info, sizeof (struct dvbfe_info));
  1166. break;
  1167. default:
  1168. dprintk(verbose, FE_ERROR, 1, "Unsupported delivery system");
  1169. return -EINVAL;
  1170. }
  1171. dprintk(verbose, FE_DEBUG, 1, "delivery system=%d", state->delsys);
  1172. return 0;
  1173. }
  1174. static int stb0899_get_delsys(struct dvb_frontend *fe, enum dvbfe_delsys *fe_delsys)
  1175. {
  1176. *fe_delsys = DVBFE_DELSYS_DVBS | DVBFE_DELSYS_DSS | DVBFE_DELSYS_DVBS2;
  1177. return 0;
  1178. }
  1179. void stb0899_set_delsys(struct stb0899_state *state)
  1180. {
  1181. u8 reg;
  1182. u8 stop_clk[2];
  1183. stop_clk[0] = stb0899_read_reg(state, STB0899_STOPCLK1);
  1184. stop_clk[1] = stb0899_read_reg(state, STB0899_STOPCLK2);
  1185. switch (state->delsys) {
  1186. case DVBFE_DELSYS_DVBS:
  1187. dprintk(verbose, FE_DEBUG, 1, "Delivery System -- DVB-S");
  1188. /* FECM/Viterbi ON */
  1189. reg = stb0899_read_reg(state, STB0899_FECM);
  1190. STB0899_SETFIELD_VAL(FECM_RSVD0, reg, 0);
  1191. STB0899_SETFIELD_VAL(FECM_VITERBI_ON, reg, 1);
  1192. stb0899_write_reg(state, STB0899_FECM, reg);
  1193. stb0899_write_reg(state, STB0899_RSULC, 0xb1);
  1194. stb0899_write_reg(state, STB0899_TSULC, 0x40);
  1195. stb0899_write_reg(state, STB0899_RSLLC, 0x42);
  1196. stb0899_write_reg(state, STB0899_TSLPL, 0x12);
  1197. reg = stb0899_read_reg(state, STB0899_TSTRES);
  1198. STB0899_SETFIELD_VAL(FRESLDPC, reg, 1);
  1199. stb0899_write_reg(state, STB0899_TSTRES, reg);
  1200. STB0899_SETFIELD_VAL(STOP_CHK8PSK, stop_clk[0], 1);
  1201. STB0899_SETFIELD_VAL(STOP_CKFEC108, stop_clk[0], 1);
  1202. STB0899_SETFIELD_VAL(STOP_CKFEC216, stop_clk[0], 1);
  1203. STB0899_SETFIELD_VAL(STOP_CKPKDLIN108, stop_clk[1], 1);
  1204. STB0899_SETFIELD_VAL(STOP_CKPKDLIN216, stop_clk[1], 1);
  1205. STB0899_SETFIELD_VAL(STOP_CKINTBUF216, stop_clk[0], 1);
  1206. STB0899_SETFIELD_VAL(STOP_CKCORE216, stop_clk[0], 1);
  1207. STB0899_SETFIELD_VAL(STOP_CKS2DMD108, stop_clk[1], 1);
  1208. break;
  1209. case DVBFE_DELSYS_DVBS2:
  1210. /* FECM/Viterbi OFF */
  1211. reg = stb0899_read_reg(state, STB0899_FECM);
  1212. STB0899_SETFIELD_VAL(FECM_RSVD0, reg, 0);
  1213. STB0899_SETFIELD_VAL(FECM_VITERBI_ON, reg, 0);
  1214. stb0899_write_reg(state, STB0899_FECM, reg);
  1215. stb0899_write_reg(state, STB0899_RSULC, 0xb1);
  1216. stb0899_write_reg(state, STB0899_TSULC, 0x42);
  1217. stb0899_write_reg(state, STB0899_RSLLC, 0x40);
  1218. stb0899_write_reg(state, STB0899_TSLPL, 0x02);
  1219. reg = stb0899_read_reg(state, STB0899_TSTRES);
  1220. STB0899_SETFIELD_VAL(FRESLDPC, reg, 0);
  1221. stb0899_write_reg(state, STB0899_TSTRES, reg);
  1222. STB0899_SETFIELD_VAL(STOP_CHK8PSK, stop_clk[0], 1);
  1223. STB0899_SETFIELD_VAL(STOP_CKFEC108, stop_clk[0], 0);
  1224. STB0899_SETFIELD_VAL(STOP_CKFEC216, stop_clk[0], 0);
  1225. STB0899_SETFIELD_VAL(STOP_CKPKDLIN108, stop_clk[1], 0);
  1226. STB0899_SETFIELD_VAL(STOP_CKPKDLIN216, stop_clk[1], 0);
  1227. STB0899_SETFIELD_VAL(STOP_CKINTBUF216, stop_clk[0], 0);
  1228. STB0899_SETFIELD_VAL(STOP_CKCORE216, stop_clk[0], 0);
  1229. STB0899_SETFIELD_VAL(STOP_CKS2DMD108, stop_clk[1], 0);
  1230. break;
  1231. case DVBFE_DELSYS_DSS:
  1232. /* FECM/Viterbi ON */
  1233. reg = stb0899_read_reg(state, STB0899_FECM);
  1234. STB0899_SETFIELD_VAL(FECM_RSVD0, reg, 1);
  1235. STB0899_SETFIELD_VAL(FECM_VITERBI_ON, reg, 1);
  1236. stb0899_write_reg(state, STB0899_FECM, reg);
  1237. stb0899_write_reg(state, STB0899_RSULC, 0xa1);
  1238. stb0899_write_reg(state, STB0899_TSULC, 0x61);
  1239. stb0899_write_reg(state, STB0899_RSLLC, 0x42);
  1240. reg = stb0899_read_reg(state, STB0899_TSTRES);
  1241. STB0899_SETFIELD_VAL(FRESLDPC, reg, 1);
  1242. stb0899_write_reg(state, STB0899_TSTRES, reg);
  1243. STB0899_SETFIELD_VAL(STOP_CHK8PSK, stop_clk[0], 1);
  1244. STB0899_SETFIELD_VAL(STOP_CKFEC108, stop_clk[0], 1);
  1245. STB0899_SETFIELD_VAL(STOP_CKFEC216, stop_clk[0], 1);
  1246. STB0899_SETFIELD_VAL(STOP_CKPKDLIN108, stop_clk[1], 1);
  1247. STB0899_SETFIELD_VAL(STOP_CKPKDLIN216, stop_clk[1], 1);
  1248. STB0899_SETFIELD_VAL(STOP_CKCORE216, stop_clk[0], 0);
  1249. STB0899_SETFIELD_VAL(STOP_CKS2DMD108, stop_clk[1], 1);
  1250. break;
  1251. default:
  1252. dprintk(verbose, FE_ERROR, 1, "Unsupported delivery system");
  1253. break;
  1254. }
  1255. STB0899_SETFIELD_VAL(STOP_CKADCI108, stop_clk[0], 0);
  1256. stb0899_write_regs(state, STB0899_STOPCLK1, stop_clk, 2);
  1257. }
  1258. /*
  1259. * stb0899_set_iterations
  1260. * set the LDPC iteration scale function
  1261. */
  1262. static void stb0899_set_iterations(struct stb0899_state *state)
  1263. {
  1264. struct stb0899_internal *internal = &state->internal;
  1265. struct stb0899_config *config = state->config;
  1266. s32 iter_scale;
  1267. u32 reg;
  1268. iter_scale = 17 * (internal->master_clk / 1000);
  1269. iter_scale += 410000;
  1270. iter_scale /= (internal->srate / 1000000);
  1271. iter_scale /= 1000;
  1272. if (iter_scale > config->ldpc_max_iter)
  1273. iter_scale = config->ldpc_max_iter;
  1274. reg = STB0899_READ_S2REG(STB0899_S2DEMOD, MAX_ITER);
  1275. STB0899_SETFIELD_VAL(MAX_ITERATIONS, reg, iter_scale);
  1276. stb0899_write_s2reg(state, STB0899_S2DEMOD, STB0899_BASE_MAX_ITER, STB0899_OFF0_MAX_ITER, reg);
  1277. }
  1278. static enum dvbfe_search stb0899_search(struct dvb_frontend *fe, struct dvbfe_params *params)
  1279. {
  1280. struct stb0899_state *state = fe->demodulator_priv;
  1281. struct stb0899_params *i_params = &state->params;
  1282. struct stb0899_internal *internal = &state->internal;
  1283. u32 SearchRange, gain;
  1284. switch (state->delsys) {
  1285. case DVBFE_DELSYS_DVBS:
  1286. dprintk(verbose, FE_ERROR, 1, "set DVB-S params");
  1287. i_params->freq = params->frequency;
  1288. i_params->srate = params->delsys.dvbs.symbol_rate;
  1289. break;
  1290. case DVBFE_DELSYS_DSS:
  1291. dprintk(verbose, FE_ERROR, 1, "set DSS params");
  1292. i_params->freq = params->frequency;
  1293. i_params->srate = params->delsys.dss.symbol_rate;
  1294. break;
  1295. case DVBFE_DELSYS_DVBS2:
  1296. dprintk(verbose, FE_ERROR, 1, "set DVB-S2 params");
  1297. i_params->freq = params->frequency;
  1298. i_params->srate = params->delsys.dvbs2.symbol_rate;
  1299. break;
  1300. default:
  1301. dprintk(verbose, FE_ERROR, 1, "Unsupported delivery system");
  1302. return -EINVAL;
  1303. }
  1304. dprintk(verbose, FE_DEBUG, 1, "delivery system=%d", state->delsys);
  1305. SearchRange = 3000000; /* Search Bandwidth (3 Mhz, was initially 10 Mhz) */
  1306. // SearchRange = 10000000; /* Search Bandwidth (3 Mhz, was initially 10 Mhz) */
  1307. dprintk(verbose, FE_DEBUG, 1, "Frequency=%d, Srate=%d", i_params->freq, i_params->srate);
  1308. /* checking Search Range is meaningless for a fixed 3 Mhz */
  1309. if (INRANGE(i_params->srate, 1000000, 45000000)) {
  1310. dprintk(verbose, FE_DEBUG, 1, "Parameters IN RANGE");
  1311. stb0899_set_delsys(state);
  1312. if (state->config->tuner_set_rfsiggain) {
  1313. if (internal->srate > 15000000)
  1314. gain = 8; /* 15Mb < srate < 45Mb, gain = 8dB */
  1315. else if (internal->srate > 5000000)
  1316. gain = 12; /* 5Mb < srate < 15Mb, gain = 12dB */
  1317. else
  1318. gain = 14; /* 1Mb < srate < 5Mb, gain = 14db */
  1319. state->config->tuner_set_rfsiggain(fe, gain);
  1320. }
  1321. if (i_params->srate <= 5000000)
  1322. stb0899_set_mclk(state, 76500000);
  1323. else
  1324. stb0899_set_mclk(state, 90000000);
  1325. switch (state->delsys) {
  1326. case DVBFE_DELSYS_DVBS:
  1327. case DVBFE_DELSYS_DSS:
  1328. dprintk(verbose, FE_DEBUG, 1, "DVB-S delivery system");
  1329. internal->freq = i_params->freq;
  1330. internal->srate = i_params->srate;
  1331. /*
  1332. * search = user search range +
  1333. * 500Khz +
  1334. * 2 * Tuner_step_size +
  1335. * 10% of the symbol rate
  1336. */
  1337. internal->srch_range = SearchRange + 1500000 + (i_params->srate / 5);
  1338. internal->derot_percent = 30;
  1339. /* What to do for tuners having no bandwidth setup ? */
  1340. if (state->config->tuner_set_bandwidth)
  1341. state->config->tuner_set_bandwidth(fe, (135 * (stb0899_carr_width(state) + SearchRange)) / 100);
  1342. if (state->config->tuner_get_bandwidth)
  1343. state->config->tuner_get_bandwidth(fe, &internal->tuner_bw);
  1344. /* Set DVB-S1 AGC */
  1345. stb0899_write_reg(state, STB0899_AGCRFCFG, 0x11);
  1346. /* Run the search algorithm */
  1347. dprintk(verbose, FE_DEBUG, 1, "running DVB-S search algo ..");
  1348. if (stb0899_dvbs_algo(state) == RANGEOK) {
  1349. internal->lock = 1;
  1350. dprintk(verbose, FE_DEBUG, 1,
  1351. "-------------------------------------> DVB-S LOCK !");
  1352. // stb0899_write_reg(state, STB0899_ERRCTRL1, 0x3d); /* Viterbi Errors */
  1353. // internal->v_status = stb0899_read_reg(state, STB0899_VSTATUS);
  1354. // internal->err_ctrl = stb0899_read_reg(state, STB0899_ERRCTRL1);
  1355. // dprintk(verbose, FE_DEBUG, 1, "VSTATUS=0x%02x", internal->v_status);
  1356. // dprintk(verbose, FE_DEBUG, 1, "ERR_CTRL=0x%02x", internal->err_ctrl);
  1357. return DVBFE_ALGO_SEARCH_SUCCESS;
  1358. } else {
  1359. internal->lock = 0;
  1360. return DVBFE_ALGO_SEARCH_FAILED;
  1361. }
  1362. break;
  1363. case DVBFE_DELSYS_DVBS2:
  1364. internal->freq = i_params->freq;
  1365. internal->srate = i_params->srate;
  1366. internal->srch_range = SearchRange;
  1367. if (state->config->tuner_set_bandwidth)
  1368. state->config->tuner_set_bandwidth(fe, (135 * (stb0899_carr_width(state) + SearchRange)) / 100);
  1369. if (state->config->tuner_get_bandwidth)
  1370. state->config->tuner_get_bandwidth(fe, &internal->tuner_bw);
  1371. // pParams->SpectralInv = pSearch->IQ_Inversion;
  1372. /* Set DVB-S2 AGC */
  1373. stb0899_write_reg(state, STB0899_AGCRFCFG, 0x1c);
  1374. /* Set IterScale =f(MCLK,SYMB) */
  1375. stb0899_set_iterations(state);
  1376. /* Run the search algorithm */
  1377. dprintk(verbose, FE_DEBUG, 1, "running DVB-S2 search algo ..");
  1378. if (stb0899_dvbs2_algo(state) == DVBS2_FEC_LOCK) {
  1379. internal->lock = 1;
  1380. dprintk(verbose, FE_DEBUG, 1,
  1381. "-------------------------------------> DVB-S2 LOCK !");
  1382. // stb0899_write_reg(state, STB0899_ERRCTRL1, 0xb6); /* Packet Errors */
  1383. // internal->v_status = stb0899_read_reg(state, STB0899_VSTATUS);
  1384. // internal->err_ctrl = stb0899_read_reg(state, STB0899_ERRCTRL1);
  1385. return DVBFE_ALGO_SEARCH_SUCCESS;
  1386. } else {
  1387. internal->lock = 0;
  1388. return DVBFE_ALGO_SEARCH_FAILED;
  1389. }
  1390. break;
  1391. default:
  1392. dprintk(verbose, FE_ERROR, 1, "Unsupported delivery system");
  1393. return DVBFE_ALGO_SEARCH_INVALID;
  1394. }
  1395. }
  1396. return DVBFE_ALGO_SEARCH_ERROR;
  1397. }
  1398. static enum stb0899_status stb0899_track_carrier(struct stb0899_state *state)
  1399. {
  1400. u8 reg;
  1401. reg = stb0899_read_reg(state, STB0899_DSTATUS);
  1402. dprintk(verbose, FE_DEBUG, 1, "--------------------> STB0899_DSTATUS=[0x%02x]", reg);
  1403. if (STB0899_GETFIELD(CARRIER_FOUND, reg)) {
  1404. dprintk(verbose, FE_DEBUG, 1, "-------------> CARRIEROK !");
  1405. return CARRIEROK;
  1406. } else {
  1407. dprintk(verbose, FE_DEBUG, 1, "-------------> NOCARRIER !");
  1408. return NOCARRIER;
  1409. }
  1410. return NOCARRIER;
  1411. }
  1412. static enum stb0899_status stb0899_get_ifagc(struct stb0899_state *state)
  1413. {
  1414. u8 reg;
  1415. reg = STB0899_READ_S2REG(STB0899_S2DEMOD, DMD_STATUS);
  1416. dprintk(verbose, FE_DEBUG, 1, "DMD_STATUS=[0x%02x]", reg);
  1417. if (STB0899_GETFIELD(IF_AGC_LOCK, reg)) {
  1418. dprintk(verbose, FE_DEBUG, 1, "------------->IF AGC LOCKED !");
  1419. return AGC1OK;
  1420. } else {
  1421. dprintk(verbose, FE_DEBUG, 1, "------------->IF AGC LOCK LOST !");
  1422. return NOAGC1;
  1423. }
  1424. return NOAGC1;
  1425. }
  1426. static int stb0899_get_s1fec(struct stb0899_internal *internal, enum dvbfe_fec *fec)
  1427. {
  1428. switch (internal->fecrate) {
  1429. case STB0899_FEC_1_2:
  1430. *fec = DVBFE_FEC_1_2;
  1431. break;
  1432. case STB0899_FEC_2_3:
  1433. *fec = DVBFE_FEC_2_3;
  1434. break;
  1435. case STB0899_FEC_3_4:
  1436. *fec = DVBFE_FEC_3_4;
  1437. break;
  1438. case STB0899_FEC_5_6:
  1439. *fec = DVBFE_FEC_5_6;
  1440. break;
  1441. case STB0899_FEC_6_7:
  1442. *fec = DVBFE_FEC_6_7;
  1443. break;
  1444. case STB0899_FEC_7_8:
  1445. *fec = DVBFE_FEC_7_8;
  1446. break;
  1447. default:
  1448. return -EINVAL;
  1449. }
  1450. return 0;
  1451. }
  1452. static int stb0899_get_modcod(struct stb0899_internal *internal, struct dvbs2_params *params)
  1453. {
  1454. switch (internal->modcod) {
  1455. case STB0899_DUMMY_PLF:
  1456. params->modulation = DVBFE_MOD_NONE;
  1457. params->fec = DVBFE_FEC_NONE;
  1458. break;
  1459. case STB0899_QPSK_14:
  1460. params->modulation = DVBFE_MOD_QPSK;
  1461. params->fec = DVBFE_FEC_1_4;
  1462. break;
  1463. case STB0899_QPSK_13:
  1464. params->modulation = DVBFE_MOD_QPSK;
  1465. params->fec = DVBFE_FEC_1_3;
  1466. break;
  1467. case STB0899_QPSK_25:
  1468. params->modulation = DVBFE_MOD_QPSK;
  1469. params->fec = DVBFE_FEC_2_5;
  1470. break;
  1471. case STB0899_QPSK_12:
  1472. params->modulation = DVBFE_MOD_QPSK;
  1473. params->fec = DVBFE_FEC_1_2;
  1474. break;
  1475. case STB0899_QPSK_35:
  1476. params->modulation = DVBFE_MOD_QPSK;
  1477. params->fec = DVBFE_FEC_3_5;
  1478. break;
  1479. case STB0899_QPSK_23:
  1480. params->modulation = DVBFE_MOD_QPSK;
  1481. params->fec = DVBFE_FEC_2_3;
  1482. break;
  1483. case STB0899_QPSK_34:
  1484. params->modulation = DVBFE_MOD_QPSK;
  1485. params->fec = DVBFE_FEC_3_4;
  1486. break;
  1487. case STB0899_QPSK_45:
  1488. params->modulation = DVBFE_MOD_QPSK;
  1489. params->fec = DVBFE_FEC_4_5;
  1490. break;
  1491. case STB0899_QPSK_56:
  1492. params->modulation = DVBFE_MOD_QPSK;
  1493. params->fec = DVBFE_FEC_5_6;
  1494. break;
  1495. case STB0899_QPSK_89:
  1496. params->modulation = DVBFE_MOD_QPSK;
  1497. params->fec = DVBFE_FEC_8_9;
  1498. break;
  1499. case STB0899_QPSK_910:
  1500. params->modulation = DVBFE_MOD_QPSK;
  1501. params->fec = DVBFE_FEC_9_10;
  1502. break;
  1503. case STB0899_8PSK_35:
  1504. params->modulation = DVBFE_MOD_8PSK;
  1505. params->fec = DVBFE_FEC_3_5;
  1506. break;
  1507. case STB0899_8PSK_23:
  1508. params->modulation = DVBFE_MOD_8PSK;
  1509. params->fec = DVBFE_FEC_2_3;
  1510. break;
  1511. case STB0899_8PSK_34:
  1512. params->modulation = DVBFE_MOD_8PSK;
  1513. params->fec = DVBFE_FEC_3_4;
  1514. break;
  1515. case STB0899_8PSK_56:
  1516. params->modulation = DVBFE_MOD_8PSK;
  1517. params->fec = DVBFE_FEC_5_6;
  1518. break;
  1519. case STB0899_8PSK_89:
  1520. params->modulation = DVBFE_MOD_8PSK;
  1521. params->fec = DVBFE_FEC_8_9;
  1522. break;
  1523. case STB0899_8PSK_910:
  1524. params->modulation = DVBFE_MOD_8PSK;
  1525. params->fec = DVBFE_FEC_9_10;
  1526. break;
  1527. case STB0899_16APSK_23:
  1528. params->modulation = DVBFE_MOD_16APSK;
  1529. params->fec = DVBFE_FEC_2_3;
  1530. break;
  1531. case STB0899_16APSK_34:
  1532. params->modulation = DVBFE_MOD_16APSK;
  1533. params->fec = DVBFE_FEC_3_4;
  1534. break;
  1535. case STB0899_16APSK_45:
  1536. params->modulation = DVBFE_MOD_16APSK;
  1537. params->fec = DVBFE_FEC_4_5;
  1538. break;
  1539. case STB0899_16APSK_56:
  1540. params->modulation = DVBFE_MOD_16APSK;
  1541. params->fec = DVBFE_FEC_5_6;
  1542. break;
  1543. case STB0899_16APSK_89:
  1544. params->modulation = DVBFE_MOD_16APSK;
  1545. params->fec = DVBFE_FEC_8_9;
  1546. break;
  1547. case STB0899_16APSK_910:
  1548. params->modulation = DVBFE_MOD_16APSK;
  1549. params->fec = DVBFE_FEC_9_10;
  1550. break;
  1551. case STB0899_32APSK_34:
  1552. params->modulation = DVBFE_MOD_32APSK;
  1553. params->fec = DVBFE_FEC_3_4;
  1554. break;
  1555. case STB0899_32APSK_45:
  1556. params->modulation = DVBFE_MOD_32APSK;
  1557. params->fec = DVBFE_FEC_4_5;
  1558. break;
  1559. case STB0899_32APSK_56:
  1560. params->modulation = DVBFE_MOD_32APSK;
  1561. params->fec = DVBFE_FEC_5_6;
  1562. break;
  1563. case STB0899_32APSK_89:
  1564. params->modulation = DVBFE_MOD_32APSK;
  1565. params->fec = DVBFE_FEC_8_9;
  1566. break;
  1567. case STB0899_32APSK_910:
  1568. params->modulation = DVBFE_MOD_32APSK;
  1569. params->fec = DVBFE_FEC_9_10;
  1570. break;
  1571. default:
  1572. return -EINVAL;
  1573. }
  1574. return 0;
  1575. }
  1576. /*
  1577. * stb0899_track
  1578. * periodically check the signal level against a specified
  1579. * threshold level and perform derotator centering.
  1580. * called once we have a lock from a succesful search
  1581. * event.
  1582. *
  1583. * Will be called periodically called to maintain the
  1584. * lock.
  1585. *
  1586. * Will be used to get parameters as well as info from
  1587. * the decoded baseband header
  1588. *
  1589. * Once a new lock has established, the internal state
  1590. * frequency (internal->freq) is updated
  1591. */
  1592. static int stb0899_track(struct dvb_frontend *fe, struct dvbfe_params *params, int *delay)
  1593. {
  1594. u32 lock_lost;
  1595. struct stb0899_state *state = fe->demodulator_priv;
  1596. struct stb0899_internal *internal = &state->internal;
  1597. lock_lost = STB0899_READ_S2REG(STB0899_S2DEMOD, LOCK_LOST);
  1598. dprintk(verbose, FE_DEBUG, 1, "Lock Lost=[0x%02x]\n", lock_lost);
  1599. if (STB0899_GETFIELD(LOCK_LOST, lock_lost))
  1600. dprintk(verbose, FE_ERROR, 1, "Demodulator LOST LOCK !\n");
  1601. *delay = HZ/10;
  1602. return 0;
  1603. }
  1604. static int stb0899_get_params(struct dvb_frontend *fe, struct dvbfe_params *params)
  1605. {
  1606. struct stb0899_state *state = fe->demodulator_priv;
  1607. struct stb0899_internal *internal = &state->internal;
  1608. params->frequency = internal->freq;
  1609. params->inversion = internal->inversion;
  1610. params->delivery = state->delsys;
  1611. switch (state->delsys) {
  1612. case DVBFE_DELSYS_DVBS:
  1613. dprintk(verbose, FE_DEBUG, 1, "Get DVB-S params");
  1614. params->delsys.dvbs.symbol_rate = internal->srate;
  1615. params->delsys.dvbs.modulation = DVBFE_MOD_QPSK;
  1616. break;
  1617. case DVBFE_DELSYS_DSS:
  1618. dprintk(verbose, FE_DEBUG, 1, "Get DSS params");
  1619. params->delsys.dss.symbol_rate = internal->srate;
  1620. params->delsys.dss.modulation = DVBFE_MOD_QPSK;
  1621. break;
  1622. case DVBFE_DELSYS_DVBS2:
  1623. dprintk(verbose, FE_DEBUG, 1, "Get DVB-S2 params");
  1624. params->delsys.dvbs2.symbol_rate = internal->srate;
  1625. break;
  1626. default:
  1627. dprintk(verbose, FE_ERROR, 1, "Unsupported delivery system");
  1628. return -EINVAL;
  1629. }
  1630. return 0;
  1631. }
  1632. static enum dvbfe_algo stb0899_frontend_algo(struct dvb_frontend *fe)
  1633. {
  1634. return DVBFE_ALGO_CUSTOM;
  1635. }
  1636. static struct dvb_frontend_ops stb0899_ops = {
  1637. .info = {
  1638. .name = "STB0899 Multistandard",
  1639. },
  1640. .release = stb0899_release,
  1641. .init = stb0899_init,
  1642. .sleep = stb0899_sleep,
  1643. // .wakeup = stb0899_wakeup,
  1644. .i2c_gate_ctrl = stb0899_i2c_gate_ctrl,
  1645. .get_info = stb0899_get_info,
  1646. .get_delsys = stb0899_get_delsys,
  1647. .get_frontend_algo = stb0899_frontend_algo,
  1648. .search = stb0899_search,
  1649. .track = stb0899_track,
  1650. .get_params = stb0899_get_params,
  1651. .read_status = stb0899_read_status,
  1652. .read_snr = stb0899_read_snr,
  1653. .read_signal_strength = stb0899_read_signal_strength,
  1654. .read_status = stb0899_read_status,
  1655. .read_ber = stb0899_read_ber,
  1656. .set_voltage = stb0899_set_voltage,
  1657. .set_tone = stb0899_set_tone,
  1658. .diseqc_send_master_cmd = stb0899_send_diseqc_msg,
  1659. .diseqc_recv_slave_reply = stb0899_recv_slave_reply,
  1660. .diseqc_send_burst = stb0899_send_diseqc_burst,
  1661. };
  1662. struct dvb_frontend *stb0899_attach(struct stb0899_config *config, struct i2c_adapter *i2c)
  1663. {
  1664. struct stb0899_state *state = NULL;
  1665. enum stb0899_inversion inversion;
  1666. state = kzalloc(sizeof (struct stb0899_state), GFP_KERNEL);
  1667. if (state == NULL)
  1668. goto error;
  1669. inversion = config->inversion;
  1670. state->verbose = verbose;
  1671. state->config = config;
  1672. state->i2c = i2c;
  1673. state->frontend.ops = stb0899_ops;
  1674. state->frontend.demodulator_priv = state;
  1675. state->internal.inversion = inversion;
  1676. stb0899_wakeup(&state->frontend);
  1677. if (stb0899_get_dev_id(state) == -ENODEV) {
  1678. printk("%s: Exiting .. !\n", __func__);
  1679. goto error;
  1680. }
  1681. printk("%s: Attaching STB0899 \n", __func__);
  1682. return &state->frontend;
  1683. error:
  1684. kfree(state);
  1685. return NULL;
  1686. }
  1687. EXPORT_SYMBOL(stb0899_attach);
  1688. MODULE_PARM_DESC(verbose, "Set Verbosity level");
  1689. MODULE_AUTHOR("Manu Abraham");
  1690. MODULE_DESCRIPTION("STB0899 Multi-Std frontend");
  1691. MODULE_LICENSE("GPL");