stb0899_drv.c 45 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665
  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. u8 tmp = data;
  490. return stb0899_write_regs(state, reg, &tmp, 1);
  491. }
  492. /*
  493. * stb0899_get_mclk
  494. * Get STB0899 master clock frequency
  495. * ExtClk: external clock frequency (Hz)
  496. */
  497. static u32 stb0899_get_mclk(struct stb0899_state *state)
  498. {
  499. u32 mclk = 0, div = 0;
  500. div = stb0899_read_reg(state, STB0899_NCOARSE);
  501. mclk = (div + 1) * state->config->xtal_freq / 6;
  502. dprintk(state->verbose, FE_DEBUG, 1, "div=%d, mclk=%d", div, mclk);
  503. return mclk;
  504. }
  505. /*
  506. * stb0899_set_mclk
  507. * Set STB0899 master Clock frequency
  508. * Mclk: demodulator master clock
  509. * ExtClk: external clock frequency (Hz)
  510. */
  511. static void stb0899_set_mclk(struct stb0899_state *state, u32 Mclk)
  512. {
  513. struct stb0899_internal *internal = &state->internal;
  514. u8 mdiv = 0;
  515. dprintk(state->verbose, FE_DEBUG, 1, "state->config=%p", state->config);
  516. mdiv = ((6 * Mclk) / state->config->xtal_freq) - 1;
  517. dprintk(state->verbose, FE_DEBUG, 1, "mdiv=%d", mdiv);
  518. stb0899_write_reg(state, STB0899_NCOARSE, mdiv);
  519. internal->master_clk = stb0899_get_mclk(state);
  520. dprintk(state->verbose, FE_DEBUG, 1, "MasterCLOCK=%d", internal->master_clk);
  521. }
  522. static int stb0899_postproc(struct stb0899_state *state, u8 ctl, int enable)
  523. {
  524. struct stb0899_config *config = state->config;
  525. const struct stb0899_postproc *postproc = config->postproc;
  526. /* post process event */
  527. if (postproc) {
  528. if (enable) {
  529. if (postproc[ctl].level == STB0899_GPIOPULLUP)
  530. stb0899_write_reg(state, postproc[ctl].gpio, 0x02);
  531. else
  532. stb0899_write_reg(state, postproc[ctl].gpio, 0x82);
  533. } else {
  534. if (postproc[ctl].level == STB0899_GPIOPULLUP)
  535. stb0899_write_reg(state, postproc[ctl].gpio, 0x82);
  536. else
  537. stb0899_write_reg(state, postproc[ctl].gpio, 0x02);
  538. }
  539. }
  540. return 0;
  541. }
  542. static void stb0899_release(struct dvb_frontend *fe)
  543. {
  544. struct stb0899_state *state = fe->demodulator_priv;
  545. dprintk(state->verbose, FE_DEBUG, 1, "Release Frontend");
  546. /* post process event */
  547. stb0899_postproc(state, STB0899_POSTPROC_GPIO_POWER, 0);
  548. kfree(state);
  549. }
  550. /*
  551. * stb0899_get_alpha
  552. * return: rolloff
  553. */
  554. static int stb0899_get_alpha(struct stb0899_state *state)
  555. {
  556. u8 mode_coeff;
  557. mode_coeff = stb0899_read_reg(state, STB0899_DEMOD);
  558. if (STB0899_GETFIELD(MODECOEFF, mode_coeff) == 1)
  559. return 20;
  560. else
  561. return 35;
  562. }
  563. /*
  564. * stb0899_init_calc
  565. */
  566. static void stb0899_init_calc(struct stb0899_state *state)
  567. {
  568. struct stb0899_internal *internal = &state->internal;
  569. int master_clk;
  570. u8 agc[2];
  571. u32 reg;
  572. /* Read registers (in burst mode) */
  573. stb0899_read_regs(state, STB0899_AGC1REF, agc, 2); /* AGC1R and AGC2O */
  574. /* Initial calculations */
  575. master_clk = stb0899_get_mclk(state);
  576. internal->t_agc1 = 0;
  577. internal->t_agc2 = 0;
  578. internal->master_clk = master_clk;
  579. internal->mclk = master_clk / 65536L;
  580. internal->rolloff = stb0899_get_alpha(state);
  581. /* DVBS2 Initial calculations */
  582. /* Set AGC value to the middle */
  583. internal->agc_gain = 8154;
  584. reg = STB0899_READ_S2REG(STB0899_S2DEMOD, IF_AGC_CNTRL);
  585. STB0899_SETFIELD_VAL(IF_GAIN_INIT, reg, internal->agc_gain);
  586. stb0899_write_s2reg(state, STB0899_S2DEMOD, STB0899_BASE_IF_AGC_CNTRL, STB0899_OFF0_IF_AGC_CNTRL, reg);
  587. reg = STB0899_READ_S2REG(STB0899_S2DEMOD, RRC_ALPHA);
  588. internal->rrc_alpha = STB0899_GETFIELD(RRC_ALPHA, reg);
  589. internal->center_freq = 0;
  590. internal->av_frame_coarse = 10;
  591. internal->av_frame_fine = 20;
  592. internal->step_size = 2;
  593. /*
  594. if ((pParams->SpectralInv == FE_IQ_NORMAL) || (pParams->SpectralInv == FE_IQ_AUTO))
  595. pParams->IQLocked = 0;
  596. else
  597. pParams->IQLocked = 1;
  598. */
  599. }
  600. static int stb0899_wait_diseqc_fifo_empty(struct stb0899_state *state, int timeout)
  601. {
  602. u8 reg = 0;
  603. unsigned long start = jiffies;
  604. while (1) {
  605. reg = stb0899_read_reg(state, STB0899_DISSTATUS);
  606. if (!STB0899_GETFIELD(FIFOFULL, reg))
  607. break;
  608. if (time_after(jiffies, start + timeout)) {
  609. dprintk(state->verbose, FE_ERROR, 1, "timed out !!");
  610. return -ETIMEDOUT;
  611. }
  612. }
  613. return 0;
  614. }
  615. static int stb0899_send_diseqc_msg(struct dvb_frontend *fe, struct dvb_diseqc_master_cmd *cmd)
  616. {
  617. struct stb0899_state *state = fe->demodulator_priv;
  618. u8 reg, i;
  619. if (cmd->msg_len > sizeof(cmd->msg))
  620. return -EINVAL;
  621. /* enable FIFO precharge */
  622. reg = stb0899_read_reg(state, STB0899_DISCNTRL1);
  623. STB0899_SETFIELD_VAL(DISPRECHARGE, reg, 1);
  624. stb0899_write_reg(state, STB0899_DISCNTRL1, reg);
  625. for (i = 0; i < cmd->msg_len; i++) {
  626. /* wait for FIFO empty */
  627. if (stb0899_wait_diseqc_fifo_empty(state, 100) < 0)
  628. return -ETIMEDOUT;
  629. stb0899_write_reg(state, STB0899_DISFIFO, cmd->msg[i]);
  630. }
  631. reg = stb0899_read_reg(state, STB0899_DISCNTRL1);
  632. STB0899_SETFIELD_VAL(DISPRECHARGE, reg, 0);
  633. stb0899_write_reg(state, STB0899_DISCNTRL1, reg);
  634. msleep(100);
  635. return 0;
  636. }
  637. static int stb0899_wait_diseqc_rxidle(struct stb0899_state *state, int timeout)
  638. {
  639. u8 reg = 0;
  640. unsigned long start = jiffies;
  641. while (!STB0899_GETFIELD(RXEND, reg)) {
  642. reg = stb0899_read_reg(state, STB0899_DISRX_ST0);
  643. if (time_after(jiffies, start + timeout)) {
  644. dprintk(state->verbose, FE_ERROR, 1, "timed out!!");
  645. return -ETIMEDOUT;
  646. }
  647. msleep(10);
  648. }
  649. return 0;
  650. }
  651. static int stb0899_recv_slave_reply(struct dvb_frontend *fe, struct dvb_diseqc_slave_reply *reply)
  652. {
  653. struct stb0899_state *state = fe->demodulator_priv;
  654. u8 reg, length = 0, i;
  655. int result;
  656. if (stb0899_wait_diseqc_rxidle(state, 100) < 0)
  657. return -ETIMEDOUT;
  658. reg = stb0899_read_reg(state, STB0899_DISRX_ST0);
  659. if (STB0899_GETFIELD(RXEND, reg)) {
  660. reg = stb0899_read_reg(state, STB0899_DISRX_ST1);
  661. length = STB0899_GETFIELD(FIFOBYTENBR, reg);
  662. if (length > sizeof (reply->msg)) {
  663. result = -EOVERFLOW;
  664. goto exit;
  665. }
  666. reply->msg_len = length;
  667. /* extract data */
  668. for (i = 0; i < length; i++)
  669. reply->msg[i] = stb0899_read_reg(state, STB0899_DISFIFO);
  670. }
  671. return 0;
  672. exit:
  673. return result;
  674. }
  675. static int stb0899_wait_diseqc_txidle(struct stb0899_state *state, int timeout)
  676. {
  677. u8 reg = 0;
  678. unsigned long start = jiffies;
  679. while (!STB0899_GETFIELD(TXIDLE, reg)) {
  680. reg = stb0899_read_reg(state, STB0899_DISSTATUS);
  681. if (time_after(jiffies, start + timeout)) {
  682. dprintk(state->verbose, FE_ERROR, 1, "timed out!!");
  683. return -ETIMEDOUT;
  684. }
  685. msleep(10);
  686. }
  687. return 0;
  688. }
  689. static int stb0899_send_diseqc_burst(struct dvb_frontend *fe,
  690. enum fe_sec_mini_cmd burst)
  691. {
  692. struct stb0899_state *state = fe->demodulator_priv;
  693. u8 reg, old_state;
  694. /* wait for diseqc idle */
  695. if (stb0899_wait_diseqc_txidle(state, 100) < 0)
  696. return -ETIMEDOUT;
  697. reg = stb0899_read_reg(state, STB0899_DISCNTRL1);
  698. old_state = reg;
  699. /* set to burst mode */
  700. STB0899_SETFIELD_VAL(DISEQCMODE, reg, 0x03);
  701. STB0899_SETFIELD_VAL(DISPRECHARGE, reg, 0x01);
  702. stb0899_write_reg(state, STB0899_DISCNTRL1, reg);
  703. switch (burst) {
  704. case SEC_MINI_A:
  705. /* unmodulated */
  706. stb0899_write_reg(state, STB0899_DISFIFO, 0x00);
  707. break;
  708. case SEC_MINI_B:
  709. /* modulated */
  710. stb0899_write_reg(state, STB0899_DISFIFO, 0xff);
  711. break;
  712. }
  713. reg = stb0899_read_reg(state, STB0899_DISCNTRL1);
  714. STB0899_SETFIELD_VAL(DISPRECHARGE, reg, 0x00);
  715. stb0899_write_reg(state, STB0899_DISCNTRL1, reg);
  716. /* wait for diseqc idle */
  717. if (stb0899_wait_diseqc_txidle(state, 100) < 0)
  718. return -ETIMEDOUT;
  719. /* restore state */
  720. stb0899_write_reg(state, STB0899_DISCNTRL1, old_state);
  721. return 0;
  722. }
  723. static int stb0899_diseqc_init(struct stb0899_state *state)
  724. {
  725. /*
  726. struct dvb_diseqc_slave_reply rx_data;
  727. */
  728. u8 f22_tx, reg;
  729. u32 mclk, tx_freq = 22000;/* count = 0, i; */
  730. reg = stb0899_read_reg(state, STB0899_DISCNTRL2);
  731. STB0899_SETFIELD_VAL(ONECHIP_TRX, reg, 0);
  732. stb0899_write_reg(state, STB0899_DISCNTRL2, reg);
  733. /* disable Tx spy */
  734. reg = stb0899_read_reg(state, STB0899_DISCNTRL1);
  735. STB0899_SETFIELD_VAL(DISEQCRESET, reg, 1);
  736. stb0899_write_reg(state, STB0899_DISCNTRL1, reg);
  737. reg = stb0899_read_reg(state, STB0899_DISCNTRL1);
  738. STB0899_SETFIELD_VAL(DISEQCRESET, reg, 0);
  739. stb0899_write_reg(state, STB0899_DISCNTRL1, reg);
  740. mclk = stb0899_get_mclk(state);
  741. f22_tx = mclk / (tx_freq * 32);
  742. stb0899_write_reg(state, STB0899_DISF22, f22_tx); /* DiSEqC Tx freq */
  743. state->rx_freq = 20000;
  744. return 0;
  745. }
  746. static int stb0899_sleep(struct dvb_frontend *fe)
  747. {
  748. struct stb0899_state *state = fe->demodulator_priv;
  749. /*
  750. u8 reg;
  751. */
  752. dprintk(state->verbose, FE_DEBUG, 1, "Going to Sleep .. (Really tired .. :-))");
  753. /* post process event */
  754. stb0899_postproc(state, STB0899_POSTPROC_GPIO_POWER, 0);
  755. return 0;
  756. }
  757. static int stb0899_wakeup(struct dvb_frontend *fe)
  758. {
  759. int rc;
  760. struct stb0899_state *state = fe->demodulator_priv;
  761. if ((rc = stb0899_write_reg(state, STB0899_SYNTCTRL, STB0899_SELOSCI)))
  762. return rc;
  763. /* Activate all clocks; DVB-S2 registers are inaccessible otherwise. */
  764. if ((rc = stb0899_write_reg(state, STB0899_STOPCLK1, 0x00)))
  765. return rc;
  766. if ((rc = stb0899_write_reg(state, STB0899_STOPCLK2, 0x00)))
  767. return rc;
  768. /* post process event */
  769. stb0899_postproc(state, STB0899_POSTPROC_GPIO_POWER, 1);
  770. return 0;
  771. }
  772. static int stb0899_init(struct dvb_frontend *fe)
  773. {
  774. int i;
  775. struct stb0899_state *state = fe->demodulator_priv;
  776. struct stb0899_config *config = state->config;
  777. dprintk(state->verbose, FE_DEBUG, 1, "Initializing STB0899 ... ");
  778. /* init device */
  779. dprintk(state->verbose, FE_DEBUG, 1, "init device");
  780. for (i = 0; config->init_dev[i].address != 0xffff; i++)
  781. stb0899_write_reg(state, config->init_dev[i].address, config->init_dev[i].data);
  782. dprintk(state->verbose, FE_DEBUG, 1, "init S2 demod");
  783. /* init S2 demod */
  784. for (i = 0; config->init_s2_demod[i].offset != 0xffff; i++)
  785. stb0899_write_s2reg(state, STB0899_S2DEMOD,
  786. config->init_s2_demod[i].base_address,
  787. config->init_s2_demod[i].offset,
  788. config->init_s2_demod[i].data);
  789. dprintk(state->verbose, FE_DEBUG, 1, "init S1 demod");
  790. /* init S1 demod */
  791. for (i = 0; config->init_s1_demod[i].address != 0xffff; i++)
  792. stb0899_write_reg(state, config->init_s1_demod[i].address, config->init_s1_demod[i].data);
  793. dprintk(state->verbose, FE_DEBUG, 1, "init S2 FEC");
  794. /* init S2 fec */
  795. for (i = 0; config->init_s2_fec[i].offset != 0xffff; i++)
  796. stb0899_write_s2reg(state, STB0899_S2FEC,
  797. config->init_s2_fec[i].base_address,
  798. config->init_s2_fec[i].offset,
  799. config->init_s2_fec[i].data);
  800. dprintk(state->verbose, FE_DEBUG, 1, "init TST");
  801. /* init test */
  802. for (i = 0; config->init_tst[i].address != 0xffff; i++)
  803. stb0899_write_reg(state, config->init_tst[i].address, config->init_tst[i].data);
  804. stb0899_init_calc(state);
  805. stb0899_diseqc_init(state);
  806. return 0;
  807. }
  808. static int stb0899_table_lookup(const struct stb0899_tab *tab, int max, int val)
  809. {
  810. int res = 0;
  811. int min = 0, med;
  812. if (val < tab[min].read)
  813. res = tab[min].real;
  814. else if (val >= tab[max].read)
  815. res = tab[max].real;
  816. else {
  817. while ((max - min) > 1) {
  818. med = (max + min) / 2;
  819. if (val >= tab[min].read && val < tab[med].read)
  820. max = med;
  821. else
  822. min = med;
  823. }
  824. res = ((val - tab[min].read) *
  825. (tab[max].real - tab[min].real) /
  826. (tab[max].read - tab[min].read)) +
  827. tab[min].real;
  828. }
  829. return res;
  830. }
  831. static int stb0899_read_signal_strength(struct dvb_frontend *fe, u16 *strength)
  832. {
  833. struct stb0899_state *state = fe->demodulator_priv;
  834. struct stb0899_internal *internal = &state->internal;
  835. int val;
  836. u32 reg;
  837. *strength = 0;
  838. switch (state->delsys) {
  839. case SYS_DVBS:
  840. case SYS_DSS:
  841. if (internal->lock) {
  842. reg = stb0899_read_reg(state, STB0899_VSTATUS);
  843. if (STB0899_GETFIELD(VSTATUS_LOCKEDVIT, reg)) {
  844. reg = stb0899_read_reg(state, STB0899_AGCIQIN);
  845. val = (s32)(s8)STB0899_GETFIELD(AGCIQVALUE, reg);
  846. *strength = stb0899_table_lookup(stb0899_dvbsrf_tab, ARRAY_SIZE(stb0899_dvbsrf_tab) - 1, val);
  847. *strength += 750;
  848. dprintk(state->verbose, FE_DEBUG, 1, "AGCIQVALUE = 0x%02x, C = %d * 0.1 dBm",
  849. val & 0xff, *strength);
  850. }
  851. }
  852. break;
  853. case SYS_DVBS2:
  854. if (internal->lock) {
  855. reg = STB0899_READ_S2REG(STB0899_S2DEMOD, IF_AGC_GAIN);
  856. val = STB0899_GETFIELD(IF_AGC_GAIN, reg);
  857. *strength = stb0899_table_lookup(stb0899_dvbs2rf_tab, ARRAY_SIZE(stb0899_dvbs2rf_tab) - 1, val);
  858. *strength += 950;
  859. dprintk(state->verbose, FE_DEBUG, 1, "IF_AGC_GAIN = 0x%04x, C = %d * 0.1 dBm",
  860. val & 0x3fff, *strength);
  861. }
  862. break;
  863. default:
  864. dprintk(state->verbose, FE_DEBUG, 1, "Unsupported delivery system");
  865. return -EINVAL;
  866. }
  867. return 0;
  868. }
  869. static int stb0899_read_snr(struct dvb_frontend *fe, u16 *snr)
  870. {
  871. struct stb0899_state *state = fe->demodulator_priv;
  872. struct stb0899_internal *internal = &state->internal;
  873. unsigned int val, quant, quantn = -1, est, estn = -1;
  874. u8 buf[2];
  875. u32 reg;
  876. *snr = 0;
  877. reg = stb0899_read_reg(state, STB0899_VSTATUS);
  878. switch (state->delsys) {
  879. case SYS_DVBS:
  880. case SYS_DSS:
  881. if (internal->lock) {
  882. if (STB0899_GETFIELD(VSTATUS_LOCKEDVIT, reg)) {
  883. stb0899_read_regs(state, STB0899_NIRM, buf, 2);
  884. val = MAKEWORD16(buf[0], buf[1]);
  885. *snr = stb0899_table_lookup(stb0899_cn_tab, ARRAY_SIZE(stb0899_cn_tab) - 1, val);
  886. dprintk(state->verbose, FE_DEBUG, 1, "NIR = 0x%02x%02x = %u, C/N = %d * 0.1 dBm\n",
  887. buf[0], buf[1], val, *snr);
  888. }
  889. }
  890. break;
  891. case SYS_DVBS2:
  892. if (internal->lock) {
  893. reg = STB0899_READ_S2REG(STB0899_S2DEMOD, UWP_CNTRL1);
  894. quant = STB0899_GETFIELD(UWP_ESN0_QUANT, reg);
  895. reg = STB0899_READ_S2REG(STB0899_S2DEMOD, UWP_STAT2);
  896. est = STB0899_GETFIELD(ESN0_EST, reg);
  897. if (est == 1)
  898. val = 301; /* C/N = 30.1 dB */
  899. else if (est == 2)
  900. val = 270; /* C/N = 27.0 dB */
  901. else {
  902. /* quantn = 100 * log(quant^2) */
  903. quantn = stb0899_table_lookup(stb0899_quant_tab, ARRAY_SIZE(stb0899_quant_tab) - 1, quant * 100);
  904. /* estn = 100 * log(est) */
  905. estn = stb0899_table_lookup(stb0899_est_tab, ARRAY_SIZE(stb0899_est_tab) - 1, est);
  906. /* snr(dBm/10) = -10*(log(est)-log(quant^2)) => snr(dBm/10) = (100*log(quant^2)-100*log(est))/10 */
  907. val = (quantn - estn) / 10;
  908. }
  909. *snr = val;
  910. dprintk(state->verbose, FE_DEBUG, 1, "Es/N0 quant = %d (%d) estimate = %u (%d), C/N = %d * 0.1 dBm",
  911. quant, quantn, est, estn, val);
  912. }
  913. break;
  914. default:
  915. dprintk(state->verbose, FE_DEBUG, 1, "Unsupported delivery system");
  916. return -EINVAL;
  917. }
  918. return 0;
  919. }
  920. static int stb0899_read_status(struct dvb_frontend *fe, enum fe_status *status)
  921. {
  922. struct stb0899_state *state = fe->demodulator_priv;
  923. struct stb0899_internal *internal = &state->internal;
  924. u8 reg;
  925. *status = 0;
  926. switch (state->delsys) {
  927. case SYS_DVBS:
  928. case SYS_DSS:
  929. dprintk(state->verbose, FE_DEBUG, 1, "Delivery system DVB-S/DSS");
  930. if (internal->lock) {
  931. reg = stb0899_read_reg(state, STB0899_VSTATUS);
  932. if (STB0899_GETFIELD(VSTATUS_LOCKEDVIT, reg)) {
  933. dprintk(state->verbose, FE_DEBUG, 1, "--------> FE_HAS_CARRIER | FE_HAS_LOCK");
  934. *status |= FE_HAS_SIGNAL | FE_HAS_CARRIER | FE_HAS_LOCK;
  935. reg = stb0899_read_reg(state, STB0899_PLPARM);
  936. if (STB0899_GETFIELD(VITCURPUN, reg)) {
  937. dprintk(state->verbose, FE_DEBUG, 1, "--------> FE_HAS_VITERBI | FE_HAS_SYNC");
  938. *status |= FE_HAS_VITERBI | FE_HAS_SYNC;
  939. /* post process event */
  940. stb0899_postproc(state, STB0899_POSTPROC_GPIO_LOCK, 1);
  941. }
  942. }
  943. }
  944. break;
  945. case SYS_DVBS2:
  946. dprintk(state->verbose, FE_DEBUG, 1, "Delivery system DVB-S2");
  947. if (internal->lock) {
  948. reg = STB0899_READ_S2REG(STB0899_S2DEMOD, DMD_STAT2);
  949. if (STB0899_GETFIELD(UWP_LOCK, reg) && STB0899_GETFIELD(CSM_LOCK, reg)) {
  950. *status |= FE_HAS_CARRIER;
  951. dprintk(state->verbose, FE_DEBUG, 1,
  952. "UWP & CSM Lock ! ---> DVB-S2 FE_HAS_CARRIER");
  953. reg = stb0899_read_reg(state, STB0899_CFGPDELSTATUS1);
  954. if (STB0899_GETFIELD(CFGPDELSTATUS_LOCK, reg)) {
  955. *status |= FE_HAS_LOCK;
  956. dprintk(state->verbose, FE_DEBUG, 1,
  957. "Packet Delineator Locked ! -----> DVB-S2 FE_HAS_LOCK");
  958. }
  959. if (STB0899_GETFIELD(CONTINUOUS_STREAM, reg)) {
  960. *status |= FE_HAS_VITERBI;
  961. dprintk(state->verbose, FE_DEBUG, 1,
  962. "Packet Delineator found VITERBI ! -----> DVB-S2 FE_HAS_VITERBI");
  963. }
  964. if (STB0899_GETFIELD(ACCEPTED_STREAM, reg)) {
  965. *status |= FE_HAS_SYNC;
  966. dprintk(state->verbose, FE_DEBUG, 1,
  967. "Packet Delineator found SYNC ! -----> DVB-S2 FE_HAS_SYNC");
  968. /* post process event */
  969. stb0899_postproc(state, STB0899_POSTPROC_GPIO_LOCK, 1);
  970. }
  971. }
  972. }
  973. break;
  974. default:
  975. dprintk(state->verbose, FE_DEBUG, 1, "Unsupported delivery system");
  976. return -EINVAL;
  977. }
  978. return 0;
  979. }
  980. /*
  981. * stb0899_get_error
  982. * viterbi error for DVB-S/DSS
  983. * packet error for DVB-S2
  984. * Bit Error Rate or Packet Error Rate * 10 ^ 7
  985. */
  986. static int stb0899_read_ber(struct dvb_frontend *fe, u32 *ber)
  987. {
  988. struct stb0899_state *state = fe->demodulator_priv;
  989. struct stb0899_internal *internal = &state->internal;
  990. u8 lsb, msb;
  991. *ber = 0;
  992. switch (state->delsys) {
  993. case SYS_DVBS:
  994. case SYS_DSS:
  995. if (internal->lock) {
  996. lsb = stb0899_read_reg(state, STB0899_ECNT1L);
  997. msb = stb0899_read_reg(state, STB0899_ECNT1M);
  998. *ber = MAKEWORD16(msb, lsb);
  999. /* Viterbi Check */
  1000. if (STB0899_GETFIELD(VSTATUS_PRFVIT, internal->v_status)) {
  1001. /* Error Rate */
  1002. *ber *= 9766;
  1003. /* ber = ber * 10 ^ 7 */
  1004. *ber /= (-1 + (1 << (2 * STB0899_GETFIELD(NOE, internal->err_ctrl))));
  1005. *ber /= 8;
  1006. }
  1007. }
  1008. break;
  1009. case SYS_DVBS2:
  1010. if (internal->lock) {
  1011. lsb = stb0899_read_reg(state, STB0899_ECNT1L);
  1012. msb = stb0899_read_reg(state, STB0899_ECNT1M);
  1013. *ber = MAKEWORD16(msb, lsb);
  1014. /* ber = ber * 10 ^ 7 */
  1015. *ber *= 10000000;
  1016. *ber /= (-1 + (1 << (4 + 2 * STB0899_GETFIELD(NOE, internal->err_ctrl))));
  1017. }
  1018. break;
  1019. default:
  1020. dprintk(state->verbose, FE_DEBUG, 1, "Unsupported delivery system");
  1021. return -EINVAL;
  1022. }
  1023. return 0;
  1024. }
  1025. static int stb0899_set_voltage(struct dvb_frontend *fe,
  1026. enum fe_sec_voltage voltage)
  1027. {
  1028. struct stb0899_state *state = fe->demodulator_priv;
  1029. switch (voltage) {
  1030. case SEC_VOLTAGE_13:
  1031. stb0899_write_reg(state, STB0899_GPIO00CFG, 0x82);
  1032. stb0899_write_reg(state, STB0899_GPIO01CFG, 0x02);
  1033. stb0899_write_reg(state, STB0899_GPIO02CFG, 0x00);
  1034. break;
  1035. case SEC_VOLTAGE_18:
  1036. stb0899_write_reg(state, STB0899_GPIO00CFG, 0x02);
  1037. stb0899_write_reg(state, STB0899_GPIO01CFG, 0x02);
  1038. stb0899_write_reg(state, STB0899_GPIO02CFG, 0x82);
  1039. break;
  1040. case SEC_VOLTAGE_OFF:
  1041. stb0899_write_reg(state, STB0899_GPIO00CFG, 0x82);
  1042. stb0899_write_reg(state, STB0899_GPIO01CFG, 0x82);
  1043. stb0899_write_reg(state, STB0899_GPIO02CFG, 0x82);
  1044. break;
  1045. default:
  1046. return -EINVAL;
  1047. }
  1048. return 0;
  1049. }
  1050. static int stb0899_set_tone(struct dvb_frontend *fe, enum fe_sec_tone_mode tone)
  1051. {
  1052. struct stb0899_state *state = fe->demodulator_priv;
  1053. struct stb0899_internal *internal = &state->internal;
  1054. u8 div, reg;
  1055. /* wait for diseqc idle */
  1056. if (stb0899_wait_diseqc_txidle(state, 100) < 0)
  1057. return -ETIMEDOUT;
  1058. switch (tone) {
  1059. case SEC_TONE_ON:
  1060. div = (internal->master_clk / 100) / 5632;
  1061. div = (div + 5) / 10;
  1062. stb0899_write_reg(state, STB0899_DISEQCOCFG, 0x66);
  1063. reg = stb0899_read_reg(state, STB0899_ACRPRESC);
  1064. STB0899_SETFIELD_VAL(ACRPRESC, reg, 0x03);
  1065. stb0899_write_reg(state, STB0899_ACRPRESC, reg);
  1066. stb0899_write_reg(state, STB0899_ACRDIV1, div);
  1067. break;
  1068. case SEC_TONE_OFF:
  1069. stb0899_write_reg(state, STB0899_DISEQCOCFG, 0x20);
  1070. break;
  1071. default:
  1072. return -EINVAL;
  1073. }
  1074. return 0;
  1075. }
  1076. int stb0899_i2c_gate_ctrl(struct dvb_frontend *fe, int enable)
  1077. {
  1078. int i2c_stat;
  1079. struct stb0899_state *state = fe->demodulator_priv;
  1080. i2c_stat = stb0899_read_reg(state, STB0899_I2CRPT);
  1081. if (i2c_stat < 0)
  1082. goto err;
  1083. if (enable) {
  1084. dprintk(state->verbose, FE_DEBUG, 1, "Enabling I2C Repeater ...");
  1085. i2c_stat |= STB0899_I2CTON;
  1086. if (stb0899_write_reg(state, STB0899_I2CRPT, i2c_stat) < 0)
  1087. goto err;
  1088. } else {
  1089. dprintk(state->verbose, FE_DEBUG, 1, "Disabling I2C Repeater ...");
  1090. i2c_stat &= ~STB0899_I2CTON;
  1091. if (stb0899_write_reg(state, STB0899_I2CRPT, i2c_stat) < 0)
  1092. goto err;
  1093. }
  1094. return 0;
  1095. err:
  1096. dprintk(state->verbose, FE_ERROR, 1, "I2C Repeater control failed");
  1097. return -EREMOTEIO;
  1098. }
  1099. static inline void CONVERT32(u32 x, char *str)
  1100. {
  1101. *str++ = (x >> 24) & 0xff;
  1102. *str++ = (x >> 16) & 0xff;
  1103. *str++ = (x >> 8) & 0xff;
  1104. *str++ = (x >> 0) & 0xff;
  1105. *str = '\0';
  1106. }
  1107. static int stb0899_get_dev_id(struct stb0899_state *state)
  1108. {
  1109. u8 chip_id, release;
  1110. u16 id;
  1111. u32 demod_ver = 0, fec_ver = 0;
  1112. char demod_str[5] = { 0 };
  1113. char fec_str[5] = { 0 };
  1114. id = stb0899_read_reg(state, STB0899_DEV_ID);
  1115. dprintk(state->verbose, FE_DEBUG, 1, "ID reg=[0x%02x]", id);
  1116. chip_id = STB0899_GETFIELD(CHIP_ID, id);
  1117. release = STB0899_GETFIELD(CHIP_REL, id);
  1118. dprintk(state->verbose, FE_ERROR, 1, "Device ID=[%d], Release=[%d]",
  1119. chip_id, release);
  1120. CONVERT32(STB0899_READ_S2REG(STB0899_S2DEMOD, DMD_CORE_ID), (char *)&demod_str);
  1121. demod_ver = STB0899_READ_S2REG(STB0899_S2DEMOD, DMD_VERSION_ID);
  1122. dprintk(state->verbose, FE_ERROR, 1, "Demodulator Core ID=[%s], Version=[%d]", (char *) &demod_str, demod_ver);
  1123. CONVERT32(STB0899_READ_S2REG(STB0899_S2FEC, FEC_CORE_ID_REG), (char *)&fec_str);
  1124. fec_ver = STB0899_READ_S2REG(STB0899_S2FEC, FEC_VER_ID_REG);
  1125. if (! (chip_id > 0)) {
  1126. dprintk(state->verbose, FE_ERROR, 1, "couldn't find a STB 0899");
  1127. return -ENODEV;
  1128. }
  1129. dprintk(state->verbose, FE_ERROR, 1, "FEC Core ID=[%s], Version=[%d]", (char*) &fec_str, fec_ver);
  1130. return 0;
  1131. }
  1132. static void stb0899_set_delivery(struct stb0899_state *state)
  1133. {
  1134. u8 reg;
  1135. u8 stop_clk[2];
  1136. stop_clk[0] = stb0899_read_reg(state, STB0899_STOPCLK1);
  1137. stop_clk[1] = stb0899_read_reg(state, STB0899_STOPCLK2);
  1138. switch (state->delsys) {
  1139. case SYS_DVBS:
  1140. dprintk(state->verbose, FE_DEBUG, 1, "Delivery System -- DVB-S");
  1141. /* FECM/Viterbi ON */
  1142. reg = stb0899_read_reg(state, STB0899_FECM);
  1143. STB0899_SETFIELD_VAL(FECM_RSVD0, reg, 0);
  1144. STB0899_SETFIELD_VAL(FECM_VITERBI_ON, reg, 1);
  1145. stb0899_write_reg(state, STB0899_FECM, reg);
  1146. stb0899_write_reg(state, STB0899_RSULC, 0xb1);
  1147. stb0899_write_reg(state, STB0899_TSULC, 0x40);
  1148. stb0899_write_reg(state, STB0899_RSLLC, 0x42);
  1149. stb0899_write_reg(state, STB0899_TSLPL, 0x12);
  1150. reg = stb0899_read_reg(state, STB0899_TSTRES);
  1151. STB0899_SETFIELD_VAL(FRESLDPC, reg, 1);
  1152. stb0899_write_reg(state, STB0899_TSTRES, reg);
  1153. STB0899_SETFIELD_VAL(STOP_CHK8PSK, stop_clk[0], 1);
  1154. STB0899_SETFIELD_VAL(STOP_CKFEC108, stop_clk[0], 1);
  1155. STB0899_SETFIELD_VAL(STOP_CKFEC216, stop_clk[0], 1);
  1156. STB0899_SETFIELD_VAL(STOP_CKPKDLIN108, stop_clk[1], 1);
  1157. STB0899_SETFIELD_VAL(STOP_CKPKDLIN216, stop_clk[1], 1);
  1158. STB0899_SETFIELD_VAL(STOP_CKINTBUF216, stop_clk[0], 1);
  1159. STB0899_SETFIELD_VAL(STOP_CKCORE216, stop_clk[0], 0);
  1160. STB0899_SETFIELD_VAL(STOP_CKS2DMD108, stop_clk[1], 1);
  1161. break;
  1162. case SYS_DVBS2:
  1163. /* FECM/Viterbi OFF */
  1164. reg = stb0899_read_reg(state, STB0899_FECM);
  1165. STB0899_SETFIELD_VAL(FECM_RSVD0, reg, 0);
  1166. STB0899_SETFIELD_VAL(FECM_VITERBI_ON, reg, 0);
  1167. stb0899_write_reg(state, STB0899_FECM, reg);
  1168. stb0899_write_reg(state, STB0899_RSULC, 0xb1);
  1169. stb0899_write_reg(state, STB0899_TSULC, 0x42);
  1170. stb0899_write_reg(state, STB0899_RSLLC, 0x40);
  1171. stb0899_write_reg(state, STB0899_TSLPL, 0x02);
  1172. reg = stb0899_read_reg(state, STB0899_TSTRES);
  1173. STB0899_SETFIELD_VAL(FRESLDPC, reg, 0);
  1174. stb0899_write_reg(state, STB0899_TSTRES, reg);
  1175. STB0899_SETFIELD_VAL(STOP_CHK8PSK, stop_clk[0], 1);
  1176. STB0899_SETFIELD_VAL(STOP_CKFEC108, stop_clk[0], 0);
  1177. STB0899_SETFIELD_VAL(STOP_CKFEC216, stop_clk[0], 0);
  1178. STB0899_SETFIELD_VAL(STOP_CKPKDLIN108, stop_clk[1], 0);
  1179. STB0899_SETFIELD_VAL(STOP_CKPKDLIN216, stop_clk[1], 0);
  1180. STB0899_SETFIELD_VAL(STOP_CKINTBUF216, stop_clk[0], 0);
  1181. STB0899_SETFIELD_VAL(STOP_CKCORE216, stop_clk[0], 0);
  1182. STB0899_SETFIELD_VAL(STOP_CKS2DMD108, stop_clk[1], 0);
  1183. break;
  1184. case SYS_DSS:
  1185. /* FECM/Viterbi ON */
  1186. reg = stb0899_read_reg(state, STB0899_FECM);
  1187. STB0899_SETFIELD_VAL(FECM_RSVD0, reg, 1);
  1188. STB0899_SETFIELD_VAL(FECM_VITERBI_ON, reg, 1);
  1189. stb0899_write_reg(state, STB0899_FECM, reg);
  1190. stb0899_write_reg(state, STB0899_RSULC, 0xa1);
  1191. stb0899_write_reg(state, STB0899_TSULC, 0x61);
  1192. stb0899_write_reg(state, STB0899_RSLLC, 0x42);
  1193. reg = stb0899_read_reg(state, STB0899_TSTRES);
  1194. STB0899_SETFIELD_VAL(FRESLDPC, reg, 1);
  1195. stb0899_write_reg(state, STB0899_TSTRES, reg);
  1196. STB0899_SETFIELD_VAL(STOP_CHK8PSK, stop_clk[0], 1);
  1197. STB0899_SETFIELD_VAL(STOP_CKFEC108, stop_clk[0], 1);
  1198. STB0899_SETFIELD_VAL(STOP_CKFEC216, stop_clk[0], 1);
  1199. STB0899_SETFIELD_VAL(STOP_CKPKDLIN108, stop_clk[1], 1);
  1200. STB0899_SETFIELD_VAL(STOP_CKPKDLIN216, stop_clk[1], 1);
  1201. STB0899_SETFIELD_VAL(STOP_CKCORE216, stop_clk[0], 0);
  1202. STB0899_SETFIELD_VAL(STOP_CKS2DMD108, stop_clk[1], 1);
  1203. break;
  1204. default:
  1205. dprintk(state->verbose, FE_ERROR, 1, "Unsupported delivery system");
  1206. break;
  1207. }
  1208. STB0899_SETFIELD_VAL(STOP_CKADCI108, stop_clk[0], 0);
  1209. stb0899_write_regs(state, STB0899_STOPCLK1, stop_clk, 2);
  1210. }
  1211. /*
  1212. * stb0899_set_iterations
  1213. * set the LDPC iteration scale function
  1214. */
  1215. static void stb0899_set_iterations(struct stb0899_state *state)
  1216. {
  1217. struct stb0899_internal *internal = &state->internal;
  1218. struct stb0899_config *config = state->config;
  1219. s32 iter_scale;
  1220. u32 reg;
  1221. iter_scale = 17 * (internal->master_clk / 1000);
  1222. iter_scale += 410000;
  1223. iter_scale /= (internal->srate / 1000000);
  1224. iter_scale /= 1000;
  1225. if (iter_scale > config->ldpc_max_iter)
  1226. iter_scale = config->ldpc_max_iter;
  1227. reg = STB0899_READ_S2REG(STB0899_S2FEC, MAX_ITER);
  1228. STB0899_SETFIELD_VAL(MAX_ITERATIONS, reg, iter_scale);
  1229. stb0899_write_s2reg(state, STB0899_S2FEC, STB0899_BASE_MAX_ITER, STB0899_OFF0_MAX_ITER, reg);
  1230. }
  1231. static enum dvbfe_search stb0899_search(struct dvb_frontend *fe)
  1232. {
  1233. struct stb0899_state *state = fe->demodulator_priv;
  1234. struct stb0899_params *i_params = &state->params;
  1235. struct stb0899_internal *internal = &state->internal;
  1236. struct stb0899_config *config = state->config;
  1237. struct dtv_frontend_properties *props = &fe->dtv_property_cache;
  1238. u32 SearchRange, gain;
  1239. i_params->freq = props->frequency;
  1240. i_params->srate = props->symbol_rate;
  1241. state->delsys = props->delivery_system;
  1242. dprintk(state->verbose, FE_DEBUG, 1, "delivery system=%d", state->delsys);
  1243. SearchRange = 10000000;
  1244. dprintk(state->verbose, FE_DEBUG, 1, "Frequency=%d, Srate=%d", i_params->freq, i_params->srate);
  1245. /* checking Search Range is meaningless for a fixed 3 Mhz */
  1246. if (INRANGE(i_params->srate, 1000000, 45000000)) {
  1247. dprintk(state->verbose, FE_DEBUG, 1, "Parameters IN RANGE");
  1248. stb0899_set_delivery(state);
  1249. if (state->config->tuner_set_rfsiggain) {
  1250. if (internal->srate > 15000000)
  1251. gain = 8; /* 15Mb < srate < 45Mb, gain = 8dB */
  1252. else if (internal->srate > 5000000)
  1253. gain = 12; /* 5Mb < srate < 15Mb, gain = 12dB */
  1254. else
  1255. gain = 14; /* 1Mb < srate < 5Mb, gain = 14db */
  1256. state->config->tuner_set_rfsiggain(fe, gain);
  1257. }
  1258. if (i_params->srate <= 5000000)
  1259. stb0899_set_mclk(state, config->lo_clk);
  1260. else
  1261. stb0899_set_mclk(state, config->hi_clk);
  1262. switch (state->delsys) {
  1263. case SYS_DVBS:
  1264. case SYS_DSS:
  1265. dprintk(state->verbose, FE_DEBUG, 1, "DVB-S delivery system");
  1266. internal->freq = i_params->freq;
  1267. internal->srate = i_params->srate;
  1268. /*
  1269. * search = user search range +
  1270. * 500Khz +
  1271. * 2 * Tuner_step_size +
  1272. * 10% of the symbol rate
  1273. */
  1274. internal->srch_range = SearchRange + 1500000 + (i_params->srate / 5);
  1275. internal->derot_percent = 30;
  1276. /* What to do for tuners having no bandwidth setup ? */
  1277. /* enable tuner I/O */
  1278. stb0899_i2c_gate_ctrl(&state->frontend, 1);
  1279. if (state->config->tuner_set_bandwidth)
  1280. state->config->tuner_set_bandwidth(fe, (13 * (stb0899_carr_width(state) + SearchRange)) / 10);
  1281. if (state->config->tuner_get_bandwidth)
  1282. state->config->tuner_get_bandwidth(fe, &internal->tuner_bw);
  1283. /* disable tuner I/O */
  1284. stb0899_i2c_gate_ctrl(&state->frontend, 0);
  1285. /* Set DVB-S1 AGC */
  1286. stb0899_write_reg(state, STB0899_AGCRFCFG, 0x11);
  1287. /* Run the search algorithm */
  1288. dprintk(state->verbose, FE_DEBUG, 1, "running DVB-S search algo ..");
  1289. if (stb0899_dvbs_algo(state) == RANGEOK) {
  1290. internal->lock = 1;
  1291. dprintk(state->verbose, FE_DEBUG, 1,
  1292. "-------------------------------------> DVB-S LOCK !");
  1293. // stb0899_write_reg(state, STB0899_ERRCTRL1, 0x3d); /* Viterbi Errors */
  1294. // internal->v_status = stb0899_read_reg(state, STB0899_VSTATUS);
  1295. // internal->err_ctrl = stb0899_read_reg(state, STB0899_ERRCTRL1);
  1296. // dprintk(state->verbose, FE_DEBUG, 1, "VSTATUS=0x%02x", internal->v_status);
  1297. // dprintk(state->verbose, FE_DEBUG, 1, "ERR_CTRL=0x%02x", internal->err_ctrl);
  1298. return DVBFE_ALGO_SEARCH_SUCCESS;
  1299. } else {
  1300. internal->lock = 0;
  1301. return DVBFE_ALGO_SEARCH_FAILED;
  1302. }
  1303. break;
  1304. case SYS_DVBS2:
  1305. internal->freq = i_params->freq;
  1306. internal->srate = i_params->srate;
  1307. internal->srch_range = SearchRange;
  1308. /* enable tuner I/O */
  1309. stb0899_i2c_gate_ctrl(&state->frontend, 1);
  1310. if (state->config->tuner_set_bandwidth)
  1311. state->config->tuner_set_bandwidth(fe, (stb0899_carr_width(state) + SearchRange));
  1312. if (state->config->tuner_get_bandwidth)
  1313. state->config->tuner_get_bandwidth(fe, &internal->tuner_bw);
  1314. /* disable tuner I/O */
  1315. stb0899_i2c_gate_ctrl(&state->frontend, 0);
  1316. // pParams->SpectralInv = pSearch->IQ_Inversion;
  1317. /* Set DVB-S2 AGC */
  1318. stb0899_write_reg(state, STB0899_AGCRFCFG, 0x1c);
  1319. /* Set IterScale =f(MCLK,SYMB) */
  1320. stb0899_set_iterations(state);
  1321. /* Run the search algorithm */
  1322. dprintk(state->verbose, FE_DEBUG, 1, "running DVB-S2 search algo ..");
  1323. if (stb0899_dvbs2_algo(state) == DVBS2_FEC_LOCK) {
  1324. internal->lock = 1;
  1325. dprintk(state->verbose, FE_DEBUG, 1,
  1326. "-------------------------------------> DVB-S2 LOCK !");
  1327. // stb0899_write_reg(state, STB0899_ERRCTRL1, 0xb6); /* Packet Errors */
  1328. // internal->v_status = stb0899_read_reg(state, STB0899_VSTATUS);
  1329. // internal->err_ctrl = stb0899_read_reg(state, STB0899_ERRCTRL1);
  1330. return DVBFE_ALGO_SEARCH_SUCCESS;
  1331. } else {
  1332. internal->lock = 0;
  1333. return DVBFE_ALGO_SEARCH_FAILED;
  1334. }
  1335. break;
  1336. default:
  1337. dprintk(state->verbose, FE_ERROR, 1, "Unsupported delivery system");
  1338. return DVBFE_ALGO_SEARCH_INVALID;
  1339. }
  1340. }
  1341. return DVBFE_ALGO_SEARCH_ERROR;
  1342. }
  1343. static int stb0899_get_frontend(struct dvb_frontend *fe)
  1344. {
  1345. struct dtv_frontend_properties *p = &fe->dtv_property_cache;
  1346. struct stb0899_state *state = fe->demodulator_priv;
  1347. struct stb0899_internal *internal = &state->internal;
  1348. dprintk(state->verbose, FE_DEBUG, 1, "Get params");
  1349. p->symbol_rate = internal->srate;
  1350. p->frequency = internal->freq;
  1351. return 0;
  1352. }
  1353. static enum dvbfe_algo stb0899_frontend_algo(struct dvb_frontend *fe)
  1354. {
  1355. return DVBFE_ALGO_CUSTOM;
  1356. }
  1357. static struct dvb_frontend_ops stb0899_ops = {
  1358. .delsys = { SYS_DVBS, SYS_DVBS2, SYS_DSS },
  1359. .info = {
  1360. .name = "STB0899 Multistandard",
  1361. .frequency_min = 950000,
  1362. .frequency_max = 2150000,
  1363. .frequency_stepsize = 0,
  1364. .frequency_tolerance = 0,
  1365. .symbol_rate_min = 5000000,
  1366. .symbol_rate_max = 45000000,
  1367. .caps = FE_CAN_INVERSION_AUTO |
  1368. FE_CAN_FEC_AUTO |
  1369. FE_CAN_2G_MODULATION |
  1370. FE_CAN_QPSK
  1371. },
  1372. .release = stb0899_release,
  1373. .init = stb0899_init,
  1374. .sleep = stb0899_sleep,
  1375. // .wakeup = stb0899_wakeup,
  1376. .i2c_gate_ctrl = stb0899_i2c_gate_ctrl,
  1377. .get_frontend_algo = stb0899_frontend_algo,
  1378. .search = stb0899_search,
  1379. .get_frontend = stb0899_get_frontend,
  1380. .read_status = stb0899_read_status,
  1381. .read_snr = stb0899_read_snr,
  1382. .read_signal_strength = stb0899_read_signal_strength,
  1383. .read_ber = stb0899_read_ber,
  1384. .set_voltage = stb0899_set_voltage,
  1385. .set_tone = stb0899_set_tone,
  1386. .diseqc_send_master_cmd = stb0899_send_diseqc_msg,
  1387. .diseqc_recv_slave_reply = stb0899_recv_slave_reply,
  1388. .diseqc_send_burst = stb0899_send_diseqc_burst,
  1389. };
  1390. struct dvb_frontend *stb0899_attach(struct stb0899_config *config, struct i2c_adapter *i2c)
  1391. {
  1392. struct stb0899_state *state = NULL;
  1393. state = kzalloc(sizeof (struct stb0899_state), GFP_KERNEL);
  1394. if (state == NULL)
  1395. goto error;
  1396. state->verbose = &verbose;
  1397. state->config = config;
  1398. state->i2c = i2c;
  1399. state->frontend.ops = stb0899_ops;
  1400. state->frontend.demodulator_priv = state;
  1401. /* use configured inversion as default -- we'll later autodetect inversion */
  1402. state->internal.inversion = config->inversion;
  1403. stb0899_wakeup(&state->frontend);
  1404. if (stb0899_get_dev_id(state) == -ENODEV) {
  1405. printk("%s: Exiting .. !\n", __func__);
  1406. goto error;
  1407. }
  1408. printk("%s: Attaching STB0899 \n", __func__);
  1409. return &state->frontend;
  1410. error:
  1411. kfree(state);
  1412. return NULL;
  1413. }
  1414. EXPORT_SYMBOL(stb0899_attach);
  1415. MODULE_PARM_DESC(verbose, "Set Verbosity level");
  1416. MODULE_AUTHOR("Manu Abraham");
  1417. MODULE_DESCRIPTION("STB0899 Multi-Std frontend");
  1418. MODULE_LICENSE("GPL");