mb86a20s.c 53 KB

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