stv0910.c 50 KB

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