mb86a20s.c 53 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151
  1. /*
  2. * Fujitu mb86a20s ISDB-T/ISDB-Tsb Module driver
  3. *
  4. * Copyright (C) 2010-2013 Mauro Carvalho Chehab
  5. * Copyright (C) 2009-2010 Douglas Landgraf <dougsland@redhat.com>
  6. *
  7. * This program is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU General Public License as
  9. * published by the Free Software Foundation version 2.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * General Public License for more details.
  15. */
  16. #include <linux/kernel.h>
  17. #include <asm/div64.h>
  18. #include "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. static 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. int interleaving[] = {
  403. 0, 1, 2, 4, 8
  404. };
  405. static unsigned char reg[] = {
  406. [0] = 0x88, /* Layer A */
  407. [1] = 0x8c, /* Layer B */
  408. [2] = 0x90, /* Layer C */
  409. };
  410. if (layer >= ARRAY_SIZE(reg))
  411. return -EINVAL;
  412. rc = mb86a20s_writereg(state, 0x6d, reg[layer]);
  413. if (rc < 0)
  414. return rc;
  415. rc = mb86a20s_readreg(state, 0x6e);
  416. if (rc < 0)
  417. return rc;
  418. return interleaving[(rc >> 4) & 0x07];
  419. }
  420. static int mb86a20s_get_segment_count(struct mb86a20s_state *state,
  421. unsigned layer)
  422. {
  423. int rc, count;
  424. static unsigned char reg[] = {
  425. [0] = 0x89, /* Layer A */
  426. [1] = 0x8d, /* Layer B */
  427. [2] = 0x91, /* Layer C */
  428. };
  429. dev_dbg(&state->i2c->dev, "%s called.\n", __func__);
  430. if (layer >= ARRAY_SIZE(reg))
  431. return -EINVAL;
  432. rc = mb86a20s_writereg(state, 0x6d, reg[layer]);
  433. if (rc < 0)
  434. return rc;
  435. rc = mb86a20s_readreg(state, 0x6e);
  436. if (rc < 0)
  437. return rc;
  438. count = (rc >> 4) & 0x0f;
  439. dev_dbg(&state->i2c->dev, "%s: segments: %d.\n", __func__, count);
  440. return count;
  441. }
  442. static void mb86a20s_reset_frontend_cache(struct dvb_frontend *fe)
  443. {
  444. struct mb86a20s_state *state = fe->demodulator_priv;
  445. struct dtv_frontend_properties *c = &fe->dtv_property_cache;
  446. dev_dbg(&state->i2c->dev, "%s called.\n", __func__);
  447. /* Fixed parameters */
  448. c->delivery_system = SYS_ISDBT;
  449. c->bandwidth_hz = 6000000;
  450. /* Initialize values that will be later autodetected */
  451. c->isdbt_layer_enabled = 0;
  452. c->transmission_mode = TRANSMISSION_MODE_AUTO;
  453. c->guard_interval = GUARD_INTERVAL_AUTO;
  454. c->isdbt_sb_mode = 0;
  455. c->isdbt_sb_segment_count = 0;
  456. }
  457. /*
  458. * Estimates the bit rate using the per-segment bit rate given by
  459. * ABNT/NBR 15601 spec (table 4).
  460. */
  461. static u32 isdbt_rate[3][5][4] = {
  462. { /* DQPSK/QPSK */
  463. { 280850, 312060, 330420, 340430 }, /* 1/2 */
  464. { 374470, 416080, 440560, 453910 }, /* 2/3 */
  465. { 421280, 468090, 495630, 510650 }, /* 3/4 */
  466. { 468090, 520100, 550700, 567390 }, /* 5/6 */
  467. { 491500, 546110, 578230, 595760 }, /* 7/8 */
  468. }, { /* QAM16 */
  469. { 561710, 624130, 660840, 680870 }, /* 1/2 */
  470. { 748950, 832170, 881120, 907820 }, /* 2/3 */
  471. { 842570, 936190, 991260, 1021300 }, /* 3/4 */
  472. { 936190, 1040210, 1101400, 1134780 }, /* 5/6 */
  473. { 983000, 1092220, 1156470, 1191520 }, /* 7/8 */
  474. }, { /* QAM64 */
  475. { 842570, 936190, 991260, 1021300 }, /* 1/2 */
  476. { 1123430, 1248260, 1321680, 1361740 }, /* 2/3 */
  477. { 1263860, 1404290, 1486900, 1531950 }, /* 3/4 */
  478. { 1404290, 1560320, 1652110, 1702170 }, /* 5/6 */
  479. { 1474500, 1638340, 1734710, 1787280 }, /* 7/8 */
  480. }
  481. };
  482. static void mb86a20s_layer_bitrate(struct dvb_frontend *fe, u32 layer,
  483. u32 modulation, u32 forward_error_correction,
  484. u32 guard_interval,
  485. u32 segment)
  486. {
  487. struct mb86a20s_state *state = fe->demodulator_priv;
  488. u32 rate;
  489. int mod, fec, guard;
  490. /*
  491. * If modulation/fec/guard is not detected, the default is
  492. * to consider the lowest bit rate, to avoid taking too long time
  493. * to get BER.
  494. */
  495. switch (modulation) {
  496. case DQPSK:
  497. case QPSK:
  498. default:
  499. mod = 0;
  500. break;
  501. case QAM_16:
  502. mod = 1;
  503. break;
  504. case QAM_64:
  505. mod = 2;
  506. break;
  507. }
  508. switch (forward_error_correction) {
  509. default:
  510. case FEC_1_2:
  511. case FEC_AUTO:
  512. fec = 0;
  513. break;
  514. case FEC_2_3:
  515. fec = 1;
  516. break;
  517. case FEC_3_4:
  518. fec = 2;
  519. break;
  520. case FEC_5_6:
  521. fec = 3;
  522. break;
  523. case FEC_7_8:
  524. fec = 4;
  525. break;
  526. }
  527. switch (guard_interval) {
  528. default:
  529. case GUARD_INTERVAL_1_4:
  530. guard = 0;
  531. break;
  532. case GUARD_INTERVAL_1_8:
  533. guard = 1;
  534. break;
  535. case GUARD_INTERVAL_1_16:
  536. guard = 2;
  537. break;
  538. case GUARD_INTERVAL_1_32:
  539. guard = 3;
  540. break;
  541. }
  542. /* Samples BER at BER_SAMPLING_RATE seconds */
  543. rate = isdbt_rate[mod][fec][guard] * segment * BER_SAMPLING_RATE;
  544. /* Avoids sampling too quickly or to overflow the register */
  545. if (rate < 256)
  546. rate = 256;
  547. else if (rate > (1 << 24) - 1)
  548. rate = (1 << 24) - 1;
  549. dev_dbg(&state->i2c->dev,
  550. "%s: layer %c bitrate: %d kbps; counter = %d (0x%06x)\n",
  551. __func__, 'A' + layer,
  552. segment * isdbt_rate[mod][fec][guard]/1000,
  553. rate, rate);
  554. state->estimated_rate[layer] = rate;
  555. }
  556. static int mb86a20s_get_frontend(struct dvb_frontend *fe)
  557. {
  558. struct mb86a20s_state *state = fe->demodulator_priv;
  559. struct dtv_frontend_properties *c = &fe->dtv_property_cache;
  560. int layer, rc;
  561. dev_dbg(&state->i2c->dev, "%s called.\n", __func__);
  562. /* Reset frontend cache to default values */
  563. mb86a20s_reset_frontend_cache(fe);
  564. /* Check for partial reception */
  565. rc = mb86a20s_writereg(state, 0x6d, 0x85);
  566. if (rc < 0)
  567. return rc;
  568. rc = mb86a20s_readreg(state, 0x6e);
  569. if (rc < 0)
  570. return rc;
  571. c->isdbt_partial_reception = (rc & 0x10) ? 1 : 0;
  572. /* Get per-layer data */
  573. for (layer = 0; layer < NUM_LAYERS; layer++) {
  574. dev_dbg(&state->i2c->dev, "%s: getting data for layer %c.\n",
  575. __func__, 'A' + layer);
  576. rc = mb86a20s_get_segment_count(state, layer);
  577. if (rc < 0)
  578. goto noperlayer_error;
  579. if (rc >= 0 && rc < 14) {
  580. c->layer[layer].segment_count = rc;
  581. } else {
  582. c->layer[layer].segment_count = 0;
  583. state->estimated_rate[layer] = 0;
  584. continue;
  585. }
  586. c->isdbt_layer_enabled |= 1 << layer;
  587. rc = mb86a20s_get_modulation(state, layer);
  588. if (rc < 0)
  589. goto noperlayer_error;
  590. dev_dbg(&state->i2c->dev, "%s: modulation %d.\n",
  591. __func__, rc);
  592. c->layer[layer].modulation = rc;
  593. rc = mb86a20s_get_fec(state, layer);
  594. if (rc < 0)
  595. goto noperlayer_error;
  596. dev_dbg(&state->i2c->dev, "%s: FEC %d.\n",
  597. __func__, rc);
  598. c->layer[layer].fec = rc;
  599. rc = mb86a20s_get_interleaving(state, layer);
  600. if (rc < 0)
  601. goto noperlayer_error;
  602. dev_dbg(&state->i2c->dev, "%s: interleaving %d.\n",
  603. __func__, rc);
  604. c->layer[layer].interleaving = rc;
  605. mb86a20s_layer_bitrate(fe, layer, c->layer[layer].modulation,
  606. c->layer[layer].fec,
  607. c->guard_interval,
  608. c->layer[layer].segment_count);
  609. }
  610. rc = mb86a20s_writereg(state, 0x6d, 0x84);
  611. if (rc < 0)
  612. return rc;
  613. if ((rc & 0x60) == 0x20) {
  614. c->isdbt_sb_mode = 1;
  615. /* At least, one segment should exist */
  616. if (!c->isdbt_sb_segment_count)
  617. c->isdbt_sb_segment_count = 1;
  618. }
  619. /* Get transmission mode and guard interval */
  620. rc = mb86a20s_readreg(state, 0x07);
  621. if (rc < 0)
  622. return rc;
  623. c->transmission_mode = TRANSMISSION_MODE_AUTO;
  624. if ((rc & 0x60) == 0x20) {
  625. /* Only modes 2 and 3 are supported */
  626. switch ((rc >> 2) & 0x03) {
  627. case 1:
  628. c->transmission_mode = TRANSMISSION_MODE_4K;
  629. break;
  630. case 2:
  631. c->transmission_mode = TRANSMISSION_MODE_8K;
  632. break;
  633. }
  634. }
  635. c->guard_interval = GUARD_INTERVAL_AUTO;
  636. if (!(rc & 0x10)) {
  637. /* Guard interval 1/32 is not supported */
  638. switch (rc & 0x3) {
  639. case 0:
  640. c->guard_interval = GUARD_INTERVAL_1_4;
  641. break;
  642. case 1:
  643. c->guard_interval = GUARD_INTERVAL_1_8;
  644. break;
  645. case 2:
  646. c->guard_interval = GUARD_INTERVAL_1_16;
  647. break;
  648. }
  649. }
  650. return 0;
  651. noperlayer_error:
  652. /* per-layer info is incomplete; discard all per-layer */
  653. c->isdbt_layer_enabled = 0;
  654. return rc;
  655. }
  656. static int mb86a20s_reset_counters(struct dvb_frontend *fe)
  657. {
  658. struct mb86a20s_state *state = fe->demodulator_priv;
  659. struct dtv_frontend_properties *c = &fe->dtv_property_cache;
  660. int rc, val;
  661. dev_dbg(&state->i2c->dev, "%s called.\n", __func__);
  662. /* Reset the counters, if the channel changed */
  663. if (state->last_frequency != c->frequency) {
  664. memset(&c->cnr, 0, sizeof(c->cnr));
  665. memset(&c->pre_bit_error, 0, sizeof(c->pre_bit_error));
  666. memset(&c->pre_bit_count, 0, sizeof(c->pre_bit_count));
  667. memset(&c->post_bit_error, 0, sizeof(c->post_bit_error));
  668. memset(&c->post_bit_count, 0, sizeof(c->post_bit_count));
  669. memset(&c->block_error, 0, sizeof(c->block_error));
  670. memset(&c->block_count, 0, sizeof(c->block_count));
  671. state->last_frequency = c->frequency;
  672. }
  673. /* Clear status for most stats */
  674. /* BER/PER counter reset */
  675. rc = mb86a20s_writeregdata(state, mb86a20s_per_ber_reset);
  676. if (rc < 0)
  677. goto err;
  678. /* CNR counter reset */
  679. rc = mb86a20s_readreg(state, 0x45);
  680. if (rc < 0)
  681. goto err;
  682. val = rc;
  683. rc = mb86a20s_writereg(state, 0x45, val | 0x10);
  684. if (rc < 0)
  685. goto err;
  686. rc = mb86a20s_writereg(state, 0x45, val & 0x6f);
  687. if (rc < 0)
  688. goto err;
  689. /* MER counter reset */
  690. rc = mb86a20s_writereg(state, 0x50, 0x50);
  691. if (rc < 0)
  692. goto err;
  693. rc = mb86a20s_readreg(state, 0x51);
  694. if (rc < 0)
  695. goto err;
  696. val = rc;
  697. rc = mb86a20s_writereg(state, 0x51, val | 0x01);
  698. if (rc < 0)
  699. goto err;
  700. rc = mb86a20s_writereg(state, 0x51, val & 0x06);
  701. if (rc < 0)
  702. goto err;
  703. goto ok;
  704. err:
  705. dev_err(&state->i2c->dev,
  706. "%s: Can't reset FE statistics (error %d).\n",
  707. __func__, rc);
  708. ok:
  709. return rc;
  710. }
  711. static int mb86a20s_get_pre_ber(struct dvb_frontend *fe,
  712. unsigned layer,
  713. u32 *error, u32 *count)
  714. {
  715. struct mb86a20s_state *state = fe->demodulator_priv;
  716. int rc, val;
  717. dev_dbg(&state->i2c->dev, "%s called.\n", __func__);
  718. if (layer >= NUM_LAYERS)
  719. return -EINVAL;
  720. /* Check if the BER measures are already available */
  721. rc = mb86a20s_readreg(state, 0x54);
  722. if (rc < 0)
  723. return rc;
  724. /* Check if data is available for that layer */
  725. if (!(rc & (1 << layer))) {
  726. dev_dbg(&state->i2c->dev,
  727. "%s: preBER for layer %c is not available yet.\n",
  728. __func__, 'A' + layer);
  729. return -EBUSY;
  730. }
  731. /* Read Bit Error Count */
  732. rc = mb86a20s_readreg(state, 0x55 + layer * 3);
  733. if (rc < 0)
  734. return rc;
  735. *error = rc << 16;
  736. rc = mb86a20s_readreg(state, 0x56 + layer * 3);
  737. if (rc < 0)
  738. return rc;
  739. *error |= rc << 8;
  740. rc = mb86a20s_readreg(state, 0x57 + layer * 3);
  741. if (rc < 0)
  742. return rc;
  743. *error |= rc;
  744. dev_dbg(&state->i2c->dev,
  745. "%s: bit error before Viterbi for layer %c: %d.\n",
  746. __func__, 'A' + layer, *error);
  747. /* Read Bit Count */
  748. rc = mb86a20s_writereg(state, 0x50, 0xa7 + layer * 3);
  749. if (rc < 0)
  750. return rc;
  751. rc = mb86a20s_readreg(state, 0x51);
  752. if (rc < 0)
  753. return rc;
  754. *count = rc << 16;
  755. rc = mb86a20s_writereg(state, 0x50, 0xa8 + layer * 3);
  756. if (rc < 0)
  757. return rc;
  758. rc = mb86a20s_readreg(state, 0x51);
  759. if (rc < 0)
  760. return rc;
  761. *count |= rc << 8;
  762. rc = mb86a20s_writereg(state, 0x50, 0xa9 + layer * 3);
  763. if (rc < 0)
  764. return rc;
  765. rc = mb86a20s_readreg(state, 0x51);
  766. if (rc < 0)
  767. return rc;
  768. *count |= rc;
  769. dev_dbg(&state->i2c->dev,
  770. "%s: bit count before Viterbi for layer %c: %d.\n",
  771. __func__, 'A' + layer, *count);
  772. /*
  773. * As we get TMCC data from the frontend, we can better estimate the
  774. * BER bit counters, in order to do the BER measure during a longer
  775. * time. Use those data, if available, to update the bit count
  776. * measure.
  777. */
  778. if (state->estimated_rate[layer]
  779. && state->estimated_rate[layer] != *count) {
  780. dev_dbg(&state->i2c->dev,
  781. "%s: updating layer %c preBER counter to %d.\n",
  782. __func__, 'A' + layer, state->estimated_rate[layer]);
  783. /* Turn off BER before Viterbi */
  784. rc = mb86a20s_writereg(state, 0x52, 0x00);
  785. /* Update counter for this layer */
  786. rc = mb86a20s_writereg(state, 0x50, 0xa7 + layer * 3);
  787. if (rc < 0)
  788. return rc;
  789. rc = mb86a20s_writereg(state, 0x51,
  790. state->estimated_rate[layer] >> 16);
  791. if (rc < 0)
  792. return rc;
  793. rc = mb86a20s_writereg(state, 0x50, 0xa8 + layer * 3);
  794. if (rc < 0)
  795. return rc;
  796. rc = mb86a20s_writereg(state, 0x51,
  797. state->estimated_rate[layer] >> 8);
  798. if (rc < 0)
  799. return rc;
  800. rc = mb86a20s_writereg(state, 0x50, 0xa9 + layer * 3);
  801. if (rc < 0)
  802. return rc;
  803. rc = mb86a20s_writereg(state, 0x51,
  804. state->estimated_rate[layer]);
  805. if (rc < 0)
  806. return rc;
  807. /* Turn on BER before Viterbi */
  808. rc = mb86a20s_writereg(state, 0x52, 0x01);
  809. /* Reset all preBER counters */
  810. rc = mb86a20s_writereg(state, 0x53, 0x00);
  811. if (rc < 0)
  812. return rc;
  813. rc = mb86a20s_writereg(state, 0x53, 0x07);
  814. } else {
  815. /* Reset counter to collect new data */
  816. rc = mb86a20s_readreg(state, 0x53);
  817. if (rc < 0)
  818. return rc;
  819. val = rc;
  820. rc = mb86a20s_writereg(state, 0x53, val & ~(1 << layer));
  821. if (rc < 0)
  822. return rc;
  823. rc = mb86a20s_writereg(state, 0x53, val | (1 << layer));
  824. }
  825. return rc;
  826. }
  827. static int mb86a20s_get_post_ber(struct dvb_frontend *fe,
  828. unsigned layer,
  829. u32 *error, u32 *count)
  830. {
  831. struct mb86a20s_state *state = fe->demodulator_priv;
  832. u32 counter, collect_rate;
  833. int rc, val;
  834. dev_dbg(&state->i2c->dev, "%s called.\n", __func__);
  835. if (layer >= NUM_LAYERS)
  836. return -EINVAL;
  837. /* Check if the BER measures are already available */
  838. rc = mb86a20s_readreg(state, 0x60);
  839. if (rc < 0)
  840. return rc;
  841. /* Check if data is available for that layer */
  842. if (!(rc & (1 << layer))) {
  843. dev_dbg(&state->i2c->dev,
  844. "%s: post BER for layer %c is not available yet.\n",
  845. __func__, 'A' + layer);
  846. return -EBUSY;
  847. }
  848. /* Read Bit Error Count */
  849. rc = mb86a20s_readreg(state, 0x64 + layer * 3);
  850. if (rc < 0)
  851. return rc;
  852. *error = rc << 16;
  853. rc = mb86a20s_readreg(state, 0x65 + layer * 3);
  854. if (rc < 0)
  855. return rc;
  856. *error |= rc << 8;
  857. rc = mb86a20s_readreg(state, 0x66 + layer * 3);
  858. if (rc < 0)
  859. return rc;
  860. *error |= rc;
  861. dev_dbg(&state->i2c->dev,
  862. "%s: post bit error for layer %c: %d.\n",
  863. __func__, 'A' + layer, *error);
  864. /* Read Bit Count */
  865. rc = mb86a20s_writereg(state, 0x50, 0xdc + layer * 2);
  866. if (rc < 0)
  867. return rc;
  868. rc = mb86a20s_readreg(state, 0x51);
  869. if (rc < 0)
  870. return rc;
  871. counter = rc << 8;
  872. rc = mb86a20s_writereg(state, 0x50, 0xdd + layer * 2);
  873. if (rc < 0)
  874. return rc;
  875. rc = mb86a20s_readreg(state, 0x51);
  876. if (rc < 0)
  877. return rc;
  878. counter |= rc;
  879. *count = counter * 204 * 8;
  880. dev_dbg(&state->i2c->dev,
  881. "%s: post bit count for layer %c: %d.\n",
  882. __func__, 'A' + layer, *count);
  883. /*
  884. * As we get TMCC data from the frontend, we can better estimate the
  885. * BER bit counters, in order to do the BER measure during a longer
  886. * time. Use those data, if available, to update the bit count
  887. * measure.
  888. */
  889. if (!state->estimated_rate[layer])
  890. goto reset_measurement;
  891. collect_rate = state->estimated_rate[layer] / 204 / 8;
  892. if (collect_rate < 32)
  893. collect_rate = 32;
  894. if (collect_rate > 65535)
  895. collect_rate = 65535;
  896. if (collect_rate != counter) {
  897. dev_dbg(&state->i2c->dev,
  898. "%s: updating postBER counter on layer %c to %d.\n",
  899. __func__, 'A' + layer, collect_rate);
  900. /* Turn off BER after Viterbi */
  901. rc = mb86a20s_writereg(state, 0x5e, 0x00);
  902. /* Update counter for this layer */
  903. rc = mb86a20s_writereg(state, 0x50, 0xdc + layer * 2);
  904. if (rc < 0)
  905. return rc;
  906. rc = mb86a20s_writereg(state, 0x51, collect_rate >> 8);
  907. if (rc < 0)
  908. return rc;
  909. rc = mb86a20s_writereg(state, 0x50, 0xdd + layer * 2);
  910. if (rc < 0)
  911. return rc;
  912. rc = mb86a20s_writereg(state, 0x51, collect_rate & 0xff);
  913. if (rc < 0)
  914. return rc;
  915. /* Turn on BER after Viterbi */
  916. rc = mb86a20s_writereg(state, 0x5e, 0x07);
  917. /* Reset all preBER counters */
  918. rc = mb86a20s_writereg(state, 0x5f, 0x00);
  919. if (rc < 0)
  920. return rc;
  921. rc = mb86a20s_writereg(state, 0x5f, 0x07);
  922. return rc;
  923. }
  924. reset_measurement:
  925. /* Reset counter to collect new data */
  926. rc = mb86a20s_readreg(state, 0x5f);
  927. if (rc < 0)
  928. return rc;
  929. val = rc;
  930. rc = mb86a20s_writereg(state, 0x5f, val & ~(1 << layer));
  931. if (rc < 0)
  932. return rc;
  933. rc = mb86a20s_writereg(state, 0x5f, val | (1 << layer));
  934. return rc;
  935. }
  936. static int mb86a20s_get_blk_error(struct dvb_frontend *fe,
  937. unsigned layer,
  938. u32 *error, u32 *count)
  939. {
  940. struct mb86a20s_state *state = fe->demodulator_priv;
  941. int rc, val;
  942. u32 collect_rate;
  943. dev_dbg(&state->i2c->dev, "%s called.\n", __func__);
  944. if (layer >= NUM_LAYERS)
  945. return -EINVAL;
  946. /* Check if the PER measures are already available */
  947. rc = mb86a20s_writereg(state, 0x50, 0xb8);
  948. if (rc < 0)
  949. return rc;
  950. rc = mb86a20s_readreg(state, 0x51);
  951. if (rc < 0)
  952. return rc;
  953. /* Check if data is available for that layer */
  954. if (!(rc & (1 << layer))) {
  955. dev_dbg(&state->i2c->dev,
  956. "%s: block counts for layer %c aren't available yet.\n",
  957. __func__, 'A' + layer);
  958. return -EBUSY;
  959. }
  960. /* Read Packet error Count */
  961. rc = mb86a20s_writereg(state, 0x50, 0xb9 + layer * 2);
  962. if (rc < 0)
  963. return rc;
  964. rc = mb86a20s_readreg(state, 0x51);
  965. if (rc < 0)
  966. return rc;
  967. *error = rc << 8;
  968. rc = mb86a20s_writereg(state, 0x50, 0xba + layer * 2);
  969. if (rc < 0)
  970. return rc;
  971. rc = mb86a20s_readreg(state, 0x51);
  972. if (rc < 0)
  973. return rc;
  974. *error |= rc;
  975. dev_dbg(&state->i2c->dev, "%s: block error for layer %c: %d.\n",
  976. __func__, 'A' + layer, *error);
  977. /* Read Bit Count */
  978. rc = mb86a20s_writereg(state, 0x50, 0xb2 + layer * 2);
  979. if (rc < 0)
  980. return rc;
  981. rc = mb86a20s_readreg(state, 0x51);
  982. if (rc < 0)
  983. return rc;
  984. *count = rc << 8;
  985. rc = mb86a20s_writereg(state, 0x50, 0xb3 + layer * 2);
  986. if (rc < 0)
  987. return rc;
  988. rc = mb86a20s_readreg(state, 0x51);
  989. if (rc < 0)
  990. return rc;
  991. *count |= rc;
  992. dev_dbg(&state->i2c->dev,
  993. "%s: block count for layer %c: %d.\n",
  994. __func__, 'A' + layer, *count);
  995. /*
  996. * As we get TMCC data from the frontend, we can better estimate the
  997. * BER bit counters, in order to do the BER measure during a longer
  998. * time. Use those data, if available, to update the bit count
  999. * measure.
  1000. */
  1001. if (!state->estimated_rate[layer])
  1002. goto reset_measurement;
  1003. collect_rate = state->estimated_rate[layer] / 204 / 8;
  1004. if (collect_rate < 32)
  1005. collect_rate = 32;
  1006. if (collect_rate > 65535)
  1007. collect_rate = 65535;
  1008. if (collect_rate != *count) {
  1009. dev_dbg(&state->i2c->dev,
  1010. "%s: updating PER counter on layer %c to %d.\n",
  1011. __func__, 'A' + layer, collect_rate);
  1012. /* Stop PER measurement */
  1013. rc = mb86a20s_writereg(state, 0x50, 0xb0);
  1014. if (rc < 0)
  1015. return rc;
  1016. rc = mb86a20s_writereg(state, 0x51, 0x00);
  1017. if (rc < 0)
  1018. return rc;
  1019. /* Update this layer's counter */
  1020. rc = mb86a20s_writereg(state, 0x50, 0xb2 + layer * 2);
  1021. if (rc < 0)
  1022. return rc;
  1023. rc = mb86a20s_writereg(state, 0x51, collect_rate >> 8);
  1024. if (rc < 0)
  1025. return rc;
  1026. rc = mb86a20s_writereg(state, 0x50, 0xb3 + layer * 2);
  1027. if (rc < 0)
  1028. return rc;
  1029. rc = mb86a20s_writereg(state, 0x51, collect_rate & 0xff);
  1030. if (rc < 0)
  1031. return rc;
  1032. /* start PER measurement */
  1033. rc = mb86a20s_writereg(state, 0x50, 0xb0);
  1034. if (rc < 0)
  1035. return rc;
  1036. rc = mb86a20s_writereg(state, 0x51, 0x07);
  1037. if (rc < 0)
  1038. return rc;
  1039. /* Reset all counters to collect new data */
  1040. rc = mb86a20s_writereg(state, 0x50, 0xb1);
  1041. if (rc < 0)
  1042. return rc;
  1043. rc = mb86a20s_writereg(state, 0x51, 0x07);
  1044. if (rc < 0)
  1045. return rc;
  1046. rc = mb86a20s_writereg(state, 0x51, 0x00);
  1047. return rc;
  1048. }
  1049. reset_measurement:
  1050. /* Reset counter to collect new data */
  1051. rc = mb86a20s_writereg(state, 0x50, 0xb1);
  1052. if (rc < 0)
  1053. return rc;
  1054. rc = mb86a20s_readreg(state, 0x51);
  1055. if (rc < 0)
  1056. return rc;
  1057. val = rc;
  1058. rc = mb86a20s_writereg(state, 0x51, val | (1 << layer));
  1059. if (rc < 0)
  1060. return rc;
  1061. rc = mb86a20s_writereg(state, 0x51, val & ~(1 << layer));
  1062. return rc;
  1063. }
  1064. struct linear_segments {
  1065. unsigned x, y;
  1066. };
  1067. /*
  1068. * All tables below return a dB/1000 measurement
  1069. */
  1070. static const struct linear_segments cnr_to_db_table[] = {
  1071. { 19648, 0},
  1072. { 18187, 1000},
  1073. { 16534, 2000},
  1074. { 14823, 3000},
  1075. { 13161, 4000},
  1076. { 11622, 5000},
  1077. { 10279, 6000},
  1078. { 9089, 7000},
  1079. { 8042, 8000},
  1080. { 7137, 9000},
  1081. { 6342, 10000},
  1082. { 5641, 11000},
  1083. { 5030, 12000},
  1084. { 4474, 13000},
  1085. { 3988, 14000},
  1086. { 3556, 15000},
  1087. { 3180, 16000},
  1088. { 2841, 17000},
  1089. { 2541, 18000},
  1090. { 2276, 19000},
  1091. { 2038, 20000},
  1092. { 1800, 21000},
  1093. { 1625, 22000},
  1094. { 1462, 23000},
  1095. { 1324, 24000},
  1096. { 1175, 25000},
  1097. { 1063, 26000},
  1098. { 980, 27000},
  1099. { 907, 28000},
  1100. { 840, 29000},
  1101. { 788, 30000},
  1102. };
  1103. static const struct linear_segments cnr_64qam_table[] = {
  1104. { 3922688, 0},
  1105. { 3920384, 1000},
  1106. { 3902720, 2000},
  1107. { 3894784, 3000},
  1108. { 3882496, 4000},
  1109. { 3872768, 5000},
  1110. { 3858944, 6000},
  1111. { 3851520, 7000},
  1112. { 3838976, 8000},
  1113. { 3829248, 9000},
  1114. { 3818240, 10000},
  1115. { 3806976, 11000},
  1116. { 3791872, 12000},
  1117. { 3767040, 13000},
  1118. { 3720960, 14000},
  1119. { 3637504, 15000},
  1120. { 3498496, 16000},
  1121. { 3296000, 17000},
  1122. { 3031040, 18000},
  1123. { 2715392, 19000},
  1124. { 2362624, 20000},
  1125. { 1963264, 21000},
  1126. { 1649664, 22000},
  1127. { 1366784, 23000},
  1128. { 1120768, 24000},
  1129. { 890880, 25000},
  1130. { 723456, 26000},
  1131. { 612096, 27000},
  1132. { 518912, 28000},
  1133. { 448256, 29000},
  1134. { 388864, 30000},
  1135. };
  1136. static const struct linear_segments cnr_16qam_table[] = {
  1137. { 5314816, 0},
  1138. { 5219072, 1000},
  1139. { 5118720, 2000},
  1140. { 4998912, 3000},
  1141. { 4875520, 4000},
  1142. { 4736000, 5000},
  1143. { 4604160, 6000},
  1144. { 4458752, 7000},
  1145. { 4300288, 8000},
  1146. { 4092928, 9000},
  1147. { 3836160, 10000},
  1148. { 3521024, 11000},
  1149. { 3155968, 12000},
  1150. { 2756864, 13000},
  1151. { 2347008, 14000},
  1152. { 1955072, 15000},
  1153. { 1593600, 16000},
  1154. { 1297920, 17000},
  1155. { 1043968, 18000},
  1156. { 839680, 19000},
  1157. { 672256, 20000},
  1158. { 523008, 21000},
  1159. { 424704, 22000},
  1160. { 345088, 23000},
  1161. { 280064, 24000},
  1162. { 221440, 25000},
  1163. { 179712, 26000},
  1164. { 151040, 27000},
  1165. { 128512, 28000},
  1166. { 110080, 29000},
  1167. { 95744, 30000},
  1168. };
  1169. static const struct linear_segments cnr_qpsk_table[] = {
  1170. { 2834176, 0},
  1171. { 2683648, 1000},
  1172. { 2536960, 2000},
  1173. { 2391808, 3000},
  1174. { 2133248, 4000},
  1175. { 1906176, 5000},
  1176. { 1666560, 6000},
  1177. { 1422080, 7000},
  1178. { 1189632, 8000},
  1179. { 976384, 9000},
  1180. { 790272, 10000},
  1181. { 633344, 11000},
  1182. { 505600, 12000},
  1183. { 402944, 13000},
  1184. { 320768, 14000},
  1185. { 255488, 15000},
  1186. { 204032, 16000},
  1187. { 163072, 17000},
  1188. { 130304, 18000},
  1189. { 105216, 19000},
  1190. { 83456, 20000},
  1191. { 65024, 21000},
  1192. { 52480, 22000},
  1193. { 42752, 23000},
  1194. { 34560, 24000},
  1195. { 27136, 25000},
  1196. { 22016, 26000},
  1197. { 18432, 27000},
  1198. { 15616, 28000},
  1199. { 13312, 29000},
  1200. { 11520, 30000},
  1201. };
  1202. static u32 interpolate_value(u32 value, const struct linear_segments *segments,
  1203. unsigned len)
  1204. {
  1205. u64 tmp64;
  1206. u32 dx, dy;
  1207. int i, ret;
  1208. if (value >= segments[0].x)
  1209. return segments[0].y;
  1210. if (value < segments[len-1].x)
  1211. return segments[len-1].y;
  1212. for (i = 1; i < len - 1; i++) {
  1213. /* If value is identical, no need to interpolate */
  1214. if (value == segments[i].x)
  1215. return segments[i].y;
  1216. if (value > segments[i].x)
  1217. break;
  1218. }
  1219. /* Linear interpolation between the two (x,y) points */
  1220. dy = segments[i].y - segments[i - 1].y;
  1221. dx = segments[i - 1].x - segments[i].x;
  1222. tmp64 = value - segments[i].x;
  1223. tmp64 *= dy;
  1224. do_div(tmp64, dx);
  1225. ret = segments[i].y - tmp64;
  1226. return ret;
  1227. }
  1228. static int mb86a20s_get_main_CNR(struct dvb_frontend *fe)
  1229. {
  1230. struct mb86a20s_state *state = fe->demodulator_priv;
  1231. struct dtv_frontend_properties *c = &fe->dtv_property_cache;
  1232. u32 cnr_linear, cnr;
  1233. int rc, val;
  1234. /* Check if CNR is available */
  1235. rc = mb86a20s_readreg(state, 0x45);
  1236. if (rc < 0)
  1237. return rc;
  1238. if (!(rc & 0x40)) {
  1239. dev_dbg(&state->i2c->dev, "%s: CNR is not available yet.\n",
  1240. __func__);
  1241. return -EBUSY;
  1242. }
  1243. val = rc;
  1244. rc = mb86a20s_readreg(state, 0x46);
  1245. if (rc < 0)
  1246. return rc;
  1247. cnr_linear = rc << 8;
  1248. rc = mb86a20s_readreg(state, 0x46);
  1249. if (rc < 0)
  1250. return rc;
  1251. cnr_linear |= rc;
  1252. cnr = interpolate_value(cnr_linear,
  1253. cnr_to_db_table, ARRAY_SIZE(cnr_to_db_table));
  1254. c->cnr.stat[0].scale = FE_SCALE_DECIBEL;
  1255. c->cnr.stat[0].svalue = cnr;
  1256. dev_dbg(&state->i2c->dev, "%s: CNR is %d.%03d dB (%d)\n",
  1257. __func__, cnr / 1000, cnr % 1000, cnr_linear);
  1258. /* CNR counter reset */
  1259. rc = mb86a20s_writereg(state, 0x45, val | 0x10);
  1260. if (rc < 0)
  1261. return rc;
  1262. rc = mb86a20s_writereg(state, 0x45, val & 0x6f);
  1263. return rc;
  1264. }
  1265. static int mb86a20s_get_blk_error_layer_CNR(struct dvb_frontend *fe)
  1266. {
  1267. struct mb86a20s_state *state = fe->demodulator_priv;
  1268. struct dtv_frontend_properties *c = &fe->dtv_property_cache;
  1269. u32 mer, cnr;
  1270. int rc, val, layer;
  1271. const struct linear_segments *segs;
  1272. unsigned segs_len;
  1273. dev_dbg(&state->i2c->dev, "%s called.\n", __func__);
  1274. /* Check if the measures are already available */
  1275. rc = mb86a20s_writereg(state, 0x50, 0x5b);
  1276. if (rc < 0)
  1277. return rc;
  1278. rc = mb86a20s_readreg(state, 0x51);
  1279. if (rc < 0)
  1280. return rc;
  1281. /* Check if data is available */
  1282. if (!(rc & 0x01)) {
  1283. dev_dbg(&state->i2c->dev,
  1284. "%s: MER measures aren't available yet.\n", __func__);
  1285. return -EBUSY;
  1286. }
  1287. /* Read all layers */
  1288. for (layer = 0; layer < NUM_LAYERS; layer++) {
  1289. if (!(c->isdbt_layer_enabled & (1 << layer))) {
  1290. c->cnr.stat[1 + layer].scale = FE_SCALE_NOT_AVAILABLE;
  1291. continue;
  1292. }
  1293. rc = mb86a20s_writereg(state, 0x50, 0x52 + layer * 3);
  1294. if (rc < 0)
  1295. return rc;
  1296. rc = mb86a20s_readreg(state, 0x51);
  1297. if (rc < 0)
  1298. return rc;
  1299. mer = rc << 16;
  1300. rc = mb86a20s_writereg(state, 0x50, 0x53 + layer * 3);
  1301. if (rc < 0)
  1302. return rc;
  1303. rc = mb86a20s_readreg(state, 0x51);
  1304. if (rc < 0)
  1305. return rc;
  1306. mer |= rc << 8;
  1307. rc = mb86a20s_writereg(state, 0x50, 0x54 + layer * 3);
  1308. if (rc < 0)
  1309. return rc;
  1310. rc = mb86a20s_readreg(state, 0x51);
  1311. if (rc < 0)
  1312. return rc;
  1313. mer |= rc;
  1314. switch (c->layer[layer].modulation) {
  1315. case DQPSK:
  1316. case QPSK:
  1317. segs = cnr_qpsk_table;
  1318. segs_len = ARRAY_SIZE(cnr_qpsk_table);
  1319. break;
  1320. case QAM_16:
  1321. segs = cnr_16qam_table;
  1322. segs_len = ARRAY_SIZE(cnr_16qam_table);
  1323. break;
  1324. default:
  1325. case QAM_64:
  1326. segs = cnr_64qam_table;
  1327. segs_len = ARRAY_SIZE(cnr_64qam_table);
  1328. break;
  1329. }
  1330. cnr = interpolate_value(mer, segs, segs_len);
  1331. c->cnr.stat[1 + layer].scale = FE_SCALE_DECIBEL;
  1332. c->cnr.stat[1 + layer].svalue = cnr;
  1333. dev_dbg(&state->i2c->dev,
  1334. "%s: CNR for layer %c is %d.%03d dB (MER = %d).\n",
  1335. __func__, 'A' + layer, cnr / 1000, cnr % 1000, mer);
  1336. }
  1337. /* Start a new MER measurement */
  1338. /* MER counter reset */
  1339. rc = mb86a20s_writereg(state, 0x50, 0x50);
  1340. if (rc < 0)
  1341. return rc;
  1342. rc = mb86a20s_readreg(state, 0x51);
  1343. if (rc < 0)
  1344. return rc;
  1345. val = rc;
  1346. rc = mb86a20s_writereg(state, 0x51, val | 0x01);
  1347. if (rc < 0)
  1348. return rc;
  1349. rc = mb86a20s_writereg(state, 0x51, val & 0x06);
  1350. if (rc < 0)
  1351. return rc;
  1352. return 0;
  1353. }
  1354. static void mb86a20s_stats_not_ready(struct dvb_frontend *fe)
  1355. {
  1356. struct mb86a20s_state *state = fe->demodulator_priv;
  1357. struct dtv_frontend_properties *c = &fe->dtv_property_cache;
  1358. int layer;
  1359. dev_dbg(&state->i2c->dev, "%s called.\n", __func__);
  1360. /* Fill the length of each status counter */
  1361. /* Only global stats */
  1362. c->strength.len = 1;
  1363. /* Per-layer stats - 3 layers + global */
  1364. c->cnr.len = NUM_LAYERS + 1;
  1365. c->pre_bit_error.len = NUM_LAYERS + 1;
  1366. c->pre_bit_count.len = NUM_LAYERS + 1;
  1367. c->post_bit_error.len = NUM_LAYERS + 1;
  1368. c->post_bit_count.len = NUM_LAYERS + 1;
  1369. c->block_error.len = NUM_LAYERS + 1;
  1370. c->block_count.len = NUM_LAYERS + 1;
  1371. /* Signal is always available */
  1372. c->strength.stat[0].scale = FE_SCALE_RELATIVE;
  1373. c->strength.stat[0].uvalue = 0;
  1374. /* Put all of them at FE_SCALE_NOT_AVAILABLE */
  1375. for (layer = 0; layer < NUM_LAYERS + 1; layer++) {
  1376. c->cnr.stat[layer].scale = FE_SCALE_NOT_AVAILABLE;
  1377. c->pre_bit_error.stat[layer].scale = FE_SCALE_NOT_AVAILABLE;
  1378. c->pre_bit_count.stat[layer].scale = FE_SCALE_NOT_AVAILABLE;
  1379. c->post_bit_error.stat[layer].scale = FE_SCALE_NOT_AVAILABLE;
  1380. c->post_bit_count.stat[layer].scale = FE_SCALE_NOT_AVAILABLE;
  1381. c->block_error.stat[layer].scale = FE_SCALE_NOT_AVAILABLE;
  1382. c->block_count.stat[layer].scale = FE_SCALE_NOT_AVAILABLE;
  1383. }
  1384. }
  1385. static int mb86a20s_get_stats(struct dvb_frontend *fe, int status_nr)
  1386. {
  1387. struct mb86a20s_state *state = fe->demodulator_priv;
  1388. struct dtv_frontend_properties *c = &fe->dtv_property_cache;
  1389. int rc = 0, layer;
  1390. u32 bit_error = 0, bit_count = 0;
  1391. u32 t_pre_bit_error = 0, t_pre_bit_count = 0;
  1392. u32 t_post_bit_error = 0, t_post_bit_count = 0;
  1393. u32 block_error = 0, block_count = 0;
  1394. u32 t_block_error = 0, t_block_count = 0;
  1395. int active_layers = 0, pre_ber_layers = 0, post_ber_layers = 0;
  1396. int per_layers = 0;
  1397. dev_dbg(&state->i2c->dev, "%s called.\n", __func__);
  1398. mb86a20s_get_main_CNR(fe);
  1399. /* Get per-layer stats */
  1400. mb86a20s_get_blk_error_layer_CNR(fe);
  1401. /*
  1402. * At state 7, only CNR is available
  1403. * For BER measures, state=9 is required
  1404. * FIXME: we may get MER measures with state=8
  1405. */
  1406. if (status_nr < 9)
  1407. return 0;
  1408. for (layer = 0; layer < NUM_LAYERS; layer++) {
  1409. if (c->isdbt_layer_enabled & (1 << layer)) {
  1410. /* Layer is active and has rc segments */
  1411. active_layers++;
  1412. /* Handle BER before vterbi */
  1413. rc = mb86a20s_get_pre_ber(fe, layer,
  1414. &bit_error, &bit_count);
  1415. if (rc >= 0) {
  1416. c->pre_bit_error.stat[1 + layer].scale = FE_SCALE_COUNTER;
  1417. c->pre_bit_error.stat[1 + layer].uvalue += bit_error;
  1418. c->pre_bit_count.stat[1 + layer].scale = FE_SCALE_COUNTER;
  1419. c->pre_bit_count.stat[1 + layer].uvalue += bit_count;
  1420. } else if (rc != -EBUSY) {
  1421. /*
  1422. * If an I/O error happened,
  1423. * measures are now unavailable
  1424. */
  1425. c->pre_bit_error.stat[1 + layer].scale = FE_SCALE_NOT_AVAILABLE;
  1426. c->pre_bit_count.stat[1 + layer].scale = FE_SCALE_NOT_AVAILABLE;
  1427. dev_err(&state->i2c->dev,
  1428. "%s: Can't get BER for layer %c (error %d).\n",
  1429. __func__, 'A' + layer, rc);
  1430. }
  1431. if (c->block_error.stat[1 + layer].scale != FE_SCALE_NOT_AVAILABLE)
  1432. pre_ber_layers++;
  1433. /* Handle BER post vterbi */
  1434. rc = mb86a20s_get_post_ber(fe, layer,
  1435. &bit_error, &bit_count);
  1436. if (rc >= 0) {
  1437. c->post_bit_error.stat[1 + layer].scale = FE_SCALE_COUNTER;
  1438. c->post_bit_error.stat[1 + layer].uvalue += bit_error;
  1439. c->post_bit_count.stat[1 + layer].scale = FE_SCALE_COUNTER;
  1440. c->post_bit_count.stat[1 + layer].uvalue += bit_count;
  1441. } else if (rc != -EBUSY) {
  1442. /*
  1443. * If an I/O error happened,
  1444. * measures are now unavailable
  1445. */
  1446. c->post_bit_error.stat[1 + layer].scale = FE_SCALE_NOT_AVAILABLE;
  1447. c->post_bit_count.stat[1 + layer].scale = FE_SCALE_NOT_AVAILABLE;
  1448. dev_err(&state->i2c->dev,
  1449. "%s: Can't get BER for layer %c (error %d).\n",
  1450. __func__, 'A' + layer, rc);
  1451. }
  1452. if (c->block_error.stat[1 + layer].scale != FE_SCALE_NOT_AVAILABLE)
  1453. post_ber_layers++;
  1454. /* Handle Block errors for PER/UCB reports */
  1455. rc = mb86a20s_get_blk_error(fe, layer,
  1456. &block_error,
  1457. &block_count);
  1458. if (rc >= 0) {
  1459. c->block_error.stat[1 + layer].scale = FE_SCALE_COUNTER;
  1460. c->block_error.stat[1 + layer].uvalue += block_error;
  1461. c->block_count.stat[1 + layer].scale = FE_SCALE_COUNTER;
  1462. c->block_count.stat[1 + layer].uvalue += block_count;
  1463. } else if (rc != -EBUSY) {
  1464. /*
  1465. * If an I/O error happened,
  1466. * measures are now unavailable
  1467. */
  1468. c->block_error.stat[1 + layer].scale = FE_SCALE_NOT_AVAILABLE;
  1469. c->block_count.stat[1 + layer].scale = FE_SCALE_NOT_AVAILABLE;
  1470. dev_err(&state->i2c->dev,
  1471. "%s: Can't get PER for layer %c (error %d).\n",
  1472. __func__, 'A' + layer, rc);
  1473. }
  1474. if (c->block_error.stat[1 + layer].scale != FE_SCALE_NOT_AVAILABLE)
  1475. per_layers++;
  1476. /* Update total preBER */
  1477. t_pre_bit_error += c->pre_bit_error.stat[1 + layer].uvalue;
  1478. t_pre_bit_count += c->pre_bit_count.stat[1 + layer].uvalue;
  1479. /* Update total postBER */
  1480. t_post_bit_error += c->post_bit_error.stat[1 + layer].uvalue;
  1481. t_post_bit_count += c->post_bit_count.stat[1 + layer].uvalue;
  1482. /* Update total PER */
  1483. t_block_error += c->block_error.stat[1 + layer].uvalue;
  1484. t_block_count += c->block_count.stat[1 + layer].uvalue;
  1485. }
  1486. }
  1487. /*
  1488. * Start showing global count if at least one error count is
  1489. * available.
  1490. */
  1491. if (pre_ber_layers) {
  1492. /*
  1493. * At least one per-layer BER measure was read. We can now
  1494. * calculate the total BER
  1495. *
  1496. * Total Bit Error/Count is calculated as the sum of the
  1497. * bit errors on all active layers.
  1498. */
  1499. c->pre_bit_error.stat[0].scale = FE_SCALE_COUNTER;
  1500. c->pre_bit_error.stat[0].uvalue = t_pre_bit_error;
  1501. c->pre_bit_count.stat[0].scale = FE_SCALE_COUNTER;
  1502. c->pre_bit_count.stat[0].uvalue = t_pre_bit_count;
  1503. } else {
  1504. c->pre_bit_error.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
  1505. c->pre_bit_count.stat[0].scale = FE_SCALE_COUNTER;
  1506. }
  1507. /*
  1508. * Start showing global count if at least one error count is
  1509. * available.
  1510. */
  1511. if (post_ber_layers) {
  1512. /*
  1513. * At least one per-layer BER measure was read. We can now
  1514. * calculate the total BER
  1515. *
  1516. * Total Bit Error/Count is calculated as the sum of the
  1517. * bit errors on all active layers.
  1518. */
  1519. c->post_bit_error.stat[0].scale = FE_SCALE_COUNTER;
  1520. c->post_bit_error.stat[0].uvalue = t_post_bit_error;
  1521. c->post_bit_count.stat[0].scale = FE_SCALE_COUNTER;
  1522. c->post_bit_count.stat[0].uvalue = t_post_bit_count;
  1523. } else {
  1524. c->post_bit_error.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
  1525. c->post_bit_count.stat[0].scale = FE_SCALE_COUNTER;
  1526. }
  1527. if (per_layers) {
  1528. /*
  1529. * At least one per-layer UCB measure was read. We can now
  1530. * calculate the total UCB
  1531. *
  1532. * Total block Error/Count is calculated as the sum of the
  1533. * block errors on all active layers.
  1534. */
  1535. c->block_error.stat[0].scale = FE_SCALE_COUNTER;
  1536. c->block_error.stat[0].uvalue = t_block_error;
  1537. c->block_count.stat[0].scale = FE_SCALE_COUNTER;
  1538. c->block_count.stat[0].uvalue = t_block_count;
  1539. } else {
  1540. c->block_error.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
  1541. c->block_count.stat[0].scale = FE_SCALE_COUNTER;
  1542. }
  1543. return rc;
  1544. }
  1545. /*
  1546. * The functions below are called via DVB callbacks, so they need to
  1547. * properly use the I2C gate control
  1548. */
  1549. static int mb86a20s_initfe(struct dvb_frontend *fe)
  1550. {
  1551. struct mb86a20s_state *state = fe->demodulator_priv;
  1552. u64 pll;
  1553. u32 fclk;
  1554. int rc;
  1555. u8 regD5 = 1, reg71, reg09 = 0x3a;
  1556. dev_dbg(&state->i2c->dev, "%s called.\n", __func__);
  1557. if (fe->ops.i2c_gate_ctrl)
  1558. fe->ops.i2c_gate_ctrl(fe, 0);
  1559. /* Initialize the frontend */
  1560. rc = mb86a20s_writeregdata(state, mb86a20s_init1);
  1561. if (rc < 0)
  1562. goto err;
  1563. if (!state->inversion)
  1564. reg09 |= 0x04;
  1565. rc = mb86a20s_writereg(state, 0x09, reg09);
  1566. if (rc < 0)
  1567. goto err;
  1568. if (!state->bw)
  1569. reg71 = 1;
  1570. else
  1571. reg71 = 0;
  1572. rc = mb86a20s_writereg(state, 0x39, reg71);
  1573. if (rc < 0)
  1574. goto err;
  1575. rc = mb86a20s_writereg(state, 0x71, state->bw);
  1576. if (rc < 0)
  1577. goto err;
  1578. if (state->subchannel) {
  1579. rc = mb86a20s_writereg(state, 0x44, state->subchannel);
  1580. if (rc < 0)
  1581. goto err;
  1582. }
  1583. fclk = state->config->fclk;
  1584. if (!fclk)
  1585. fclk = 32571428;
  1586. /* Adjust IF frequency to match tuner */
  1587. if (fe->ops.tuner_ops.get_if_frequency)
  1588. fe->ops.tuner_ops.get_if_frequency(fe, &state->if_freq);
  1589. if (!state->if_freq)
  1590. state->if_freq = 3300000;
  1591. pll = (((u64)1) << 34) * state->if_freq;
  1592. do_div(pll, 63 * fclk);
  1593. pll = (1 << 25) - pll;
  1594. rc = mb86a20s_writereg(state, 0x28, 0x2a);
  1595. if (rc < 0)
  1596. goto err;
  1597. rc = mb86a20s_writereg(state, 0x29, (pll >> 16) & 0xff);
  1598. if (rc < 0)
  1599. goto err;
  1600. rc = mb86a20s_writereg(state, 0x2a, (pll >> 8) & 0xff);
  1601. if (rc < 0)
  1602. goto err;
  1603. rc = mb86a20s_writereg(state, 0x2b, pll & 0xff);
  1604. if (rc < 0)
  1605. goto err;
  1606. dev_dbg(&state->i2c->dev, "%s: fclk=%d, IF=%d, clock reg=0x%06llx\n",
  1607. __func__, fclk, state->if_freq, (long long)pll);
  1608. /* pll = freq[Hz] * 2^24/10^6 / 16.285714286 */
  1609. pll = state->if_freq * 1677721600L;
  1610. do_div(pll, 1628571429L);
  1611. rc = mb86a20s_writereg(state, 0x28, 0x20);
  1612. if (rc < 0)
  1613. goto err;
  1614. rc = mb86a20s_writereg(state, 0x29, (pll >> 16) & 0xff);
  1615. if (rc < 0)
  1616. goto err;
  1617. rc = mb86a20s_writereg(state, 0x2a, (pll >> 8) & 0xff);
  1618. if (rc < 0)
  1619. goto err;
  1620. rc = mb86a20s_writereg(state, 0x2b, pll & 0xff);
  1621. if (rc < 0)
  1622. goto err;
  1623. dev_dbg(&state->i2c->dev, "%s: IF=%d, IF reg=0x%06llx\n",
  1624. __func__, state->if_freq, (long long)pll);
  1625. if (!state->config->is_serial)
  1626. regD5 &= ~1;
  1627. rc = mb86a20s_writereg(state, 0x50, 0xd5);
  1628. if (rc < 0)
  1629. goto err;
  1630. rc = mb86a20s_writereg(state, 0x51, regD5);
  1631. if (rc < 0)
  1632. goto err;
  1633. rc = mb86a20s_writeregdata(state, mb86a20s_init2);
  1634. if (rc < 0)
  1635. goto err;
  1636. err:
  1637. if (fe->ops.i2c_gate_ctrl)
  1638. fe->ops.i2c_gate_ctrl(fe, 1);
  1639. if (rc < 0) {
  1640. state->need_init = true;
  1641. dev_info(&state->i2c->dev,
  1642. "mb86a20s: Init failed. Will try again later\n");
  1643. } else {
  1644. state->need_init = false;
  1645. dev_dbg(&state->i2c->dev, "Initialization succeeded.\n");
  1646. }
  1647. return rc;
  1648. }
  1649. static int mb86a20s_set_frontend(struct dvb_frontend *fe)
  1650. {
  1651. struct mb86a20s_state *state = fe->demodulator_priv;
  1652. struct dtv_frontend_properties *c = &fe->dtv_property_cache;
  1653. int rc, if_freq;
  1654. dev_dbg(&state->i2c->dev, "%s called.\n", __func__);
  1655. if (!c->isdbt_layer_enabled)
  1656. c->isdbt_layer_enabled = 7;
  1657. if (c->isdbt_layer_enabled == 1)
  1658. state->bw = MB86A20S_1SEG;
  1659. else if (c->isdbt_partial_reception)
  1660. state->bw = MB86A20S_13SEG_PARTIAL;
  1661. else
  1662. state->bw = MB86A20S_13SEG;
  1663. if (c->inversion == INVERSION_ON)
  1664. state->inversion = true;
  1665. else
  1666. state->inversion = false;
  1667. if (!c->isdbt_sb_mode) {
  1668. state->subchannel = 0;
  1669. } else {
  1670. if (c->isdbt_sb_subchannel >= ARRAY_SIZE(mb86a20s_subchannel))
  1671. c->isdbt_sb_subchannel = 0;
  1672. state->subchannel = mb86a20s_subchannel[c->isdbt_sb_subchannel];
  1673. }
  1674. /*
  1675. * Gate should already be opened, but it doesn't hurt to
  1676. * double-check
  1677. */
  1678. if (fe->ops.i2c_gate_ctrl)
  1679. fe->ops.i2c_gate_ctrl(fe, 1);
  1680. fe->ops.tuner_ops.set_params(fe);
  1681. if (fe->ops.tuner_ops.get_if_frequency)
  1682. fe->ops.tuner_ops.get_if_frequency(fe, &if_freq);
  1683. /*
  1684. * Make it more reliable: if, for some reason, the initial
  1685. * device initialization doesn't happen, initialize it when
  1686. * a SBTVD parameters are adjusted.
  1687. *
  1688. * Unfortunately, due to a hard to track bug at tda829x/tda18271,
  1689. * the agc callback logic is not called during DVB attach time,
  1690. * causing mb86a20s to not be initialized with Kworld SBTVD.
  1691. * So, this hack is needed, in order to make Kworld SBTVD to work.
  1692. *
  1693. * It is also needed to change the IF after the initial init.
  1694. *
  1695. * HACK: Always init the frontend when set_frontend is called:
  1696. * it was noticed that, on some devices, it fails to lock on a
  1697. * different channel. So, it is better to reset everything, even
  1698. * wasting some time, than to loose channel lock.
  1699. */
  1700. mb86a20s_initfe(fe);
  1701. if (fe->ops.i2c_gate_ctrl)
  1702. fe->ops.i2c_gate_ctrl(fe, 0);
  1703. rc = mb86a20s_writeregdata(state, mb86a20s_reset_reception);
  1704. mb86a20s_reset_counters(fe);
  1705. mb86a20s_stats_not_ready(fe);
  1706. if (fe->ops.i2c_gate_ctrl)
  1707. fe->ops.i2c_gate_ctrl(fe, 1);
  1708. return rc;
  1709. }
  1710. static int mb86a20s_read_status_and_stats(struct dvb_frontend *fe,
  1711. fe_status_t *status)
  1712. {
  1713. struct mb86a20s_state *state = fe->demodulator_priv;
  1714. int rc, status_nr;
  1715. dev_dbg(&state->i2c->dev, "%s called.\n", __func__);
  1716. if (fe->ops.i2c_gate_ctrl)
  1717. fe->ops.i2c_gate_ctrl(fe, 0);
  1718. /* Get lock */
  1719. status_nr = mb86a20s_read_status(fe, status);
  1720. if (status_nr < 7) {
  1721. mb86a20s_stats_not_ready(fe);
  1722. mb86a20s_reset_frontend_cache(fe);
  1723. }
  1724. if (status_nr < 0) {
  1725. dev_err(&state->i2c->dev,
  1726. "%s: Can't read frontend lock status\n", __func__);
  1727. goto error;
  1728. }
  1729. /* Get signal strength */
  1730. rc = mb86a20s_read_signal_strength(fe);
  1731. if (rc < 0) {
  1732. dev_err(&state->i2c->dev,
  1733. "%s: Can't reset VBER registers.\n", __func__);
  1734. mb86a20s_stats_not_ready(fe);
  1735. mb86a20s_reset_frontend_cache(fe);
  1736. rc = 0; /* Status is OK */
  1737. goto error;
  1738. }
  1739. if (status_nr >= 7) {
  1740. /* Get TMCC info*/
  1741. rc = mb86a20s_get_frontend(fe);
  1742. if (rc < 0) {
  1743. dev_err(&state->i2c->dev,
  1744. "%s: Can't get FE TMCC data.\n", __func__);
  1745. rc = 0; /* Status is OK */
  1746. goto error;
  1747. }
  1748. /* Get statistics */
  1749. rc = mb86a20s_get_stats(fe, status_nr);
  1750. if (rc < 0 && rc != -EBUSY) {
  1751. dev_err(&state->i2c->dev,
  1752. "%s: Can't get FE statistics.\n", __func__);
  1753. rc = 0;
  1754. goto error;
  1755. }
  1756. rc = 0; /* Don't return EBUSY to userspace */
  1757. }
  1758. goto ok;
  1759. error:
  1760. mb86a20s_stats_not_ready(fe);
  1761. ok:
  1762. if (fe->ops.i2c_gate_ctrl)
  1763. fe->ops.i2c_gate_ctrl(fe, 1);
  1764. return rc;
  1765. }
  1766. static int mb86a20s_read_signal_strength_from_cache(struct dvb_frontend *fe,
  1767. u16 *strength)
  1768. {
  1769. struct dtv_frontend_properties *c = &fe->dtv_property_cache;
  1770. *strength = c->strength.stat[0].uvalue;
  1771. return 0;
  1772. }
  1773. static int mb86a20s_get_frontend_dummy(struct dvb_frontend *fe)
  1774. {
  1775. /*
  1776. * get_frontend is now handled together with other stats
  1777. * retrival, when read_status() is called, as some statistics
  1778. * will depend on the layers detection.
  1779. */
  1780. return 0;
  1781. };
  1782. static int mb86a20s_tune(struct dvb_frontend *fe,
  1783. bool re_tune,
  1784. unsigned int mode_flags,
  1785. unsigned int *delay,
  1786. fe_status_t *status)
  1787. {
  1788. struct mb86a20s_state *state = fe->demodulator_priv;
  1789. int rc = 0;
  1790. dev_dbg(&state->i2c->dev, "%s called.\n", __func__);
  1791. if (re_tune)
  1792. rc = mb86a20s_set_frontend(fe);
  1793. if (!(mode_flags & FE_TUNE_MODE_ONESHOT))
  1794. mb86a20s_read_status_and_stats(fe, status);
  1795. return rc;
  1796. }
  1797. static void mb86a20s_release(struct dvb_frontend *fe)
  1798. {
  1799. struct mb86a20s_state *state = fe->demodulator_priv;
  1800. dev_dbg(&state->i2c->dev, "%s called.\n", __func__);
  1801. kfree(state);
  1802. }
  1803. static struct dvb_frontend_ops mb86a20s_ops;
  1804. struct dvb_frontend *mb86a20s_attach(const struct mb86a20s_config *config,
  1805. struct i2c_adapter *i2c)
  1806. {
  1807. struct mb86a20s_state *state;
  1808. u8 rev;
  1809. dev_dbg(&i2c->dev, "%s called.\n", __func__);
  1810. /* allocate memory for the internal state */
  1811. state = kzalloc(sizeof(struct mb86a20s_state), GFP_KERNEL);
  1812. if (state == NULL) {
  1813. dev_err(&i2c->dev,
  1814. "%s: unable to allocate memory for state\n", __func__);
  1815. goto error;
  1816. }
  1817. /* setup the state */
  1818. state->config = config;
  1819. state->i2c = i2c;
  1820. /* create dvb_frontend */
  1821. memcpy(&state->frontend.ops, &mb86a20s_ops,
  1822. sizeof(struct dvb_frontend_ops));
  1823. state->frontend.demodulator_priv = state;
  1824. /* Check if it is a mb86a20s frontend */
  1825. rev = mb86a20s_readreg(state, 0);
  1826. if (rev == 0x13) {
  1827. dev_info(&i2c->dev,
  1828. "Detected a Fujitsu mb86a20s frontend\n");
  1829. } else {
  1830. dev_dbg(&i2c->dev,
  1831. "Frontend revision %d is unknown - aborting.\n",
  1832. rev);
  1833. goto error;
  1834. }
  1835. return &state->frontend;
  1836. error:
  1837. kfree(state);
  1838. return NULL;
  1839. }
  1840. EXPORT_SYMBOL(mb86a20s_attach);
  1841. static struct dvb_frontend_ops mb86a20s_ops = {
  1842. .delsys = { SYS_ISDBT },
  1843. /* Use dib8000 values per default */
  1844. .info = {
  1845. .name = "Fujitsu mb86A20s",
  1846. .caps = FE_CAN_RECOVER |
  1847. FE_CAN_FEC_1_2 | FE_CAN_FEC_2_3 | FE_CAN_FEC_3_4 |
  1848. FE_CAN_FEC_5_6 | FE_CAN_FEC_7_8 | FE_CAN_FEC_AUTO |
  1849. FE_CAN_QPSK | FE_CAN_QAM_16 | FE_CAN_QAM_64 |
  1850. FE_CAN_TRANSMISSION_MODE_AUTO | FE_CAN_QAM_AUTO |
  1851. FE_CAN_GUARD_INTERVAL_AUTO | FE_CAN_HIERARCHY_AUTO,
  1852. /* Actually, those values depend on the used tuner */
  1853. .frequency_min = 45000000,
  1854. .frequency_max = 864000000,
  1855. .frequency_stepsize = 62500,
  1856. },
  1857. .release = mb86a20s_release,
  1858. .init = mb86a20s_initfe,
  1859. .set_frontend = mb86a20s_set_frontend,
  1860. .get_frontend = mb86a20s_get_frontend_dummy,
  1861. .read_status = mb86a20s_read_status_and_stats,
  1862. .read_signal_strength = mb86a20s_read_signal_strength_from_cache,
  1863. .tune = mb86a20s_tune,
  1864. };
  1865. MODULE_DESCRIPTION("DVB Frontend module for Fujitsu mb86A20s hardware");
  1866. MODULE_AUTHOR("Mauro Carvalho Chehab");
  1867. MODULE_LICENSE("GPL");