mb86a20s.c 44 KB

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