mb86a20s.c 53 KB

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