stb0899_drv.c 51 KB

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