stb0899_drv.c 51 KB

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