stb0899_drv.c 51 KB

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