mb86a20s.c 51 KB

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