mb86a20s.c 50 KB

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