mb86a20s.c 52 KB

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