mb86a20s.c 51 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088
  1. /*
  2. * Fujitu mb86a20s ISDB-T/ISDB-Tsb Module driver
  3. *
  4. * Copyright (C) 2010-2013 Mauro Carvalho Chehab <mchehab@redhat.com>
  5. * Copyright (C) 2009-2010 Douglas Landgraf <dougsland@redhat.com>
  6. *
  7. * This program is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU General Public License as
  9. * published by the Free Software Foundation version 2.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * General Public License for more details.
  15. */
  16. #include <linux/kernel.h>
  17. #include <asm/div64.h>
  18. #include "dvb_frontend.h"
  19. #include "mb86a20s.h"
  20. static int debug = 1;
  21. module_param(debug, int, 0644);
  22. MODULE_PARM_DESC(debug, "Activates frontend debugging (default:0)");
  23. struct mb86a20s_state {
  24. struct i2c_adapter *i2c;
  25. const struct mb86a20s_config *config;
  26. u32 last_frequency;
  27. struct dvb_frontend frontend;
  28. u32 if_freq;
  29. u32 estimated_rate[3];
  30. unsigned long get_strength_time;
  31. bool need_init;
  32. };
  33. struct regdata {
  34. u8 reg;
  35. u8 data;
  36. };
  37. #define BER_SAMPLING_RATE 1 /* Seconds */
  38. /*
  39. * Initialization sequence: Use whatevere default values that PV SBTVD
  40. * does on its initialisation, obtained via USB snoop
  41. */
  42. static struct regdata mb86a20s_init1[] = {
  43. { 0x70, 0x0f },
  44. { 0x70, 0xff },
  45. { 0x08, 0x01 },
  46. { 0x09, 0x3e },
  47. { 0x50, 0xd1 }, { 0x51, 0x22 },
  48. { 0x39, 0x01 },
  49. { 0x71, 0x00 },
  50. { 0x28, 0x2a }, { 0x29, 0x00 }, { 0x2a, 0xff }, { 0x2b, 0x80 },
  51. };
  52. static struct regdata mb86a20s_init2[] = {
  53. { 0x28, 0x22 }, { 0x29, 0x00 }, { 0x2a, 0x1f }, { 0x2b, 0xf0 },
  54. { 0x3b, 0x21 },
  55. { 0x3c, 0x3a },
  56. { 0x01, 0x0d },
  57. { 0x04, 0x08 }, { 0x05, 0x05 },
  58. { 0x04, 0x0e }, { 0x05, 0x00 },
  59. { 0x04, 0x0f }, { 0x05, 0x14 },
  60. { 0x04, 0x0b }, { 0x05, 0x8c },
  61. { 0x04, 0x00 }, { 0x05, 0x00 },
  62. { 0x04, 0x01 }, { 0x05, 0x07 },
  63. { 0x04, 0x02 }, { 0x05, 0x0f },
  64. { 0x04, 0x03 }, { 0x05, 0xa0 },
  65. { 0x04, 0x09 }, { 0x05, 0x00 },
  66. { 0x04, 0x0a }, { 0x05, 0xff },
  67. { 0x04, 0x27 }, { 0x05, 0x64 },
  68. { 0x04, 0x28 }, { 0x05, 0x00 },
  69. { 0x04, 0x1e }, { 0x05, 0xff },
  70. { 0x04, 0x29 }, { 0x05, 0x0a },
  71. { 0x04, 0x32 }, { 0x05, 0x0a },
  72. { 0x04, 0x14 }, { 0x05, 0x02 },
  73. { 0x04, 0x04 }, { 0x05, 0x00 },
  74. { 0x04, 0x05 }, { 0x05, 0x22 },
  75. { 0x04, 0x06 }, { 0x05, 0x0e },
  76. { 0x04, 0x07 }, { 0x05, 0xd8 },
  77. { 0x04, 0x12 }, { 0x05, 0x00 },
  78. { 0x04, 0x13 }, { 0x05, 0xff },
  79. { 0x04, 0x15 }, { 0x05, 0x4e },
  80. { 0x04, 0x16 }, { 0x05, 0x20 },
  81. /*
  82. * On this demod, when the bit count reaches the count below,
  83. * it collects the bit error count. The bit counters are initialized
  84. * to 65535 here. This warrants that all of them will be quickly
  85. * calculated when device gets locked. As TMCC is parsed, the values
  86. * will be adjusted later in the driver's code.
  87. */
  88. { 0x52, 0x01 }, /* Turn on BER before Viterbi */
  89. { 0x50, 0xa7 }, { 0x51, 0x00 },
  90. { 0x50, 0xa8 }, { 0x51, 0xff },
  91. { 0x50, 0xa9 }, { 0x51, 0xff },
  92. { 0x50, 0xaa }, { 0x51, 0x00 },
  93. { 0x50, 0xab }, { 0x51, 0xff },
  94. { 0x50, 0xac }, { 0x51, 0xff },
  95. { 0x50, 0xad }, { 0x51, 0x00 },
  96. { 0x50, 0xae }, { 0x51, 0xff },
  97. { 0x50, 0xaf }, { 0x51, 0xff },
  98. /*
  99. * On this demod, post BER counts blocks. When the count reaches the
  100. * value below, it collects the block error count. The block counters
  101. * are initialized to 127 here. This warrants that all of them will be
  102. * quickly calculated when device gets locked. As TMCC is parsed, the
  103. * values will be adjusted later in the driver's code.
  104. */
  105. { 0x5e, 0x07 }, /* Turn on BER after Viterbi */
  106. { 0x50, 0xdc }, { 0x51, 0x00 },
  107. { 0x50, 0xdd }, { 0x51, 0x7f },
  108. { 0x50, 0xde }, { 0x51, 0x00 },
  109. { 0x50, 0xdf }, { 0x51, 0x7f },
  110. { 0x50, 0xe0 }, { 0x51, 0x00 },
  111. { 0x50, 0xe1 }, { 0x51, 0x7f },
  112. /*
  113. * On this demod, when the block count reaches the count below,
  114. * it collects the block error count. The block counters are initialized
  115. * to 127 here. This warrants that all of them will be quickly
  116. * calculated when device gets locked. As TMCC is parsed, the values
  117. * will be adjusted later in the driver's code.
  118. */
  119. { 0x50, 0xb0 }, { 0x51, 0x07 }, /* Enable PER */
  120. { 0x50, 0xb2 }, { 0x51, 0x00 },
  121. { 0x50, 0xb3 }, { 0x51, 0x7f },
  122. { 0x50, 0xb4 }, { 0x51, 0x00 },
  123. { 0x50, 0xb5 }, { 0x51, 0x7f },
  124. { 0x50, 0xb6 }, { 0x51, 0x00 },
  125. { 0x50, 0xb7 }, { 0x51, 0x7f },
  126. { 0x50, 0x50 }, { 0x51, 0x02 }, /* MER manual mode */
  127. { 0x50, 0x51 }, { 0x51, 0x04 }, /* MER symbol 4 */
  128. { 0x45, 0x04 }, /* CN symbol 4 */
  129. { 0x48, 0x04 }, /* CN manual mode */
  130. { 0x50, 0xd5 }, { 0x51, 0x01 }, /* Serial */
  131. { 0x50, 0xd6 }, { 0x51, 0x1f },
  132. { 0x50, 0xd2 }, { 0x51, 0x03 },
  133. { 0x50, 0xd7 }, { 0x51, 0x3f },
  134. { 0x28, 0x74 }, { 0x29, 0x00 }, { 0x28, 0x74 }, { 0x29, 0x40 },
  135. { 0x28, 0x46 }, { 0x29, 0x2c }, { 0x28, 0x46 }, { 0x29, 0x0c },
  136. { 0x04, 0x40 }, { 0x05, 0x00 },
  137. { 0x28, 0x00 }, { 0x29, 0x10 },
  138. { 0x28, 0x05 }, { 0x29, 0x02 },
  139. { 0x1c, 0x01 },
  140. { 0x28, 0x06 }, { 0x29, 0x00 }, { 0x2a, 0x00 }, { 0x2b, 0x03 },
  141. { 0x28, 0x07 }, { 0x29, 0x00 }, { 0x2a, 0x00 }, { 0x2b, 0x0d },
  142. { 0x28, 0x08 }, { 0x29, 0x00 }, { 0x2a, 0x00 }, { 0x2b, 0x02 },
  143. { 0x28, 0x09 }, { 0x29, 0x00 }, { 0x2a, 0x00 }, { 0x2b, 0x01 },
  144. { 0x28, 0x0a }, { 0x29, 0x00 }, { 0x2a, 0x00 }, { 0x2b, 0x21 },
  145. { 0x28, 0x0b }, { 0x29, 0x00 }, { 0x2a, 0x00 }, { 0x2b, 0x29 },
  146. { 0x28, 0x0c }, { 0x29, 0x00 }, { 0x2a, 0x00 }, { 0x2b, 0x16 },
  147. { 0x28, 0x0d }, { 0x29, 0x00 }, { 0x2a, 0x00 }, { 0x2b, 0x31 },
  148. { 0x28, 0x0e }, { 0x29, 0x00 }, { 0x2a, 0x00 }, { 0x2b, 0x0e },
  149. { 0x28, 0x0f }, { 0x29, 0x00 }, { 0x2a, 0x00 }, { 0x2b, 0x4e },
  150. { 0x28, 0x10 }, { 0x29, 0x00 }, { 0x2a, 0x00 }, { 0x2b, 0x46 },
  151. { 0x28, 0x11 }, { 0x29, 0x00 }, { 0x2a, 0x00 }, { 0x2b, 0x0f },
  152. { 0x28, 0x12 }, { 0x29, 0x00 }, { 0x2a, 0x00 }, { 0x2b, 0x56 },
  153. { 0x28, 0x13 }, { 0x29, 0x00 }, { 0x2a, 0x00 }, { 0x2b, 0x35 },
  154. { 0x28, 0x14 }, { 0x29, 0x00 }, { 0x2a, 0x01 }, { 0x2b, 0xbe },
  155. { 0x28, 0x15 }, { 0x29, 0x00 }, { 0x2a, 0x01 }, { 0x2b, 0x84 },
  156. { 0x28, 0x16 }, { 0x29, 0x00 }, { 0x2a, 0x03 }, { 0x2b, 0xee },
  157. { 0x28, 0x17 }, { 0x29, 0x00 }, { 0x2a, 0x00 }, { 0x2b, 0x98 },
  158. { 0x28, 0x18 }, { 0x29, 0x00 }, { 0x2a, 0x00 }, { 0x2b, 0x9f },
  159. { 0x28, 0x19 }, { 0x29, 0x00 }, { 0x2a, 0x07 }, { 0x2b, 0xb2 },
  160. { 0x28, 0x1a }, { 0x29, 0x00 }, { 0x2a, 0x06 }, { 0x2b, 0xc2 },
  161. { 0x28, 0x1b }, { 0x29, 0x00 }, { 0x2a, 0x07 }, { 0x2b, 0x4a },
  162. { 0x28, 0x1c }, { 0x29, 0x00 }, { 0x2a, 0x01 }, { 0x2b, 0xbc },
  163. { 0x28, 0x1d }, { 0x29, 0x00 }, { 0x2a, 0x04 }, { 0x2b, 0xba },
  164. { 0x28, 0x1e }, { 0x29, 0x00 }, { 0x2a, 0x06 }, { 0x2b, 0x14 },
  165. { 0x50, 0x1e }, { 0x51, 0x5d },
  166. { 0x50, 0x22 }, { 0x51, 0x00 },
  167. { 0x50, 0x23 }, { 0x51, 0xc8 },
  168. { 0x50, 0x24 }, { 0x51, 0x00 },
  169. { 0x50, 0x25 }, { 0x51, 0xf0 },
  170. { 0x50, 0x26 }, { 0x51, 0x00 },
  171. { 0x50, 0x27 }, { 0x51, 0xc3 },
  172. { 0x50, 0x39 }, { 0x51, 0x02 },
  173. { 0x28, 0x6a }, { 0x29, 0x00 }, { 0x2a, 0x00 }, { 0x2b, 0x00 },
  174. { 0xd0, 0x00 },
  175. };
  176. static struct regdata mb86a20s_reset_reception[] = {
  177. { 0x70, 0xf0 },
  178. { 0x70, 0xff },
  179. { 0x08, 0x01 },
  180. { 0x08, 0x00 },
  181. };
  182. static struct regdata mb86a20s_per_ber_reset[] = {
  183. { 0x53, 0x00 }, /* pre BER Counter reset */
  184. { 0x53, 0x07 },
  185. { 0x5f, 0x00 }, /* post BER Counter reset */
  186. { 0x5f, 0x07 },
  187. { 0x50, 0xb1 }, /* PER Counter reset */
  188. { 0x51, 0x07 },
  189. { 0x51, 0x00 },
  190. };
  191. /*
  192. * I2C read/write functions and macros
  193. */
  194. static int mb86a20s_i2c_writereg(struct mb86a20s_state *state,
  195. u8 i2c_addr, u8 reg, u8 data)
  196. {
  197. u8 buf[] = { reg, data };
  198. struct i2c_msg msg = {
  199. .addr = i2c_addr, .flags = 0, .buf = buf, .len = 2
  200. };
  201. int rc;
  202. rc = i2c_transfer(state->i2c, &msg, 1);
  203. if (rc != 1) {
  204. dev_err(&state->i2c->dev,
  205. "%s: writereg error (rc == %i, reg == 0x%02x, data == 0x%02x)\n",
  206. __func__, rc, reg, data);
  207. return rc;
  208. }
  209. return 0;
  210. }
  211. static int mb86a20s_i2c_writeregdata(struct mb86a20s_state *state,
  212. u8 i2c_addr, struct regdata *rd, int size)
  213. {
  214. int i, rc;
  215. for (i = 0; i < size; i++) {
  216. rc = mb86a20s_i2c_writereg(state, i2c_addr, rd[i].reg,
  217. rd[i].data);
  218. if (rc < 0)
  219. return rc;
  220. }
  221. return 0;
  222. }
  223. static int mb86a20s_i2c_readreg(struct mb86a20s_state *state,
  224. u8 i2c_addr, u8 reg)
  225. {
  226. u8 val;
  227. int rc;
  228. struct i2c_msg msg[] = {
  229. { .addr = i2c_addr, .flags = 0, .buf = &reg, .len = 1 },
  230. { .addr = i2c_addr, .flags = I2C_M_RD, .buf = &val, .len = 1 }
  231. };
  232. rc = i2c_transfer(state->i2c, msg, 2);
  233. if (rc != 2) {
  234. dev_err(&state->i2c->dev, "%s: reg=0x%x (error=%d)\n",
  235. __func__, reg, rc);
  236. return (rc < 0) ? rc : -EIO;
  237. }
  238. return val;
  239. }
  240. #define mb86a20s_readreg(state, reg) \
  241. mb86a20s_i2c_readreg(state, state->config->demod_address, reg)
  242. #define mb86a20s_writereg(state, reg, val) \
  243. mb86a20s_i2c_writereg(state, state->config->demod_address, reg, val)
  244. #define mb86a20s_writeregdata(state, regdata) \
  245. mb86a20s_i2c_writeregdata(state, state->config->demod_address, \
  246. regdata, ARRAY_SIZE(regdata))
  247. /*
  248. * Ancillary internal routines (likely compiled inlined)
  249. *
  250. * The functions below assume that gateway lock has already obtained
  251. */
  252. static int mb86a20s_read_status(struct dvb_frontend *fe, fe_status_t *status)
  253. {
  254. struct mb86a20s_state *state = fe->demodulator_priv;
  255. int val;
  256. *status = 0;
  257. val = mb86a20s_readreg(state, 0x0a) & 0xf;
  258. if (val < 0)
  259. return val;
  260. if (val >= 2)
  261. *status |= FE_HAS_SIGNAL;
  262. if (val >= 4)
  263. *status |= FE_HAS_CARRIER;
  264. if (val >= 5)
  265. *status |= FE_HAS_VITERBI;
  266. if (val >= 7)
  267. *status |= FE_HAS_SYNC;
  268. if (val >= 8) /* Maybe 9? */
  269. *status |= FE_HAS_LOCK;
  270. dev_dbg(&state->i2c->dev, "%s: Status = 0x%02x (state = %d)\n",
  271. __func__, *status, val);
  272. return val;
  273. }
  274. static int mb86a20s_read_signal_strength(struct dvb_frontend *fe)
  275. {
  276. struct mb86a20s_state *state = fe->demodulator_priv;
  277. struct dtv_frontend_properties *c = &fe->dtv_property_cache;
  278. int rc;
  279. unsigned rf_max, rf_min, rf;
  280. if (state->get_strength_time &&
  281. (!time_after(jiffies, state->get_strength_time)))
  282. return c->strength.stat[0].uvalue;
  283. /* Reset its value if an error happen */
  284. c->strength.stat[0].uvalue = 0;
  285. /* Does a binary search to get RF strength */
  286. rf_max = 0xfff;
  287. rf_min = 0;
  288. do {
  289. rf = (rf_max + rf_min) / 2;
  290. rc = mb86a20s_writereg(state, 0x04, 0x1f);
  291. if (rc < 0)
  292. return rc;
  293. rc = mb86a20s_writereg(state, 0x05, rf >> 8);
  294. if (rc < 0)
  295. return rc;
  296. rc = mb86a20s_writereg(state, 0x04, 0x20);
  297. if (rc < 0)
  298. return rc;
  299. rc = mb86a20s_writereg(state, 0x05, rf);
  300. if (rc < 0)
  301. return rc;
  302. rc = mb86a20s_readreg(state, 0x02);
  303. if (rc < 0)
  304. return rc;
  305. if (rc & 0x08)
  306. rf_min = (rf_max + rf_min) / 2;
  307. else
  308. rf_max = (rf_max + rf_min) / 2;
  309. if (rf_max - rf_min < 4) {
  310. rf = (rf_max + rf_min) / 2;
  311. /* Rescale it from 2^12 (4096) to 2^16 */
  312. rf = rf << (16 - 12);
  313. if (rf)
  314. rf |= (1 << 12) - 1;
  315. dev_dbg(&state->i2c->dev,
  316. "%s: signal strength = %d (%d < RF=%d < %d)\n",
  317. __func__, rf, rf_min, rf >> 4, rf_max);
  318. c->strength.stat[0].uvalue = rf;
  319. state->get_strength_time = jiffies +
  320. msecs_to_jiffies(1000);
  321. return 0;
  322. }
  323. } while (1);
  324. }
  325. static int mb86a20s_get_modulation(struct mb86a20s_state *state,
  326. unsigned layer)
  327. {
  328. int rc;
  329. static unsigned char reg[] = {
  330. [0] = 0x86, /* Layer A */
  331. [1] = 0x8a, /* Layer B */
  332. [2] = 0x8e, /* Layer C */
  333. };
  334. if (layer >= ARRAY_SIZE(reg))
  335. return -EINVAL;
  336. rc = mb86a20s_writereg(state, 0x6d, reg[layer]);
  337. if (rc < 0)
  338. return rc;
  339. rc = mb86a20s_readreg(state, 0x6e);
  340. if (rc < 0)
  341. return rc;
  342. switch ((rc >> 4) & 0x07) {
  343. case 0:
  344. return DQPSK;
  345. case 1:
  346. return QPSK;
  347. case 2:
  348. return QAM_16;
  349. case 3:
  350. return QAM_64;
  351. default:
  352. return QAM_AUTO;
  353. }
  354. }
  355. static int mb86a20s_get_fec(struct mb86a20s_state *state,
  356. unsigned layer)
  357. {
  358. int rc;
  359. static unsigned char reg[] = {
  360. [0] = 0x87, /* Layer A */
  361. [1] = 0x8b, /* Layer B */
  362. [2] = 0x8f, /* Layer C */
  363. };
  364. if (layer >= ARRAY_SIZE(reg))
  365. return -EINVAL;
  366. rc = mb86a20s_writereg(state, 0x6d, reg[layer]);
  367. if (rc < 0)
  368. return rc;
  369. rc = mb86a20s_readreg(state, 0x6e);
  370. if (rc < 0)
  371. return rc;
  372. switch ((rc >> 4) & 0x07) {
  373. case 0:
  374. return FEC_1_2;
  375. case 1:
  376. return FEC_2_3;
  377. case 2:
  378. return FEC_3_4;
  379. case 3:
  380. return FEC_5_6;
  381. case 4:
  382. return FEC_7_8;
  383. default:
  384. return FEC_AUTO;
  385. }
  386. }
  387. static int mb86a20s_get_interleaving(struct mb86a20s_state *state,
  388. unsigned layer)
  389. {
  390. int rc;
  391. static unsigned char reg[] = {
  392. [0] = 0x88, /* Layer A */
  393. [1] = 0x8c, /* Layer B */
  394. [2] = 0x90, /* Layer C */
  395. };
  396. if (layer >= ARRAY_SIZE(reg))
  397. return -EINVAL;
  398. rc = mb86a20s_writereg(state, 0x6d, reg[layer]);
  399. if (rc < 0)
  400. return rc;
  401. rc = mb86a20s_readreg(state, 0x6e);
  402. if (rc < 0)
  403. return rc;
  404. switch ((rc >> 4) & 0x07) {
  405. case 1:
  406. return GUARD_INTERVAL_1_4;
  407. case 2:
  408. return GUARD_INTERVAL_1_8;
  409. case 3:
  410. return GUARD_INTERVAL_1_16;
  411. case 4:
  412. return GUARD_INTERVAL_1_32;
  413. default:
  414. case 0:
  415. return GUARD_INTERVAL_AUTO;
  416. }
  417. }
  418. static int mb86a20s_get_segment_count(struct mb86a20s_state *state,
  419. unsigned layer)
  420. {
  421. int rc, count;
  422. static unsigned char reg[] = {
  423. [0] = 0x89, /* Layer A */
  424. [1] = 0x8d, /* Layer B */
  425. [2] = 0x91, /* Layer C */
  426. };
  427. dev_dbg(&state->i2c->dev, "%s called.\n", __func__);
  428. if (layer >= ARRAY_SIZE(reg))
  429. return -EINVAL;
  430. rc = mb86a20s_writereg(state, 0x6d, reg[layer]);
  431. if (rc < 0)
  432. return rc;
  433. rc = mb86a20s_readreg(state, 0x6e);
  434. if (rc < 0)
  435. return rc;
  436. count = (rc >> 4) & 0x0f;
  437. dev_dbg(&state->i2c->dev, "%s: segments: %d.\n", __func__, count);
  438. return count;
  439. }
  440. static void mb86a20s_reset_frontend_cache(struct dvb_frontend *fe)
  441. {
  442. struct mb86a20s_state *state = fe->demodulator_priv;
  443. struct dtv_frontend_properties *c = &fe->dtv_property_cache;
  444. dev_dbg(&state->i2c->dev, "%s called.\n", __func__);
  445. /* Fixed parameters */
  446. c->delivery_system = SYS_ISDBT;
  447. c->bandwidth_hz = 6000000;
  448. /* Initialize values that will be later autodetected */
  449. c->isdbt_layer_enabled = 0;
  450. c->transmission_mode = TRANSMISSION_MODE_AUTO;
  451. c->guard_interval = GUARD_INTERVAL_AUTO;
  452. c->isdbt_sb_mode = 0;
  453. c->isdbt_sb_segment_count = 0;
  454. }
  455. /*
  456. * Estimates the bit rate using the per-segment bit rate given by
  457. * ABNT/NBR 15601 spec (table 4).
  458. */
  459. static u32 isdbt_rate[3][5][4] = {
  460. { /* DQPSK/QPSK */
  461. { 280850, 312060, 330420, 340430 }, /* 1/2 */
  462. { 374470, 416080, 440560, 453910 }, /* 2/3 */
  463. { 421280, 468090, 495630, 510650 }, /* 3/4 */
  464. { 468090, 520100, 550700, 567390 }, /* 5/6 */
  465. { 491500, 546110, 578230, 595760 }, /* 7/8 */
  466. }, { /* QAM16 */
  467. { 561710, 624130, 660840, 680870 }, /* 1/2 */
  468. { 748950, 832170, 881120, 907820 }, /* 2/3 */
  469. { 842570, 936190, 991260, 1021300 }, /* 3/4 */
  470. { 936190, 1040210, 1101400, 1134780 }, /* 5/6 */
  471. { 983000, 1092220, 1156470, 1191520 }, /* 7/8 */
  472. }, { /* QAM64 */
  473. { 842570, 936190, 991260, 1021300 }, /* 1/2 */
  474. { 1123430, 1248260, 1321680, 1361740 }, /* 2/3 */
  475. { 1263860, 1404290, 1486900, 1531950 }, /* 3/4 */
  476. { 1404290, 1560320, 1652110, 1702170 }, /* 5/6 */
  477. { 1474500, 1638340, 1734710, 1787280 }, /* 7/8 */
  478. }
  479. };
  480. static void mb86a20s_layer_bitrate(struct dvb_frontend *fe, u32 layer,
  481. u32 modulation, u32 fec, u32 interleaving,
  482. u32 segment)
  483. {
  484. struct mb86a20s_state *state = fe->demodulator_priv;
  485. u32 rate;
  486. int m, f, i;
  487. /*
  488. * If modulation/fec/interleaving is not detected, the default is
  489. * to consider the lowest bit rate, to avoid taking too long time
  490. * to get BER.
  491. */
  492. switch (modulation) {
  493. case DQPSK:
  494. case QPSK:
  495. default:
  496. m = 0;
  497. break;
  498. case QAM_16:
  499. m = 1;
  500. break;
  501. case QAM_64:
  502. m = 2;
  503. break;
  504. }
  505. switch (fec) {
  506. default:
  507. case FEC_1_2:
  508. case FEC_AUTO:
  509. f = 0;
  510. break;
  511. case FEC_2_3:
  512. f = 1;
  513. break;
  514. case FEC_3_4:
  515. f = 2;
  516. break;
  517. case FEC_5_6:
  518. f = 3;
  519. break;
  520. case FEC_7_8:
  521. f = 4;
  522. break;
  523. }
  524. switch (interleaving) {
  525. default:
  526. case GUARD_INTERVAL_1_4:
  527. i = 0;
  528. break;
  529. case GUARD_INTERVAL_1_8:
  530. i = 1;
  531. break;
  532. case GUARD_INTERVAL_1_16:
  533. i = 2;
  534. break;
  535. case GUARD_INTERVAL_1_32:
  536. i = 3;
  537. break;
  538. }
  539. /* Samples BER at BER_SAMPLING_RATE seconds */
  540. rate = isdbt_rate[m][f][i] * segment * BER_SAMPLING_RATE;
  541. /* Avoids sampling too quickly or to overflow the register */
  542. if (rate < 256)
  543. rate = 256;
  544. else if (rate > (1 << 24) - 1)
  545. rate = (1 << 24) - 1;
  546. dev_dbg(&state->i2c->dev,
  547. "%s: layer %c bitrate: %d kbps; counter = %d (0x%06x)\n",
  548. __func__, 'A' + layer, segment * isdbt_rate[m][f][i]/1000,
  549. rate, rate);
  550. state->estimated_rate[i] = 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 i, 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 (i = 0; i < 3; i++) {
  570. dev_dbg(&state->i2c->dev, "%s: getting data for layer %c.\n",
  571. __func__, 'A' + i);
  572. rc = mb86a20s_get_segment_count(state, i);
  573. if (rc < 0)
  574. goto noperlayer_error;
  575. if (rc >= 0 && rc < 14) {
  576. c->layer[i].segment_count = rc;
  577. } else {
  578. c->layer[i].segment_count = 0;
  579. state->estimated_rate[i] = 0;
  580. continue;
  581. }
  582. c->isdbt_layer_enabled |= 1 << i;
  583. rc = mb86a20s_get_modulation(state, i);
  584. if (rc < 0)
  585. goto noperlayer_error;
  586. dev_dbg(&state->i2c->dev, "%s: modulation %d.\n",
  587. __func__, rc);
  588. c->layer[i].modulation = rc;
  589. rc = mb86a20s_get_fec(state, i);
  590. if (rc < 0)
  591. goto noperlayer_error;
  592. dev_dbg(&state->i2c->dev, "%s: FEC %d.\n",
  593. __func__, rc);
  594. c->layer[i].fec = rc;
  595. rc = mb86a20s_get_interleaving(state, i);
  596. if (rc < 0)
  597. goto noperlayer_error;
  598. dev_dbg(&state->i2c->dev, "%s: interleaving %d.\n",
  599. __func__, rc);
  600. c->layer[i].interleaving = rc;
  601. mb86a20s_layer_bitrate(fe, i, c->layer[i].modulation,
  602. c->layer[i].fec,
  603. c->layer[i].interleaving,
  604. c->layer[i].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. if ((rc & 0x60) == 0x20) {
  620. switch (rc & 0x0c >> 2) {
  621. case 0:
  622. c->transmission_mode = TRANSMISSION_MODE_2K;
  623. break;
  624. case 1:
  625. c->transmission_mode = TRANSMISSION_MODE_4K;
  626. break;
  627. case 2:
  628. c->transmission_mode = TRANSMISSION_MODE_8K;
  629. break;
  630. }
  631. }
  632. if (!(rc & 0x10)) {
  633. switch (rc & 0x3) {
  634. case 0:
  635. c->guard_interval = GUARD_INTERVAL_1_4;
  636. break;
  637. case 1:
  638. c->guard_interval = GUARD_INTERVAL_1_8;
  639. break;
  640. case 2:
  641. c->guard_interval = GUARD_INTERVAL_1_16;
  642. break;
  643. }
  644. }
  645. return 0;
  646. noperlayer_error:
  647. /* per-layer info is incomplete; discard all per-layer */
  648. c->isdbt_layer_enabled = 0;
  649. return rc;
  650. }
  651. static int mb86a20s_reset_counters(struct dvb_frontend *fe)
  652. {
  653. struct mb86a20s_state *state = fe->demodulator_priv;
  654. struct dtv_frontend_properties *c = &fe->dtv_property_cache;
  655. int rc, val;
  656. dev_dbg(&state->i2c->dev, "%s called.\n", __func__);
  657. /* Reset the counters, if the channel changed */
  658. if (state->last_frequency != c->frequency) {
  659. memset(&c->strength, 0, sizeof(c->strength));
  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 >= 3)
  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 >= 3)
  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 >= 3)
  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 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 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 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. 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, 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, i;
  1267. 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 (i = 0; i < 3; i++) {
  1285. if (!(c->isdbt_layer_enabled & (1 << i))) {
  1286. c->cnr.stat[1 + i].scale = FE_SCALE_NOT_AVAILABLE;
  1287. continue;
  1288. }
  1289. rc = mb86a20s_writereg(state, 0x50, 0x52 + i * 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 + i * 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 + i * 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[i].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 + i].scale = FE_SCALE_DECIBEL;
  1328. c->cnr.stat[1 + i].svalue = cnr;
  1329. dev_dbg(&state->i2c->dev,
  1330. "%s: CNR for layer %c is %d.%03d dB (MER = %d).\n",
  1331. __func__, 'A' + i, 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 i;
  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 = 4;
  1361. c->pre_bit_error.len = 4;
  1362. c->pre_bit_count.len = 4;
  1363. c->post_bit_error.len = 4;
  1364. c->post_bit_count.len = 4;
  1365. c->block_error.len = 4;
  1366. c->block_count.len = 4;
  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 (i = 0; i < 4; i++) {
  1372. c->cnr.stat[i].scale = FE_SCALE_NOT_AVAILABLE;
  1373. c->pre_bit_error.stat[i].scale = FE_SCALE_NOT_AVAILABLE;
  1374. c->pre_bit_count.stat[i].scale = FE_SCALE_NOT_AVAILABLE;
  1375. c->post_bit_error.stat[i].scale = FE_SCALE_NOT_AVAILABLE;
  1376. c->post_bit_count.stat[i].scale = FE_SCALE_NOT_AVAILABLE;
  1377. c->block_error.stat[i].scale = FE_SCALE_NOT_AVAILABLE;
  1378. c->block_count.stat[i].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, i;
  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 (i = 0; i < 3; i++) {
  1405. if (c->isdbt_layer_enabled & (1 << i)) {
  1406. /* Layer is active and has rc segments */
  1407. active_layers++;
  1408. /* Handle BER before vterbi */
  1409. rc = mb86a20s_get_pre_ber(fe, i,
  1410. &bit_error, &bit_count);
  1411. if (rc >= 0) {
  1412. c->pre_bit_error.stat[1 + i].scale = FE_SCALE_COUNTER;
  1413. c->pre_bit_error.stat[1 + i].uvalue += bit_error;
  1414. c->pre_bit_count.stat[1 + i].scale = FE_SCALE_COUNTER;
  1415. c->pre_bit_count.stat[1 + i].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 + i].scale = FE_SCALE_NOT_AVAILABLE;
  1422. c->pre_bit_count.stat[1 + i].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' + i, rc);
  1426. }
  1427. if (c->block_error.stat[1 + i].scale != FE_SCALE_NOT_AVAILABLE)
  1428. pre_ber_layers++;
  1429. /* Handle BER post vterbi */
  1430. rc = mb86a20s_get_post_ber(fe, i,
  1431. &bit_error, &bit_count);
  1432. if (rc >= 0) {
  1433. c->post_bit_error.stat[1 + i].scale = FE_SCALE_COUNTER;
  1434. c->post_bit_error.stat[1 + i].uvalue += bit_error;
  1435. c->post_bit_count.stat[1 + i].scale = FE_SCALE_COUNTER;
  1436. c->post_bit_count.stat[1 + i].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 + i].scale = FE_SCALE_NOT_AVAILABLE;
  1443. c->post_bit_count.stat[1 + i].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' + i, rc);
  1447. }
  1448. if (c->block_error.stat[1 + i].scale != FE_SCALE_NOT_AVAILABLE)
  1449. post_ber_layers++;
  1450. /* Handle Block errors for PER/UCB reports */
  1451. rc = mb86a20s_get_blk_error(fe, i,
  1452. &block_error,
  1453. &block_count);
  1454. if (rc >= 0) {
  1455. c->block_error.stat[1 + i].scale = FE_SCALE_COUNTER;
  1456. c->block_error.stat[1 + i].uvalue += block_error;
  1457. c->block_count.stat[1 + i].scale = FE_SCALE_COUNTER;
  1458. c->block_count.stat[1 + i].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 + i].scale = FE_SCALE_NOT_AVAILABLE;
  1465. c->block_count.stat[1 + i].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' + i, rc);
  1469. }
  1470. if (c->block_error.stat[1 + i].scale != FE_SCALE_NOT_AVAILABLE)
  1471. per_layers++;
  1472. /* Update total preBER */
  1473. t_pre_bit_error += c->pre_bit_error.stat[1 + i].uvalue;
  1474. t_pre_bit_count += c->pre_bit_count.stat[1 + i].uvalue;
  1475. /* Update total postBER */
  1476. t_post_bit_error += c->post_bit_error.stat[1 + i].uvalue;
  1477. t_post_bit_count += c->post_bit_count.stat[1 + i].uvalue;
  1478. /* Update total PER */
  1479. t_block_error += c->block_error.stat[1 + i].uvalue;
  1480. t_block_count += c->block_count.stat[1 + i].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. int rc;
  1550. u8 regD5 = 1;
  1551. dev_dbg(&state->i2c->dev, "%s called.\n", __func__);
  1552. if (fe->ops.i2c_gate_ctrl)
  1553. fe->ops.i2c_gate_ctrl(fe, 0);
  1554. /* Initialize the frontend */
  1555. rc = mb86a20s_writeregdata(state, mb86a20s_init1);
  1556. if (rc < 0)
  1557. goto err;
  1558. /* Adjust IF frequency to match tuner */
  1559. if (fe->ops.tuner_ops.get_if_frequency)
  1560. fe->ops.tuner_ops.get_if_frequency(fe, &state->if_freq);
  1561. if (!state->if_freq)
  1562. state->if_freq = 3300000;
  1563. /* pll = freq[Hz] * 2^24/10^6 / 16.285714286 */
  1564. pll = state->if_freq * 1677721600L;
  1565. do_div(pll, 1628571429L);
  1566. rc = mb86a20s_writereg(state, 0x28, 0x20);
  1567. if (rc < 0)
  1568. goto err;
  1569. rc = mb86a20s_writereg(state, 0x29, (pll >> 16) & 0xff);
  1570. if (rc < 0)
  1571. goto err;
  1572. rc = mb86a20s_writereg(state, 0x2a, (pll >> 8) & 0xff);
  1573. if (rc < 0)
  1574. goto err;
  1575. rc = mb86a20s_writereg(state, 0x2b, pll & 0xff);
  1576. if (rc < 0)
  1577. goto err;
  1578. dev_dbg(&state->i2c->dev, "%s: IF=%d, PLL=0x%06llx\n",
  1579. __func__, state->if_freq, (long long)pll);
  1580. if (!state->config->is_serial) {
  1581. regD5 &= ~1;
  1582. rc = mb86a20s_writereg(state, 0x50, 0xd5);
  1583. if (rc < 0)
  1584. goto err;
  1585. rc = mb86a20s_writereg(state, 0x51, regD5);
  1586. if (rc < 0)
  1587. goto err;
  1588. }
  1589. rc = mb86a20s_writeregdata(state, mb86a20s_init2);
  1590. if (rc < 0)
  1591. goto err;
  1592. err:
  1593. if (fe->ops.i2c_gate_ctrl)
  1594. fe->ops.i2c_gate_ctrl(fe, 1);
  1595. if (rc < 0) {
  1596. state->need_init = true;
  1597. dev_info(&state->i2c->dev,
  1598. "mb86a20s: Init failed. Will try again later\n");
  1599. } else {
  1600. state->need_init = false;
  1601. dev_dbg(&state->i2c->dev, "Initialization succeeded.\n");
  1602. }
  1603. return rc;
  1604. }
  1605. static int mb86a20s_set_frontend(struct dvb_frontend *fe)
  1606. {
  1607. struct mb86a20s_state *state = fe->demodulator_priv;
  1608. int rc, if_freq;
  1609. #if 0
  1610. /*
  1611. * FIXME: Properly implement the set frontend properties
  1612. */
  1613. struct dtv_frontend_properties *c = &fe->dtv_property_cache;
  1614. #endif
  1615. dev_dbg(&state->i2c->dev, "%s called.\n", __func__);
  1616. /*
  1617. * Gate should already be opened, but it doesn't hurt to
  1618. * double-check
  1619. */
  1620. if (fe->ops.i2c_gate_ctrl)
  1621. fe->ops.i2c_gate_ctrl(fe, 1);
  1622. fe->ops.tuner_ops.set_params(fe);
  1623. if (fe->ops.tuner_ops.get_if_frequency) {
  1624. fe->ops.tuner_ops.get_if_frequency(fe, &if_freq);
  1625. /*
  1626. * If the IF frequency changed, re-initialize the
  1627. * frontend. This is needed by some drivers like tda18271,
  1628. * that only sets the IF after receiving a set_params() call
  1629. */
  1630. if (if_freq != state->if_freq)
  1631. state->need_init = true;
  1632. }
  1633. /*
  1634. * Make it more reliable: if, for some reason, the initial
  1635. * device initialization doesn't happen, initialize it when
  1636. * a SBTVD parameters are adjusted.
  1637. *
  1638. * Unfortunately, due to a hard to track bug at tda829x/tda18271,
  1639. * the agc callback logic is not called during DVB attach time,
  1640. * causing mb86a20s to not be initialized with Kworld SBTVD.
  1641. * So, this hack is needed, in order to make Kworld SBTVD to work.
  1642. *
  1643. * It is also needed to change the IF after the initial init.
  1644. */
  1645. if (state->need_init)
  1646. mb86a20s_initfe(fe);
  1647. if (fe->ops.i2c_gate_ctrl)
  1648. fe->ops.i2c_gate_ctrl(fe, 0);
  1649. rc = mb86a20s_writeregdata(state, mb86a20s_reset_reception);
  1650. mb86a20s_reset_counters(fe);
  1651. if (fe->ops.i2c_gate_ctrl)
  1652. fe->ops.i2c_gate_ctrl(fe, 1);
  1653. return rc;
  1654. }
  1655. static int mb86a20s_read_status_and_stats(struct dvb_frontend *fe,
  1656. fe_status_t *status)
  1657. {
  1658. struct mb86a20s_state *state = fe->demodulator_priv;
  1659. int rc, status_nr;
  1660. dev_dbg(&state->i2c->dev, "%s called.\n", __func__);
  1661. if (fe->ops.i2c_gate_ctrl)
  1662. fe->ops.i2c_gate_ctrl(fe, 0);
  1663. /* Get lock */
  1664. status_nr = mb86a20s_read_status(fe, status);
  1665. if (status_nr < 7) {
  1666. mb86a20s_stats_not_ready(fe);
  1667. mb86a20s_reset_frontend_cache(fe);
  1668. }
  1669. if (status_nr < 0) {
  1670. dev_err(&state->i2c->dev,
  1671. "%s: Can't read frontend lock status\n", __func__);
  1672. goto error;
  1673. }
  1674. /* Get signal strength */
  1675. rc = mb86a20s_read_signal_strength(fe);
  1676. if (rc < 0) {
  1677. dev_err(&state->i2c->dev,
  1678. "%s: Can't reset VBER registers.\n", __func__);
  1679. mb86a20s_stats_not_ready(fe);
  1680. mb86a20s_reset_frontend_cache(fe);
  1681. rc = 0; /* Status is OK */
  1682. goto error;
  1683. }
  1684. if (status_nr >= 7) {
  1685. /* Get TMCC info*/
  1686. rc = mb86a20s_get_frontend(fe);
  1687. if (rc < 0) {
  1688. dev_err(&state->i2c->dev,
  1689. "%s: Can't get FE TMCC data.\n", __func__);
  1690. rc = 0; /* Status is OK */
  1691. goto error;
  1692. }
  1693. /* Get statistics */
  1694. rc = mb86a20s_get_stats(fe, status_nr);
  1695. if (rc < 0 && rc != -EBUSY) {
  1696. dev_err(&state->i2c->dev,
  1697. "%s: Can't get FE statistics.\n", __func__);
  1698. rc = 0;
  1699. goto error;
  1700. }
  1701. rc = 0; /* Don't return EBUSY to userspace */
  1702. }
  1703. goto ok;
  1704. error:
  1705. mb86a20s_stats_not_ready(fe);
  1706. ok:
  1707. if (fe->ops.i2c_gate_ctrl)
  1708. fe->ops.i2c_gate_ctrl(fe, 1);
  1709. return rc;
  1710. }
  1711. static int mb86a20s_read_signal_strength_from_cache(struct dvb_frontend *fe,
  1712. u16 *strength)
  1713. {
  1714. struct dtv_frontend_properties *c = &fe->dtv_property_cache;
  1715. *strength = c->strength.stat[0].uvalue;
  1716. return 0;
  1717. }
  1718. static int mb86a20s_get_frontend_dummy(struct dvb_frontend *fe)
  1719. {
  1720. /*
  1721. * get_frontend is now handled together with other stats
  1722. * retrival, when read_status() is called, as some statistics
  1723. * will depend on the layers detection.
  1724. */
  1725. return 0;
  1726. };
  1727. static int mb86a20s_tune(struct dvb_frontend *fe,
  1728. bool re_tune,
  1729. unsigned int mode_flags,
  1730. unsigned int *delay,
  1731. fe_status_t *status)
  1732. {
  1733. struct mb86a20s_state *state = fe->demodulator_priv;
  1734. int rc = 0;
  1735. dev_dbg(&state->i2c->dev, "%s called.\n", __func__);
  1736. if (re_tune)
  1737. rc = mb86a20s_set_frontend(fe);
  1738. if (!(mode_flags & FE_TUNE_MODE_ONESHOT))
  1739. mb86a20s_read_status_and_stats(fe, status);
  1740. return rc;
  1741. }
  1742. static void mb86a20s_release(struct dvb_frontend *fe)
  1743. {
  1744. struct mb86a20s_state *state = fe->demodulator_priv;
  1745. dev_dbg(&state->i2c->dev, "%s called.\n", __func__);
  1746. kfree(state);
  1747. }
  1748. static struct dvb_frontend_ops mb86a20s_ops;
  1749. struct dvb_frontend *mb86a20s_attach(const struct mb86a20s_config *config,
  1750. struct i2c_adapter *i2c)
  1751. {
  1752. struct mb86a20s_state *state;
  1753. u8 rev;
  1754. dev_dbg(&i2c->dev, "%s called.\n", __func__);
  1755. /* allocate memory for the internal state */
  1756. state = kzalloc(sizeof(struct mb86a20s_state), GFP_KERNEL);
  1757. if (state == NULL) {
  1758. dev_err(&i2c->dev,
  1759. "%s: unable to allocate memory for state\n", __func__);
  1760. goto error;
  1761. }
  1762. /* setup the state */
  1763. state->config = config;
  1764. state->i2c = i2c;
  1765. /* create dvb_frontend */
  1766. memcpy(&state->frontend.ops, &mb86a20s_ops,
  1767. sizeof(struct dvb_frontend_ops));
  1768. state->frontend.demodulator_priv = state;
  1769. /* Check if it is a mb86a20s frontend */
  1770. rev = mb86a20s_readreg(state, 0);
  1771. if (rev == 0x13) {
  1772. dev_info(&i2c->dev,
  1773. "Detected a Fujitsu mb86a20s frontend\n");
  1774. } else {
  1775. dev_dbg(&i2c->dev,
  1776. "Frontend revision %d is unknown - aborting.\n",
  1777. rev);
  1778. goto error;
  1779. }
  1780. return &state->frontend;
  1781. error:
  1782. kfree(state);
  1783. return NULL;
  1784. }
  1785. EXPORT_SYMBOL(mb86a20s_attach);
  1786. static struct dvb_frontend_ops mb86a20s_ops = {
  1787. .delsys = { SYS_ISDBT },
  1788. /* Use dib8000 values per default */
  1789. .info = {
  1790. .name = "Fujitsu mb86A20s",
  1791. .caps = FE_CAN_INVERSION_AUTO | FE_CAN_RECOVER |
  1792. FE_CAN_FEC_1_2 | FE_CAN_FEC_2_3 | FE_CAN_FEC_3_4 |
  1793. FE_CAN_FEC_5_6 | FE_CAN_FEC_7_8 | FE_CAN_FEC_AUTO |
  1794. FE_CAN_QPSK | FE_CAN_QAM_16 | FE_CAN_QAM_64 |
  1795. FE_CAN_TRANSMISSION_MODE_AUTO | FE_CAN_QAM_AUTO |
  1796. FE_CAN_GUARD_INTERVAL_AUTO | FE_CAN_HIERARCHY_AUTO,
  1797. /* Actually, those values depend on the used tuner */
  1798. .frequency_min = 45000000,
  1799. .frequency_max = 864000000,
  1800. .frequency_stepsize = 62500,
  1801. },
  1802. .release = mb86a20s_release,
  1803. .init = mb86a20s_initfe,
  1804. .set_frontend = mb86a20s_set_frontend,
  1805. .get_frontend = mb86a20s_get_frontend_dummy,
  1806. .read_status = mb86a20s_read_status_and_stats,
  1807. .read_signal_strength = mb86a20s_read_signal_strength_from_cache,
  1808. .tune = mb86a20s_tune,
  1809. };
  1810. MODULE_DESCRIPTION("DVB Frontend module for Fujitsu mb86A20s hardware");
  1811. MODULE_AUTHOR("Mauro Carvalho Chehab <mchehab@redhat.com>");
  1812. MODULE_LICENSE("GPL");