stv0910.c 50 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Driver for the ST STV0910 DVB-S/S2 demodulator.
  4. *
  5. * Copyright (C) 2014-2015 Ralph Metzler <[email protected]>
  6. * Marcus Metzler <[email protected]>
  7. * developed for Digital Devices GmbH
  8. */
  9. #include <linux/kernel.h>
  10. #include <linux/module.h>
  11. #include <linux/moduleparam.h>
  12. #include <linux/init.h>
  13. #include <linux/delay.h>
  14. #include <linux/firmware.h>
  15. #include <linux/i2c.h>
  16. #include <asm/div64.h>
  17. #include <media/dvb_frontend.h>
  18. #include "stv0910.h"
  19. #include "stv0910_regs.h"
  20. #define EXT_CLOCK 30000000
  21. #define TUNING_DELAY 200
  22. #define BER_SRC_S 0x20
  23. #define BER_SRC_S2 0x20
  24. static LIST_HEAD(stvlist);
  25. enum receive_mode { RCVMODE_NONE, RCVMODE_DVBS, RCVMODE_DVBS2, RCVMODE_AUTO };
  26. enum dvbs2_fectype { DVBS2_64K, DVBS2_16K };
  27. enum dvbs2_mod_cod {
  28. DVBS2_DUMMY_PLF, DVBS2_QPSK_1_4, DVBS2_QPSK_1_3, DVBS2_QPSK_2_5,
  29. DVBS2_QPSK_1_2, DVBS2_QPSK_3_5, DVBS2_QPSK_2_3, DVBS2_QPSK_3_4,
  30. DVBS2_QPSK_4_5, DVBS2_QPSK_5_6, DVBS2_QPSK_8_9, DVBS2_QPSK_9_10,
  31. DVBS2_8PSK_3_5, DVBS2_8PSK_2_3, DVBS2_8PSK_3_4, DVBS2_8PSK_5_6,
  32. DVBS2_8PSK_8_9, DVBS2_8PSK_9_10, DVBS2_16APSK_2_3, DVBS2_16APSK_3_4,
  33. DVBS2_16APSK_4_5, DVBS2_16APSK_5_6, DVBS2_16APSK_8_9, DVBS2_16APSK_9_10,
  34. DVBS2_32APSK_3_4, DVBS2_32APSK_4_5, DVBS2_32APSK_5_6, DVBS2_32APSK_8_9,
  35. DVBS2_32APSK_9_10
  36. };
  37. enum fe_stv0910_mod_cod {
  38. FE_DUMMY_PLF, FE_QPSK_14, FE_QPSK_13, FE_QPSK_25,
  39. FE_QPSK_12, FE_QPSK_35, FE_QPSK_23, FE_QPSK_34,
  40. FE_QPSK_45, FE_QPSK_56, FE_QPSK_89, FE_QPSK_910,
  41. FE_8PSK_35, FE_8PSK_23, FE_8PSK_34, FE_8PSK_56,
  42. FE_8PSK_89, FE_8PSK_910, FE_16APSK_23, FE_16APSK_34,
  43. FE_16APSK_45, FE_16APSK_56, FE_16APSK_89, FE_16APSK_910,
  44. FE_32APSK_34, FE_32APSK_45, FE_32APSK_56, FE_32APSK_89,
  45. FE_32APSK_910
  46. };
  47. enum fe_stv0910_roll_off { FE_SAT_35, FE_SAT_25, FE_SAT_20, FE_SAT_15 };
  48. static inline u32 muldiv32(u32 a, u32 b, u32 c)
  49. {
  50. u64 tmp64;
  51. tmp64 = (u64)a * (u64)b;
  52. do_div(tmp64, c);
  53. return (u32)tmp64;
  54. }
  55. struct stv_base {
  56. struct list_head stvlist;
  57. u8 adr;
  58. struct i2c_adapter *i2c;
  59. struct mutex i2c_lock; /* shared I2C access protect */
  60. struct mutex reg_lock; /* shared register write protect */
  61. int count;
  62. u32 extclk;
  63. u32 mclk;
  64. };
  65. struct stv {
  66. struct stv_base *base;
  67. struct dvb_frontend fe;
  68. int nr;
  69. u16 regoff;
  70. u8 i2crpt;
  71. u8 tscfgh;
  72. u8 tsgeneral;
  73. u8 tsspeed;
  74. u8 single;
  75. unsigned long tune_time;
  76. s32 search_range;
  77. u32 started;
  78. u32 demod_lock_time;
  79. enum receive_mode receive_mode;
  80. u32 demod_timeout;
  81. u32 fec_timeout;
  82. u32 first_time_lock;
  83. u8 demod_bits;
  84. u32 symbol_rate;
  85. u8 last_viterbi_rate;
  86. enum fe_code_rate puncture_rate;
  87. enum fe_stv0910_mod_cod mod_cod;
  88. enum dvbs2_fectype fectype;
  89. u32 pilots;
  90. enum fe_stv0910_roll_off feroll_off;
  91. int is_standard_broadcast;
  92. int is_vcm;
  93. u32 cur_scrambling_code;
  94. u32 last_bernumerator;
  95. u32 last_berdenominator;
  96. u8 berscale;
  97. u8 vth[6];
  98. };
  99. struct sinit_table {
  100. u16 address;
  101. u8 data;
  102. };
  103. struct slookup {
  104. s16 value;
  105. u32 reg_value;
  106. };
  107. static int write_reg(struct stv *state, u16 reg, u8 val)
  108. {
  109. struct i2c_adapter *adap = state->base->i2c;
  110. u8 data[3] = {reg >> 8, reg & 0xff, val};
  111. struct i2c_msg msg = {.addr = state->base->adr, .flags = 0,
  112. .buf = data, .len = 3};
  113. if (i2c_transfer(adap, &msg, 1) != 1) {
  114. dev_warn(&adap->dev, "i2c write error ([%02x] %04x: %02x)\n",
  115. state->base->adr, reg, val);
  116. return -EIO;
  117. }
  118. return 0;
  119. }
  120. static inline int i2c_read_regs16(struct i2c_adapter *adapter, u8 adr,
  121. u16 reg, u8 *val, int count)
  122. {
  123. u8 msg[2] = {reg >> 8, reg & 0xff};
  124. struct i2c_msg msgs[2] = {{.addr = adr, .flags = 0,
  125. .buf = msg, .len = 2},
  126. {.addr = adr, .flags = I2C_M_RD,
  127. .buf = val, .len = count } };
  128. if (i2c_transfer(adapter, msgs, 2) != 2) {
  129. dev_warn(&adapter->dev, "i2c read error ([%02x] %04x)\n",
  130. adr, reg);
  131. return -EIO;
  132. }
  133. return 0;
  134. }
  135. static int read_reg(struct stv *state, u16 reg, u8 *val)
  136. {
  137. return i2c_read_regs16(state->base->i2c, state->base->adr,
  138. reg, val, 1);
  139. }
  140. static int read_regs(struct stv *state, u16 reg, u8 *val, int len)
  141. {
  142. return i2c_read_regs16(state->base->i2c, state->base->adr,
  143. reg, val, len);
  144. }
  145. static int write_shared_reg(struct stv *state, u16 reg, u8 mask, u8 val)
  146. {
  147. int status;
  148. u8 tmp;
  149. mutex_lock(&state->base->reg_lock);
  150. status = read_reg(state, reg, &tmp);
  151. if (!status)
  152. status = write_reg(state, reg, (tmp & ~mask) | (val & mask));
  153. mutex_unlock(&state->base->reg_lock);
  154. return status;
  155. }
  156. static int write_field(struct stv *state, u32 field, u8 val)
  157. {
  158. int status;
  159. u8 shift, mask, old, new;
  160. status = read_reg(state, field >> 16, &old);
  161. if (status)
  162. return status;
  163. mask = field & 0xff;
  164. shift = (field >> 12) & 0xf;
  165. new = ((val << shift) & mask) | (old & ~mask);
  166. if (new == old)
  167. return 0;
  168. return write_reg(state, field >> 16, new);
  169. }
  170. #define SET_FIELD(_reg, _val) \
  171. write_field(state, state->nr ? FSTV0910_P2_##_reg : \
  172. FSTV0910_P1_##_reg, _val)
  173. #define SET_REG(_reg, _val) \
  174. write_reg(state, state->nr ? RSTV0910_P2_##_reg : \
  175. RSTV0910_P1_##_reg, _val)
  176. #define GET_REG(_reg, _val) \
  177. read_reg(state, state->nr ? RSTV0910_P2_##_reg : \
  178. RSTV0910_P1_##_reg, _val)
  179. static const struct slookup s1_sn_lookup[] = {
  180. { 0, 9242 }, /* C/N= 0dB */
  181. { 5, 9105 }, /* C/N= 0.5dB */
  182. { 10, 8950 }, /* C/N= 1.0dB */
  183. { 15, 8780 }, /* C/N= 1.5dB */
  184. { 20, 8566 }, /* C/N= 2.0dB */
  185. { 25, 8366 }, /* C/N= 2.5dB */
  186. { 30, 8146 }, /* C/N= 3.0dB */
  187. { 35, 7908 }, /* C/N= 3.5dB */
  188. { 40, 7666 }, /* C/N= 4.0dB */
  189. { 45, 7405 }, /* C/N= 4.5dB */
  190. { 50, 7136 }, /* C/N= 5.0dB */
  191. { 55, 6861 }, /* C/N= 5.5dB */
  192. { 60, 6576 }, /* C/N= 6.0dB */
  193. { 65, 6330 }, /* C/N= 6.5dB */
  194. { 70, 6048 }, /* C/N= 7.0dB */
  195. { 75, 5768 }, /* C/N= 7.5dB */
  196. { 80, 5492 }, /* C/N= 8.0dB */
  197. { 85, 5224 }, /* C/N= 8.5dB */
  198. { 90, 4959 }, /* C/N= 9.0dB */
  199. { 95, 4709 }, /* C/N= 9.5dB */
  200. { 100, 4467 }, /* C/N=10.0dB */
  201. { 105, 4236 }, /* C/N=10.5dB */
  202. { 110, 4013 }, /* C/N=11.0dB */
  203. { 115, 3800 }, /* C/N=11.5dB */
  204. { 120, 3598 }, /* C/N=12.0dB */
  205. { 125, 3406 }, /* C/N=12.5dB */
  206. { 130, 3225 }, /* C/N=13.0dB */
  207. { 135, 3052 }, /* C/N=13.5dB */
  208. { 140, 2889 }, /* C/N=14.0dB */
  209. { 145, 2733 }, /* C/N=14.5dB */
  210. { 150, 2587 }, /* C/N=15.0dB */
  211. { 160, 2318 }, /* C/N=16.0dB */
  212. { 170, 2077 }, /* C/N=17.0dB */
  213. { 180, 1862 }, /* C/N=18.0dB */
  214. { 190, 1670 }, /* C/N=19.0dB */
  215. { 200, 1499 }, /* C/N=20.0dB */
  216. { 210, 1347 }, /* C/N=21.0dB */
  217. { 220, 1213 }, /* C/N=22.0dB */
  218. { 230, 1095 }, /* C/N=23.0dB */
  219. { 240, 992 }, /* C/N=24.0dB */
  220. { 250, 900 }, /* C/N=25.0dB */
  221. { 260, 826 }, /* C/N=26.0dB */
  222. { 270, 758 }, /* C/N=27.0dB */
  223. { 280, 702 }, /* C/N=28.0dB */
  224. { 290, 653 }, /* C/N=29.0dB */
  225. { 300, 613 }, /* C/N=30.0dB */
  226. { 310, 579 }, /* C/N=31.0dB */
  227. { 320, 550 }, /* C/N=32.0dB */
  228. { 330, 526 }, /* C/N=33.0dB */
  229. { 350, 490 }, /* C/N=33.0dB */
  230. { 400, 445 }, /* C/N=40.0dB */
  231. { 450, 430 }, /* C/N=45.0dB */
  232. { 500, 426 }, /* C/N=50.0dB */
  233. { 510, 425 } /* C/N=51.0dB */
  234. };
  235. static const struct slookup s2_sn_lookup[] = {
  236. { -30, 13950 }, /* C/N=-2.5dB */
  237. { -25, 13580 }, /* C/N=-2.5dB */
  238. { -20, 13150 }, /* C/N=-2.0dB */
  239. { -15, 12760 }, /* C/N=-1.5dB */
  240. { -10, 12345 }, /* C/N=-1.0dB */
  241. { -5, 11900 }, /* C/N=-0.5dB */
  242. { 0, 11520 }, /* C/N= 0dB */
  243. { 5, 11080 }, /* C/N= 0.5dB */
  244. { 10, 10630 }, /* C/N= 1.0dB */
  245. { 15, 10210 }, /* C/N= 1.5dB */
  246. { 20, 9790 }, /* C/N= 2.0dB */
  247. { 25, 9390 }, /* C/N= 2.5dB */
  248. { 30, 8970 }, /* C/N= 3.0dB */
  249. { 35, 8575 }, /* C/N= 3.5dB */
  250. { 40, 8180 }, /* C/N= 4.0dB */
  251. { 45, 7800 }, /* C/N= 4.5dB */
  252. { 50, 7430 }, /* C/N= 5.0dB */
  253. { 55, 7080 }, /* C/N= 5.5dB */
  254. { 60, 6720 }, /* C/N= 6.0dB */
  255. { 65, 6320 }, /* C/N= 6.5dB */
  256. { 70, 6060 }, /* C/N= 7.0dB */
  257. { 75, 5760 }, /* C/N= 7.5dB */
  258. { 80, 5480 }, /* C/N= 8.0dB */
  259. { 85, 5200 }, /* C/N= 8.5dB */
  260. { 90, 4930 }, /* C/N= 9.0dB */
  261. { 95, 4680 }, /* C/N= 9.5dB */
  262. { 100, 4425 }, /* C/N=10.0dB */
  263. { 105, 4210 }, /* C/N=10.5dB */
  264. { 110, 3980 }, /* C/N=11.0dB */
  265. { 115, 3765 }, /* C/N=11.5dB */
  266. { 120, 3570 }, /* C/N=12.0dB */
  267. { 125, 3315 }, /* C/N=12.5dB */
  268. { 130, 3140 }, /* C/N=13.0dB */
  269. { 135, 2980 }, /* C/N=13.5dB */
  270. { 140, 2820 }, /* C/N=14.0dB */
  271. { 145, 2670 }, /* C/N=14.5dB */
  272. { 150, 2535 }, /* C/N=15.0dB */
  273. { 160, 2270 }, /* C/N=16.0dB */
  274. { 170, 2035 }, /* C/N=17.0dB */
  275. { 180, 1825 }, /* C/N=18.0dB */
  276. { 190, 1650 }, /* C/N=19.0dB */
  277. { 200, 1485 }, /* C/N=20.0dB */
  278. { 210, 1340 }, /* C/N=21.0dB */
  279. { 220, 1212 }, /* C/N=22.0dB */
  280. { 230, 1100 }, /* C/N=23.0dB */
  281. { 240, 1000 }, /* C/N=24.0dB */
  282. { 250, 910 }, /* C/N=25.0dB */
  283. { 260, 836 }, /* C/N=26.0dB */
  284. { 270, 772 }, /* C/N=27.0dB */
  285. { 280, 718 }, /* C/N=28.0dB */
  286. { 290, 671 }, /* C/N=29.0dB */
  287. { 300, 635 }, /* C/N=30.0dB */
  288. { 310, 602 }, /* C/N=31.0dB */
  289. { 320, 575 }, /* C/N=32.0dB */
  290. { 330, 550 }, /* C/N=33.0dB */
  291. { 350, 517 }, /* C/N=35.0dB */
  292. { 400, 480 }, /* C/N=40.0dB */
  293. { 450, 466 }, /* C/N=45.0dB */
  294. { 500, 464 }, /* C/N=50.0dB */
  295. { 510, 463 }, /* C/N=51.0dB */
  296. };
  297. static const struct slookup padc_lookup[] = {
  298. { 0, 118000 }, /* PADC= +0dBm */
  299. { -100, 93600 }, /* PADC= -1dBm */
  300. { -200, 74500 }, /* PADC= -2dBm */
  301. { -300, 59100 }, /* PADC= -3dBm */
  302. { -400, 47000 }, /* PADC= -4dBm */
  303. { -500, 37300 }, /* PADC= -5dBm */
  304. { -600, 29650 }, /* PADC= -6dBm */
  305. { -700, 23520 }, /* PADC= -7dBm */
  306. { -900, 14850 }, /* PADC= -9dBm */
  307. { -1100, 9380 }, /* PADC=-11dBm */
  308. { -1300, 5910 }, /* PADC=-13dBm */
  309. { -1500, 3730 }, /* PADC=-15dBm */
  310. { -1700, 2354 }, /* PADC=-17dBm */
  311. { -1900, 1485 }, /* PADC=-19dBm */
  312. { -2000, 1179 }, /* PADC=-20dBm */
  313. { -2100, 1000 }, /* PADC=-21dBm */
  314. };
  315. /*********************************************************************
  316. * Tracking carrier loop carrier QPSK 1/4 to 8PSK 9/10 long Frame
  317. *********************************************************************/
  318. static const u8 s2car_loop[] = {
  319. /*
  320. * Modcod 2MPon 2MPoff 5MPon 5MPoff 10MPon 10MPoff
  321. * 20MPon 20MPoff 30MPon 30MPoff
  322. */
  323. /* FE_QPSK_14 */
  324. 0x0C, 0x3C, 0x0B, 0x3C, 0x2A, 0x2C, 0x2A, 0x1C, 0x3A, 0x3B,
  325. /* FE_QPSK_13 */
  326. 0x0C, 0x3C, 0x0B, 0x3C, 0x2A, 0x2C, 0x3A, 0x0C, 0x3A, 0x2B,
  327. /* FE_QPSK_25 */
  328. 0x1C, 0x3C, 0x1B, 0x3C, 0x3A, 0x1C, 0x3A, 0x3B, 0x3A, 0x2B,
  329. /* FE_QPSK_12 */
  330. 0x0C, 0x1C, 0x2B, 0x1C, 0x0B, 0x2C, 0x0B, 0x0C, 0x2A, 0x2B,
  331. /* FE_QPSK_35 */
  332. 0x1C, 0x1C, 0x2B, 0x1C, 0x0B, 0x2C, 0x0B, 0x0C, 0x2A, 0x2B,
  333. /* FE_QPSK_23 */
  334. 0x2C, 0x2C, 0x2B, 0x1C, 0x0B, 0x2C, 0x0B, 0x0C, 0x2A, 0x2B,
  335. /* FE_QPSK_34 */
  336. 0x3C, 0x2C, 0x3B, 0x2C, 0x1B, 0x1C, 0x1B, 0x3B, 0x3A, 0x1B,
  337. /* FE_QPSK_45 */
  338. 0x0D, 0x3C, 0x3B, 0x2C, 0x1B, 0x1C, 0x1B, 0x3B, 0x3A, 0x1B,
  339. /* FE_QPSK_56 */
  340. 0x1D, 0x3C, 0x0C, 0x2C, 0x2B, 0x1C, 0x1B, 0x3B, 0x0B, 0x1B,
  341. /* FE_QPSK_89 */
  342. 0x3D, 0x0D, 0x0C, 0x2C, 0x2B, 0x0C, 0x2B, 0x2B, 0x0B, 0x0B,
  343. /* FE_QPSK_910 */
  344. 0x1E, 0x0D, 0x1C, 0x2C, 0x3B, 0x0C, 0x2B, 0x2B, 0x1B, 0x0B,
  345. /* FE_8PSK_35 */
  346. 0x28, 0x09, 0x28, 0x09, 0x28, 0x09, 0x28, 0x08, 0x28, 0x27,
  347. /* FE_8PSK_23 */
  348. 0x19, 0x29, 0x19, 0x29, 0x19, 0x29, 0x38, 0x19, 0x28, 0x09,
  349. /* FE_8PSK_34 */
  350. 0x1A, 0x0B, 0x1A, 0x3A, 0x0A, 0x2A, 0x39, 0x2A, 0x39, 0x1A,
  351. /* FE_8PSK_56 */
  352. 0x2B, 0x2B, 0x1B, 0x1B, 0x0B, 0x1B, 0x1A, 0x0B, 0x1A, 0x1A,
  353. /* FE_8PSK_89 */
  354. 0x0C, 0x0C, 0x3B, 0x3B, 0x1B, 0x1B, 0x2A, 0x0B, 0x2A, 0x2A,
  355. /* FE_8PSK_910 */
  356. 0x0C, 0x1C, 0x0C, 0x3B, 0x2B, 0x1B, 0x3A, 0x0B, 0x2A, 0x2A,
  357. /**********************************************************************
  358. * Tracking carrier loop carrier 16APSK 2/3 to 32APSK 9/10 long Frame
  359. **********************************************************************/
  360. /*
  361. * Modcod 2MPon 2MPoff 5MPon 5MPoff 10MPon 10MPoff 20MPon
  362. * 20MPoff 30MPon 30MPoff
  363. */
  364. /* FE_16APSK_23 */
  365. 0x0A, 0x0A, 0x0A, 0x0A, 0x1A, 0x0A, 0x39, 0x0A, 0x29, 0x0A,
  366. /* FE_16APSK_34 */
  367. 0x0A, 0x0A, 0x0A, 0x0A, 0x0B, 0x0A, 0x2A, 0x0A, 0x1A, 0x0A,
  368. /* FE_16APSK_45 */
  369. 0x0A, 0x0A, 0x0A, 0x0A, 0x1B, 0x0A, 0x3A, 0x0A, 0x2A, 0x0A,
  370. /* FE_16APSK_56 */
  371. 0x0A, 0x0A, 0x0A, 0x0A, 0x1B, 0x0A, 0x3A, 0x0A, 0x2A, 0x0A,
  372. /* FE_16APSK_89 */
  373. 0x0A, 0x0A, 0x0A, 0x0A, 0x2B, 0x0A, 0x0B, 0x0A, 0x3A, 0x0A,
  374. /* FE_16APSK_910 */
  375. 0x0A, 0x0A, 0x0A, 0x0A, 0x2B, 0x0A, 0x0B, 0x0A, 0x3A, 0x0A,
  376. /* FE_32APSK_34 */
  377. 0x09, 0x09, 0x09, 0x09, 0x09, 0x09, 0x09, 0x09, 0x09, 0x09,
  378. /* FE_32APSK_45 */
  379. 0x09, 0x09, 0x09, 0x09, 0x09, 0x09, 0x09, 0x09, 0x09, 0x09,
  380. /* FE_32APSK_56 */
  381. 0x09, 0x09, 0x09, 0x09, 0x09, 0x09, 0x09, 0x09, 0x09, 0x09,
  382. /* FE_32APSK_89 */
  383. 0x09, 0x09, 0x09, 0x09, 0x09, 0x09, 0x09, 0x09, 0x09, 0x09,
  384. /* FE_32APSK_910 */
  385. 0x09, 0x09, 0x09, 0x09, 0x09, 0x09, 0x09, 0x09, 0x09, 0x09,
  386. };
  387. static u8 get_optim_cloop(struct stv *state,
  388. enum fe_stv0910_mod_cod mod_cod, u32 pilots)
  389. {
  390. int i = 0;
  391. if (mod_cod >= FE_32APSK_910)
  392. i = ((int)FE_32APSK_910 - (int)FE_QPSK_14) * 10;
  393. else if (mod_cod >= FE_QPSK_14)
  394. i = ((int)mod_cod - (int)FE_QPSK_14) * 10;
  395. if (state->symbol_rate <= 3000000)
  396. i += 0;
  397. else if (state->symbol_rate <= 7000000)
  398. i += 2;
  399. else if (state->symbol_rate <= 15000000)
  400. i += 4;
  401. else if (state->symbol_rate <= 25000000)
  402. i += 6;
  403. else
  404. i += 8;
  405. if (!pilots)
  406. i += 1;
  407. return s2car_loop[i];
  408. }
  409. static int get_cur_symbol_rate(struct stv *state, u32 *p_symbol_rate)
  410. {
  411. int status = 0;
  412. u8 symb_freq0;
  413. u8 symb_freq1;
  414. u8 symb_freq2;
  415. u8 symb_freq3;
  416. u8 tim_offs0;
  417. u8 tim_offs1;
  418. u8 tim_offs2;
  419. u32 symbol_rate;
  420. s32 timing_offset;
  421. *p_symbol_rate = 0;
  422. if (!state->started)
  423. return status;
  424. read_reg(state, RSTV0910_P2_SFR3 + state->regoff, &symb_freq3);
  425. read_reg(state, RSTV0910_P2_SFR2 + state->regoff, &symb_freq2);
  426. read_reg(state, RSTV0910_P2_SFR1 + state->regoff, &symb_freq1);
  427. read_reg(state, RSTV0910_P2_SFR0 + state->regoff, &symb_freq0);
  428. read_reg(state, RSTV0910_P2_TMGREG2 + state->regoff, &tim_offs2);
  429. read_reg(state, RSTV0910_P2_TMGREG1 + state->regoff, &tim_offs1);
  430. read_reg(state, RSTV0910_P2_TMGREG0 + state->regoff, &tim_offs0);
  431. symbol_rate = ((u32)symb_freq3 << 24) | ((u32)symb_freq2 << 16) |
  432. ((u32)symb_freq1 << 8) | (u32)symb_freq0;
  433. timing_offset = ((u32)tim_offs2 << 16) | ((u32)tim_offs1 << 8) |
  434. (u32)tim_offs0;
  435. if ((timing_offset & (1 << 23)) != 0)
  436. timing_offset |= 0xFF000000; /* Sign extent */
  437. symbol_rate = (u32)(((u64)symbol_rate * state->base->mclk) >> 32);
  438. timing_offset = (s32)(((s64)symbol_rate * (s64)timing_offset) >> 29);
  439. *p_symbol_rate = symbol_rate + timing_offset;
  440. return 0;
  441. }
  442. static int get_signal_parameters(struct stv *state)
  443. {
  444. u8 tmp;
  445. if (!state->started)
  446. return -EINVAL;
  447. if (state->receive_mode == RCVMODE_DVBS2) {
  448. read_reg(state, RSTV0910_P2_DMDMODCOD + state->regoff, &tmp);
  449. state->mod_cod = (enum fe_stv0910_mod_cod)((tmp & 0x7c) >> 2);
  450. state->pilots = (tmp & 0x01) != 0;
  451. state->fectype = (enum dvbs2_fectype)((tmp & 0x02) >> 1);
  452. } else if (state->receive_mode == RCVMODE_DVBS) {
  453. read_reg(state, RSTV0910_P2_VITCURPUN + state->regoff, &tmp);
  454. state->puncture_rate = FEC_NONE;
  455. switch (tmp & 0x1F) {
  456. case 0x0d:
  457. state->puncture_rate = FEC_1_2;
  458. break;
  459. case 0x12:
  460. state->puncture_rate = FEC_2_3;
  461. break;
  462. case 0x15:
  463. state->puncture_rate = FEC_3_4;
  464. break;
  465. case 0x18:
  466. state->puncture_rate = FEC_5_6;
  467. break;
  468. case 0x1a:
  469. state->puncture_rate = FEC_7_8;
  470. break;
  471. }
  472. state->is_vcm = 0;
  473. state->is_standard_broadcast = 1;
  474. state->feroll_off = FE_SAT_35;
  475. }
  476. return 0;
  477. }
  478. static int tracking_optimization(struct stv *state)
  479. {
  480. u8 tmp;
  481. read_reg(state, RSTV0910_P2_DMDCFGMD + state->regoff, &tmp);
  482. tmp &= ~0xC0;
  483. switch (state->receive_mode) {
  484. case RCVMODE_DVBS:
  485. tmp |= 0x40;
  486. break;
  487. case RCVMODE_DVBS2:
  488. tmp |= 0x80;
  489. break;
  490. default:
  491. tmp |= 0xC0;
  492. break;
  493. }
  494. write_reg(state, RSTV0910_P2_DMDCFGMD + state->regoff, tmp);
  495. if (state->receive_mode == RCVMODE_DVBS2) {
  496. /* Disable Reed-Solomon */
  497. write_shared_reg(state,
  498. RSTV0910_TSTTSRS, state->nr ? 0x02 : 0x01,
  499. 0x03);
  500. if (state->fectype == DVBS2_64K) {
  501. u8 aclc = get_optim_cloop(state, state->mod_cod,
  502. state->pilots);
  503. if (state->mod_cod <= FE_QPSK_910) {
  504. write_reg(state, RSTV0910_P2_ACLC2S2Q +
  505. state->regoff, aclc);
  506. } else if (state->mod_cod <= FE_8PSK_910) {
  507. write_reg(state, RSTV0910_P2_ACLC2S2Q +
  508. state->regoff, 0x2a);
  509. write_reg(state, RSTV0910_P2_ACLC2S28 +
  510. state->regoff, aclc);
  511. } else if (state->mod_cod <= FE_16APSK_910) {
  512. write_reg(state, RSTV0910_P2_ACLC2S2Q +
  513. state->regoff, 0x2a);
  514. write_reg(state, RSTV0910_P2_ACLC2S216A +
  515. state->regoff, aclc);
  516. } else if (state->mod_cod <= FE_32APSK_910) {
  517. write_reg(state, RSTV0910_P2_ACLC2S2Q +
  518. state->regoff, 0x2a);
  519. write_reg(state, RSTV0910_P2_ACLC2S232A +
  520. state->regoff, aclc);
  521. }
  522. }
  523. }
  524. return 0;
  525. }
  526. static s32 table_lookup(const struct slookup *table,
  527. int table_size, u32 reg_value)
  528. {
  529. s32 value;
  530. int imin = 0;
  531. int imax = table_size - 1;
  532. int i;
  533. s32 reg_diff;
  534. /* Assumes Table[0].RegValue > Table[imax].RegValue */
  535. if (reg_value >= table[0].reg_value) {
  536. value = table[0].value;
  537. } else if (reg_value <= table[imax].reg_value) {
  538. value = table[imax].value;
  539. } else {
  540. while ((imax - imin) > 1) {
  541. i = (imax + imin) / 2;
  542. if ((table[imin].reg_value >= reg_value) &&
  543. (reg_value >= table[i].reg_value))
  544. imax = i;
  545. else
  546. imin = i;
  547. }
  548. reg_diff = table[imax].reg_value - table[imin].reg_value;
  549. value = table[imin].value;
  550. if (reg_diff != 0)
  551. value += ((s32)(reg_value - table[imin].reg_value) *
  552. (s32)(table[imax].value
  553. - table[imin].value))
  554. / (reg_diff);
  555. }
  556. return value;
  557. }
  558. static int get_signal_to_noise(struct stv *state, s32 *signal_to_noise)
  559. {
  560. u8 data0;
  561. u8 data1;
  562. u16 data;
  563. int n_lookup;
  564. const struct slookup *lookup;
  565. *signal_to_noise = 0;
  566. if (!state->started)
  567. return -EINVAL;
  568. if (state->receive_mode == RCVMODE_DVBS2) {
  569. read_reg(state, RSTV0910_P2_NNOSPLHT1 + state->regoff,
  570. &data1);
  571. read_reg(state, RSTV0910_P2_NNOSPLHT0 + state->regoff,
  572. &data0);
  573. n_lookup = ARRAY_SIZE(s2_sn_lookup);
  574. lookup = s2_sn_lookup;
  575. } else {
  576. read_reg(state, RSTV0910_P2_NNOSDATAT1 + state->regoff,
  577. &data1);
  578. read_reg(state, RSTV0910_P2_NNOSDATAT0 + state->regoff,
  579. &data0);
  580. n_lookup = ARRAY_SIZE(s1_sn_lookup);
  581. lookup = s1_sn_lookup;
  582. }
  583. data = (((u16)data1) << 8) | (u16)data0;
  584. *signal_to_noise = table_lookup(lookup, n_lookup, data);
  585. return 0;
  586. }
  587. static int get_bit_error_rate_s(struct stv *state, u32 *bernumerator,
  588. u32 *berdenominator)
  589. {
  590. u8 regs[3];
  591. int status = read_regs(state,
  592. RSTV0910_P2_ERRCNT12 + state->regoff,
  593. regs, 3);
  594. if (status)
  595. return -EINVAL;
  596. if ((regs[0] & 0x80) == 0) {
  597. state->last_berdenominator = 1ULL << ((state->berscale * 2) +
  598. 10 + 3);
  599. state->last_bernumerator = ((u32)(regs[0] & 0x7F) << 16) |
  600. ((u32)regs[1] << 8) | regs[2];
  601. if (state->last_bernumerator < 256 && state->berscale < 6) {
  602. state->berscale += 1;
  603. status = write_reg(state, RSTV0910_P2_ERRCTRL1 +
  604. state->regoff,
  605. 0x20 | state->berscale);
  606. } else if (state->last_bernumerator > 1024 &&
  607. state->berscale > 2) {
  608. state->berscale -= 1;
  609. status = write_reg(state, RSTV0910_P2_ERRCTRL1 +
  610. state->regoff, 0x20 |
  611. state->berscale);
  612. }
  613. }
  614. *bernumerator = state->last_bernumerator;
  615. *berdenominator = state->last_berdenominator;
  616. return 0;
  617. }
  618. static u32 dvbs2_nbch(enum dvbs2_mod_cod mod_cod, enum dvbs2_fectype fectype)
  619. {
  620. static const u32 nbch[][2] = {
  621. { 0, 0}, /* DUMMY_PLF */
  622. {16200, 3240}, /* QPSK_1_4, */
  623. {21600, 5400}, /* QPSK_1_3, */
  624. {25920, 6480}, /* QPSK_2_5, */
  625. {32400, 7200}, /* QPSK_1_2, */
  626. {38880, 9720}, /* QPSK_3_5, */
  627. {43200, 10800}, /* QPSK_2_3, */
  628. {48600, 11880}, /* QPSK_3_4, */
  629. {51840, 12600}, /* QPSK_4_5, */
  630. {54000, 13320}, /* QPSK_5_6, */
  631. {57600, 14400}, /* QPSK_8_9, */
  632. {58320, 16000}, /* QPSK_9_10, */
  633. {43200, 9720}, /* 8PSK_3_5, */
  634. {48600, 10800}, /* 8PSK_2_3, */
  635. {51840, 11880}, /* 8PSK_3_4, */
  636. {54000, 13320}, /* 8PSK_5_6, */
  637. {57600, 14400}, /* 8PSK_8_9, */
  638. {58320, 16000}, /* 8PSK_9_10, */
  639. {43200, 10800}, /* 16APSK_2_3, */
  640. {48600, 11880}, /* 16APSK_3_4, */
  641. {51840, 12600}, /* 16APSK_4_5, */
  642. {54000, 13320}, /* 16APSK_5_6, */
  643. {57600, 14400}, /* 16APSK_8_9, */
  644. {58320, 16000}, /* 16APSK_9_10 */
  645. {48600, 11880}, /* 32APSK_3_4, */
  646. {51840, 12600}, /* 32APSK_4_5, */
  647. {54000, 13320}, /* 32APSK_5_6, */
  648. {57600, 14400}, /* 32APSK_8_9, */
  649. {58320, 16000}, /* 32APSK_9_10 */
  650. };
  651. if (mod_cod >= DVBS2_QPSK_1_4 &&
  652. mod_cod <= DVBS2_32APSK_9_10 && fectype <= DVBS2_16K)
  653. return nbch[mod_cod][fectype];
  654. return 64800;
  655. }
  656. static int get_bit_error_rate_s2(struct stv *state, u32 *bernumerator,
  657. u32 *berdenominator)
  658. {
  659. u8 regs[3];
  660. int status = read_regs(state, RSTV0910_P2_ERRCNT12 + state->regoff,
  661. regs, 3);
  662. if (status)
  663. return -EINVAL;
  664. if ((regs[0] & 0x80) == 0) {
  665. state->last_berdenominator =
  666. dvbs2_nbch((enum dvbs2_mod_cod)state->mod_cod,
  667. state->fectype) <<
  668. (state->berscale * 2);
  669. state->last_bernumerator = (((u32)regs[0] & 0x7F) << 16) |
  670. ((u32)regs[1] << 8) | regs[2];
  671. if (state->last_bernumerator < 256 && state->berscale < 6) {
  672. state->berscale += 1;
  673. write_reg(state, RSTV0910_P2_ERRCTRL1 + state->regoff,
  674. 0x20 | state->berscale);
  675. } else if (state->last_bernumerator > 1024 &&
  676. state->berscale > 2) {
  677. state->berscale -= 1;
  678. write_reg(state, RSTV0910_P2_ERRCTRL1 + state->regoff,
  679. 0x20 | state->berscale);
  680. }
  681. }
  682. *bernumerator = state->last_bernumerator;
  683. *berdenominator = state->last_berdenominator;
  684. return status;
  685. }
  686. static int get_bit_error_rate(struct stv *state, u32 *bernumerator,
  687. u32 *berdenominator)
  688. {
  689. *bernumerator = 0;
  690. *berdenominator = 1;
  691. switch (state->receive_mode) {
  692. case RCVMODE_DVBS:
  693. return get_bit_error_rate_s(state,
  694. bernumerator, berdenominator);
  695. case RCVMODE_DVBS2:
  696. return get_bit_error_rate_s2(state,
  697. bernumerator, berdenominator);
  698. default:
  699. break;
  700. }
  701. return 0;
  702. }
  703. static int set_mclock(struct stv *state, u32 master_clock)
  704. {
  705. u32 idf = 1;
  706. u32 odf = 4;
  707. u32 quartz = state->base->extclk / 1000000;
  708. u32 fphi = master_clock / 1000000;
  709. u32 ndiv = (fphi * odf * idf) / quartz;
  710. u32 cp = 7;
  711. u32 fvco;
  712. if (ndiv >= 7 && ndiv <= 71)
  713. cp = 7;
  714. else if (ndiv >= 72 && ndiv <= 79)
  715. cp = 8;
  716. else if (ndiv >= 80 && ndiv <= 87)
  717. cp = 9;
  718. else if (ndiv >= 88 && ndiv <= 95)
  719. cp = 10;
  720. else if (ndiv >= 96 && ndiv <= 103)
  721. cp = 11;
  722. else if (ndiv >= 104 && ndiv <= 111)
  723. cp = 12;
  724. else if (ndiv >= 112 && ndiv <= 119)
  725. cp = 13;
  726. else if (ndiv >= 120 && ndiv <= 127)
  727. cp = 14;
  728. else if (ndiv >= 128 && ndiv <= 135)
  729. cp = 15;
  730. else if (ndiv >= 136 && ndiv <= 143)
  731. cp = 16;
  732. else if (ndiv >= 144 && ndiv <= 151)
  733. cp = 17;
  734. else if (ndiv >= 152 && ndiv <= 159)
  735. cp = 18;
  736. else if (ndiv >= 160 && ndiv <= 167)
  737. cp = 19;
  738. else if (ndiv >= 168 && ndiv <= 175)
  739. cp = 20;
  740. else if (ndiv >= 176 && ndiv <= 183)
  741. cp = 21;
  742. else if (ndiv >= 184 && ndiv <= 191)
  743. cp = 22;
  744. else if (ndiv >= 192 && ndiv <= 199)
  745. cp = 23;
  746. else if (ndiv >= 200 && ndiv <= 207)
  747. cp = 24;
  748. else if (ndiv >= 208 && ndiv <= 215)
  749. cp = 25;
  750. else if (ndiv >= 216 && ndiv <= 223)
  751. cp = 26;
  752. else if (ndiv >= 224 && ndiv <= 225)
  753. cp = 27;
  754. write_reg(state, RSTV0910_NCOARSE, (cp << 3) | idf);
  755. write_reg(state, RSTV0910_NCOARSE2, odf);
  756. write_reg(state, RSTV0910_NCOARSE1, ndiv);
  757. fvco = (quartz * 2 * ndiv) / idf;
  758. state->base->mclk = fvco / (2 * odf) * 1000000;
  759. return 0;
  760. }
  761. static int stop(struct stv *state)
  762. {
  763. if (state->started) {
  764. u8 tmp;
  765. write_reg(state, RSTV0910_P2_TSCFGH + state->regoff,
  766. state->tscfgh | 0x01);
  767. read_reg(state, RSTV0910_P2_PDELCTRL1 + state->regoff, &tmp);
  768. tmp &= ~0x01; /* release reset DVBS2 packet delin */
  769. write_reg(state, RSTV0910_P2_PDELCTRL1 + state->regoff, tmp);
  770. /* Blind optim*/
  771. write_reg(state, RSTV0910_P2_AGC2O + state->regoff, 0x5B);
  772. /* Stop the demod */
  773. write_reg(state, RSTV0910_P2_DMDISTATE + state->regoff, 0x5c);
  774. state->started = 0;
  775. }
  776. state->receive_mode = RCVMODE_NONE;
  777. return 0;
  778. }
  779. static void set_pls(struct stv *state, u32 pls_code)
  780. {
  781. if (pls_code == state->cur_scrambling_code)
  782. return;
  783. /* PLROOT2 bit 2 = gold code */
  784. write_reg(state, RSTV0910_P2_PLROOT0 + state->regoff,
  785. pls_code & 0xff);
  786. write_reg(state, RSTV0910_P2_PLROOT1 + state->regoff,
  787. (pls_code >> 8) & 0xff);
  788. write_reg(state, RSTV0910_P2_PLROOT2 + state->regoff,
  789. 0x04 | ((pls_code >> 16) & 0x03));
  790. state->cur_scrambling_code = pls_code;
  791. }
  792. static void set_isi(struct stv *state, u32 isi)
  793. {
  794. if (isi == NO_STREAM_ID_FILTER)
  795. return;
  796. if (isi == 0x80000000) {
  797. SET_FIELD(FORCE_CONTINUOUS, 1);
  798. SET_FIELD(TSOUT_NOSYNC, 1);
  799. } else {
  800. SET_FIELD(FILTER_EN, 1);
  801. write_reg(state, RSTV0910_P2_ISIENTRY + state->regoff,
  802. isi & 0xff);
  803. write_reg(state, RSTV0910_P2_ISIBITENA + state->regoff, 0xff);
  804. }
  805. SET_FIELD(ALGOSWRST, 1);
  806. SET_FIELD(ALGOSWRST, 0);
  807. }
  808. static void set_stream_modes(struct stv *state,
  809. struct dtv_frontend_properties *p)
  810. {
  811. set_isi(state, p->stream_id);
  812. set_pls(state, p->scrambling_sequence_index);
  813. }
  814. static int init_search_param(struct stv *state,
  815. struct dtv_frontend_properties *p)
  816. {
  817. SET_FIELD(FORCE_CONTINUOUS, 0);
  818. SET_FIELD(FRAME_MODE, 0);
  819. SET_FIELD(FILTER_EN, 0);
  820. SET_FIELD(TSOUT_NOSYNC, 0);
  821. SET_FIELD(TSFIFO_EMBINDVB, 0);
  822. SET_FIELD(TSDEL_SYNCBYTE, 0);
  823. SET_REG(UPLCCST0, 0xe0);
  824. SET_FIELD(TSINS_TOKEN, 0);
  825. SET_FIELD(HYSTERESIS_THRESHOLD, 0);
  826. SET_FIELD(ISIOBS_MODE, 1);
  827. set_stream_modes(state, p);
  828. return 0;
  829. }
  830. static int enable_puncture_rate(struct stv *state, enum fe_code_rate rate)
  831. {
  832. u8 val;
  833. switch (rate) {
  834. case FEC_1_2:
  835. val = 0x01;
  836. break;
  837. case FEC_2_3:
  838. val = 0x02;
  839. break;
  840. case FEC_3_4:
  841. val = 0x04;
  842. break;
  843. case FEC_5_6:
  844. val = 0x08;
  845. break;
  846. case FEC_7_8:
  847. val = 0x20;
  848. break;
  849. case FEC_NONE:
  850. default:
  851. val = 0x2f;
  852. break;
  853. }
  854. return write_reg(state, RSTV0910_P2_PRVIT + state->regoff, val);
  855. }
  856. static int set_vth_default(struct stv *state)
  857. {
  858. state->vth[0] = 0xd7;
  859. state->vth[1] = 0x85;
  860. state->vth[2] = 0x58;
  861. state->vth[3] = 0x3a;
  862. state->vth[4] = 0x34;
  863. state->vth[5] = 0x28;
  864. write_reg(state, RSTV0910_P2_VTH12 + state->regoff + 0, state->vth[0]);
  865. write_reg(state, RSTV0910_P2_VTH12 + state->regoff + 1, state->vth[1]);
  866. write_reg(state, RSTV0910_P2_VTH12 + state->regoff + 2, state->vth[2]);
  867. write_reg(state, RSTV0910_P2_VTH12 + state->regoff + 3, state->vth[3]);
  868. write_reg(state, RSTV0910_P2_VTH12 + state->regoff + 4, state->vth[4]);
  869. write_reg(state, RSTV0910_P2_VTH12 + state->regoff + 5, state->vth[5]);
  870. return 0;
  871. }
  872. static int set_vth(struct stv *state)
  873. {
  874. static const struct slookup vthlookup_table[] = {
  875. {250, 8780}, /* C/N= 1.5dB */
  876. {100, 7405}, /* C/N= 4.5dB */
  877. {40, 6330}, /* C/N= 6.5dB */
  878. {12, 5224}, /* C/N= 8.5dB */
  879. {5, 4236} /* C/N=10.5dB */
  880. };
  881. int i;
  882. u8 tmp[2];
  883. int status = read_regs(state,
  884. RSTV0910_P2_NNOSDATAT1 + state->regoff,
  885. tmp, 2);
  886. u16 reg_value = (tmp[0] << 8) | tmp[1];
  887. s32 vth = table_lookup(vthlookup_table, ARRAY_SIZE(vthlookup_table),
  888. reg_value);
  889. for (i = 0; i < 6; i += 1)
  890. if (state->vth[i] > vth)
  891. state->vth[i] = vth;
  892. write_reg(state, RSTV0910_P2_VTH12 + state->regoff + 0, state->vth[0]);
  893. write_reg(state, RSTV0910_P2_VTH12 + state->regoff + 1, state->vth[1]);
  894. write_reg(state, RSTV0910_P2_VTH12 + state->regoff + 2, state->vth[2]);
  895. write_reg(state, RSTV0910_P2_VTH12 + state->regoff + 3, state->vth[3]);
  896. write_reg(state, RSTV0910_P2_VTH12 + state->regoff + 4, state->vth[4]);
  897. write_reg(state, RSTV0910_P2_VTH12 + state->regoff + 5, state->vth[5]);
  898. return status;
  899. }
  900. static int start(struct stv *state, struct dtv_frontend_properties *p)
  901. {
  902. s32 freq;
  903. u8 reg_dmdcfgmd;
  904. u16 symb;
  905. if (p->symbol_rate < 100000 || p->symbol_rate > 70000000)
  906. return -EINVAL;
  907. state->receive_mode = RCVMODE_NONE;
  908. state->demod_lock_time = 0;
  909. /* Demod Stop */
  910. if (state->started)
  911. write_reg(state, RSTV0910_P2_DMDISTATE + state->regoff, 0x5C);
  912. init_search_param(state, p);
  913. if (p->symbol_rate <= 1000000) { /* SR <=1Msps */
  914. state->demod_timeout = 3000;
  915. state->fec_timeout = 2000;
  916. } else if (p->symbol_rate <= 2000000) { /* 1Msps < SR <=2Msps */
  917. state->demod_timeout = 2500;
  918. state->fec_timeout = 1300;
  919. } else if (p->symbol_rate <= 5000000) { /* 2Msps< SR <=5Msps */
  920. state->demod_timeout = 1000;
  921. state->fec_timeout = 650;
  922. } else if (p->symbol_rate <= 10000000) { /* 5Msps< SR <=10Msps */
  923. state->demod_timeout = 700;
  924. state->fec_timeout = 350;
  925. } else if (p->symbol_rate < 20000000) { /* 10Msps< SR <=20Msps */
  926. state->demod_timeout = 400;
  927. state->fec_timeout = 200;
  928. } else { /* SR >=20Msps */
  929. state->demod_timeout = 300;
  930. state->fec_timeout = 200;
  931. }
  932. /* Set the Init Symbol rate */
  933. symb = muldiv32(p->symbol_rate, 65536, state->base->mclk);
  934. write_reg(state, RSTV0910_P2_SFRINIT1 + state->regoff,
  935. ((symb >> 8) & 0x7F));
  936. write_reg(state, RSTV0910_P2_SFRINIT0 + state->regoff, (symb & 0xFF));
  937. state->demod_bits |= 0x80;
  938. write_reg(state, RSTV0910_P2_DEMOD + state->regoff, state->demod_bits);
  939. /* FE_STV0910_SetSearchStandard */
  940. read_reg(state, RSTV0910_P2_DMDCFGMD + state->regoff, &reg_dmdcfgmd);
  941. write_reg(state, RSTV0910_P2_DMDCFGMD + state->regoff,
  942. reg_dmdcfgmd |= 0xC0);
  943. write_shared_reg(state,
  944. RSTV0910_TSTTSRS, state->nr ? 0x02 : 0x01, 0x00);
  945. /* Disable DSS */
  946. write_reg(state, RSTV0910_P2_FECM + state->regoff, 0x00);
  947. write_reg(state, RSTV0910_P2_PRVIT + state->regoff, 0x2F);
  948. enable_puncture_rate(state, FEC_NONE);
  949. /* 8PSK 3/5, 8PSK 2/3 Poff tracking optimization WA */
  950. write_reg(state, RSTV0910_P2_ACLC2S2Q + state->regoff, 0x0B);
  951. write_reg(state, RSTV0910_P2_ACLC2S28 + state->regoff, 0x0A);
  952. write_reg(state, RSTV0910_P2_BCLC2S2Q + state->regoff, 0x84);
  953. write_reg(state, RSTV0910_P2_BCLC2S28 + state->regoff, 0x84);
  954. write_reg(state, RSTV0910_P2_CARHDR + state->regoff, 0x1C);
  955. write_reg(state, RSTV0910_P2_CARFREQ + state->regoff, 0x79);
  956. write_reg(state, RSTV0910_P2_ACLC2S216A + state->regoff, 0x29);
  957. write_reg(state, RSTV0910_P2_ACLC2S232A + state->regoff, 0x09);
  958. write_reg(state, RSTV0910_P2_BCLC2S216A + state->regoff, 0x84);
  959. write_reg(state, RSTV0910_P2_BCLC2S232A + state->regoff, 0x84);
  960. /*
  961. * Reset CAR3, bug DVBS2->DVBS1 lock
  962. * Note: The bit is only pulsed -> no lock on shared register needed
  963. */
  964. write_reg(state, RSTV0910_TSTRES0, state->nr ? 0x04 : 0x08);
  965. write_reg(state, RSTV0910_TSTRES0, 0);
  966. set_vth_default(state);
  967. /* Reset demod */
  968. write_reg(state, RSTV0910_P2_DMDISTATE + state->regoff, 0x1F);
  969. write_reg(state, RSTV0910_P2_CARCFG + state->regoff, 0x46);
  970. if (p->symbol_rate <= 5000000)
  971. freq = (state->search_range / 2000) + 80;
  972. else
  973. freq = (state->search_range / 2000) + 1600;
  974. freq = (freq << 16) / (state->base->mclk / 1000);
  975. write_reg(state, RSTV0910_P2_CFRUP1 + state->regoff,
  976. (freq >> 8) & 0xff);
  977. write_reg(state, RSTV0910_P2_CFRUP0 + state->regoff, (freq & 0xff));
  978. /* CFR Low Setting */
  979. freq = -freq;
  980. write_reg(state, RSTV0910_P2_CFRLOW1 + state->regoff,
  981. (freq >> 8) & 0xff);
  982. write_reg(state, RSTV0910_P2_CFRLOW0 + state->regoff, (freq & 0xff));
  983. /* init the demod frequency offset to 0 */
  984. write_reg(state, RSTV0910_P2_CFRINIT1 + state->regoff, 0);
  985. write_reg(state, RSTV0910_P2_CFRINIT0 + state->regoff, 0);
  986. write_reg(state, RSTV0910_P2_DMDISTATE + state->regoff, 0x1F);
  987. /* Trigger acq */
  988. write_reg(state, RSTV0910_P2_DMDISTATE + state->regoff, 0x15);
  989. state->demod_lock_time += TUNING_DELAY;
  990. state->started = 1;
  991. return 0;
  992. }
  993. static int init_diseqc(struct stv *state)
  994. {
  995. u16 offs = state->nr ? 0x40 : 0; /* Address offset */
  996. u8 freq = ((state->base->mclk + 11000 * 32) / (22000 * 32));
  997. /* Disable receiver */
  998. write_reg(state, RSTV0910_P1_DISRXCFG + offs, 0x00);
  999. write_reg(state, RSTV0910_P1_DISTXCFG + offs, 0xBA); /* Reset = 1 */
  1000. write_reg(state, RSTV0910_P1_DISTXCFG + offs, 0x3A); /* Reset = 0 */
  1001. write_reg(state, RSTV0910_P1_DISTXF22 + offs, freq);
  1002. return 0;
  1003. }
  1004. static int probe(struct stv *state)
  1005. {
  1006. u8 id;
  1007. state->receive_mode = RCVMODE_NONE;
  1008. state->started = 0;
  1009. if (read_reg(state, RSTV0910_MID, &id) < 0)
  1010. return -ENODEV;
  1011. if (id != 0x51)
  1012. return -EINVAL;
  1013. /* Configure the I2C repeater to off */
  1014. write_reg(state, RSTV0910_P1_I2CRPT, 0x24);
  1015. /* Configure the I2C repeater to off */
  1016. write_reg(state, RSTV0910_P2_I2CRPT, 0x24);
  1017. /* Set the I2C to oversampling ratio */
  1018. write_reg(state, RSTV0910_I2CCFG, 0x88); /* state->i2ccfg */
  1019. write_reg(state, RSTV0910_OUTCFG, 0x00); /* OUTCFG */
  1020. write_reg(state, RSTV0910_PADCFG, 0x05); /* RFAGC Pads Dev = 05 */
  1021. write_reg(state, RSTV0910_SYNTCTRL, 0x02); /* SYNTCTRL */
  1022. write_reg(state, RSTV0910_TSGENERAL, state->tsgeneral); /* TSGENERAL */
  1023. write_reg(state, RSTV0910_CFGEXT, 0x02); /* CFGEXT */
  1024. if (state->single)
  1025. write_reg(state, RSTV0910_GENCFG, 0x14); /* GENCFG */
  1026. else
  1027. write_reg(state, RSTV0910_GENCFG, 0x15); /* GENCFG */
  1028. write_reg(state, RSTV0910_P1_TNRCFG2, 0x02); /* IQSWAP = 0 */
  1029. write_reg(state, RSTV0910_P2_TNRCFG2, 0x82); /* IQSWAP = 1 */
  1030. write_reg(state, RSTV0910_P1_CAR3CFG, 0x02);
  1031. write_reg(state, RSTV0910_P2_CAR3CFG, 0x02);
  1032. write_reg(state, RSTV0910_P1_DMDCFG4, 0x04);
  1033. write_reg(state, RSTV0910_P2_DMDCFG4, 0x04);
  1034. write_reg(state, RSTV0910_TSTRES0, 0x80); /* LDPC Reset */
  1035. write_reg(state, RSTV0910_TSTRES0, 0x00);
  1036. write_reg(state, RSTV0910_P1_TSPIDFLT1, 0x00);
  1037. write_reg(state, RSTV0910_P2_TSPIDFLT1, 0x00);
  1038. write_reg(state, RSTV0910_P1_TMGCFG2, 0x80);
  1039. write_reg(state, RSTV0910_P2_TMGCFG2, 0x80);
  1040. set_mclock(state, 135000000);
  1041. /* TS output */
  1042. write_reg(state, RSTV0910_P1_TSCFGH, state->tscfgh | 0x01);
  1043. write_reg(state, RSTV0910_P1_TSCFGH, state->tscfgh);
  1044. write_reg(state, RSTV0910_P1_TSCFGM, 0xC0); /* Manual speed */
  1045. write_reg(state, RSTV0910_P1_TSCFGL, 0x20);
  1046. write_reg(state, RSTV0910_P1_TSSPEED, state->tsspeed);
  1047. write_reg(state, RSTV0910_P2_TSCFGH, state->tscfgh | 0x01);
  1048. write_reg(state, RSTV0910_P2_TSCFGH, state->tscfgh);
  1049. write_reg(state, RSTV0910_P2_TSCFGM, 0xC0); /* Manual speed */
  1050. write_reg(state, RSTV0910_P2_TSCFGL, 0x20);
  1051. write_reg(state, RSTV0910_P2_TSSPEED, state->tsspeed);
  1052. /* Reset stream merger */
  1053. write_reg(state, RSTV0910_P1_TSCFGH, state->tscfgh | 0x01);
  1054. write_reg(state, RSTV0910_P2_TSCFGH, state->tscfgh | 0x01);
  1055. write_reg(state, RSTV0910_P1_TSCFGH, state->tscfgh);
  1056. write_reg(state, RSTV0910_P2_TSCFGH, state->tscfgh);
  1057. write_reg(state, RSTV0910_P1_I2CRPT, state->i2crpt);
  1058. write_reg(state, RSTV0910_P2_I2CRPT, state->i2crpt);
  1059. write_reg(state, RSTV0910_P1_TSINSDELM, 0x17);
  1060. write_reg(state, RSTV0910_P1_TSINSDELL, 0xff);
  1061. write_reg(state, RSTV0910_P2_TSINSDELM, 0x17);
  1062. write_reg(state, RSTV0910_P2_TSINSDELL, 0xff);
  1063. init_diseqc(state);
  1064. return 0;
  1065. }
  1066. static int gate_ctrl(struct dvb_frontend *fe, int enable)
  1067. {
  1068. struct stv *state = fe->demodulator_priv;
  1069. u8 i2crpt = state->i2crpt & ~0x86;
  1070. /*
  1071. * mutex_lock note: Concurrent I2C gate bus accesses must be
  1072. * prevented (STV0910 = dual demod on a single IC with a single I2C
  1073. * gate/bus, and two tuners attached), similar to most (if not all)
  1074. * other I2C host interfaces/buses.
  1075. *
  1076. * enable=1 (open I2C gate) will grab the lock
  1077. * enable=0 (close I2C gate) releases the lock
  1078. */
  1079. if (enable) {
  1080. mutex_lock(&state->base->i2c_lock);
  1081. i2crpt |= 0x80;
  1082. } else {
  1083. i2crpt |= 0x02;
  1084. }
  1085. if (write_reg(state, state->nr ? RSTV0910_P2_I2CRPT :
  1086. RSTV0910_P1_I2CRPT, i2crpt) < 0) {
  1087. /* don't hold the I2C bus lock on failure */
  1088. if (!WARN_ON(!mutex_is_locked(&state->base->i2c_lock)))
  1089. mutex_unlock(&state->base->i2c_lock);
  1090. dev_err(&state->base->i2c->dev,
  1091. "%s() write_reg failure (enable=%d)\n",
  1092. __func__, enable);
  1093. return -EIO;
  1094. }
  1095. state->i2crpt = i2crpt;
  1096. if (!enable)
  1097. if (!WARN_ON(!mutex_is_locked(&state->base->i2c_lock)))
  1098. mutex_unlock(&state->base->i2c_lock);
  1099. return 0;
  1100. }
  1101. static void release(struct dvb_frontend *fe)
  1102. {
  1103. struct stv *state = fe->demodulator_priv;
  1104. state->base->count--;
  1105. if (state->base->count == 0) {
  1106. list_del(&state->base->stvlist);
  1107. kfree(state->base);
  1108. }
  1109. kfree(state);
  1110. }
  1111. static int set_parameters(struct dvb_frontend *fe)
  1112. {
  1113. int stat = 0;
  1114. struct stv *state = fe->demodulator_priv;
  1115. struct dtv_frontend_properties *p = &fe->dtv_property_cache;
  1116. stop(state);
  1117. if (fe->ops.tuner_ops.set_params)
  1118. fe->ops.tuner_ops.set_params(fe);
  1119. state->symbol_rate = p->symbol_rate;
  1120. stat = start(state, p);
  1121. return stat;
  1122. }
  1123. static int manage_matype_info(struct stv *state)
  1124. {
  1125. if (!state->started)
  1126. return -EINVAL;
  1127. if (state->receive_mode == RCVMODE_DVBS2) {
  1128. u8 bbheader[2];
  1129. read_regs(state, RSTV0910_P2_MATSTR1 + state->regoff,
  1130. bbheader, 2);
  1131. state->feroll_off =
  1132. (enum fe_stv0910_roll_off)(bbheader[0] & 0x03);
  1133. state->is_vcm = (bbheader[0] & 0x10) == 0;
  1134. state->is_standard_broadcast = (bbheader[0] & 0xFC) == 0xF0;
  1135. } else if (state->receive_mode == RCVMODE_DVBS) {
  1136. state->is_vcm = 0;
  1137. state->is_standard_broadcast = 1;
  1138. state->feroll_off = FE_SAT_35;
  1139. }
  1140. return 0;
  1141. }
  1142. static int read_snr(struct dvb_frontend *fe)
  1143. {
  1144. struct stv *state = fe->demodulator_priv;
  1145. struct dtv_frontend_properties *p = &fe->dtv_property_cache;
  1146. s32 snrval;
  1147. if (!get_signal_to_noise(state, &snrval)) {
  1148. p->cnr.stat[0].scale = FE_SCALE_DECIBEL;
  1149. p->cnr.stat[0].svalue = 100 * snrval; /* fix scale */
  1150. } else {
  1151. p->cnr.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
  1152. }
  1153. return 0;
  1154. }
  1155. static int read_ber(struct dvb_frontend *fe)
  1156. {
  1157. struct stv *state = fe->demodulator_priv;
  1158. struct dtv_frontend_properties *p = &fe->dtv_property_cache;
  1159. u32 n, d;
  1160. get_bit_error_rate(state, &n, &d);
  1161. p->pre_bit_error.stat[0].scale = FE_SCALE_COUNTER;
  1162. p->pre_bit_error.stat[0].uvalue = n;
  1163. p->pre_bit_count.stat[0].scale = FE_SCALE_COUNTER;
  1164. p->pre_bit_count.stat[0].uvalue = d;
  1165. return 0;
  1166. }
  1167. static void read_signal_strength(struct dvb_frontend *fe)
  1168. {
  1169. struct stv *state = fe->demodulator_priv;
  1170. struct dtv_frontend_properties *p = &state->fe.dtv_property_cache;
  1171. u8 reg[2];
  1172. u16 agc;
  1173. s32 padc, power = 0;
  1174. int i;
  1175. read_regs(state, RSTV0910_P2_AGCIQIN1 + state->regoff, reg, 2);
  1176. agc = (((u32)reg[0]) << 8) | reg[1];
  1177. for (i = 0; i < 5; i += 1) {
  1178. read_regs(state, RSTV0910_P2_POWERI + state->regoff, reg, 2);
  1179. power += (u32)reg[0] * (u32)reg[0]
  1180. + (u32)reg[1] * (u32)reg[1];
  1181. usleep_range(3000, 4000);
  1182. }
  1183. power /= 5;
  1184. padc = table_lookup(padc_lookup, ARRAY_SIZE(padc_lookup), power) + 352;
  1185. p->strength.stat[0].scale = FE_SCALE_DECIBEL;
  1186. p->strength.stat[0].svalue = (padc - agc);
  1187. }
  1188. static int read_status(struct dvb_frontend *fe, enum fe_status *status)
  1189. {
  1190. struct stv *state = fe->demodulator_priv;
  1191. struct dtv_frontend_properties *p = &fe->dtv_property_cache;
  1192. u8 dmd_state = 0;
  1193. u8 dstatus = 0;
  1194. enum receive_mode cur_receive_mode = RCVMODE_NONE;
  1195. u32 feclock = 0;
  1196. *status = 0;
  1197. read_reg(state, RSTV0910_P2_DMDSTATE + state->regoff, &dmd_state);
  1198. if (dmd_state & 0x40) {
  1199. read_reg(state, RSTV0910_P2_DSTATUS + state->regoff, &dstatus);
  1200. if (dstatus & 0x08)
  1201. cur_receive_mode = (dmd_state & 0x20) ?
  1202. RCVMODE_DVBS : RCVMODE_DVBS2;
  1203. }
  1204. if (cur_receive_mode == RCVMODE_NONE) {
  1205. set_vth(state);
  1206. /* reset signal statistics */
  1207. p->strength.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
  1208. p->cnr.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
  1209. p->pre_bit_error.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
  1210. p->pre_bit_count.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
  1211. return 0;
  1212. }
  1213. *status |= (FE_HAS_SIGNAL
  1214. | FE_HAS_CARRIER
  1215. | FE_HAS_VITERBI
  1216. | FE_HAS_SYNC);
  1217. if (state->receive_mode == RCVMODE_NONE) {
  1218. state->receive_mode = cur_receive_mode;
  1219. state->demod_lock_time = jiffies;
  1220. state->first_time_lock = 1;
  1221. get_signal_parameters(state);
  1222. tracking_optimization(state);
  1223. write_reg(state, RSTV0910_P2_TSCFGH + state->regoff,
  1224. state->tscfgh);
  1225. usleep_range(3000, 4000);
  1226. write_reg(state, RSTV0910_P2_TSCFGH + state->regoff,
  1227. state->tscfgh | 0x01);
  1228. write_reg(state, RSTV0910_P2_TSCFGH + state->regoff,
  1229. state->tscfgh);
  1230. }
  1231. if (dmd_state & 0x40) {
  1232. if (state->receive_mode == RCVMODE_DVBS2) {
  1233. u8 pdelstatus;
  1234. read_reg(state,
  1235. RSTV0910_P2_PDELSTATUS1 + state->regoff,
  1236. &pdelstatus);
  1237. feclock = (pdelstatus & 0x02) != 0;
  1238. } else {
  1239. u8 vstatus;
  1240. read_reg(state,
  1241. RSTV0910_P2_VSTATUSVIT + state->regoff,
  1242. &vstatus);
  1243. feclock = (vstatus & 0x08) != 0;
  1244. }
  1245. }
  1246. if (feclock) {
  1247. *status |= FE_HAS_LOCK;
  1248. if (state->first_time_lock) {
  1249. u8 tmp;
  1250. state->first_time_lock = 0;
  1251. manage_matype_info(state);
  1252. if (state->receive_mode == RCVMODE_DVBS2) {
  1253. /*
  1254. * FSTV0910_P2_MANUALSX_ROLLOFF,
  1255. * FSTV0910_P2_MANUALS2_ROLLOFF = 0
  1256. */
  1257. state->demod_bits &= ~0x84;
  1258. write_reg(state,
  1259. RSTV0910_P2_DEMOD + state->regoff,
  1260. state->demod_bits);
  1261. read_reg(state,
  1262. RSTV0910_P2_PDELCTRL2 + state->regoff,
  1263. &tmp);
  1264. /* reset DVBS2 packet delinator error counter */
  1265. tmp |= 0x40;
  1266. write_reg(state,
  1267. RSTV0910_P2_PDELCTRL2 + state->regoff,
  1268. tmp);
  1269. /* reset DVBS2 packet delinator error counter */
  1270. tmp &= ~0x40;
  1271. write_reg(state,
  1272. RSTV0910_P2_PDELCTRL2 + state->regoff,
  1273. tmp);
  1274. state->berscale = 2;
  1275. state->last_bernumerator = 0;
  1276. state->last_berdenominator = 1;
  1277. /* force to PRE BCH Rate */
  1278. write_reg(state,
  1279. RSTV0910_P2_ERRCTRL1 + state->regoff,
  1280. BER_SRC_S2 | state->berscale);
  1281. } else {
  1282. state->berscale = 2;
  1283. state->last_bernumerator = 0;
  1284. state->last_berdenominator = 1;
  1285. /* force to PRE RS Rate */
  1286. write_reg(state,
  1287. RSTV0910_P2_ERRCTRL1 + state->regoff,
  1288. BER_SRC_S | state->berscale);
  1289. }
  1290. /* Reset the Total packet counter */
  1291. write_reg(state,
  1292. RSTV0910_P2_FBERCPT4 + state->regoff, 0x00);
  1293. /*
  1294. * Reset the packet Error counter2 (and Set it to
  1295. * infinite error count mode)
  1296. */
  1297. write_reg(state,
  1298. RSTV0910_P2_ERRCTRL2 + state->regoff, 0xc1);
  1299. set_vth_default(state);
  1300. if (state->receive_mode == RCVMODE_DVBS)
  1301. enable_puncture_rate(state,
  1302. state->puncture_rate);
  1303. }
  1304. /* Use highest signaled ModCod for quality */
  1305. if (state->is_vcm) {
  1306. u8 tmp;
  1307. enum fe_stv0910_mod_cod mod_cod;
  1308. read_reg(state, RSTV0910_P2_DMDMODCOD + state->regoff,
  1309. &tmp);
  1310. mod_cod = (enum fe_stv0910_mod_cod)((tmp & 0x7c) >> 2);
  1311. if (mod_cod > state->mod_cod)
  1312. state->mod_cod = mod_cod;
  1313. }
  1314. }
  1315. /* read signal statistics */
  1316. /* read signal strength */
  1317. read_signal_strength(fe);
  1318. /* read carrier/noise on FE_HAS_CARRIER */
  1319. if (*status & FE_HAS_CARRIER)
  1320. read_snr(fe);
  1321. else
  1322. p->cnr.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
  1323. /* read ber */
  1324. if (*status & FE_HAS_VITERBI) {
  1325. read_ber(fe);
  1326. } else {
  1327. p->pre_bit_error.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
  1328. p->pre_bit_count.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
  1329. }
  1330. return 0;
  1331. }
  1332. static int get_frontend(struct dvb_frontend *fe,
  1333. struct dtv_frontend_properties *p)
  1334. {
  1335. struct stv *state = fe->demodulator_priv;
  1336. u8 tmp;
  1337. u32 symbolrate;
  1338. if (state->receive_mode == RCVMODE_DVBS2) {
  1339. u32 mc;
  1340. const enum fe_modulation modcod2mod[0x20] = {
  1341. QPSK, QPSK, QPSK, QPSK,
  1342. QPSK, QPSK, QPSK, QPSK,
  1343. QPSK, QPSK, QPSK, QPSK,
  1344. PSK_8, PSK_8, PSK_8, PSK_8,
  1345. PSK_8, PSK_8, APSK_16, APSK_16,
  1346. APSK_16, APSK_16, APSK_16, APSK_16,
  1347. APSK_32, APSK_32, APSK_32, APSK_32,
  1348. APSK_32,
  1349. };
  1350. const enum fe_code_rate modcod2fec[0x20] = {
  1351. FEC_NONE, FEC_NONE, FEC_NONE, FEC_2_5,
  1352. FEC_1_2, FEC_3_5, FEC_2_3, FEC_3_4,
  1353. FEC_4_5, FEC_5_6, FEC_8_9, FEC_9_10,
  1354. FEC_3_5, FEC_2_3, FEC_3_4, FEC_5_6,
  1355. FEC_8_9, FEC_9_10, FEC_2_3, FEC_3_4,
  1356. FEC_4_5, FEC_5_6, FEC_8_9, FEC_9_10,
  1357. FEC_3_4, FEC_4_5, FEC_5_6, FEC_8_9,
  1358. FEC_9_10
  1359. };
  1360. read_reg(state, RSTV0910_P2_DMDMODCOD + state->regoff, &tmp);
  1361. mc = ((tmp & 0x7c) >> 2);
  1362. p->pilot = (tmp & 0x01) ? PILOT_ON : PILOT_OFF;
  1363. p->modulation = modcod2mod[mc];
  1364. p->fec_inner = modcod2fec[mc];
  1365. } else if (state->receive_mode == RCVMODE_DVBS) {
  1366. read_reg(state, RSTV0910_P2_VITCURPUN + state->regoff, &tmp);
  1367. switch (tmp & 0x1F) {
  1368. case 0x0d:
  1369. p->fec_inner = FEC_1_2;
  1370. break;
  1371. case 0x12:
  1372. p->fec_inner = FEC_2_3;
  1373. break;
  1374. case 0x15:
  1375. p->fec_inner = FEC_3_4;
  1376. break;
  1377. case 0x18:
  1378. p->fec_inner = FEC_5_6;
  1379. break;
  1380. case 0x1a:
  1381. p->fec_inner = FEC_7_8;
  1382. break;
  1383. default:
  1384. p->fec_inner = FEC_NONE;
  1385. break;
  1386. }
  1387. p->rolloff = ROLLOFF_35;
  1388. }
  1389. if (state->receive_mode != RCVMODE_NONE) {
  1390. get_cur_symbol_rate(state, &symbolrate);
  1391. p->symbol_rate = symbolrate;
  1392. }
  1393. return 0;
  1394. }
  1395. static int tune(struct dvb_frontend *fe, bool re_tune,
  1396. unsigned int mode_flags,
  1397. unsigned int *delay, enum fe_status *status)
  1398. {
  1399. struct stv *state = fe->demodulator_priv;
  1400. int r;
  1401. if (re_tune) {
  1402. r = set_parameters(fe);
  1403. if (r)
  1404. return r;
  1405. state->tune_time = jiffies;
  1406. }
  1407. r = read_status(fe, status);
  1408. if (r)
  1409. return r;
  1410. if (*status & FE_HAS_LOCK)
  1411. return 0;
  1412. *delay = HZ;
  1413. return 0;
  1414. }
  1415. static enum dvbfe_algo get_algo(struct dvb_frontend *fe)
  1416. {
  1417. return DVBFE_ALGO_HW;
  1418. }
  1419. static int set_tone(struct dvb_frontend *fe, enum fe_sec_tone_mode tone)
  1420. {
  1421. struct stv *state = fe->demodulator_priv;
  1422. u16 offs = state->nr ? 0x40 : 0;
  1423. switch (tone) {
  1424. case SEC_TONE_ON:
  1425. return write_reg(state, RSTV0910_P1_DISTXCFG + offs, 0x38);
  1426. case SEC_TONE_OFF:
  1427. return write_reg(state, RSTV0910_P1_DISTXCFG + offs, 0x3a);
  1428. default:
  1429. break;
  1430. }
  1431. return -EINVAL;
  1432. }
  1433. static int wait_dis(struct stv *state, u8 flag, u8 val)
  1434. {
  1435. int i;
  1436. u8 stat;
  1437. u16 offs = state->nr ? 0x40 : 0;
  1438. for (i = 0; i < 10; i++) {
  1439. read_reg(state, RSTV0910_P1_DISTXSTATUS + offs, &stat);
  1440. if ((stat & flag) == val)
  1441. return 0;
  1442. usleep_range(10000, 11000);
  1443. }
  1444. return -ETIMEDOUT;
  1445. }
  1446. static int send_master_cmd(struct dvb_frontend *fe,
  1447. struct dvb_diseqc_master_cmd *cmd)
  1448. {
  1449. struct stv *state = fe->demodulator_priv;
  1450. int i;
  1451. SET_FIELD(DISEQC_MODE, 2);
  1452. SET_FIELD(DIS_PRECHARGE, 1);
  1453. for (i = 0; i < cmd->msg_len; i++) {
  1454. wait_dis(state, 0x40, 0x00);
  1455. SET_REG(DISTXFIFO, cmd->msg[i]);
  1456. }
  1457. SET_FIELD(DIS_PRECHARGE, 0);
  1458. wait_dis(state, 0x20, 0x20);
  1459. return 0;
  1460. }
  1461. static int send_burst(struct dvb_frontend *fe, enum fe_sec_mini_cmd burst)
  1462. {
  1463. struct stv *state = fe->demodulator_priv;
  1464. u8 value;
  1465. if (burst == SEC_MINI_A) {
  1466. SET_FIELD(DISEQC_MODE, 3);
  1467. value = 0x00;
  1468. } else {
  1469. SET_FIELD(DISEQC_MODE, 2);
  1470. value = 0xFF;
  1471. }
  1472. SET_FIELD(DIS_PRECHARGE, 1);
  1473. wait_dis(state, 0x40, 0x00);
  1474. SET_REG(DISTXFIFO, value);
  1475. SET_FIELD(DIS_PRECHARGE, 0);
  1476. wait_dis(state, 0x20, 0x20);
  1477. return 0;
  1478. }
  1479. static int sleep(struct dvb_frontend *fe)
  1480. {
  1481. struct stv *state = fe->demodulator_priv;
  1482. stop(state);
  1483. return 0;
  1484. }
  1485. static const struct dvb_frontend_ops stv0910_ops = {
  1486. .delsys = { SYS_DVBS, SYS_DVBS2, SYS_DSS },
  1487. .info = {
  1488. .name = "ST STV0910",
  1489. .frequency_min_hz = 950 * MHz,
  1490. .frequency_max_hz = 2150 * MHz,
  1491. .symbol_rate_min = 100000,
  1492. .symbol_rate_max = 70000000,
  1493. .caps = FE_CAN_INVERSION_AUTO |
  1494. FE_CAN_FEC_AUTO |
  1495. FE_CAN_QPSK |
  1496. FE_CAN_2G_MODULATION |
  1497. FE_CAN_MULTISTREAM
  1498. },
  1499. .sleep = sleep,
  1500. .release = release,
  1501. .i2c_gate_ctrl = gate_ctrl,
  1502. .set_frontend = set_parameters,
  1503. .get_frontend_algo = get_algo,
  1504. .get_frontend = get_frontend,
  1505. .tune = tune,
  1506. .read_status = read_status,
  1507. .set_tone = set_tone,
  1508. .diseqc_send_master_cmd = send_master_cmd,
  1509. .diseqc_send_burst = send_burst,
  1510. };
  1511. static struct stv_base *match_base(struct i2c_adapter *i2c, u8 adr)
  1512. {
  1513. struct stv_base *p;
  1514. list_for_each_entry(p, &stvlist, stvlist)
  1515. if (p->i2c == i2c && p->adr == adr)
  1516. return p;
  1517. return NULL;
  1518. }
  1519. static void stv0910_init_stats(struct stv *state)
  1520. {
  1521. struct dtv_frontend_properties *p = &state->fe.dtv_property_cache;
  1522. p->strength.len = 1;
  1523. p->strength.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
  1524. p->cnr.len = 1;
  1525. p->cnr.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
  1526. p->pre_bit_error.len = 1;
  1527. p->pre_bit_error.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
  1528. p->pre_bit_count.len = 1;
  1529. p->pre_bit_count.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
  1530. }
  1531. struct dvb_frontend *stv0910_attach(struct i2c_adapter *i2c,
  1532. struct stv0910_cfg *cfg,
  1533. int nr)
  1534. {
  1535. struct stv *state;
  1536. struct stv_base *base;
  1537. state = kzalloc(sizeof(*state), GFP_KERNEL);
  1538. if (!state)
  1539. return NULL;
  1540. state->tscfgh = 0x20 | (cfg->parallel ? 0 : 0x40);
  1541. state->tsgeneral = (cfg->parallel == 2) ? 0x02 : 0x00;
  1542. state->i2crpt = 0x0A | ((cfg->rptlvl & 0x07) << 4);
  1543. /* use safe tsspeed value if unspecified through stv0910_cfg */
  1544. state->tsspeed = (cfg->tsspeed ? cfg->tsspeed : 0x28);
  1545. state->nr = nr;
  1546. state->regoff = state->nr ? 0 : 0x200;
  1547. state->search_range = 16000000;
  1548. state->demod_bits = 0x10; /* Inversion : Auto with reset to 0 */
  1549. state->receive_mode = RCVMODE_NONE;
  1550. state->cur_scrambling_code = (~0U);
  1551. state->single = cfg->single ? 1 : 0;
  1552. base = match_base(i2c, cfg->adr);
  1553. if (base) {
  1554. base->count++;
  1555. state->base = base;
  1556. } else {
  1557. base = kzalloc(sizeof(*base), GFP_KERNEL);
  1558. if (!base)
  1559. goto fail;
  1560. base->i2c = i2c;
  1561. base->adr = cfg->adr;
  1562. base->count = 1;
  1563. base->extclk = cfg->clk ? cfg->clk : 30000000;
  1564. mutex_init(&base->i2c_lock);
  1565. mutex_init(&base->reg_lock);
  1566. state->base = base;
  1567. if (probe(state) < 0) {
  1568. dev_info(&i2c->dev, "No demod found at adr %02X on %s\n",
  1569. cfg->adr, dev_name(&i2c->dev));
  1570. kfree(base);
  1571. goto fail;
  1572. }
  1573. list_add(&base->stvlist, &stvlist);
  1574. }
  1575. state->fe.ops = stv0910_ops;
  1576. state->fe.demodulator_priv = state;
  1577. state->nr = nr;
  1578. dev_info(&i2c->dev, "%s demod found at adr %02X on %s\n",
  1579. state->fe.ops.info.name, cfg->adr, dev_name(&i2c->dev));
  1580. stv0910_init_stats(state);
  1581. return &state->fe;
  1582. fail:
  1583. kfree(state);
  1584. return NULL;
  1585. }
  1586. EXPORT_SYMBOL_GPL(stv0910_attach);
  1587. MODULE_DESCRIPTION("ST STV0910 multistandard frontend driver");
  1588. MODULE_AUTHOR("Ralph and Marcus Metzler, Manfred Voelkel");
  1589. MODULE_LICENSE("GPL v2");