mb86a20s.c 53 KB

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