mb86a20s.c 54 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162
  1. /*
  2. * Fujitu mb86a20s ISDB-T/ISDB-Tsb Module driver
  3. *
  4. * Copyright (C) 2010-2013 Mauro Carvalho Chehab <mchehab@redhat.com>
  5. * Copyright (C) 2009-2010 Douglas Landgraf <dougsland@redhat.com>
  6. *
  7. * This program is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU General Public License as
  9. * published by the Free Software Foundation version 2.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * General Public License for more details.
  15. */
  16. #include <linux/kernel.h>
  17. #include <asm/div64.h>
  18. #include "dvb_frontend.h"
  19. #include "mb86a20s.h"
  20. #define NUM_LAYERS 3
  21. static int debug = 1;
  22. module_param(debug, int, 0644);
  23. MODULE_PARM_DESC(debug, "Activates frontend debugging (default:0)");
  24. enum mb86a20s_bandwidth {
  25. MB86A20S_13SEG = 0,
  26. MB86A20S_13SEG_PARTIAL = 1,
  27. MB86A20S_1SEG = 2,
  28. MB86A20S_3SEG = 3,
  29. };
  30. u8 mb86a20s_subchannel[] = {
  31. 0xb0, 0xc0, 0xd0, 0xe0,
  32. 0xf0, 0x00, 0x10, 0x20,
  33. };
  34. struct mb86a20s_state {
  35. struct i2c_adapter *i2c;
  36. const struct mb86a20s_config *config;
  37. u32 last_frequency;
  38. struct dvb_frontend frontend;
  39. u32 if_freq;
  40. enum mb86a20s_bandwidth bw;
  41. bool inversion;
  42. u32 subchannel;
  43. u32 estimated_rate[NUM_LAYERS];
  44. unsigned long get_strength_time;
  45. bool need_init;
  46. };
  47. struct regdata {
  48. u8 reg;
  49. u8 data;
  50. };
  51. #define BER_SAMPLING_RATE 1 /* Seconds */
  52. /*
  53. * Initialization sequence: Use whatevere default values that PV SBTVD
  54. * does on its initialisation, obtained via USB snoop
  55. */
  56. static struct regdata mb86a20s_init1[] = {
  57. { 0x70, 0x0f },
  58. { 0x70, 0xff },
  59. { 0x08, 0x01 },
  60. { 0x50, 0xd1 }, { 0x51, 0x20 },
  61. };
  62. static struct regdata mb86a20s_init2[] = {
  63. { 0x28, 0x22 }, { 0x29, 0x00 }, { 0x2a, 0x1f }, { 0x2b, 0xf0 },
  64. { 0x3b, 0x21 },
  65. { 0x3c, 0x38 },
  66. { 0x01, 0x0d },
  67. { 0x04, 0x08 }, { 0x05, 0x03 },
  68. { 0x04, 0x0e }, { 0x05, 0x00 },
  69. { 0x04, 0x0f }, { 0x05, 0x37 },
  70. { 0x04, 0x0b }, { 0x05, 0x78 },
  71. { 0x04, 0x00 }, { 0x05, 0x00 },
  72. { 0x04, 0x01 }, { 0x05, 0x1e },
  73. { 0x04, 0x02 }, { 0x05, 0x07 },
  74. { 0x04, 0x03 }, { 0x05, 0xd0 },
  75. { 0x04, 0x09 }, { 0x05, 0x00 },
  76. { 0x04, 0x0a }, { 0x05, 0xff },
  77. { 0x04, 0x27 }, { 0x05, 0x00 },
  78. { 0x04, 0x28 }, { 0x05, 0x00 },
  79. { 0x04, 0x1e }, { 0x05, 0x00 },
  80. { 0x04, 0x29 }, { 0x05, 0x64 },
  81. { 0x04, 0x32 }, { 0x05, 0x02 },
  82. { 0x04, 0x14 }, { 0x05, 0x02 },
  83. { 0x04, 0x04 }, { 0x05, 0x00 },
  84. { 0x04, 0x05 }, { 0x05, 0x22 },
  85. { 0x04, 0x06 }, { 0x05, 0x0e },
  86. { 0x04, 0x07 }, { 0x05, 0xd8 },
  87. { 0x04, 0x12 }, { 0x05, 0x00 },
  88. { 0x04, 0x13 }, { 0x05, 0xff },
  89. { 0x04, 0x15 }, { 0x05, 0x4e },
  90. { 0x04, 0x16 }, { 0x05, 0x20 },
  91. /*
  92. * On this demod, when the bit count reaches the count below,
  93. * it collects the bit error count. The bit counters are initialized
  94. * to 65535 here. This warrants that all of them will be quickly
  95. * calculated when device gets locked. As TMCC is parsed, the values
  96. * will be adjusted later in the driver's code.
  97. */
  98. { 0x52, 0x01 }, /* Turn on BER before Viterbi */
  99. { 0x50, 0xa7 }, { 0x51, 0x00 },
  100. { 0x50, 0xa8 }, { 0x51, 0xff },
  101. { 0x50, 0xa9 }, { 0x51, 0xff },
  102. { 0x50, 0xaa }, { 0x51, 0x00 },
  103. { 0x50, 0xab }, { 0x51, 0xff },
  104. { 0x50, 0xac }, { 0x51, 0xff },
  105. { 0x50, 0xad }, { 0x51, 0x00 },
  106. { 0x50, 0xae }, { 0x51, 0xff },
  107. { 0x50, 0xaf }, { 0x51, 0xff },
  108. /*
  109. * On this demod, post BER counts blocks. When the count reaches the
  110. * value below, it collects the block error count. The block counters
  111. * are initialized to 127 here. This warrants that all of them will be
  112. * quickly calculated when device gets locked. As TMCC is parsed, the
  113. * values will be adjusted later in the driver's code.
  114. */
  115. { 0x5e, 0x07 }, /* Turn on BER after Viterbi */
  116. { 0x50, 0xdc }, { 0x51, 0x00 },
  117. { 0x50, 0xdd }, { 0x51, 0x7f },
  118. { 0x50, 0xde }, { 0x51, 0x00 },
  119. { 0x50, 0xdf }, { 0x51, 0x7f },
  120. { 0x50, 0xe0 }, { 0x51, 0x00 },
  121. { 0x50, 0xe1 }, { 0x51, 0x7f },
  122. /*
  123. * On this demod, when the block count reaches the count below,
  124. * it collects the block error count. The block counters are initialized
  125. * to 127 here. This warrants that all of them will be quickly
  126. * calculated when device gets locked. As TMCC is parsed, the values
  127. * will be adjusted later in the driver's code.
  128. */
  129. { 0x50, 0xb0 }, { 0x51, 0x07 }, /* Enable PER */
  130. { 0x50, 0xb2 }, { 0x51, 0x00 },
  131. { 0x50, 0xb3 }, { 0x51, 0x7f },
  132. { 0x50, 0xb4 }, { 0x51, 0x00 },
  133. { 0x50, 0xb5 }, { 0x51, 0x7f },
  134. { 0x50, 0xb6 }, { 0x51, 0x00 },
  135. { 0x50, 0xb7 }, { 0x51, 0x7f },
  136. { 0x50, 0x50 }, { 0x51, 0x02 }, /* MER manual mode */
  137. { 0x50, 0x51 }, { 0x51, 0x04 }, /* MER symbol 4 */
  138. { 0x45, 0x04 }, /* CN symbol 4 */
  139. { 0x48, 0x04 }, /* CN manual mode */
  140. { 0x50, 0xd5 }, { 0x51, 0x01 }, /* Serial */
  141. { 0x50, 0xd6 }, { 0x51, 0x1f },
  142. { 0x50, 0xd2 }, { 0x51, 0x03 },
  143. { 0x50, 0xd7 }, { 0x51, 0xbf },
  144. { 0x28, 0x74 }, { 0x29, 0x00 }, { 0x2a, 0x00 }, { 0x2b, 0xff },
  145. { 0x28, 0x46 }, { 0x29, 0x00 }, { 0x2a, 0x1a }, { 0x2b, 0x0c },
  146. { 0x04, 0x40 }, { 0x05, 0x00 },
  147. { 0x28, 0x00 }, { 0x2b, 0x08 },
  148. { 0x28, 0x05 }, { 0x2b, 0x00 },
  149. { 0x1c, 0x01 },
  150. { 0x28, 0x06 }, { 0x29, 0x00 }, { 0x2a, 0x00 }, { 0x2b, 0x1f },
  151. { 0x28, 0x07 }, { 0x29, 0x00 }, { 0x2a, 0x00 }, { 0x2b, 0x18 },
  152. { 0x28, 0x08 }, { 0x29, 0x00 }, { 0x2a, 0x00 }, { 0x2b, 0x12 },
  153. { 0x28, 0x09 }, { 0x29, 0x00 }, { 0x2a, 0x00 }, { 0x2b, 0x30 },
  154. { 0x28, 0x0a }, { 0x29, 0x00 }, { 0x2a, 0x00 }, { 0x2b, 0x37 },
  155. { 0x28, 0x0b }, { 0x29, 0x00 }, { 0x2a, 0x00 }, { 0x2b, 0x02 },
  156. { 0x28, 0x0c }, { 0x29, 0x00 }, { 0x2a, 0x00 }, { 0x2b, 0x09 },
  157. { 0x28, 0x0d }, { 0x29, 0x00 }, { 0x2a, 0x00 }, { 0x2b, 0x06 },
  158. { 0x28, 0x0e }, { 0x29, 0x00 }, { 0x2a, 0x00 }, { 0x2b, 0x7b },
  159. { 0x28, 0x0f }, { 0x29, 0x00 }, { 0x2a, 0x00 }, { 0x2b, 0x76 },
  160. { 0x28, 0x10 }, { 0x29, 0x00 }, { 0x2a, 0x00 }, { 0x2b, 0x7d },
  161. { 0x28, 0x11 }, { 0x29, 0x00 }, { 0x2a, 0x00 }, { 0x2b, 0x08 },
  162. { 0x28, 0x12 }, { 0x29, 0x00 }, { 0x2a, 0x00 }, { 0x2b, 0x0b },
  163. { 0x28, 0x13 }, { 0x29, 0x00 }, { 0x2a, 0x00 }, { 0x2b, 0x00 },
  164. { 0x28, 0x14 }, { 0x29, 0x00 }, { 0x2a, 0x01 }, { 0x2b, 0xf2 },
  165. { 0x28, 0x15 }, { 0x29, 0x00 }, { 0x2a, 0x01 }, { 0x2b, 0xf3 },
  166. { 0x28, 0x16 }, { 0x29, 0x00 }, { 0x2a, 0x00 }, { 0x2b, 0x05 },
  167. { 0x28, 0x17 }, { 0x29, 0x00 }, { 0x2a, 0x00 }, { 0x2b, 0x16 },
  168. { 0x28, 0x18 }, { 0x29, 0x00 }, { 0x2a, 0x00 }, { 0x2b, 0x0f },
  169. { 0x28, 0x19 }, { 0x29, 0x00 }, { 0x2a, 0x07 }, { 0x2b, 0xef },
  170. { 0x28, 0x1a }, { 0x29, 0x00 }, { 0x2a, 0x07 }, { 0x2b, 0xd8 },
  171. { 0x28, 0x1b }, { 0x29, 0x00 }, { 0x2a, 0x07 }, { 0x2b, 0xf1 },
  172. { 0x28, 0x1c }, { 0x29, 0x00 }, { 0x2a, 0x00 }, { 0x2b, 0x3d },
  173. { 0x28, 0x1d }, { 0x29, 0x00 }, { 0x2a, 0x00 }, { 0x2b, 0x94 },
  174. { 0x28, 0x1e }, { 0x29, 0x00 }, { 0x2a, 0x00 }, { 0x2b, 0xba },
  175. { 0x50, 0x1e }, { 0x51, 0x5d },
  176. { 0x50, 0x22 }, { 0x51, 0x00 },
  177. { 0x50, 0x23 }, { 0x51, 0xc8 },
  178. { 0x50, 0x24 }, { 0x51, 0x00 },
  179. { 0x50, 0x25 }, { 0x51, 0xf0 },
  180. { 0x50, 0x26 }, { 0x51, 0x00 },
  181. { 0x50, 0x27 }, { 0x51, 0xc3 },
  182. { 0x50, 0x39 }, { 0x51, 0x02 },
  183. { 0xec, 0x0f },
  184. { 0xeb, 0x1f },
  185. { 0x28, 0x6a }, { 0x29, 0x00 }, { 0x2a, 0x00 }, { 0x2b, 0x00 },
  186. { 0xd0, 0x00 },
  187. };
  188. static struct regdata mb86a20s_reset_reception[] = {
  189. { 0x70, 0xf0 },
  190. { 0x70, 0xff },
  191. { 0x08, 0x01 },
  192. { 0x08, 0x00 },
  193. };
  194. static struct regdata mb86a20s_per_ber_reset[] = {
  195. { 0x53, 0x00 }, /* pre BER Counter reset */
  196. { 0x53, 0x07 },
  197. { 0x5f, 0x00 }, /* post BER Counter reset */
  198. { 0x5f, 0x07 },
  199. { 0x50, 0xb1 }, /* PER Counter reset */
  200. { 0x51, 0x07 },
  201. { 0x51, 0x00 },
  202. };
  203. /*
  204. * I2C read/write functions and macros
  205. */
  206. static int mb86a20s_i2c_writereg(struct mb86a20s_state *state,
  207. u8 i2c_addr, u8 reg, u8 data)
  208. {
  209. u8 buf[] = { reg, data };
  210. struct i2c_msg msg = {
  211. .addr = i2c_addr, .flags = 0, .buf = buf, .len = 2
  212. };
  213. int rc;
  214. rc = i2c_transfer(state->i2c, &msg, 1);
  215. if (rc != 1) {
  216. dev_err(&state->i2c->dev,
  217. "%s: writereg error (rc == %i, reg == 0x%02x, data == 0x%02x)\n",
  218. __func__, rc, reg, data);
  219. return rc;
  220. }
  221. return 0;
  222. }
  223. static int mb86a20s_i2c_writeregdata(struct mb86a20s_state *state,
  224. u8 i2c_addr, struct regdata *rd, int size)
  225. {
  226. int i, rc;
  227. for (i = 0; i < size; i++) {
  228. rc = mb86a20s_i2c_writereg(state, i2c_addr, rd[i].reg,
  229. rd[i].data);
  230. if (rc < 0)
  231. return rc;
  232. }
  233. return 0;
  234. }
  235. static int mb86a20s_i2c_readreg(struct mb86a20s_state *state,
  236. u8 i2c_addr, u8 reg)
  237. {
  238. u8 val;
  239. int rc;
  240. struct i2c_msg msg[] = {
  241. { .addr = i2c_addr, .flags = 0, .buf = &reg, .len = 1 },
  242. { .addr = i2c_addr, .flags = I2C_M_RD, .buf = &val, .len = 1 }
  243. };
  244. rc = i2c_transfer(state->i2c, msg, 2);
  245. if (rc != 2) {
  246. dev_err(&state->i2c->dev, "%s: reg=0x%x (error=%d)\n",
  247. __func__, reg, rc);
  248. return (rc < 0) ? rc : -EIO;
  249. }
  250. return val;
  251. }
  252. #define mb86a20s_readreg(state, reg) \
  253. mb86a20s_i2c_readreg(state, state->config->demod_address, reg)
  254. #define mb86a20s_writereg(state, reg, val) \
  255. mb86a20s_i2c_writereg(state, state->config->demod_address, reg, val)
  256. #define mb86a20s_writeregdata(state, regdata) \
  257. mb86a20s_i2c_writeregdata(state, state->config->demod_address, \
  258. regdata, ARRAY_SIZE(regdata))
  259. /*
  260. * Ancillary internal routines (likely compiled inlined)
  261. *
  262. * The functions below assume that gateway lock has already obtained
  263. */
  264. static int mb86a20s_read_status(struct dvb_frontend *fe, fe_status_t *status)
  265. {
  266. struct mb86a20s_state *state = fe->demodulator_priv;
  267. int val;
  268. *status = 0;
  269. val = mb86a20s_readreg(state, 0x0a) & 0xf;
  270. if (val < 0)
  271. return val;
  272. if (val >= 2)
  273. *status |= FE_HAS_SIGNAL;
  274. if (val >= 4)
  275. *status |= FE_HAS_CARRIER;
  276. if (val >= 5)
  277. *status |= FE_HAS_VITERBI;
  278. if (val >= 7)
  279. *status |= FE_HAS_SYNC;
  280. if (val >= 8) /* Maybe 9? */
  281. *status |= FE_HAS_LOCK;
  282. dev_dbg(&state->i2c->dev, "%s: Status = 0x%02x (state = %d)\n",
  283. __func__, *status, val);
  284. return val;
  285. }
  286. static int mb86a20s_read_signal_strength(struct dvb_frontend *fe)
  287. {
  288. struct mb86a20s_state *state = fe->demodulator_priv;
  289. struct dtv_frontend_properties *c = &fe->dtv_property_cache;
  290. int rc;
  291. unsigned rf_max, rf_min, rf;
  292. if (state->get_strength_time &&
  293. (!time_after(jiffies, state->get_strength_time)))
  294. return c->strength.stat[0].uvalue;
  295. /* Reset its value if an error happen */
  296. c->strength.stat[0].uvalue = 0;
  297. /* Does a binary search to get RF strength */
  298. rf_max = 0xfff;
  299. rf_min = 0;
  300. do {
  301. rf = (rf_max + rf_min) / 2;
  302. rc = mb86a20s_writereg(state, 0x04, 0x1f);
  303. if (rc < 0)
  304. return rc;
  305. rc = mb86a20s_writereg(state, 0x05, rf >> 8);
  306. if (rc < 0)
  307. return rc;
  308. rc = mb86a20s_writereg(state, 0x04, 0x20);
  309. if (rc < 0)
  310. return rc;
  311. rc = mb86a20s_writereg(state, 0x05, rf);
  312. if (rc < 0)
  313. return rc;
  314. rc = mb86a20s_readreg(state, 0x02);
  315. if (rc < 0)
  316. return rc;
  317. if (rc & 0x08)
  318. rf_min = (rf_max + rf_min) / 2;
  319. else
  320. rf_max = (rf_max + rf_min) / 2;
  321. if (rf_max - rf_min < 4) {
  322. rf = (rf_max + rf_min) / 2;
  323. /* Rescale it from 2^12 (4096) to 2^16 */
  324. rf = rf << (16 - 12);
  325. if (rf)
  326. rf |= (1 << 12) - 1;
  327. dev_dbg(&state->i2c->dev,
  328. "%s: signal strength = %d (%d < RF=%d < %d)\n",
  329. __func__, rf, rf_min, rf >> 4, rf_max);
  330. c->strength.stat[0].uvalue = rf;
  331. state->get_strength_time = jiffies +
  332. msecs_to_jiffies(1000);
  333. return 0;
  334. }
  335. } while (1);
  336. }
  337. static int mb86a20s_get_modulation(struct mb86a20s_state *state,
  338. unsigned layer)
  339. {
  340. int rc;
  341. static unsigned char reg[] = {
  342. [0] = 0x86, /* Layer A */
  343. [1] = 0x8a, /* Layer B */
  344. [2] = 0x8e, /* Layer C */
  345. };
  346. if (layer >= ARRAY_SIZE(reg))
  347. return -EINVAL;
  348. rc = mb86a20s_writereg(state, 0x6d, reg[layer]);
  349. if (rc < 0)
  350. return rc;
  351. rc = mb86a20s_readreg(state, 0x6e);
  352. if (rc < 0)
  353. return rc;
  354. switch ((rc >> 4) & 0x07) {
  355. case 0:
  356. return DQPSK;
  357. case 1:
  358. return QPSK;
  359. case 2:
  360. return QAM_16;
  361. case 3:
  362. return QAM_64;
  363. default:
  364. return QAM_AUTO;
  365. }
  366. }
  367. static int mb86a20s_get_fec(struct mb86a20s_state *state,
  368. unsigned layer)
  369. {
  370. int rc;
  371. static unsigned char reg[] = {
  372. [0] = 0x87, /* Layer A */
  373. [1] = 0x8b, /* Layer B */
  374. [2] = 0x8f, /* Layer C */
  375. };
  376. if (layer >= ARRAY_SIZE(reg))
  377. return -EINVAL;
  378. rc = mb86a20s_writereg(state, 0x6d, reg[layer]);
  379. if (rc < 0)
  380. return rc;
  381. rc = mb86a20s_readreg(state, 0x6e);
  382. if (rc < 0)
  383. return rc;
  384. switch ((rc >> 4) & 0x07) {
  385. case 0:
  386. return FEC_1_2;
  387. case 1:
  388. return FEC_2_3;
  389. case 2:
  390. return FEC_3_4;
  391. case 3:
  392. return FEC_5_6;
  393. case 4:
  394. return FEC_7_8;
  395. default:
  396. return FEC_AUTO;
  397. }
  398. }
  399. static int mb86a20s_get_interleaving(struct mb86a20s_state *state,
  400. unsigned layer)
  401. {
  402. int rc;
  403. static unsigned char reg[] = {
  404. [0] = 0x88, /* Layer A */
  405. [1] = 0x8c, /* Layer B */
  406. [2] = 0x90, /* Layer C */
  407. };
  408. if (layer >= ARRAY_SIZE(reg))
  409. return -EINVAL;
  410. rc = mb86a20s_writereg(state, 0x6d, reg[layer]);
  411. if (rc < 0)
  412. return rc;
  413. rc = mb86a20s_readreg(state, 0x6e);
  414. if (rc < 0)
  415. return rc;
  416. switch ((rc >> 4) & 0x07) {
  417. case 1:
  418. return GUARD_INTERVAL_1_4;
  419. case 2:
  420. return GUARD_INTERVAL_1_8;
  421. case 3:
  422. return GUARD_INTERVAL_1_16;
  423. case 4:
  424. return GUARD_INTERVAL_1_32;
  425. default:
  426. case 0:
  427. return GUARD_INTERVAL_AUTO;
  428. }
  429. }
  430. static int mb86a20s_get_segment_count(struct mb86a20s_state *state,
  431. unsigned layer)
  432. {
  433. int rc, count;
  434. static unsigned char reg[] = {
  435. [0] = 0x89, /* Layer A */
  436. [1] = 0x8d, /* Layer B */
  437. [2] = 0x91, /* Layer C */
  438. };
  439. dev_dbg(&state->i2c->dev, "%s called.\n", __func__);
  440. if (layer >= ARRAY_SIZE(reg))
  441. return -EINVAL;
  442. rc = mb86a20s_writereg(state, 0x6d, reg[layer]);
  443. if (rc < 0)
  444. return rc;
  445. rc = mb86a20s_readreg(state, 0x6e);
  446. if (rc < 0)
  447. return rc;
  448. count = (rc >> 4) & 0x0f;
  449. dev_dbg(&state->i2c->dev, "%s: segments: %d.\n", __func__, count);
  450. return count;
  451. }
  452. static void mb86a20s_reset_frontend_cache(struct dvb_frontend *fe)
  453. {
  454. struct mb86a20s_state *state = fe->demodulator_priv;
  455. struct dtv_frontend_properties *c = &fe->dtv_property_cache;
  456. dev_dbg(&state->i2c->dev, "%s called.\n", __func__);
  457. /* Fixed parameters */
  458. c->delivery_system = SYS_ISDBT;
  459. c->bandwidth_hz = 6000000;
  460. /* Initialize values that will be later autodetected */
  461. c->isdbt_layer_enabled = 0;
  462. c->transmission_mode = TRANSMISSION_MODE_AUTO;
  463. c->guard_interval = GUARD_INTERVAL_AUTO;
  464. c->isdbt_sb_mode = 0;
  465. c->isdbt_sb_segment_count = 0;
  466. }
  467. /*
  468. * Estimates the bit rate using the per-segment bit rate given by
  469. * ABNT/NBR 15601 spec (table 4).
  470. */
  471. static u32 isdbt_rate[3][5][4] = {
  472. { /* DQPSK/QPSK */
  473. { 280850, 312060, 330420, 340430 }, /* 1/2 */
  474. { 374470, 416080, 440560, 453910 }, /* 2/3 */
  475. { 421280, 468090, 495630, 510650 }, /* 3/4 */
  476. { 468090, 520100, 550700, 567390 }, /* 5/6 */
  477. { 491500, 546110, 578230, 595760 }, /* 7/8 */
  478. }, { /* QAM16 */
  479. { 561710, 624130, 660840, 680870 }, /* 1/2 */
  480. { 748950, 832170, 881120, 907820 }, /* 2/3 */
  481. { 842570, 936190, 991260, 1021300 }, /* 3/4 */
  482. { 936190, 1040210, 1101400, 1134780 }, /* 5/6 */
  483. { 983000, 1092220, 1156470, 1191520 }, /* 7/8 */
  484. }, { /* QAM64 */
  485. { 842570, 936190, 991260, 1021300 }, /* 1/2 */
  486. { 1123430, 1248260, 1321680, 1361740 }, /* 2/3 */
  487. { 1263860, 1404290, 1486900, 1531950 }, /* 3/4 */
  488. { 1404290, 1560320, 1652110, 1702170 }, /* 5/6 */
  489. { 1474500, 1638340, 1734710, 1787280 }, /* 7/8 */
  490. }
  491. };
  492. static void mb86a20s_layer_bitrate(struct dvb_frontend *fe, u32 layer,
  493. u32 modulation, u32 forward_error_correction,
  494. u32 interleaving,
  495. u32 segment)
  496. {
  497. struct mb86a20s_state *state = fe->demodulator_priv;
  498. u32 rate;
  499. int mod, fec, guard;
  500. /*
  501. * If modulation/fec/interleaving is not detected, the default is
  502. * to consider the lowest bit rate, to avoid taking too long time
  503. * to get BER.
  504. */
  505. switch (modulation) {
  506. case DQPSK:
  507. case QPSK:
  508. default:
  509. mod = 0;
  510. break;
  511. case QAM_16:
  512. mod = 1;
  513. break;
  514. case QAM_64:
  515. mod = 2;
  516. break;
  517. }
  518. switch (forward_error_correction) {
  519. default:
  520. case FEC_1_2:
  521. case FEC_AUTO:
  522. fec = 0;
  523. break;
  524. case FEC_2_3:
  525. fec = 1;
  526. break;
  527. case FEC_3_4:
  528. fec = 2;
  529. break;
  530. case FEC_5_6:
  531. fec = 3;
  532. break;
  533. case FEC_7_8:
  534. fec = 4;
  535. break;
  536. }
  537. switch (interleaving) {
  538. default:
  539. case GUARD_INTERVAL_1_4:
  540. guard = 0;
  541. break;
  542. case GUARD_INTERVAL_1_8:
  543. guard = 1;
  544. break;
  545. case GUARD_INTERVAL_1_16:
  546. guard = 2;
  547. break;
  548. case GUARD_INTERVAL_1_32:
  549. guard = 3;
  550. break;
  551. }
  552. /* Samples BER at BER_SAMPLING_RATE seconds */
  553. rate = isdbt_rate[mod][fec][guard] * segment * BER_SAMPLING_RATE;
  554. /* Avoids sampling too quickly or to overflow the register */
  555. if (rate < 256)
  556. rate = 256;
  557. else if (rate > (1 << 24) - 1)
  558. rate = (1 << 24) - 1;
  559. dev_dbg(&state->i2c->dev,
  560. "%s: layer %c bitrate: %d kbps; counter = %d (0x%06x)\n",
  561. __func__, 'A' + layer,
  562. segment * isdbt_rate[mod][fec][guard]/1000,
  563. rate, rate);
  564. state->estimated_rate[layer] = rate;
  565. }
  566. static int mb86a20s_get_frontend(struct dvb_frontend *fe)
  567. {
  568. struct mb86a20s_state *state = fe->demodulator_priv;
  569. struct dtv_frontend_properties *c = &fe->dtv_property_cache;
  570. int layer, rc;
  571. dev_dbg(&state->i2c->dev, "%s called.\n", __func__);
  572. /* Reset frontend cache to default values */
  573. mb86a20s_reset_frontend_cache(fe);
  574. /* Check for partial reception */
  575. rc = mb86a20s_writereg(state, 0x6d, 0x85);
  576. if (rc < 0)
  577. return rc;
  578. rc = mb86a20s_readreg(state, 0x6e);
  579. if (rc < 0)
  580. return rc;
  581. c->isdbt_partial_reception = (rc & 0x10) ? 1 : 0;
  582. /* Get per-layer data */
  583. for (layer = 0; layer < NUM_LAYERS; layer++) {
  584. dev_dbg(&state->i2c->dev, "%s: getting data for layer %c.\n",
  585. __func__, 'A' + layer);
  586. rc = mb86a20s_get_segment_count(state, layer);
  587. if (rc < 0)
  588. goto noperlayer_error;
  589. if (rc >= 0 && rc < 14) {
  590. c->layer[layer].segment_count = rc;
  591. } else {
  592. c->layer[layer].segment_count = 0;
  593. state->estimated_rate[layer] = 0;
  594. continue;
  595. }
  596. c->isdbt_layer_enabled |= 1 << layer;
  597. rc = mb86a20s_get_modulation(state, layer);
  598. if (rc < 0)
  599. goto noperlayer_error;
  600. dev_dbg(&state->i2c->dev, "%s: modulation %d.\n",
  601. __func__, rc);
  602. c->layer[layer].modulation = rc;
  603. rc = mb86a20s_get_fec(state, layer);
  604. if (rc < 0)
  605. goto noperlayer_error;
  606. dev_dbg(&state->i2c->dev, "%s: FEC %d.\n",
  607. __func__, rc);
  608. c->layer[layer].fec = rc;
  609. rc = mb86a20s_get_interleaving(state, layer);
  610. if (rc < 0)
  611. goto noperlayer_error;
  612. dev_dbg(&state->i2c->dev, "%s: interleaving %d.\n",
  613. __func__, rc);
  614. c->layer[layer].interleaving = rc;
  615. mb86a20s_layer_bitrate(fe, layer, c->layer[layer].modulation,
  616. c->layer[layer].fec,
  617. c->layer[layer].interleaving,
  618. c->layer[layer].segment_count);
  619. }
  620. rc = mb86a20s_writereg(state, 0x6d, 0x84);
  621. if (rc < 0)
  622. return rc;
  623. if ((rc & 0x60) == 0x20) {
  624. c->isdbt_sb_mode = 1;
  625. /* At least, one segment should exist */
  626. if (!c->isdbt_sb_segment_count)
  627. c->isdbt_sb_segment_count = 1;
  628. }
  629. /* Get transmission mode and guard interval */
  630. rc = mb86a20s_readreg(state, 0x07);
  631. if (rc < 0)
  632. return rc;
  633. if ((rc & 0x60) == 0x20) {
  634. switch (rc & 0x0c >> 2) {
  635. case 0:
  636. c->transmission_mode = TRANSMISSION_MODE_2K;
  637. break;
  638. case 1:
  639. c->transmission_mode = TRANSMISSION_MODE_4K;
  640. break;
  641. case 2:
  642. c->transmission_mode = TRANSMISSION_MODE_8K;
  643. break;
  644. }
  645. }
  646. if (!(rc & 0x10)) {
  647. switch (rc & 0x3) {
  648. case 0:
  649. c->guard_interval = GUARD_INTERVAL_1_4;
  650. break;
  651. case 1:
  652. c->guard_interval = GUARD_INTERVAL_1_8;
  653. break;
  654. case 2:
  655. c->guard_interval = GUARD_INTERVAL_1_16;
  656. break;
  657. }
  658. }
  659. return 0;
  660. noperlayer_error:
  661. /* per-layer info is incomplete; discard all per-layer */
  662. c->isdbt_layer_enabled = 0;
  663. return rc;
  664. }
  665. static int mb86a20s_reset_counters(struct dvb_frontend *fe)
  666. {
  667. struct mb86a20s_state *state = fe->demodulator_priv;
  668. struct dtv_frontend_properties *c = &fe->dtv_property_cache;
  669. int rc, val;
  670. dev_dbg(&state->i2c->dev, "%s called.\n", __func__);
  671. /* Reset the counters, if the channel changed */
  672. if (state->last_frequency != c->frequency) {
  673. memset(&c->cnr, 0, sizeof(c->cnr));
  674. memset(&c->pre_bit_error, 0, sizeof(c->pre_bit_error));
  675. memset(&c->pre_bit_count, 0, sizeof(c->pre_bit_count));
  676. memset(&c->post_bit_error, 0, sizeof(c->post_bit_error));
  677. memset(&c->post_bit_count, 0, sizeof(c->post_bit_count));
  678. memset(&c->block_error, 0, sizeof(c->block_error));
  679. memset(&c->block_count, 0, sizeof(c->block_count));
  680. state->last_frequency = c->frequency;
  681. }
  682. /* Clear status for most stats */
  683. /* BER/PER counter reset */
  684. rc = mb86a20s_writeregdata(state, mb86a20s_per_ber_reset);
  685. if (rc < 0)
  686. goto err;
  687. /* CNR counter reset */
  688. rc = mb86a20s_readreg(state, 0x45);
  689. if (rc < 0)
  690. goto err;
  691. val = rc;
  692. rc = mb86a20s_writereg(state, 0x45, val | 0x10);
  693. if (rc < 0)
  694. goto err;
  695. rc = mb86a20s_writereg(state, 0x45, val & 0x6f);
  696. if (rc < 0)
  697. goto err;
  698. /* MER counter reset */
  699. rc = mb86a20s_writereg(state, 0x50, 0x50);
  700. if (rc < 0)
  701. goto err;
  702. rc = mb86a20s_readreg(state, 0x51);
  703. if (rc < 0)
  704. goto err;
  705. val = rc;
  706. rc = mb86a20s_writereg(state, 0x51, val | 0x01);
  707. if (rc < 0)
  708. goto err;
  709. rc = mb86a20s_writereg(state, 0x51, val & 0x06);
  710. if (rc < 0)
  711. goto err;
  712. goto ok;
  713. err:
  714. dev_err(&state->i2c->dev,
  715. "%s: Can't reset FE statistics (error %d).\n",
  716. __func__, rc);
  717. ok:
  718. return rc;
  719. }
  720. static int mb86a20s_get_pre_ber(struct dvb_frontend *fe,
  721. unsigned layer,
  722. u32 *error, u32 *count)
  723. {
  724. struct mb86a20s_state *state = fe->demodulator_priv;
  725. int rc, val;
  726. dev_dbg(&state->i2c->dev, "%s called.\n", __func__);
  727. if (layer >= NUM_LAYERS)
  728. return -EINVAL;
  729. /* Check if the BER measures are already available */
  730. rc = mb86a20s_readreg(state, 0x54);
  731. if (rc < 0)
  732. return rc;
  733. /* Check if data is available for that layer */
  734. if (!(rc & (1 << layer))) {
  735. dev_dbg(&state->i2c->dev,
  736. "%s: preBER for layer %c is not available yet.\n",
  737. __func__, 'A' + layer);
  738. return -EBUSY;
  739. }
  740. /* Read Bit Error Count */
  741. rc = mb86a20s_readreg(state, 0x55 + layer * 3);
  742. if (rc < 0)
  743. return rc;
  744. *error = rc << 16;
  745. rc = mb86a20s_readreg(state, 0x56 + layer * 3);
  746. if (rc < 0)
  747. return rc;
  748. *error |= rc << 8;
  749. rc = mb86a20s_readreg(state, 0x57 + layer * 3);
  750. if (rc < 0)
  751. return rc;
  752. *error |= rc;
  753. dev_dbg(&state->i2c->dev,
  754. "%s: bit error before Viterbi for layer %c: %d.\n",
  755. __func__, 'A' + layer, *error);
  756. /* Read Bit Count */
  757. rc = mb86a20s_writereg(state, 0x50, 0xa7 + layer * 3);
  758. if (rc < 0)
  759. return rc;
  760. rc = mb86a20s_readreg(state, 0x51);
  761. if (rc < 0)
  762. return rc;
  763. *count = rc << 16;
  764. rc = mb86a20s_writereg(state, 0x50, 0xa8 + layer * 3);
  765. if (rc < 0)
  766. return rc;
  767. rc = mb86a20s_readreg(state, 0x51);
  768. if (rc < 0)
  769. return rc;
  770. *count |= rc << 8;
  771. rc = mb86a20s_writereg(state, 0x50, 0xa9 + layer * 3);
  772. if (rc < 0)
  773. return rc;
  774. rc = mb86a20s_readreg(state, 0x51);
  775. if (rc < 0)
  776. return rc;
  777. *count |= rc;
  778. dev_dbg(&state->i2c->dev,
  779. "%s: bit count before Viterbi for layer %c: %d.\n",
  780. __func__, 'A' + layer, *count);
  781. /*
  782. * As we get TMCC data from the frontend, we can better estimate the
  783. * BER bit counters, in order to do the BER measure during a longer
  784. * time. Use those data, if available, to update the bit count
  785. * measure.
  786. */
  787. if (state->estimated_rate[layer]
  788. && state->estimated_rate[layer] != *count) {
  789. dev_dbg(&state->i2c->dev,
  790. "%s: updating layer %c preBER counter to %d.\n",
  791. __func__, 'A' + layer, state->estimated_rate[layer]);
  792. /* Turn off BER before Viterbi */
  793. rc = mb86a20s_writereg(state, 0x52, 0x00);
  794. /* Update counter for this layer */
  795. rc = mb86a20s_writereg(state, 0x50, 0xa7 + layer * 3);
  796. if (rc < 0)
  797. return rc;
  798. rc = mb86a20s_writereg(state, 0x51,
  799. state->estimated_rate[layer] >> 16);
  800. if (rc < 0)
  801. return rc;
  802. rc = mb86a20s_writereg(state, 0x50, 0xa8 + layer * 3);
  803. if (rc < 0)
  804. return rc;
  805. rc = mb86a20s_writereg(state, 0x51,
  806. state->estimated_rate[layer] >> 8);
  807. if (rc < 0)
  808. return rc;
  809. rc = mb86a20s_writereg(state, 0x50, 0xa9 + layer * 3);
  810. if (rc < 0)
  811. return rc;
  812. rc = mb86a20s_writereg(state, 0x51,
  813. state->estimated_rate[layer]);
  814. if (rc < 0)
  815. return rc;
  816. /* Turn on BER before Viterbi */
  817. rc = mb86a20s_writereg(state, 0x52, 0x01);
  818. /* Reset all preBER counters */
  819. rc = mb86a20s_writereg(state, 0x53, 0x00);
  820. if (rc < 0)
  821. return rc;
  822. rc = mb86a20s_writereg(state, 0x53, 0x07);
  823. } else {
  824. /* Reset counter to collect new data */
  825. rc = mb86a20s_readreg(state, 0x53);
  826. if (rc < 0)
  827. return rc;
  828. val = rc;
  829. rc = mb86a20s_writereg(state, 0x53, val & ~(1 << layer));
  830. if (rc < 0)
  831. return rc;
  832. rc = mb86a20s_writereg(state, 0x53, val | (1 << layer));
  833. }
  834. return rc;
  835. }
  836. static int mb86a20s_get_post_ber(struct dvb_frontend *fe,
  837. unsigned layer,
  838. u32 *error, u32 *count)
  839. {
  840. struct mb86a20s_state *state = fe->demodulator_priv;
  841. u32 counter, collect_rate;
  842. int rc, val;
  843. dev_dbg(&state->i2c->dev, "%s called.\n", __func__);
  844. if (layer >= NUM_LAYERS)
  845. return -EINVAL;
  846. /* Check if the BER measures are already available */
  847. rc = mb86a20s_readreg(state, 0x60);
  848. if (rc < 0)
  849. return rc;
  850. /* Check if data is available for that layer */
  851. if (!(rc & (1 << layer))) {
  852. dev_dbg(&state->i2c->dev,
  853. "%s: post BER for layer %c is not available yet.\n",
  854. __func__, 'A' + layer);
  855. return -EBUSY;
  856. }
  857. /* Read Bit Error Count */
  858. rc = mb86a20s_readreg(state, 0x64 + layer * 3);
  859. if (rc < 0)
  860. return rc;
  861. *error = rc << 16;
  862. rc = mb86a20s_readreg(state, 0x65 + layer * 3);
  863. if (rc < 0)
  864. return rc;
  865. *error |= rc << 8;
  866. rc = mb86a20s_readreg(state, 0x66 + layer * 3);
  867. if (rc < 0)
  868. return rc;
  869. *error |= rc;
  870. dev_dbg(&state->i2c->dev,
  871. "%s: post bit error for layer %c: %d.\n",
  872. __func__, 'A' + layer, *error);
  873. /* Read Bit Count */
  874. rc = mb86a20s_writereg(state, 0x50, 0xdc + layer * 2);
  875. if (rc < 0)
  876. return rc;
  877. rc = mb86a20s_readreg(state, 0x51);
  878. if (rc < 0)
  879. return rc;
  880. counter = rc << 8;
  881. rc = mb86a20s_writereg(state, 0x50, 0xdd + layer * 2);
  882. if (rc < 0)
  883. return rc;
  884. rc = mb86a20s_readreg(state, 0x51);
  885. if (rc < 0)
  886. return rc;
  887. counter |= rc;
  888. *count = counter * 204 * 8;
  889. dev_dbg(&state->i2c->dev,
  890. "%s: post bit count for layer %c: %d.\n",
  891. __func__, 'A' + layer, *count);
  892. /*
  893. * As we get TMCC data from the frontend, we can better estimate the
  894. * BER bit counters, in order to do the BER measure during a longer
  895. * time. Use those data, if available, to update the bit count
  896. * measure.
  897. */
  898. if (!state->estimated_rate[layer])
  899. goto reset_measurement;
  900. collect_rate = state->estimated_rate[layer] / 204 / 8;
  901. if (collect_rate < 32)
  902. collect_rate = 32;
  903. if (collect_rate > 65535)
  904. collect_rate = 65535;
  905. if (collect_rate != counter) {
  906. dev_dbg(&state->i2c->dev,
  907. "%s: updating postBER counter on layer %c to %d.\n",
  908. __func__, 'A' + layer, collect_rate);
  909. /* Turn off BER after Viterbi */
  910. rc = mb86a20s_writereg(state, 0x5e, 0x00);
  911. /* Update counter for this layer */
  912. rc = mb86a20s_writereg(state, 0x50, 0xdc + layer * 2);
  913. if (rc < 0)
  914. return rc;
  915. rc = mb86a20s_writereg(state, 0x51, collect_rate >> 8);
  916. if (rc < 0)
  917. return rc;
  918. rc = mb86a20s_writereg(state, 0x50, 0xdd + layer * 2);
  919. if (rc < 0)
  920. return rc;
  921. rc = mb86a20s_writereg(state, 0x51, collect_rate & 0xff);
  922. if (rc < 0)
  923. return rc;
  924. /* Turn on BER after Viterbi */
  925. rc = mb86a20s_writereg(state, 0x5e, 0x07);
  926. /* Reset all preBER counters */
  927. rc = mb86a20s_writereg(state, 0x5f, 0x00);
  928. if (rc < 0)
  929. return rc;
  930. rc = mb86a20s_writereg(state, 0x5f, 0x07);
  931. return rc;
  932. }
  933. reset_measurement:
  934. /* Reset counter to collect new data */
  935. rc = mb86a20s_readreg(state, 0x5f);
  936. if (rc < 0)
  937. return rc;
  938. val = rc;
  939. rc = mb86a20s_writereg(state, 0x5f, val & ~(1 << layer));
  940. if (rc < 0)
  941. return rc;
  942. rc = mb86a20s_writereg(state, 0x5f, val | (1 << layer));
  943. return rc;
  944. }
  945. static int mb86a20s_get_blk_error(struct dvb_frontend *fe,
  946. unsigned layer,
  947. u32 *error, u32 *count)
  948. {
  949. struct mb86a20s_state *state = fe->demodulator_priv;
  950. int rc, val;
  951. u32 collect_rate;
  952. dev_dbg(&state->i2c->dev, "%s called.\n", __func__);
  953. if (layer >= NUM_LAYERS)
  954. return -EINVAL;
  955. /* Check if the PER measures are already available */
  956. rc = mb86a20s_writereg(state, 0x50, 0xb8);
  957. if (rc < 0)
  958. return rc;
  959. rc = mb86a20s_readreg(state, 0x51);
  960. if (rc < 0)
  961. return rc;
  962. /* Check if data is available for that layer */
  963. if (!(rc & (1 << layer))) {
  964. dev_dbg(&state->i2c->dev,
  965. "%s: block counts for layer %c aren't available yet.\n",
  966. __func__, 'A' + layer);
  967. return -EBUSY;
  968. }
  969. /* Read Packet error Count */
  970. rc = mb86a20s_writereg(state, 0x50, 0xb9 + layer * 2);
  971. if (rc < 0)
  972. return rc;
  973. rc = mb86a20s_readreg(state, 0x51);
  974. if (rc < 0)
  975. return rc;
  976. *error = rc << 8;
  977. rc = mb86a20s_writereg(state, 0x50, 0xba + layer * 2);
  978. if (rc < 0)
  979. return rc;
  980. rc = mb86a20s_readreg(state, 0x51);
  981. if (rc < 0)
  982. return rc;
  983. *error |= rc;
  984. dev_dbg(&state->i2c->dev, "%s: block error for layer %c: %d.\n",
  985. __func__, 'A' + layer, *error);
  986. /* Read Bit Count */
  987. rc = mb86a20s_writereg(state, 0x50, 0xb2 + layer * 2);
  988. if (rc < 0)
  989. return rc;
  990. rc = mb86a20s_readreg(state, 0x51);
  991. if (rc < 0)
  992. return rc;
  993. *count = rc << 8;
  994. rc = mb86a20s_writereg(state, 0x50, 0xb3 + layer * 2);
  995. if (rc < 0)
  996. return rc;
  997. rc = mb86a20s_readreg(state, 0x51);
  998. if (rc < 0)
  999. return rc;
  1000. *count |= rc;
  1001. dev_dbg(&state->i2c->dev,
  1002. "%s: block count for layer %c: %d.\n",
  1003. __func__, 'A' + layer, *count);
  1004. /*
  1005. * As we get TMCC data from the frontend, we can better estimate the
  1006. * BER bit counters, in order to do the BER measure during a longer
  1007. * time. Use those data, if available, to update the bit count
  1008. * measure.
  1009. */
  1010. if (!state->estimated_rate[layer])
  1011. goto reset_measurement;
  1012. collect_rate = state->estimated_rate[layer] / 204 / 8;
  1013. if (collect_rate < 32)
  1014. collect_rate = 32;
  1015. if (collect_rate > 65535)
  1016. collect_rate = 65535;
  1017. if (collect_rate != *count) {
  1018. dev_dbg(&state->i2c->dev,
  1019. "%s: updating PER counter on layer %c to %d.\n",
  1020. __func__, 'A' + layer, collect_rate);
  1021. /* Stop PER measurement */
  1022. rc = mb86a20s_writereg(state, 0x50, 0xb0);
  1023. if (rc < 0)
  1024. return rc;
  1025. rc = mb86a20s_writereg(state, 0x51, 0x00);
  1026. if (rc < 0)
  1027. return rc;
  1028. /* Update this layer's counter */
  1029. rc = mb86a20s_writereg(state, 0x50, 0xb2 + layer * 2);
  1030. if (rc < 0)
  1031. return rc;
  1032. rc = mb86a20s_writereg(state, 0x51, collect_rate >> 8);
  1033. if (rc < 0)
  1034. return rc;
  1035. rc = mb86a20s_writereg(state, 0x50, 0xb3 + layer * 2);
  1036. if (rc < 0)
  1037. return rc;
  1038. rc = mb86a20s_writereg(state, 0x51, collect_rate & 0xff);
  1039. if (rc < 0)
  1040. return rc;
  1041. /* start PER measurement */
  1042. rc = mb86a20s_writereg(state, 0x50, 0xb0);
  1043. if (rc < 0)
  1044. return rc;
  1045. rc = mb86a20s_writereg(state, 0x51, 0x07);
  1046. if (rc < 0)
  1047. return rc;
  1048. /* Reset all counters to collect new data */
  1049. rc = mb86a20s_writereg(state, 0x50, 0xb1);
  1050. if (rc < 0)
  1051. return rc;
  1052. rc = mb86a20s_writereg(state, 0x51, 0x07);
  1053. if (rc < 0)
  1054. return rc;
  1055. rc = mb86a20s_writereg(state, 0x51, 0x00);
  1056. return rc;
  1057. }
  1058. reset_measurement:
  1059. /* Reset counter to collect new data */
  1060. rc = mb86a20s_writereg(state, 0x50, 0xb1);
  1061. if (rc < 0)
  1062. return rc;
  1063. rc = mb86a20s_readreg(state, 0x51);
  1064. if (rc < 0)
  1065. return rc;
  1066. val = rc;
  1067. rc = mb86a20s_writereg(state, 0x51, val | (1 << layer));
  1068. if (rc < 0)
  1069. return rc;
  1070. rc = mb86a20s_writereg(state, 0x51, val & ~(1 << layer));
  1071. return rc;
  1072. }
  1073. struct linear_segments {
  1074. unsigned x, y;
  1075. };
  1076. /*
  1077. * All tables below return a dB/1000 measurement
  1078. */
  1079. static struct linear_segments cnr_to_db_table[] = {
  1080. { 19648, 0},
  1081. { 18187, 1000},
  1082. { 16534, 2000},
  1083. { 14823, 3000},
  1084. { 13161, 4000},
  1085. { 11622, 5000},
  1086. { 10279, 6000},
  1087. { 9089, 7000},
  1088. { 8042, 8000},
  1089. { 7137, 9000},
  1090. { 6342, 10000},
  1091. { 5641, 11000},
  1092. { 5030, 12000},
  1093. { 4474, 13000},
  1094. { 3988, 14000},
  1095. { 3556, 15000},
  1096. { 3180, 16000},
  1097. { 2841, 17000},
  1098. { 2541, 18000},
  1099. { 2276, 19000},
  1100. { 2038, 20000},
  1101. { 1800, 21000},
  1102. { 1625, 22000},
  1103. { 1462, 23000},
  1104. { 1324, 24000},
  1105. { 1175, 25000},
  1106. { 1063, 26000},
  1107. { 980, 27000},
  1108. { 907, 28000},
  1109. { 840, 29000},
  1110. { 788, 30000},
  1111. };
  1112. static struct linear_segments cnr_64qam_table[] = {
  1113. { 3922688, 0},
  1114. { 3920384, 1000},
  1115. { 3902720, 2000},
  1116. { 3894784, 3000},
  1117. { 3882496, 4000},
  1118. { 3872768, 5000},
  1119. { 3858944, 6000},
  1120. { 3851520, 7000},
  1121. { 3838976, 8000},
  1122. { 3829248, 9000},
  1123. { 3818240, 10000},
  1124. { 3806976, 11000},
  1125. { 3791872, 12000},
  1126. { 3767040, 13000},
  1127. { 3720960, 14000},
  1128. { 3637504, 15000},
  1129. { 3498496, 16000},
  1130. { 3296000, 17000},
  1131. { 3031040, 18000},
  1132. { 2715392, 19000},
  1133. { 2362624, 20000},
  1134. { 1963264, 21000},
  1135. { 1649664, 22000},
  1136. { 1366784, 23000},
  1137. { 1120768, 24000},
  1138. { 890880, 25000},
  1139. { 723456, 26000},
  1140. { 612096, 27000},
  1141. { 518912, 28000},
  1142. { 448256, 29000},
  1143. { 388864, 30000},
  1144. };
  1145. static struct linear_segments cnr_16qam_table[] = {
  1146. { 5314816, 0},
  1147. { 5219072, 1000},
  1148. { 5118720, 2000},
  1149. { 4998912, 3000},
  1150. { 4875520, 4000},
  1151. { 4736000, 5000},
  1152. { 4604160, 6000},
  1153. { 4458752, 7000},
  1154. { 4300288, 8000},
  1155. { 4092928, 9000},
  1156. { 3836160, 10000},
  1157. { 3521024, 11000},
  1158. { 3155968, 12000},
  1159. { 2756864, 13000},
  1160. { 2347008, 14000},
  1161. { 1955072, 15000},
  1162. { 1593600, 16000},
  1163. { 1297920, 17000},
  1164. { 1043968, 18000},
  1165. { 839680, 19000},
  1166. { 672256, 20000},
  1167. { 523008, 21000},
  1168. { 424704, 22000},
  1169. { 345088, 23000},
  1170. { 280064, 24000},
  1171. { 221440, 25000},
  1172. { 179712, 26000},
  1173. { 151040, 27000},
  1174. { 128512, 28000},
  1175. { 110080, 29000},
  1176. { 95744, 30000},
  1177. };
  1178. struct linear_segments cnr_qpsk_table[] = {
  1179. { 2834176, 0},
  1180. { 2683648, 1000},
  1181. { 2536960, 2000},
  1182. { 2391808, 3000},
  1183. { 2133248, 4000},
  1184. { 1906176, 5000},
  1185. { 1666560, 6000},
  1186. { 1422080, 7000},
  1187. { 1189632, 8000},
  1188. { 976384, 9000},
  1189. { 790272, 10000},
  1190. { 633344, 11000},
  1191. { 505600, 12000},
  1192. { 402944, 13000},
  1193. { 320768, 14000},
  1194. { 255488, 15000},
  1195. { 204032, 16000},
  1196. { 163072, 17000},
  1197. { 130304, 18000},
  1198. { 105216, 19000},
  1199. { 83456, 20000},
  1200. { 65024, 21000},
  1201. { 52480, 22000},
  1202. { 42752, 23000},
  1203. { 34560, 24000},
  1204. { 27136, 25000},
  1205. { 22016, 26000},
  1206. { 18432, 27000},
  1207. { 15616, 28000},
  1208. { 13312, 29000},
  1209. { 11520, 30000},
  1210. };
  1211. static u32 interpolate_value(u32 value, struct linear_segments *segments,
  1212. unsigned len)
  1213. {
  1214. u64 tmp64;
  1215. u32 dx, dy;
  1216. int i, ret;
  1217. if (value >= segments[0].x)
  1218. return segments[0].y;
  1219. if (value < segments[len-1].x)
  1220. return segments[len-1].y;
  1221. for (i = 1; i < len - 1; i++) {
  1222. /* If value is identical, no need to interpolate */
  1223. if (value == segments[i].x)
  1224. return segments[i].y;
  1225. if (value > segments[i].x)
  1226. break;
  1227. }
  1228. /* Linear interpolation between the two (x,y) points */
  1229. dy = segments[i].y - segments[i - 1].y;
  1230. dx = segments[i - 1].x - segments[i].x;
  1231. tmp64 = value - segments[i].x;
  1232. tmp64 *= dy;
  1233. do_div(tmp64, dx);
  1234. ret = segments[i].y - tmp64;
  1235. return ret;
  1236. }
  1237. static int mb86a20s_get_main_CNR(struct dvb_frontend *fe)
  1238. {
  1239. struct mb86a20s_state *state = fe->demodulator_priv;
  1240. struct dtv_frontend_properties *c = &fe->dtv_property_cache;
  1241. u32 cnr_linear, cnr;
  1242. int rc, val;
  1243. /* Check if CNR is available */
  1244. rc = mb86a20s_readreg(state, 0x45);
  1245. if (rc < 0)
  1246. return rc;
  1247. if (!(rc & 0x40)) {
  1248. dev_dbg(&state->i2c->dev, "%s: CNR is not available yet.\n",
  1249. __func__);
  1250. return -EBUSY;
  1251. }
  1252. val = rc;
  1253. rc = mb86a20s_readreg(state, 0x46);
  1254. if (rc < 0)
  1255. return rc;
  1256. cnr_linear = rc << 8;
  1257. rc = mb86a20s_readreg(state, 0x46);
  1258. if (rc < 0)
  1259. return rc;
  1260. cnr_linear |= rc;
  1261. cnr = interpolate_value(cnr_linear,
  1262. cnr_to_db_table, ARRAY_SIZE(cnr_to_db_table));
  1263. c->cnr.stat[0].scale = FE_SCALE_DECIBEL;
  1264. c->cnr.stat[0].svalue = cnr;
  1265. dev_dbg(&state->i2c->dev, "%s: CNR is %d.%03d dB (%d)\n",
  1266. __func__, cnr / 1000, cnr % 1000, cnr_linear);
  1267. /* CNR counter reset */
  1268. rc = mb86a20s_writereg(state, 0x45, val | 0x10);
  1269. if (rc < 0)
  1270. return rc;
  1271. rc = mb86a20s_writereg(state, 0x45, val & 0x6f);
  1272. return rc;
  1273. }
  1274. static int mb86a20s_get_blk_error_layer_CNR(struct dvb_frontend *fe)
  1275. {
  1276. struct mb86a20s_state *state = fe->demodulator_priv;
  1277. struct dtv_frontend_properties *c = &fe->dtv_property_cache;
  1278. u32 mer, cnr;
  1279. int rc, val, layer;
  1280. struct linear_segments *segs;
  1281. unsigned segs_len;
  1282. dev_dbg(&state->i2c->dev, "%s called.\n", __func__);
  1283. /* Check if the measures are already available */
  1284. rc = mb86a20s_writereg(state, 0x50, 0x5b);
  1285. if (rc < 0)
  1286. return rc;
  1287. rc = mb86a20s_readreg(state, 0x51);
  1288. if (rc < 0)
  1289. return rc;
  1290. /* Check if data is available */
  1291. if (!(rc & 0x01)) {
  1292. dev_dbg(&state->i2c->dev,
  1293. "%s: MER measures aren't available yet.\n", __func__);
  1294. return -EBUSY;
  1295. }
  1296. /* Read all layers */
  1297. for (layer = 0; layer < NUM_LAYERS; layer++) {
  1298. if (!(c->isdbt_layer_enabled & (1 << layer))) {
  1299. c->cnr.stat[1 + layer].scale = FE_SCALE_NOT_AVAILABLE;
  1300. continue;
  1301. }
  1302. rc = mb86a20s_writereg(state, 0x50, 0x52 + layer * 3);
  1303. if (rc < 0)
  1304. return rc;
  1305. rc = mb86a20s_readreg(state, 0x51);
  1306. if (rc < 0)
  1307. return rc;
  1308. mer = rc << 16;
  1309. rc = mb86a20s_writereg(state, 0x50, 0x53 + layer * 3);
  1310. if (rc < 0)
  1311. return rc;
  1312. rc = mb86a20s_readreg(state, 0x51);
  1313. if (rc < 0)
  1314. return rc;
  1315. mer |= rc << 8;
  1316. rc = mb86a20s_writereg(state, 0x50, 0x54 + layer * 3);
  1317. if (rc < 0)
  1318. return rc;
  1319. rc = mb86a20s_readreg(state, 0x51);
  1320. if (rc < 0)
  1321. return rc;
  1322. mer |= rc;
  1323. switch (c->layer[layer].modulation) {
  1324. case DQPSK:
  1325. case QPSK:
  1326. segs = cnr_qpsk_table;
  1327. segs_len = ARRAY_SIZE(cnr_qpsk_table);
  1328. break;
  1329. case QAM_16:
  1330. segs = cnr_16qam_table;
  1331. segs_len = ARRAY_SIZE(cnr_16qam_table);
  1332. break;
  1333. default:
  1334. case QAM_64:
  1335. segs = cnr_64qam_table;
  1336. segs_len = ARRAY_SIZE(cnr_64qam_table);
  1337. break;
  1338. }
  1339. cnr = interpolate_value(mer, segs, segs_len);
  1340. c->cnr.stat[1 + layer].scale = FE_SCALE_DECIBEL;
  1341. c->cnr.stat[1 + layer].svalue = cnr;
  1342. dev_dbg(&state->i2c->dev,
  1343. "%s: CNR for layer %c is %d.%03d dB (MER = %d).\n",
  1344. __func__, 'A' + layer, cnr / 1000, cnr % 1000, mer);
  1345. }
  1346. /* Start a new MER measurement */
  1347. /* MER counter reset */
  1348. rc = mb86a20s_writereg(state, 0x50, 0x50);
  1349. if (rc < 0)
  1350. return rc;
  1351. rc = mb86a20s_readreg(state, 0x51);
  1352. if (rc < 0)
  1353. return rc;
  1354. val = rc;
  1355. rc = mb86a20s_writereg(state, 0x51, val | 0x01);
  1356. if (rc < 0)
  1357. return rc;
  1358. rc = mb86a20s_writereg(state, 0x51, val & 0x06);
  1359. if (rc < 0)
  1360. return rc;
  1361. return 0;
  1362. }
  1363. static void mb86a20s_stats_not_ready(struct dvb_frontend *fe)
  1364. {
  1365. struct mb86a20s_state *state = fe->demodulator_priv;
  1366. struct dtv_frontend_properties *c = &fe->dtv_property_cache;
  1367. int layer;
  1368. dev_dbg(&state->i2c->dev, "%s called.\n", __func__);
  1369. /* Fill the length of each status counter */
  1370. /* Only global stats */
  1371. c->strength.len = 1;
  1372. /* Per-layer stats - 3 layers + global */
  1373. c->cnr.len = NUM_LAYERS + 1;
  1374. c->pre_bit_error.len = NUM_LAYERS + 1;
  1375. c->pre_bit_count.len = NUM_LAYERS + 1;
  1376. c->post_bit_error.len = NUM_LAYERS + 1;
  1377. c->post_bit_count.len = NUM_LAYERS + 1;
  1378. c->block_error.len = NUM_LAYERS + 1;
  1379. c->block_count.len = NUM_LAYERS + 1;
  1380. /* Signal is always available */
  1381. c->strength.stat[0].scale = FE_SCALE_RELATIVE;
  1382. c->strength.stat[0].uvalue = 0;
  1383. /* Put all of them at FE_SCALE_NOT_AVAILABLE */
  1384. for (layer = 0; layer < NUM_LAYERS + 1; layer++) {
  1385. c->cnr.stat[layer].scale = FE_SCALE_NOT_AVAILABLE;
  1386. c->pre_bit_error.stat[layer].scale = FE_SCALE_NOT_AVAILABLE;
  1387. c->pre_bit_count.stat[layer].scale = FE_SCALE_NOT_AVAILABLE;
  1388. c->post_bit_error.stat[layer].scale = FE_SCALE_NOT_AVAILABLE;
  1389. c->post_bit_count.stat[layer].scale = FE_SCALE_NOT_AVAILABLE;
  1390. c->block_error.stat[layer].scale = FE_SCALE_NOT_AVAILABLE;
  1391. c->block_count.stat[layer].scale = FE_SCALE_NOT_AVAILABLE;
  1392. }
  1393. }
  1394. static int mb86a20s_get_stats(struct dvb_frontend *fe, int status_nr)
  1395. {
  1396. struct mb86a20s_state *state = fe->demodulator_priv;
  1397. struct dtv_frontend_properties *c = &fe->dtv_property_cache;
  1398. int rc = 0, layer;
  1399. u32 bit_error = 0, bit_count = 0;
  1400. u32 t_pre_bit_error = 0, t_pre_bit_count = 0;
  1401. u32 t_post_bit_error = 0, t_post_bit_count = 0;
  1402. u32 block_error = 0, block_count = 0;
  1403. u32 t_block_error = 0, t_block_count = 0;
  1404. int active_layers = 0, pre_ber_layers = 0, post_ber_layers = 0;
  1405. int per_layers = 0;
  1406. dev_dbg(&state->i2c->dev, "%s called.\n", __func__);
  1407. mb86a20s_get_main_CNR(fe);
  1408. /* Get per-layer stats */
  1409. mb86a20s_get_blk_error_layer_CNR(fe);
  1410. /*
  1411. * At state 7, only CNR is available
  1412. * For BER measures, state=9 is required
  1413. * FIXME: we may get MER measures with state=8
  1414. */
  1415. if (status_nr < 9)
  1416. return 0;
  1417. for (layer = 0; layer < NUM_LAYERS; layer++) {
  1418. if (c->isdbt_layer_enabled & (1 << layer)) {
  1419. /* Layer is active and has rc segments */
  1420. active_layers++;
  1421. /* Handle BER before vterbi */
  1422. rc = mb86a20s_get_pre_ber(fe, layer,
  1423. &bit_error, &bit_count);
  1424. if (rc >= 0) {
  1425. c->pre_bit_error.stat[1 + layer].scale = FE_SCALE_COUNTER;
  1426. c->pre_bit_error.stat[1 + layer].uvalue += bit_error;
  1427. c->pre_bit_count.stat[1 + layer].scale = FE_SCALE_COUNTER;
  1428. c->pre_bit_count.stat[1 + layer].uvalue += bit_count;
  1429. } else if (rc != -EBUSY) {
  1430. /*
  1431. * If an I/O error happened,
  1432. * measures are now unavailable
  1433. */
  1434. c->pre_bit_error.stat[1 + layer].scale = FE_SCALE_NOT_AVAILABLE;
  1435. c->pre_bit_count.stat[1 + layer].scale = FE_SCALE_NOT_AVAILABLE;
  1436. dev_err(&state->i2c->dev,
  1437. "%s: Can't get BER for layer %c (error %d).\n",
  1438. __func__, 'A' + layer, rc);
  1439. }
  1440. if (c->block_error.stat[1 + layer].scale != FE_SCALE_NOT_AVAILABLE)
  1441. pre_ber_layers++;
  1442. /* Handle BER post vterbi */
  1443. rc = mb86a20s_get_post_ber(fe, layer,
  1444. &bit_error, &bit_count);
  1445. if (rc >= 0) {
  1446. c->post_bit_error.stat[1 + layer].scale = FE_SCALE_COUNTER;
  1447. c->post_bit_error.stat[1 + layer].uvalue += bit_error;
  1448. c->post_bit_count.stat[1 + layer].scale = FE_SCALE_COUNTER;
  1449. c->post_bit_count.stat[1 + layer].uvalue += bit_count;
  1450. } else if (rc != -EBUSY) {
  1451. /*
  1452. * If an I/O error happened,
  1453. * measures are now unavailable
  1454. */
  1455. c->post_bit_error.stat[1 + layer].scale = FE_SCALE_NOT_AVAILABLE;
  1456. c->post_bit_count.stat[1 + layer].scale = FE_SCALE_NOT_AVAILABLE;
  1457. dev_err(&state->i2c->dev,
  1458. "%s: Can't get BER for layer %c (error %d).\n",
  1459. __func__, 'A' + layer, rc);
  1460. }
  1461. if (c->block_error.stat[1 + layer].scale != FE_SCALE_NOT_AVAILABLE)
  1462. post_ber_layers++;
  1463. /* Handle Block errors for PER/UCB reports */
  1464. rc = mb86a20s_get_blk_error(fe, layer,
  1465. &block_error,
  1466. &block_count);
  1467. if (rc >= 0) {
  1468. c->block_error.stat[1 + layer].scale = FE_SCALE_COUNTER;
  1469. c->block_error.stat[1 + layer].uvalue += block_error;
  1470. c->block_count.stat[1 + layer].scale = FE_SCALE_COUNTER;
  1471. c->block_count.stat[1 + layer].uvalue += block_count;
  1472. } else if (rc != -EBUSY) {
  1473. /*
  1474. * If an I/O error happened,
  1475. * measures are now unavailable
  1476. */
  1477. c->block_error.stat[1 + layer].scale = FE_SCALE_NOT_AVAILABLE;
  1478. c->block_count.stat[1 + layer].scale = FE_SCALE_NOT_AVAILABLE;
  1479. dev_err(&state->i2c->dev,
  1480. "%s: Can't get PER for layer %c (error %d).\n",
  1481. __func__, 'A' + layer, rc);
  1482. }
  1483. if (c->block_error.stat[1 + layer].scale != FE_SCALE_NOT_AVAILABLE)
  1484. per_layers++;
  1485. /* Update total preBER */
  1486. t_pre_bit_error += c->pre_bit_error.stat[1 + layer].uvalue;
  1487. t_pre_bit_count += c->pre_bit_count.stat[1 + layer].uvalue;
  1488. /* Update total postBER */
  1489. t_post_bit_error += c->post_bit_error.stat[1 + layer].uvalue;
  1490. t_post_bit_count += c->post_bit_count.stat[1 + layer].uvalue;
  1491. /* Update total PER */
  1492. t_block_error += c->block_error.stat[1 + layer].uvalue;
  1493. t_block_count += c->block_count.stat[1 + layer].uvalue;
  1494. }
  1495. }
  1496. /*
  1497. * Start showing global count if at least one error count is
  1498. * available.
  1499. */
  1500. if (pre_ber_layers) {
  1501. /*
  1502. * At least one per-layer BER measure was read. We can now
  1503. * calculate the total BER
  1504. *
  1505. * Total Bit Error/Count is calculated as the sum of the
  1506. * bit errors on all active layers.
  1507. */
  1508. c->pre_bit_error.stat[0].scale = FE_SCALE_COUNTER;
  1509. c->pre_bit_error.stat[0].uvalue = t_pre_bit_error;
  1510. c->pre_bit_count.stat[0].scale = FE_SCALE_COUNTER;
  1511. c->pre_bit_count.stat[0].uvalue = t_pre_bit_count;
  1512. } else {
  1513. c->pre_bit_error.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
  1514. c->pre_bit_count.stat[0].scale = FE_SCALE_COUNTER;
  1515. }
  1516. /*
  1517. * Start showing global count if at least one error count is
  1518. * available.
  1519. */
  1520. if (post_ber_layers) {
  1521. /*
  1522. * At least one per-layer BER measure was read. We can now
  1523. * calculate the total BER
  1524. *
  1525. * Total Bit Error/Count is calculated as the sum of the
  1526. * bit errors on all active layers.
  1527. */
  1528. c->post_bit_error.stat[0].scale = FE_SCALE_COUNTER;
  1529. c->post_bit_error.stat[0].uvalue = t_post_bit_error;
  1530. c->post_bit_count.stat[0].scale = FE_SCALE_COUNTER;
  1531. c->post_bit_count.stat[0].uvalue = t_post_bit_count;
  1532. } else {
  1533. c->post_bit_error.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
  1534. c->post_bit_count.stat[0].scale = FE_SCALE_COUNTER;
  1535. }
  1536. if (per_layers) {
  1537. /*
  1538. * At least one per-layer UCB measure was read. We can now
  1539. * calculate the total UCB
  1540. *
  1541. * Total block Error/Count is calculated as the sum of the
  1542. * block errors on all active layers.
  1543. */
  1544. c->block_error.stat[0].scale = FE_SCALE_COUNTER;
  1545. c->block_error.stat[0].uvalue = t_block_error;
  1546. c->block_count.stat[0].scale = FE_SCALE_COUNTER;
  1547. c->block_count.stat[0].uvalue = t_block_count;
  1548. } else {
  1549. c->block_error.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
  1550. c->block_count.stat[0].scale = FE_SCALE_COUNTER;
  1551. }
  1552. return rc;
  1553. }
  1554. /*
  1555. * The functions below are called via DVB callbacks, so they need to
  1556. * properly use the I2C gate control
  1557. */
  1558. static int mb86a20s_initfe(struct dvb_frontend *fe)
  1559. {
  1560. struct mb86a20s_state *state = fe->demodulator_priv;
  1561. u64 pll;
  1562. u32 fclk;
  1563. int rc;
  1564. u8 regD5 = 1, reg71, reg09 = 0x3a;
  1565. dev_dbg(&state->i2c->dev, "%s called.\n", __func__);
  1566. if (fe->ops.i2c_gate_ctrl)
  1567. fe->ops.i2c_gate_ctrl(fe, 0);
  1568. /* Initialize the frontend */
  1569. rc = mb86a20s_writeregdata(state, mb86a20s_init1);
  1570. if (rc < 0)
  1571. goto err;
  1572. if (!state->inversion)
  1573. reg09 |= 0x04;
  1574. rc = mb86a20s_writereg(state, 0x09, reg09);
  1575. if (rc < 0)
  1576. goto err;
  1577. if (!state->bw)
  1578. reg71 = 1;
  1579. else
  1580. reg71 = 0;
  1581. rc = mb86a20s_writereg(state, 0x39, reg71);
  1582. if (rc < 0)
  1583. goto err;
  1584. rc = mb86a20s_writereg(state, 0x71, state->bw);
  1585. if (rc < 0)
  1586. goto err;
  1587. if (state->subchannel) {
  1588. rc = mb86a20s_writereg(state, 0x44, state->subchannel);
  1589. if (rc < 0)
  1590. goto err;
  1591. }
  1592. fclk = state->config->fclk;
  1593. if (!fclk)
  1594. fclk = 32571428;
  1595. /* Adjust IF frequency to match tuner */
  1596. if (fe->ops.tuner_ops.get_if_frequency)
  1597. fe->ops.tuner_ops.get_if_frequency(fe, &state->if_freq);
  1598. if (!state->if_freq)
  1599. state->if_freq = 3300000;
  1600. pll = (((u64)1) << 34) * state->if_freq;
  1601. do_div(pll, 63 * fclk);
  1602. pll = (1 << 25) - pll;
  1603. rc = mb86a20s_writereg(state, 0x28, 0x2a);
  1604. if (rc < 0)
  1605. goto err;
  1606. rc = mb86a20s_writereg(state, 0x29, (pll >> 16) & 0xff);
  1607. if (rc < 0)
  1608. goto err;
  1609. rc = mb86a20s_writereg(state, 0x2a, (pll >> 8) & 0xff);
  1610. if (rc < 0)
  1611. goto err;
  1612. rc = mb86a20s_writereg(state, 0x2b, pll & 0xff);
  1613. if (rc < 0)
  1614. goto err;
  1615. dev_dbg(&state->i2c->dev, "%s: fclk=%d, IF=%d, clock reg=0x%06llx\n",
  1616. __func__, fclk, state->if_freq, (long long)pll);
  1617. /* pll = freq[Hz] * 2^24/10^6 / 16.285714286 */
  1618. pll = state->if_freq * 1677721600L;
  1619. do_div(pll, 1628571429L);
  1620. rc = mb86a20s_writereg(state, 0x28, 0x20);
  1621. if (rc < 0)
  1622. goto err;
  1623. rc = mb86a20s_writereg(state, 0x29, (pll >> 16) & 0xff);
  1624. if (rc < 0)
  1625. goto err;
  1626. rc = mb86a20s_writereg(state, 0x2a, (pll >> 8) & 0xff);
  1627. if (rc < 0)
  1628. goto err;
  1629. rc = mb86a20s_writereg(state, 0x2b, pll & 0xff);
  1630. if (rc < 0)
  1631. goto err;
  1632. dev_dbg(&state->i2c->dev, "%s: IF=%d, IF reg=0x%06llx\n",
  1633. __func__, state->if_freq, (long long)pll);
  1634. if (!state->config->is_serial) {
  1635. regD5 &= ~1;
  1636. rc = mb86a20s_writereg(state, 0x50, 0xd5);
  1637. if (rc < 0)
  1638. goto err;
  1639. rc = mb86a20s_writereg(state, 0x51, regD5);
  1640. if (rc < 0)
  1641. goto err;
  1642. }
  1643. rc = mb86a20s_writeregdata(state, mb86a20s_init2);
  1644. if (rc < 0)
  1645. goto err;
  1646. err:
  1647. if (fe->ops.i2c_gate_ctrl)
  1648. fe->ops.i2c_gate_ctrl(fe, 1);
  1649. if (rc < 0) {
  1650. state->need_init = true;
  1651. dev_info(&state->i2c->dev,
  1652. "mb86a20s: Init failed. Will try again later\n");
  1653. } else {
  1654. state->need_init = false;
  1655. dev_dbg(&state->i2c->dev, "Initialization succeeded.\n");
  1656. }
  1657. return rc;
  1658. }
  1659. static int mb86a20s_set_frontend(struct dvb_frontend *fe)
  1660. {
  1661. struct mb86a20s_state *state = fe->demodulator_priv;
  1662. struct dtv_frontend_properties *c = &fe->dtv_property_cache;
  1663. int rc, if_freq;
  1664. dev_dbg(&state->i2c->dev, "%s called.\n", __func__);
  1665. if (!c->isdbt_layer_enabled)
  1666. c->isdbt_layer_enabled = 7;
  1667. if (c->isdbt_layer_enabled == 1)
  1668. state->bw = MB86A20S_1SEG;
  1669. else if (c->isdbt_partial_reception)
  1670. state->bw = MB86A20S_13SEG_PARTIAL;
  1671. else
  1672. state->bw = MB86A20S_13SEG;
  1673. if (c->inversion == INVERSION_ON)
  1674. state->inversion = true;
  1675. else
  1676. state->inversion = false;
  1677. if (!c->isdbt_sb_mode) {
  1678. state->subchannel = 0;
  1679. } else {
  1680. if (c->isdbt_sb_subchannel >= ARRAY_SIZE(mb86a20s_subchannel))
  1681. c->isdbt_sb_subchannel = 0;
  1682. state->subchannel = mb86a20s_subchannel[c->isdbt_sb_subchannel];
  1683. }
  1684. /*
  1685. * Gate should already be opened, but it doesn't hurt to
  1686. * double-check
  1687. */
  1688. if (fe->ops.i2c_gate_ctrl)
  1689. fe->ops.i2c_gate_ctrl(fe, 1);
  1690. fe->ops.tuner_ops.set_params(fe);
  1691. if (fe->ops.tuner_ops.get_if_frequency)
  1692. fe->ops.tuner_ops.get_if_frequency(fe, &if_freq);
  1693. /*
  1694. * Make it more reliable: if, for some reason, the initial
  1695. * device initialization doesn't happen, initialize it when
  1696. * a SBTVD parameters are adjusted.
  1697. *
  1698. * Unfortunately, due to a hard to track bug at tda829x/tda18271,
  1699. * the agc callback logic is not called during DVB attach time,
  1700. * causing mb86a20s to not be initialized with Kworld SBTVD.
  1701. * So, this hack is needed, in order to make Kworld SBTVD to work.
  1702. *
  1703. * It is also needed to change the IF after the initial init.
  1704. *
  1705. * HACK: Always init the frontend when set_frontend is called:
  1706. * it was noticed that, on some devices, it fails to lock on a
  1707. * different channel. So, it is better to reset everything, even
  1708. * wasting some time, than to loose channel lock.
  1709. */
  1710. mb86a20s_initfe(fe);
  1711. if (fe->ops.i2c_gate_ctrl)
  1712. fe->ops.i2c_gate_ctrl(fe, 0);
  1713. rc = mb86a20s_writeregdata(state, mb86a20s_reset_reception);
  1714. mb86a20s_reset_counters(fe);
  1715. mb86a20s_stats_not_ready(fe);
  1716. if (fe->ops.i2c_gate_ctrl)
  1717. fe->ops.i2c_gate_ctrl(fe, 1);
  1718. return rc;
  1719. }
  1720. static int mb86a20s_read_status_and_stats(struct dvb_frontend *fe,
  1721. fe_status_t *status)
  1722. {
  1723. struct mb86a20s_state *state = fe->demodulator_priv;
  1724. int rc, status_nr;
  1725. dev_dbg(&state->i2c->dev, "%s called.\n", __func__);
  1726. if (fe->ops.i2c_gate_ctrl)
  1727. fe->ops.i2c_gate_ctrl(fe, 0);
  1728. /* Get lock */
  1729. status_nr = mb86a20s_read_status(fe, status);
  1730. if (status_nr < 7) {
  1731. mb86a20s_stats_not_ready(fe);
  1732. mb86a20s_reset_frontend_cache(fe);
  1733. }
  1734. if (status_nr < 0) {
  1735. dev_err(&state->i2c->dev,
  1736. "%s: Can't read frontend lock status\n", __func__);
  1737. goto error;
  1738. }
  1739. /* Get signal strength */
  1740. rc = mb86a20s_read_signal_strength(fe);
  1741. if (rc < 0) {
  1742. dev_err(&state->i2c->dev,
  1743. "%s: Can't reset VBER registers.\n", __func__);
  1744. mb86a20s_stats_not_ready(fe);
  1745. mb86a20s_reset_frontend_cache(fe);
  1746. rc = 0; /* Status is OK */
  1747. goto error;
  1748. }
  1749. if (status_nr >= 7) {
  1750. /* Get TMCC info*/
  1751. rc = mb86a20s_get_frontend(fe);
  1752. if (rc < 0) {
  1753. dev_err(&state->i2c->dev,
  1754. "%s: Can't get FE TMCC data.\n", __func__);
  1755. rc = 0; /* Status is OK */
  1756. goto error;
  1757. }
  1758. /* Get statistics */
  1759. rc = mb86a20s_get_stats(fe, status_nr);
  1760. if (rc < 0 && rc != -EBUSY) {
  1761. dev_err(&state->i2c->dev,
  1762. "%s: Can't get FE statistics.\n", __func__);
  1763. rc = 0;
  1764. goto error;
  1765. }
  1766. rc = 0; /* Don't return EBUSY to userspace */
  1767. }
  1768. goto ok;
  1769. error:
  1770. mb86a20s_stats_not_ready(fe);
  1771. ok:
  1772. if (fe->ops.i2c_gate_ctrl)
  1773. fe->ops.i2c_gate_ctrl(fe, 1);
  1774. return rc;
  1775. }
  1776. static int mb86a20s_read_signal_strength_from_cache(struct dvb_frontend *fe,
  1777. u16 *strength)
  1778. {
  1779. struct dtv_frontend_properties *c = &fe->dtv_property_cache;
  1780. *strength = c->strength.stat[0].uvalue;
  1781. return 0;
  1782. }
  1783. static int mb86a20s_get_frontend_dummy(struct dvb_frontend *fe)
  1784. {
  1785. /*
  1786. * get_frontend is now handled together with other stats
  1787. * retrival, when read_status() is called, as some statistics
  1788. * will depend on the layers detection.
  1789. */
  1790. return 0;
  1791. };
  1792. static int mb86a20s_tune(struct dvb_frontend *fe,
  1793. bool re_tune,
  1794. unsigned int mode_flags,
  1795. unsigned int *delay,
  1796. fe_status_t *status)
  1797. {
  1798. struct mb86a20s_state *state = fe->demodulator_priv;
  1799. int rc = 0;
  1800. dev_dbg(&state->i2c->dev, "%s called.\n", __func__);
  1801. if (re_tune)
  1802. rc = mb86a20s_set_frontend(fe);
  1803. if (!(mode_flags & FE_TUNE_MODE_ONESHOT))
  1804. mb86a20s_read_status_and_stats(fe, status);
  1805. return rc;
  1806. }
  1807. static void mb86a20s_release(struct dvb_frontend *fe)
  1808. {
  1809. struct mb86a20s_state *state = fe->demodulator_priv;
  1810. dev_dbg(&state->i2c->dev, "%s called.\n", __func__);
  1811. kfree(state);
  1812. }
  1813. static struct dvb_frontend_ops mb86a20s_ops;
  1814. struct dvb_frontend *mb86a20s_attach(const struct mb86a20s_config *config,
  1815. struct i2c_adapter *i2c)
  1816. {
  1817. struct mb86a20s_state *state;
  1818. u8 rev;
  1819. dev_dbg(&i2c->dev, "%s called.\n", __func__);
  1820. /* allocate memory for the internal state */
  1821. state = kzalloc(sizeof(struct mb86a20s_state), GFP_KERNEL);
  1822. if (state == NULL) {
  1823. dev_err(&i2c->dev,
  1824. "%s: unable to allocate memory for state\n", __func__);
  1825. goto error;
  1826. }
  1827. /* setup the state */
  1828. state->config = config;
  1829. state->i2c = i2c;
  1830. /* create dvb_frontend */
  1831. memcpy(&state->frontend.ops, &mb86a20s_ops,
  1832. sizeof(struct dvb_frontend_ops));
  1833. state->frontend.demodulator_priv = state;
  1834. /* Check if it is a mb86a20s frontend */
  1835. rev = mb86a20s_readreg(state, 0);
  1836. if (rev == 0x13) {
  1837. dev_info(&i2c->dev,
  1838. "Detected a Fujitsu mb86a20s frontend\n");
  1839. } else {
  1840. dev_dbg(&i2c->dev,
  1841. "Frontend revision %d is unknown - aborting.\n",
  1842. rev);
  1843. goto error;
  1844. }
  1845. return &state->frontend;
  1846. error:
  1847. kfree(state);
  1848. return NULL;
  1849. }
  1850. EXPORT_SYMBOL(mb86a20s_attach);
  1851. static struct dvb_frontend_ops mb86a20s_ops = {
  1852. .delsys = { SYS_ISDBT },
  1853. /* Use dib8000 values per default */
  1854. .info = {
  1855. .name = "Fujitsu mb86A20s",
  1856. .caps = FE_CAN_RECOVER |
  1857. FE_CAN_FEC_1_2 | FE_CAN_FEC_2_3 | FE_CAN_FEC_3_4 |
  1858. FE_CAN_FEC_5_6 | FE_CAN_FEC_7_8 | FE_CAN_FEC_AUTO |
  1859. FE_CAN_QPSK | FE_CAN_QAM_16 | FE_CAN_QAM_64 |
  1860. FE_CAN_TRANSMISSION_MODE_AUTO | FE_CAN_QAM_AUTO |
  1861. FE_CAN_GUARD_INTERVAL_AUTO | FE_CAN_HIERARCHY_AUTO,
  1862. /* Actually, those values depend on the used tuner */
  1863. .frequency_min = 45000000,
  1864. .frequency_max = 864000000,
  1865. .frequency_stepsize = 62500,
  1866. },
  1867. .release = mb86a20s_release,
  1868. .init = mb86a20s_initfe,
  1869. .set_frontend = mb86a20s_set_frontend,
  1870. .get_frontend = mb86a20s_get_frontend_dummy,
  1871. .read_status = mb86a20s_read_status_and_stats,
  1872. .read_signal_strength = mb86a20s_read_signal_strength_from_cache,
  1873. .tune = mb86a20s_tune,
  1874. };
  1875. MODULE_DESCRIPTION("DVB Frontend module for Fujitsu mb86A20s hardware");
  1876. MODULE_AUTHOR("Mauro Carvalho Chehab <mchehab@redhat.com>");
  1877. MODULE_LICENSE("GPL");