stv0910.c 50 KB

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