mb86a20s.c 51 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084
  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->cnr, 0, sizeof(c->cnr));
  662. memset(&c->pre_bit_error, 0, sizeof(c->pre_bit_error));
  663. memset(&c->pre_bit_count, 0, sizeof(c->pre_bit_count));
  664. memset(&c->post_bit_error, 0, sizeof(c->post_bit_error));
  665. memset(&c->post_bit_count, 0, sizeof(c->post_bit_count));
  666. memset(&c->block_error, 0, sizeof(c->block_error));
  667. memset(&c->block_count, 0, sizeof(c->block_count));
  668. state->last_frequency = c->frequency;
  669. }
  670. /* Clear status for most stats */
  671. /* BER/PER counter reset */
  672. rc = mb86a20s_writeregdata(state, mb86a20s_per_ber_reset);
  673. if (rc < 0)
  674. goto err;
  675. /* CNR counter reset */
  676. rc = mb86a20s_readreg(state, 0x45);
  677. if (rc < 0)
  678. goto err;
  679. val = rc;
  680. rc = mb86a20s_writereg(state, 0x45, val | 0x10);
  681. if (rc < 0)
  682. goto err;
  683. rc = mb86a20s_writereg(state, 0x45, val & 0x6f);
  684. if (rc < 0)
  685. goto err;
  686. /* MER counter reset */
  687. rc = mb86a20s_writereg(state, 0x50, 0x50);
  688. if (rc < 0)
  689. goto err;
  690. rc = mb86a20s_readreg(state, 0x51);
  691. if (rc < 0)
  692. goto err;
  693. val = rc;
  694. rc = mb86a20s_writereg(state, 0x51, val | 0x01);
  695. if (rc < 0)
  696. goto err;
  697. rc = mb86a20s_writereg(state, 0x51, val & 0x06);
  698. if (rc < 0)
  699. goto err;
  700. goto ok;
  701. err:
  702. dev_err(&state->i2c->dev,
  703. "%s: Can't reset FE statistics (error %d).\n",
  704. __func__, rc);
  705. ok:
  706. return rc;
  707. }
  708. static int mb86a20s_get_pre_ber(struct dvb_frontend *fe,
  709. unsigned layer,
  710. u32 *error, u32 *count)
  711. {
  712. struct mb86a20s_state *state = fe->demodulator_priv;
  713. int rc, val;
  714. dev_dbg(&state->i2c->dev, "%s called.\n", __func__);
  715. if (layer >= 3)
  716. return -EINVAL;
  717. /* Check if the BER measures are already available */
  718. rc = mb86a20s_readreg(state, 0x54);
  719. if (rc < 0)
  720. return rc;
  721. /* Check if data is available for that layer */
  722. if (!(rc & (1 << layer))) {
  723. dev_dbg(&state->i2c->dev,
  724. "%s: preBER for layer %c is not available yet.\n",
  725. __func__, 'A' + layer);
  726. return -EBUSY;
  727. }
  728. /* Read Bit Error Count */
  729. rc = mb86a20s_readreg(state, 0x55 + layer * 3);
  730. if (rc < 0)
  731. return rc;
  732. *error = rc << 16;
  733. rc = mb86a20s_readreg(state, 0x56 + layer * 3);
  734. if (rc < 0)
  735. return rc;
  736. *error |= rc << 8;
  737. rc = mb86a20s_readreg(state, 0x57 + layer * 3);
  738. if (rc < 0)
  739. return rc;
  740. *error |= rc;
  741. dev_dbg(&state->i2c->dev,
  742. "%s: bit error before Viterbi for layer %c: %d.\n",
  743. __func__, 'A' + layer, *error);
  744. /* Read Bit Count */
  745. rc = mb86a20s_writereg(state, 0x50, 0xa7 + layer * 3);
  746. if (rc < 0)
  747. return rc;
  748. rc = mb86a20s_readreg(state, 0x51);
  749. if (rc < 0)
  750. return rc;
  751. *count = rc << 16;
  752. rc = mb86a20s_writereg(state, 0x50, 0xa8 + layer * 3);
  753. if (rc < 0)
  754. return rc;
  755. rc = mb86a20s_readreg(state, 0x51);
  756. if (rc < 0)
  757. return rc;
  758. *count |= rc << 8;
  759. rc = mb86a20s_writereg(state, 0x50, 0xa9 + layer * 3);
  760. if (rc < 0)
  761. return rc;
  762. rc = mb86a20s_readreg(state, 0x51);
  763. if (rc < 0)
  764. return rc;
  765. *count |= rc;
  766. dev_dbg(&state->i2c->dev,
  767. "%s: bit count before Viterbi for layer %c: %d.\n",
  768. __func__, 'A' + layer, *count);
  769. /*
  770. * As we get TMCC data from the frontend, we can better estimate the
  771. * BER bit counters, in order to do the BER measure during a longer
  772. * time. Use those data, if available, to update the bit count
  773. * measure.
  774. */
  775. if (state->estimated_rate[layer]
  776. && state->estimated_rate[layer] != *count) {
  777. dev_dbg(&state->i2c->dev,
  778. "%s: updating layer %c preBER counter to %d.\n",
  779. __func__, 'A' + layer, state->estimated_rate[layer]);
  780. /* Turn off BER before Viterbi */
  781. rc = mb86a20s_writereg(state, 0x52, 0x00);
  782. /* Update counter for this layer */
  783. rc = mb86a20s_writereg(state, 0x50, 0xa7 + layer * 3);
  784. if (rc < 0)
  785. return rc;
  786. rc = mb86a20s_writereg(state, 0x51,
  787. state->estimated_rate[layer] >> 16);
  788. if (rc < 0)
  789. return rc;
  790. rc = mb86a20s_writereg(state, 0x50, 0xa8 + layer * 3);
  791. if (rc < 0)
  792. return rc;
  793. rc = mb86a20s_writereg(state, 0x51,
  794. state->estimated_rate[layer] >> 8);
  795. if (rc < 0)
  796. return rc;
  797. rc = mb86a20s_writereg(state, 0x50, 0xa9 + layer * 3);
  798. if (rc < 0)
  799. return rc;
  800. rc = mb86a20s_writereg(state, 0x51,
  801. state->estimated_rate[layer]);
  802. if (rc < 0)
  803. return rc;
  804. /* Turn on BER before Viterbi */
  805. rc = mb86a20s_writereg(state, 0x52, 0x01);
  806. /* Reset all preBER counters */
  807. rc = mb86a20s_writereg(state, 0x53, 0x00);
  808. if (rc < 0)
  809. return rc;
  810. rc = mb86a20s_writereg(state, 0x53, 0x07);
  811. } else {
  812. /* Reset counter to collect new data */
  813. rc = mb86a20s_readreg(state, 0x53);
  814. if (rc < 0)
  815. return rc;
  816. val = rc;
  817. rc = mb86a20s_writereg(state, 0x53, val & ~(1 << layer));
  818. if (rc < 0)
  819. return rc;
  820. rc = mb86a20s_writereg(state, 0x53, val | (1 << layer));
  821. }
  822. return rc;
  823. }
  824. static int mb86a20s_get_post_ber(struct dvb_frontend *fe,
  825. unsigned layer,
  826. u32 *error, u32 *count)
  827. {
  828. struct mb86a20s_state *state = fe->demodulator_priv;
  829. u32 counter, collect_rate;
  830. int rc, val;
  831. dev_dbg(&state->i2c->dev, "%s called.\n", __func__);
  832. if (layer >= 3)
  833. return -EINVAL;
  834. /* Check if the BER measures are already available */
  835. rc = mb86a20s_readreg(state, 0x60);
  836. if (rc < 0)
  837. return rc;
  838. /* Check if data is available for that layer */
  839. if (!(rc & (1 << layer))) {
  840. dev_dbg(&state->i2c->dev,
  841. "%s: post BER for layer %c is not available yet.\n",
  842. __func__, 'A' + layer);
  843. return -EBUSY;
  844. }
  845. /* Read Bit Error Count */
  846. rc = mb86a20s_readreg(state, 0x64 + layer * 3);
  847. if (rc < 0)
  848. return rc;
  849. *error = rc << 16;
  850. rc = mb86a20s_readreg(state, 0x65 + layer * 3);
  851. if (rc < 0)
  852. return rc;
  853. *error |= rc << 8;
  854. rc = mb86a20s_readreg(state, 0x66 + layer * 3);
  855. if (rc < 0)
  856. return rc;
  857. *error |= rc;
  858. dev_dbg(&state->i2c->dev,
  859. "%s: post bit error for layer %c: %d.\n",
  860. __func__, 'A' + layer, *error);
  861. /* Read Bit Count */
  862. rc = mb86a20s_writereg(state, 0x50, 0xdc + layer * 2);
  863. if (rc < 0)
  864. return rc;
  865. rc = mb86a20s_readreg(state, 0x51);
  866. if (rc < 0)
  867. return rc;
  868. counter = rc << 8;
  869. rc = mb86a20s_writereg(state, 0x50, 0xdd + layer * 2);
  870. if (rc < 0)
  871. return rc;
  872. rc = mb86a20s_readreg(state, 0x51);
  873. if (rc < 0)
  874. return rc;
  875. counter |= rc;
  876. *count = counter * 204 * 8;
  877. dev_dbg(&state->i2c->dev,
  878. "%s: post bit count for layer %c: %d.\n",
  879. __func__, 'A' + layer, *count);
  880. /*
  881. * As we get TMCC data from the frontend, we can better estimate the
  882. * BER bit counters, in order to do the BER measure during a longer
  883. * time. Use those data, if available, to update the bit count
  884. * measure.
  885. */
  886. if (!state->estimated_rate[layer])
  887. goto reset_measurement;
  888. collect_rate = state->estimated_rate[layer] / 204 / 8;
  889. if (collect_rate < 32)
  890. collect_rate = 32;
  891. if (collect_rate > 65535)
  892. collect_rate = 65535;
  893. if (collect_rate != counter) {
  894. dev_dbg(&state->i2c->dev,
  895. "%s: updating postBER counter on layer %c to %d.\n",
  896. __func__, 'A' + layer, collect_rate);
  897. /* Turn off BER after Viterbi */
  898. rc = mb86a20s_writereg(state, 0x5e, 0x00);
  899. /* Update counter for this layer */
  900. rc = mb86a20s_writereg(state, 0x50, 0xdc + layer * 2);
  901. if (rc < 0)
  902. return rc;
  903. rc = mb86a20s_writereg(state, 0x51, collect_rate >> 8);
  904. if (rc < 0)
  905. return rc;
  906. rc = mb86a20s_writereg(state, 0x50, 0xdd + layer * 2);
  907. if (rc < 0)
  908. return rc;
  909. rc = mb86a20s_writereg(state, 0x51, collect_rate & 0xff);
  910. if (rc < 0)
  911. return rc;
  912. /* Turn on BER after Viterbi */
  913. rc = mb86a20s_writereg(state, 0x5e, 0x07);
  914. /* Reset all preBER counters */
  915. rc = mb86a20s_writereg(state, 0x5f, 0x00);
  916. if (rc < 0)
  917. return rc;
  918. rc = mb86a20s_writereg(state, 0x5f, 0x07);
  919. return rc;
  920. }
  921. reset_measurement:
  922. /* Reset counter to collect new data */
  923. rc = mb86a20s_readreg(state, 0x5f);
  924. if (rc < 0)
  925. return rc;
  926. val = rc;
  927. rc = mb86a20s_writereg(state, 0x5f, val & ~(1 << layer));
  928. if (rc < 0)
  929. return rc;
  930. rc = mb86a20s_writereg(state, 0x5f, val | (1 << layer));
  931. return rc;
  932. }
  933. static int mb86a20s_get_blk_error(struct dvb_frontend *fe,
  934. unsigned layer,
  935. u32 *error, u32 *count)
  936. {
  937. struct mb86a20s_state *state = fe->demodulator_priv;
  938. int rc, val;
  939. u32 collect_rate;
  940. dev_dbg(&state->i2c->dev, "%s called.\n", __func__);
  941. if (layer >= 3)
  942. return -EINVAL;
  943. /* Check if the PER measures are already available */
  944. rc = mb86a20s_writereg(state, 0x50, 0xb8);
  945. if (rc < 0)
  946. return rc;
  947. rc = mb86a20s_readreg(state, 0x51);
  948. if (rc < 0)
  949. return rc;
  950. /* Check if data is available for that layer */
  951. if (!(rc & (1 << layer))) {
  952. dev_dbg(&state->i2c->dev,
  953. "%s: block counts for layer %c aren't available yet.\n",
  954. __func__, 'A' + layer);
  955. return -EBUSY;
  956. }
  957. /* Read Packet error Count */
  958. rc = mb86a20s_writereg(state, 0x50, 0xb9 + layer * 2);
  959. if (rc < 0)
  960. return rc;
  961. rc = mb86a20s_readreg(state, 0x51);
  962. if (rc < 0)
  963. return rc;
  964. *error = rc << 8;
  965. rc = mb86a20s_writereg(state, 0x50, 0xba + layer * 2);
  966. if (rc < 0)
  967. return rc;
  968. rc = mb86a20s_readreg(state, 0x51);
  969. if (rc < 0)
  970. return rc;
  971. *error |= rc;
  972. dev_dbg(&state->i2c->dev, "%s: block error for layer %c: %d.\n",
  973. __func__, 'A' + layer, *error);
  974. /* Read Bit Count */
  975. rc = mb86a20s_writereg(state, 0x50, 0xb2 + layer * 2);
  976. if (rc < 0)
  977. return rc;
  978. rc = mb86a20s_readreg(state, 0x51);
  979. if (rc < 0)
  980. return rc;
  981. *count = rc << 8;
  982. rc = mb86a20s_writereg(state, 0x50, 0xb3 + layer * 2);
  983. if (rc < 0)
  984. return rc;
  985. rc = mb86a20s_readreg(state, 0x51);
  986. if (rc < 0)
  987. return rc;
  988. *count |= rc;
  989. dev_dbg(&state->i2c->dev,
  990. "%s: block count for layer %c: %d.\n",
  991. __func__, 'A' + layer, *count);
  992. /*
  993. * As we get TMCC data from the frontend, we can better estimate the
  994. * BER bit counters, in order to do the BER measure during a longer
  995. * time. Use those data, if available, to update the bit count
  996. * measure.
  997. */
  998. if (!state->estimated_rate[layer])
  999. goto reset_measurement;
  1000. collect_rate = state->estimated_rate[layer] / 204 / 8;
  1001. if (collect_rate < 32)
  1002. collect_rate = 32;
  1003. if (collect_rate > 65535)
  1004. collect_rate = 65535;
  1005. if (collect_rate != *count) {
  1006. dev_dbg(&state->i2c->dev,
  1007. "%s: updating PER counter on layer %c to %d.\n",
  1008. __func__, 'A' + layer, collect_rate);
  1009. /* Stop PER measurement */
  1010. rc = mb86a20s_writereg(state, 0x50, 0xb0);
  1011. if (rc < 0)
  1012. return rc;
  1013. rc = mb86a20s_writereg(state, 0x51, 0x00);
  1014. if (rc < 0)
  1015. return rc;
  1016. /* Update this layer's counter */
  1017. rc = mb86a20s_writereg(state, 0x50, 0xb2 + layer * 2);
  1018. if (rc < 0)
  1019. return rc;
  1020. rc = mb86a20s_writereg(state, 0x51, collect_rate >> 8);
  1021. if (rc < 0)
  1022. return rc;
  1023. rc = mb86a20s_writereg(state, 0x50, 0xb3 + layer * 2);
  1024. if (rc < 0)
  1025. return rc;
  1026. rc = mb86a20s_writereg(state, 0x51, collect_rate & 0xff);
  1027. if (rc < 0)
  1028. return rc;
  1029. /* start PER measurement */
  1030. rc = mb86a20s_writereg(state, 0x50, 0xb0);
  1031. if (rc < 0)
  1032. return rc;
  1033. rc = mb86a20s_writereg(state, 0x51, 0x07);
  1034. if (rc < 0)
  1035. return rc;
  1036. /* Reset all counters to collect new data */
  1037. rc = mb86a20s_writereg(state, 0x50, 0xb1);
  1038. if (rc < 0)
  1039. return rc;
  1040. rc = mb86a20s_writereg(state, 0x51, 0x07);
  1041. if (rc < 0)
  1042. return rc;
  1043. rc = mb86a20s_writereg(state, 0x51, 0x00);
  1044. return rc;
  1045. }
  1046. reset_measurement:
  1047. /* Reset counter to collect new data */
  1048. rc = mb86a20s_writereg(state, 0x50, 0xb1);
  1049. if (rc < 0)
  1050. return rc;
  1051. rc = mb86a20s_readreg(state, 0x51);
  1052. if (rc < 0)
  1053. return rc;
  1054. val = rc;
  1055. rc = mb86a20s_writereg(state, 0x51, val | (1 << layer));
  1056. if (rc < 0)
  1057. return rc;
  1058. rc = mb86a20s_writereg(state, 0x51, val & ~(1 << layer));
  1059. return rc;
  1060. }
  1061. struct linear_segments {
  1062. unsigned x, y;
  1063. };
  1064. /*
  1065. * All tables below return a dB/1000 measurement
  1066. */
  1067. static struct linear_segments cnr_to_db_table[] = {
  1068. { 19648, 0},
  1069. { 18187, 1000},
  1070. { 16534, 2000},
  1071. { 14823, 3000},
  1072. { 13161, 4000},
  1073. { 11622, 5000},
  1074. { 10279, 6000},
  1075. { 9089, 7000},
  1076. { 8042, 8000},
  1077. { 7137, 9000},
  1078. { 6342, 10000},
  1079. { 5641, 11000},
  1080. { 5030, 12000},
  1081. { 4474, 13000},
  1082. { 3988, 14000},
  1083. { 3556, 15000},
  1084. { 3180, 16000},
  1085. { 2841, 17000},
  1086. { 2541, 18000},
  1087. { 2276, 19000},
  1088. { 2038, 20000},
  1089. { 1800, 21000},
  1090. { 1625, 22000},
  1091. { 1462, 23000},
  1092. { 1324, 24000},
  1093. { 1175, 25000},
  1094. { 1063, 26000},
  1095. { 980, 27000},
  1096. { 907, 28000},
  1097. { 840, 29000},
  1098. { 788, 30000},
  1099. };
  1100. static struct linear_segments cnr_64qam_table[] = {
  1101. { 3922688, 0},
  1102. { 3920384, 1000},
  1103. { 3902720, 2000},
  1104. { 3894784, 3000},
  1105. { 3882496, 4000},
  1106. { 3872768, 5000},
  1107. { 3858944, 6000},
  1108. { 3851520, 7000},
  1109. { 3838976, 8000},
  1110. { 3829248, 9000},
  1111. { 3818240, 10000},
  1112. { 3806976, 11000},
  1113. { 3791872, 12000},
  1114. { 3767040, 13000},
  1115. { 3720960, 14000},
  1116. { 3637504, 15000},
  1117. { 3498496, 16000},
  1118. { 3296000, 17000},
  1119. { 3031040, 18000},
  1120. { 2715392, 19000},
  1121. { 2362624, 20000},
  1122. { 1963264, 21000},
  1123. { 1649664, 22000},
  1124. { 1366784, 23000},
  1125. { 1120768, 24000},
  1126. { 890880, 25000},
  1127. { 723456, 26000},
  1128. { 612096, 27000},
  1129. { 518912, 28000},
  1130. { 448256, 29000},
  1131. { 388864, 30000},
  1132. };
  1133. static struct linear_segments cnr_16qam_table[] = {
  1134. { 5314816, 0},
  1135. { 5219072, 1000},
  1136. { 5118720, 2000},
  1137. { 4998912, 3000},
  1138. { 4875520, 4000},
  1139. { 4736000, 5000},
  1140. { 4604160, 6000},
  1141. { 4458752, 7000},
  1142. { 4300288, 8000},
  1143. { 4092928, 9000},
  1144. { 3836160, 10000},
  1145. { 3521024, 11000},
  1146. { 3155968, 12000},
  1147. { 2756864, 13000},
  1148. { 2347008, 14000},
  1149. { 1955072, 15000},
  1150. { 1593600, 16000},
  1151. { 1297920, 17000},
  1152. { 1043968, 18000},
  1153. { 839680, 19000},
  1154. { 672256, 20000},
  1155. { 523008, 21000},
  1156. { 424704, 22000},
  1157. { 345088, 23000},
  1158. { 280064, 24000},
  1159. { 221440, 25000},
  1160. { 179712, 26000},
  1161. { 151040, 27000},
  1162. { 128512, 28000},
  1163. { 110080, 29000},
  1164. { 95744, 30000},
  1165. };
  1166. struct linear_segments cnr_qpsk_table[] = {
  1167. { 2834176, 0},
  1168. { 2683648, 1000},
  1169. { 2536960, 2000},
  1170. { 2391808, 3000},
  1171. { 2133248, 4000},
  1172. { 1906176, 5000},
  1173. { 1666560, 6000},
  1174. { 1422080, 7000},
  1175. { 1189632, 8000},
  1176. { 976384, 9000},
  1177. { 790272, 10000},
  1178. { 633344, 11000},
  1179. { 505600, 12000},
  1180. { 402944, 13000},
  1181. { 320768, 14000},
  1182. { 255488, 15000},
  1183. { 204032, 16000},
  1184. { 163072, 17000},
  1185. { 130304, 18000},
  1186. { 105216, 19000},
  1187. { 83456, 20000},
  1188. { 65024, 21000},
  1189. { 52480, 22000},
  1190. { 42752, 23000},
  1191. { 34560, 24000},
  1192. { 27136, 25000},
  1193. { 22016, 26000},
  1194. { 18432, 27000},
  1195. { 15616, 28000},
  1196. { 13312, 29000},
  1197. { 11520, 30000},
  1198. };
  1199. static u32 interpolate_value(u32 value, struct linear_segments *segments,
  1200. unsigned len)
  1201. {
  1202. u64 tmp64;
  1203. u32 dx, dy;
  1204. int i, ret;
  1205. if (value >= segments[0].x)
  1206. return segments[0].y;
  1207. if (value < segments[len-1].x)
  1208. return segments[len-1].y;
  1209. for (i = 1; i < len - 1; i++) {
  1210. /* If value is identical, no need to interpolate */
  1211. if (value == segments[i].x)
  1212. return segments[i].y;
  1213. if (value > segments[i].x)
  1214. break;
  1215. }
  1216. /* Linear interpolation between the two (x,y) points */
  1217. dy = segments[i].y - segments[i - 1].y;
  1218. dx = segments[i - 1].x - segments[i].x;
  1219. tmp64 = value - segments[i].x;
  1220. tmp64 *= dy;
  1221. do_div(tmp64, dx);
  1222. ret = segments[i].y - tmp64;
  1223. return ret;
  1224. }
  1225. static int mb86a20s_get_main_CNR(struct dvb_frontend *fe)
  1226. {
  1227. struct mb86a20s_state *state = fe->demodulator_priv;
  1228. struct dtv_frontend_properties *c = &fe->dtv_property_cache;
  1229. u32 cnr_linear, cnr;
  1230. int rc, val;
  1231. /* Check if CNR is available */
  1232. rc = mb86a20s_readreg(state, 0x45);
  1233. if (rc < 0)
  1234. return rc;
  1235. if (!(rc & 0x40)) {
  1236. dev_dbg(&state->i2c->dev, "%s: CNR is not available yet.\n",
  1237. __func__);
  1238. return -EBUSY;
  1239. }
  1240. val = rc;
  1241. rc = mb86a20s_readreg(state, 0x46);
  1242. if (rc < 0)
  1243. return rc;
  1244. cnr_linear = rc << 8;
  1245. rc = mb86a20s_readreg(state, 0x46);
  1246. if (rc < 0)
  1247. return rc;
  1248. cnr_linear |= rc;
  1249. cnr = interpolate_value(cnr_linear,
  1250. cnr_to_db_table, ARRAY_SIZE(cnr_to_db_table));
  1251. c->cnr.stat[0].scale = FE_SCALE_DECIBEL;
  1252. c->cnr.stat[0].svalue = cnr;
  1253. dev_dbg(&state->i2c->dev, "%s: CNR is %d.%03d dB (%d)\n",
  1254. __func__, cnr / 1000, cnr % 1000, cnr_linear);
  1255. /* CNR counter reset */
  1256. rc = mb86a20s_writereg(state, 0x45, val | 0x10);
  1257. if (rc < 0)
  1258. return rc;
  1259. rc = mb86a20s_writereg(state, 0x45, val & 0x6f);
  1260. return rc;
  1261. }
  1262. static int mb86a20s_get_blk_error_layer_CNR(struct dvb_frontend *fe)
  1263. {
  1264. struct mb86a20s_state *state = fe->demodulator_priv;
  1265. struct dtv_frontend_properties *c = &fe->dtv_property_cache;
  1266. u32 mer, cnr;
  1267. int rc, val, i;
  1268. struct linear_segments *segs;
  1269. unsigned segs_len;
  1270. dev_dbg(&state->i2c->dev, "%s called.\n", __func__);
  1271. /* Check if the measures are already available */
  1272. rc = mb86a20s_writereg(state, 0x50, 0x5b);
  1273. if (rc < 0)
  1274. return rc;
  1275. rc = mb86a20s_readreg(state, 0x51);
  1276. if (rc < 0)
  1277. return rc;
  1278. /* Check if data is available */
  1279. if (!(rc & 0x01)) {
  1280. dev_dbg(&state->i2c->dev,
  1281. "%s: MER measures aren't available yet.\n", __func__);
  1282. return -EBUSY;
  1283. }
  1284. /* Read all layers */
  1285. for (i = 0; i < 3; i++) {
  1286. if (!(c->isdbt_layer_enabled & (1 << i))) {
  1287. c->cnr.stat[1 + i].scale = FE_SCALE_NOT_AVAILABLE;
  1288. continue;
  1289. }
  1290. rc = mb86a20s_writereg(state, 0x50, 0x52 + i * 3);
  1291. if (rc < 0)
  1292. return rc;
  1293. rc = mb86a20s_readreg(state, 0x51);
  1294. if (rc < 0)
  1295. return rc;
  1296. mer = rc << 16;
  1297. rc = mb86a20s_writereg(state, 0x50, 0x53 + i * 3);
  1298. if (rc < 0)
  1299. return rc;
  1300. rc = mb86a20s_readreg(state, 0x51);
  1301. if (rc < 0)
  1302. return rc;
  1303. mer |= rc << 8;
  1304. rc = mb86a20s_writereg(state, 0x50, 0x54 + i * 3);
  1305. if (rc < 0)
  1306. return rc;
  1307. rc = mb86a20s_readreg(state, 0x51);
  1308. if (rc < 0)
  1309. return rc;
  1310. mer |= rc;
  1311. switch (c->layer[i].modulation) {
  1312. case DQPSK:
  1313. case QPSK:
  1314. segs = cnr_qpsk_table;
  1315. segs_len = ARRAY_SIZE(cnr_qpsk_table);
  1316. break;
  1317. case QAM_16:
  1318. segs = cnr_16qam_table;
  1319. segs_len = ARRAY_SIZE(cnr_16qam_table);
  1320. break;
  1321. default:
  1322. case QAM_64:
  1323. segs = cnr_64qam_table;
  1324. segs_len = ARRAY_SIZE(cnr_64qam_table);
  1325. break;
  1326. }
  1327. cnr = interpolate_value(mer, segs, segs_len);
  1328. c->cnr.stat[1 + i].scale = FE_SCALE_DECIBEL;
  1329. c->cnr.stat[1 + i].svalue = cnr;
  1330. dev_dbg(&state->i2c->dev,
  1331. "%s: CNR for layer %c is %d.%03d dB (MER = %d).\n",
  1332. __func__, 'A' + i, cnr / 1000, cnr % 1000, mer);
  1333. }
  1334. /* Start a new MER measurement */
  1335. /* MER counter reset */
  1336. rc = mb86a20s_writereg(state, 0x50, 0x50);
  1337. if (rc < 0)
  1338. return rc;
  1339. rc = mb86a20s_readreg(state, 0x51);
  1340. if (rc < 0)
  1341. return rc;
  1342. val = rc;
  1343. rc = mb86a20s_writereg(state, 0x51, val | 0x01);
  1344. if (rc < 0)
  1345. return rc;
  1346. rc = mb86a20s_writereg(state, 0x51, val & 0x06);
  1347. if (rc < 0)
  1348. return rc;
  1349. return 0;
  1350. }
  1351. static void mb86a20s_stats_not_ready(struct dvb_frontend *fe)
  1352. {
  1353. struct mb86a20s_state *state = fe->demodulator_priv;
  1354. struct dtv_frontend_properties *c = &fe->dtv_property_cache;
  1355. int i;
  1356. dev_dbg(&state->i2c->dev, "%s called.\n", __func__);
  1357. /* Fill the length of each status counter */
  1358. /* Only global stats */
  1359. c->strength.len = 1;
  1360. /* Per-layer stats - 3 layers + global */
  1361. c->cnr.len = 4;
  1362. c->pre_bit_error.len = 4;
  1363. c->pre_bit_count.len = 4;
  1364. c->post_bit_error.len = 4;
  1365. c->post_bit_count.len = 4;
  1366. c->block_error.len = 4;
  1367. c->block_count.len = 4;
  1368. /* Signal is always available */
  1369. c->strength.stat[0].scale = FE_SCALE_RELATIVE;
  1370. c->strength.stat[0].uvalue = 0;
  1371. /* Put all of them at FE_SCALE_NOT_AVAILABLE */
  1372. for (i = 0; i < 4; i++) {
  1373. c->cnr.stat[i].scale = FE_SCALE_NOT_AVAILABLE;
  1374. c->pre_bit_error.stat[i].scale = FE_SCALE_NOT_AVAILABLE;
  1375. c->pre_bit_count.stat[i].scale = FE_SCALE_NOT_AVAILABLE;
  1376. c->post_bit_error.stat[i].scale = FE_SCALE_NOT_AVAILABLE;
  1377. c->post_bit_count.stat[i].scale = FE_SCALE_NOT_AVAILABLE;
  1378. c->block_error.stat[i].scale = FE_SCALE_NOT_AVAILABLE;
  1379. c->block_count.stat[i].scale = FE_SCALE_NOT_AVAILABLE;
  1380. }
  1381. }
  1382. static int mb86a20s_get_stats(struct dvb_frontend *fe, int status_nr)
  1383. {
  1384. struct mb86a20s_state *state = fe->demodulator_priv;
  1385. struct dtv_frontend_properties *c = &fe->dtv_property_cache;
  1386. int rc = 0, i;
  1387. u32 bit_error = 0, bit_count = 0;
  1388. u32 t_pre_bit_error = 0, t_pre_bit_count = 0;
  1389. u32 t_post_bit_error = 0, t_post_bit_count = 0;
  1390. u32 block_error = 0, block_count = 0;
  1391. u32 t_block_error = 0, t_block_count = 0;
  1392. int active_layers = 0, pre_ber_layers = 0, post_ber_layers = 0;
  1393. int per_layers = 0;
  1394. dev_dbg(&state->i2c->dev, "%s called.\n", __func__);
  1395. mb86a20s_get_main_CNR(fe);
  1396. /* Get per-layer stats */
  1397. mb86a20s_get_blk_error_layer_CNR(fe);
  1398. /*
  1399. * At state 7, only CNR is available
  1400. * For BER measures, state=9 is required
  1401. * FIXME: we may get MER measures with state=8
  1402. */
  1403. if (status_nr < 9)
  1404. return 0;
  1405. for (i = 0; i < 3; i++) {
  1406. if (c->isdbt_layer_enabled & (1 << i)) {
  1407. /* Layer is active and has rc segments */
  1408. active_layers++;
  1409. /* Handle BER before vterbi */
  1410. rc = mb86a20s_get_pre_ber(fe, i,
  1411. &bit_error, &bit_count);
  1412. if (rc >= 0) {
  1413. c->pre_bit_error.stat[1 + i].scale = FE_SCALE_COUNTER;
  1414. c->pre_bit_error.stat[1 + i].uvalue += bit_error;
  1415. c->pre_bit_count.stat[1 + i].scale = FE_SCALE_COUNTER;
  1416. c->pre_bit_count.stat[1 + i].uvalue += bit_count;
  1417. } else if (rc != -EBUSY) {
  1418. /*
  1419. * If an I/O error happened,
  1420. * measures are now unavailable
  1421. */
  1422. c->pre_bit_error.stat[1 + i].scale = FE_SCALE_NOT_AVAILABLE;
  1423. c->pre_bit_count.stat[1 + i].scale = FE_SCALE_NOT_AVAILABLE;
  1424. dev_err(&state->i2c->dev,
  1425. "%s: Can't get BER for layer %c (error %d).\n",
  1426. __func__, 'A' + i, rc);
  1427. }
  1428. if (c->block_error.stat[1 + i].scale != FE_SCALE_NOT_AVAILABLE)
  1429. pre_ber_layers++;
  1430. /* Handle BER post vterbi */
  1431. rc = mb86a20s_get_post_ber(fe, i,
  1432. &bit_error, &bit_count);
  1433. if (rc >= 0) {
  1434. c->post_bit_error.stat[1 + i].scale = FE_SCALE_COUNTER;
  1435. c->post_bit_error.stat[1 + i].uvalue += bit_error;
  1436. c->post_bit_count.stat[1 + i].scale = FE_SCALE_COUNTER;
  1437. c->post_bit_count.stat[1 + i].uvalue += bit_count;
  1438. } else if (rc != -EBUSY) {
  1439. /*
  1440. * If an I/O error happened,
  1441. * measures are now unavailable
  1442. */
  1443. c->post_bit_error.stat[1 + i].scale = FE_SCALE_NOT_AVAILABLE;
  1444. c->post_bit_count.stat[1 + i].scale = FE_SCALE_NOT_AVAILABLE;
  1445. dev_err(&state->i2c->dev,
  1446. "%s: Can't get BER for layer %c (error %d).\n",
  1447. __func__, 'A' + i, rc);
  1448. }
  1449. if (c->block_error.stat[1 + i].scale != FE_SCALE_NOT_AVAILABLE)
  1450. post_ber_layers++;
  1451. /* Handle Block errors for PER/UCB reports */
  1452. rc = mb86a20s_get_blk_error(fe, i,
  1453. &block_error,
  1454. &block_count);
  1455. if (rc >= 0) {
  1456. c->block_error.stat[1 + i].scale = FE_SCALE_COUNTER;
  1457. c->block_error.stat[1 + i].uvalue += block_error;
  1458. c->block_count.stat[1 + i].scale = FE_SCALE_COUNTER;
  1459. c->block_count.stat[1 + i].uvalue += block_count;
  1460. } else if (rc != -EBUSY) {
  1461. /*
  1462. * If an I/O error happened,
  1463. * measures are now unavailable
  1464. */
  1465. c->block_error.stat[1 + i].scale = FE_SCALE_NOT_AVAILABLE;
  1466. c->block_count.stat[1 + i].scale = FE_SCALE_NOT_AVAILABLE;
  1467. dev_err(&state->i2c->dev,
  1468. "%s: Can't get PER for layer %c (error %d).\n",
  1469. __func__, 'A' + i, rc);
  1470. }
  1471. if (c->block_error.stat[1 + i].scale != FE_SCALE_NOT_AVAILABLE)
  1472. per_layers++;
  1473. /* Update total preBER */
  1474. t_pre_bit_error += c->pre_bit_error.stat[1 + i].uvalue;
  1475. t_pre_bit_count += c->pre_bit_count.stat[1 + i].uvalue;
  1476. /* Update total postBER */
  1477. t_post_bit_error += c->post_bit_error.stat[1 + i].uvalue;
  1478. t_post_bit_count += c->post_bit_count.stat[1 + i].uvalue;
  1479. /* Update total PER */
  1480. t_block_error += c->block_error.stat[1 + i].uvalue;
  1481. t_block_count += c->block_count.stat[1 + i].uvalue;
  1482. }
  1483. }
  1484. /*
  1485. * Start showing global count if at least one error count is
  1486. * available.
  1487. */
  1488. if (pre_ber_layers) {
  1489. /*
  1490. * At least one per-layer BER measure was read. We can now
  1491. * calculate the total BER
  1492. *
  1493. * Total Bit Error/Count is calculated as the sum of the
  1494. * bit errors on all active layers.
  1495. */
  1496. c->pre_bit_error.stat[0].scale = FE_SCALE_COUNTER;
  1497. c->pre_bit_error.stat[0].uvalue = t_pre_bit_error;
  1498. c->pre_bit_count.stat[0].scale = FE_SCALE_COUNTER;
  1499. c->pre_bit_count.stat[0].uvalue = t_pre_bit_count;
  1500. } else {
  1501. c->pre_bit_error.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
  1502. c->pre_bit_count.stat[0].scale = FE_SCALE_COUNTER;
  1503. }
  1504. /*
  1505. * Start showing global count if at least one error count is
  1506. * available.
  1507. */
  1508. if (post_ber_layers) {
  1509. /*
  1510. * At least one per-layer BER measure was read. We can now
  1511. * calculate the total BER
  1512. *
  1513. * Total Bit Error/Count is calculated as the sum of the
  1514. * bit errors on all active layers.
  1515. */
  1516. c->post_bit_error.stat[0].scale = FE_SCALE_COUNTER;
  1517. c->post_bit_error.stat[0].uvalue = t_post_bit_error;
  1518. c->post_bit_count.stat[0].scale = FE_SCALE_COUNTER;
  1519. c->post_bit_count.stat[0].uvalue = t_post_bit_count;
  1520. } else {
  1521. c->post_bit_error.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
  1522. c->post_bit_count.stat[0].scale = FE_SCALE_COUNTER;
  1523. }
  1524. if (per_layers) {
  1525. /*
  1526. * At least one per-layer UCB measure was read. We can now
  1527. * calculate the total UCB
  1528. *
  1529. * Total block Error/Count is calculated as the sum of the
  1530. * block errors on all active layers.
  1531. */
  1532. c->block_error.stat[0].scale = FE_SCALE_COUNTER;
  1533. c->block_error.stat[0].uvalue = t_block_error;
  1534. c->block_count.stat[0].scale = FE_SCALE_COUNTER;
  1535. c->block_count.stat[0].uvalue = t_block_count;
  1536. } else {
  1537. c->block_error.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
  1538. c->block_count.stat[0].scale = FE_SCALE_COUNTER;
  1539. }
  1540. return rc;
  1541. }
  1542. /*
  1543. * The functions below are called via DVB callbacks, so they need to
  1544. * properly use the I2C gate control
  1545. */
  1546. static int mb86a20s_initfe(struct dvb_frontend *fe)
  1547. {
  1548. struct mb86a20s_state *state = fe->demodulator_priv;
  1549. u64 pll;
  1550. int rc;
  1551. u8 regD5 = 1;
  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. /* Adjust IF frequency to match tuner */
  1560. if (fe->ops.tuner_ops.get_if_frequency)
  1561. fe->ops.tuner_ops.get_if_frequency(fe, &state->if_freq);
  1562. if (!state->if_freq)
  1563. state->if_freq = 3300000;
  1564. /* pll = freq[Hz] * 2^24/10^6 / 16.285714286 */
  1565. pll = state->if_freq * 1677721600L;
  1566. do_div(pll, 1628571429L);
  1567. rc = mb86a20s_writereg(state, 0x28, 0x20);
  1568. if (rc < 0)
  1569. goto err;
  1570. rc = mb86a20s_writereg(state, 0x29, (pll >> 16) & 0xff);
  1571. if (rc < 0)
  1572. goto err;
  1573. rc = mb86a20s_writereg(state, 0x2a, (pll >> 8) & 0xff);
  1574. if (rc < 0)
  1575. goto err;
  1576. rc = mb86a20s_writereg(state, 0x2b, pll & 0xff);
  1577. if (rc < 0)
  1578. goto err;
  1579. dev_dbg(&state->i2c->dev, "%s: IF=%d, PLL=0x%06llx\n",
  1580. __func__, state->if_freq, (long long)pll);
  1581. if (!state->config->is_serial) {
  1582. regD5 &= ~1;
  1583. rc = mb86a20s_writereg(state, 0x50, 0xd5);
  1584. if (rc < 0)
  1585. goto err;
  1586. rc = mb86a20s_writereg(state, 0x51, regD5);
  1587. if (rc < 0)
  1588. goto err;
  1589. }
  1590. rc = mb86a20s_writeregdata(state, mb86a20s_init2);
  1591. if (rc < 0)
  1592. goto err;
  1593. err:
  1594. if (fe->ops.i2c_gate_ctrl)
  1595. fe->ops.i2c_gate_ctrl(fe, 1);
  1596. if (rc < 0) {
  1597. state->need_init = true;
  1598. dev_info(&state->i2c->dev,
  1599. "mb86a20s: Init failed. Will try again later\n");
  1600. } else {
  1601. state->need_init = false;
  1602. dev_dbg(&state->i2c->dev, "Initialization succeeded.\n");
  1603. }
  1604. return rc;
  1605. }
  1606. static int mb86a20s_set_frontend(struct dvb_frontend *fe)
  1607. {
  1608. struct mb86a20s_state *state = fe->demodulator_priv;
  1609. int rc, if_freq;
  1610. #if 0
  1611. /*
  1612. * FIXME: Properly implement the set frontend properties
  1613. */
  1614. struct dtv_frontend_properties *c = &fe->dtv_property_cache;
  1615. #endif
  1616. dev_dbg(&state->i2c->dev, "%s called.\n", __func__);
  1617. /*
  1618. * Gate should already be opened, but it doesn't hurt to
  1619. * double-check
  1620. */
  1621. if (fe->ops.i2c_gate_ctrl)
  1622. fe->ops.i2c_gate_ctrl(fe, 1);
  1623. fe->ops.tuner_ops.set_params(fe);
  1624. if (fe->ops.tuner_ops.get_if_frequency)
  1625. fe->ops.tuner_ops.get_if_frequency(fe, &if_freq);
  1626. /*
  1627. * Make it more reliable: if, for some reason, the initial
  1628. * device initialization doesn't happen, initialize it when
  1629. * a SBTVD parameters are adjusted.
  1630. *
  1631. * Unfortunately, due to a hard to track bug at tda829x/tda18271,
  1632. * the agc callback logic is not called during DVB attach time,
  1633. * causing mb86a20s to not be initialized with Kworld SBTVD.
  1634. * So, this hack is needed, in order to make Kworld SBTVD to work.
  1635. *
  1636. * It is also needed to change the IF after the initial init.
  1637. *
  1638. * HACK: Always init the frontend when set_frontend is called:
  1639. * it was noticed that, on some devices, it fails to lock on a
  1640. * different channel. So, it is better to reset everything, even
  1641. * wasting some time, than to loose channel lock.
  1642. */
  1643. mb86a20s_initfe(fe);
  1644. if (fe->ops.i2c_gate_ctrl)
  1645. fe->ops.i2c_gate_ctrl(fe, 0);
  1646. rc = mb86a20s_writeregdata(state, mb86a20s_reset_reception);
  1647. mb86a20s_reset_counters(fe);
  1648. if (fe->ops.i2c_gate_ctrl)
  1649. fe->ops.i2c_gate_ctrl(fe, 1);
  1650. return rc;
  1651. }
  1652. static int mb86a20s_read_status_and_stats(struct dvb_frontend *fe,
  1653. fe_status_t *status)
  1654. {
  1655. struct mb86a20s_state *state = fe->demodulator_priv;
  1656. int rc, status_nr;
  1657. dev_dbg(&state->i2c->dev, "%s called.\n", __func__);
  1658. if (fe->ops.i2c_gate_ctrl)
  1659. fe->ops.i2c_gate_ctrl(fe, 0);
  1660. /* Get lock */
  1661. status_nr = mb86a20s_read_status(fe, status);
  1662. if (status_nr < 7) {
  1663. mb86a20s_stats_not_ready(fe);
  1664. mb86a20s_reset_frontend_cache(fe);
  1665. }
  1666. if (status_nr < 0) {
  1667. dev_err(&state->i2c->dev,
  1668. "%s: Can't read frontend lock status\n", __func__);
  1669. goto error;
  1670. }
  1671. /* Get signal strength */
  1672. rc = mb86a20s_read_signal_strength(fe);
  1673. if (rc < 0) {
  1674. dev_err(&state->i2c->dev,
  1675. "%s: Can't reset VBER registers.\n", __func__);
  1676. mb86a20s_stats_not_ready(fe);
  1677. mb86a20s_reset_frontend_cache(fe);
  1678. rc = 0; /* Status is OK */
  1679. goto error;
  1680. }
  1681. if (status_nr >= 7) {
  1682. /* Get TMCC info*/
  1683. rc = mb86a20s_get_frontend(fe);
  1684. if (rc < 0) {
  1685. dev_err(&state->i2c->dev,
  1686. "%s: Can't get FE TMCC data.\n", __func__);
  1687. rc = 0; /* Status is OK */
  1688. goto error;
  1689. }
  1690. /* Get statistics */
  1691. rc = mb86a20s_get_stats(fe, status_nr);
  1692. if (rc < 0 && rc != -EBUSY) {
  1693. dev_err(&state->i2c->dev,
  1694. "%s: Can't get FE statistics.\n", __func__);
  1695. rc = 0;
  1696. goto error;
  1697. }
  1698. rc = 0; /* Don't return EBUSY to userspace */
  1699. }
  1700. goto ok;
  1701. error:
  1702. mb86a20s_stats_not_ready(fe);
  1703. ok:
  1704. if (fe->ops.i2c_gate_ctrl)
  1705. fe->ops.i2c_gate_ctrl(fe, 1);
  1706. return rc;
  1707. }
  1708. static int mb86a20s_read_signal_strength_from_cache(struct dvb_frontend *fe,
  1709. u16 *strength)
  1710. {
  1711. struct dtv_frontend_properties *c = &fe->dtv_property_cache;
  1712. *strength = c->strength.stat[0].uvalue;
  1713. return 0;
  1714. }
  1715. static int mb86a20s_get_frontend_dummy(struct dvb_frontend *fe)
  1716. {
  1717. /*
  1718. * get_frontend is now handled together with other stats
  1719. * retrival, when read_status() is called, as some statistics
  1720. * will depend on the layers detection.
  1721. */
  1722. return 0;
  1723. };
  1724. static int mb86a20s_tune(struct dvb_frontend *fe,
  1725. bool re_tune,
  1726. unsigned int mode_flags,
  1727. unsigned int *delay,
  1728. fe_status_t *status)
  1729. {
  1730. struct mb86a20s_state *state = fe->demodulator_priv;
  1731. int rc = 0;
  1732. dev_dbg(&state->i2c->dev, "%s called.\n", __func__);
  1733. if (re_tune)
  1734. rc = mb86a20s_set_frontend(fe);
  1735. if (!(mode_flags & FE_TUNE_MODE_ONESHOT))
  1736. mb86a20s_read_status_and_stats(fe, status);
  1737. return rc;
  1738. }
  1739. static void mb86a20s_release(struct dvb_frontend *fe)
  1740. {
  1741. struct mb86a20s_state *state = fe->demodulator_priv;
  1742. dev_dbg(&state->i2c->dev, "%s called.\n", __func__);
  1743. kfree(state);
  1744. }
  1745. static struct dvb_frontend_ops mb86a20s_ops;
  1746. struct dvb_frontend *mb86a20s_attach(const struct mb86a20s_config *config,
  1747. struct i2c_adapter *i2c)
  1748. {
  1749. struct mb86a20s_state *state;
  1750. u8 rev;
  1751. dev_dbg(&i2c->dev, "%s called.\n", __func__);
  1752. /* allocate memory for the internal state */
  1753. state = kzalloc(sizeof(struct mb86a20s_state), GFP_KERNEL);
  1754. if (state == NULL) {
  1755. dev_err(&i2c->dev,
  1756. "%s: unable to allocate memory for state\n", __func__);
  1757. goto error;
  1758. }
  1759. /* setup the state */
  1760. state->config = config;
  1761. state->i2c = i2c;
  1762. /* create dvb_frontend */
  1763. memcpy(&state->frontend.ops, &mb86a20s_ops,
  1764. sizeof(struct dvb_frontend_ops));
  1765. state->frontend.demodulator_priv = state;
  1766. /* Check if it is a mb86a20s frontend */
  1767. rev = mb86a20s_readreg(state, 0);
  1768. if (rev == 0x13) {
  1769. dev_info(&i2c->dev,
  1770. "Detected a Fujitsu mb86a20s frontend\n");
  1771. } else {
  1772. dev_dbg(&i2c->dev,
  1773. "Frontend revision %d is unknown - aborting.\n",
  1774. rev);
  1775. goto error;
  1776. }
  1777. return &state->frontend;
  1778. error:
  1779. kfree(state);
  1780. return NULL;
  1781. }
  1782. EXPORT_SYMBOL(mb86a20s_attach);
  1783. static struct dvb_frontend_ops mb86a20s_ops = {
  1784. .delsys = { SYS_ISDBT },
  1785. /* Use dib8000 values per default */
  1786. .info = {
  1787. .name = "Fujitsu mb86A20s",
  1788. .caps = FE_CAN_INVERSION_AUTO | FE_CAN_RECOVER |
  1789. FE_CAN_FEC_1_2 | FE_CAN_FEC_2_3 | FE_CAN_FEC_3_4 |
  1790. FE_CAN_FEC_5_6 | FE_CAN_FEC_7_8 | FE_CAN_FEC_AUTO |
  1791. FE_CAN_QPSK | FE_CAN_QAM_16 | FE_CAN_QAM_64 |
  1792. FE_CAN_TRANSMISSION_MODE_AUTO | FE_CAN_QAM_AUTO |
  1793. FE_CAN_GUARD_INTERVAL_AUTO | FE_CAN_HIERARCHY_AUTO,
  1794. /* Actually, those values depend on the used tuner */
  1795. .frequency_min = 45000000,
  1796. .frequency_max = 864000000,
  1797. .frequency_stepsize = 62500,
  1798. },
  1799. .release = mb86a20s_release,
  1800. .init = mb86a20s_initfe,
  1801. .set_frontend = mb86a20s_set_frontend,
  1802. .get_frontend = mb86a20s_get_frontend_dummy,
  1803. .read_status = mb86a20s_read_status_and_stats,
  1804. .read_signal_strength = mb86a20s_read_signal_strength_from_cache,
  1805. .tune = mb86a20s_tune,
  1806. };
  1807. MODULE_DESCRIPTION("DVB Frontend module for Fujitsu mb86A20s hardware");
  1808. MODULE_AUTHOR("Mauro Carvalho Chehab <mchehab@redhat.com>");
  1809. MODULE_LICENSE("GPL");