stb0899_drv.c 51 KB

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