stb0899_drv.c 45 KB

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