mb86a20s.c 53 KB

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