mb86a20s.c 53 KB

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