mb86a20s.c 53 KB

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