mb86a20s.c 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284
  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. { 0x50, 0xb0 }, { 0x51, 0x07 },
  103. { 0x50, 0xb2 }, { 0x51, 0xff },
  104. { 0x50, 0xb3 }, { 0x51, 0xff },
  105. { 0x50, 0xb4 }, { 0x51, 0xff },
  106. { 0x50, 0xb5 }, { 0x51, 0xff },
  107. { 0x50, 0xb6 }, { 0x51, 0xff },
  108. { 0x50, 0xb7 }, { 0x51, 0xff },
  109. { 0x50, 0x50 }, { 0x51, 0x02 },
  110. { 0x50, 0x51 }, { 0x51, 0x04 }, /* MER symbol 4 */
  111. { 0x45, 0x04 }, /* CN symbol 4 */
  112. { 0x48, 0x04 },
  113. { 0x50, 0xd5 }, { 0x51, 0x01 }, /* Serial */
  114. { 0x50, 0xd6 }, { 0x51, 0x1f },
  115. { 0x50, 0xd2 }, { 0x51, 0x03 },
  116. { 0x50, 0xd7 }, { 0x51, 0x3f },
  117. { 0x28, 0x74 }, { 0x29, 0x00 }, { 0x28, 0x74 }, { 0x29, 0x40 },
  118. { 0x28, 0x46 }, { 0x29, 0x2c }, { 0x28, 0x46 }, { 0x29, 0x0c },
  119. { 0x04, 0x40 }, { 0x05, 0x00 },
  120. { 0x28, 0x00 }, { 0x29, 0x10 },
  121. { 0x28, 0x05 }, { 0x29, 0x02 },
  122. { 0x1c, 0x01 },
  123. { 0x28, 0x06 }, { 0x29, 0x00 }, { 0x2a, 0x00 }, { 0x2b, 0x03 },
  124. { 0x28, 0x07 }, { 0x29, 0x00 }, { 0x2a, 0x00 }, { 0x2b, 0x0d },
  125. { 0x28, 0x08 }, { 0x29, 0x00 }, { 0x2a, 0x00 }, { 0x2b, 0x02 },
  126. { 0x28, 0x09 }, { 0x29, 0x00 }, { 0x2a, 0x00 }, { 0x2b, 0x01 },
  127. { 0x28, 0x0a }, { 0x29, 0x00 }, { 0x2a, 0x00 }, { 0x2b, 0x21 },
  128. { 0x28, 0x0b }, { 0x29, 0x00 }, { 0x2a, 0x00 }, { 0x2b, 0x29 },
  129. { 0x28, 0x0c }, { 0x29, 0x00 }, { 0x2a, 0x00 }, { 0x2b, 0x16 },
  130. { 0x28, 0x0d }, { 0x29, 0x00 }, { 0x2a, 0x00 }, { 0x2b, 0x31 },
  131. { 0x28, 0x0e }, { 0x29, 0x00 }, { 0x2a, 0x00 }, { 0x2b, 0x0e },
  132. { 0x28, 0x0f }, { 0x29, 0x00 }, { 0x2a, 0x00 }, { 0x2b, 0x4e },
  133. { 0x28, 0x10 }, { 0x29, 0x00 }, { 0x2a, 0x00 }, { 0x2b, 0x46 },
  134. { 0x28, 0x11 }, { 0x29, 0x00 }, { 0x2a, 0x00 }, { 0x2b, 0x0f },
  135. { 0x28, 0x12 }, { 0x29, 0x00 }, { 0x2a, 0x00 }, { 0x2b, 0x56 },
  136. { 0x28, 0x13 }, { 0x29, 0x00 }, { 0x2a, 0x00 }, { 0x2b, 0x35 },
  137. { 0x28, 0x14 }, { 0x29, 0x00 }, { 0x2a, 0x01 }, { 0x2b, 0xbe },
  138. { 0x28, 0x15 }, { 0x29, 0x00 }, { 0x2a, 0x01 }, { 0x2b, 0x84 },
  139. { 0x28, 0x16 }, { 0x29, 0x00 }, { 0x2a, 0x03 }, { 0x2b, 0xee },
  140. { 0x28, 0x17 }, { 0x29, 0x00 }, { 0x2a, 0x00 }, { 0x2b, 0x98 },
  141. { 0x28, 0x18 }, { 0x29, 0x00 }, { 0x2a, 0x00 }, { 0x2b, 0x9f },
  142. { 0x28, 0x19 }, { 0x29, 0x00 }, { 0x2a, 0x07 }, { 0x2b, 0xb2 },
  143. { 0x28, 0x1a }, { 0x29, 0x00 }, { 0x2a, 0x06 }, { 0x2b, 0xc2 },
  144. { 0x28, 0x1b }, { 0x29, 0x00 }, { 0x2a, 0x07 }, { 0x2b, 0x4a },
  145. { 0x28, 0x1c }, { 0x29, 0x00 }, { 0x2a, 0x01 }, { 0x2b, 0xbc },
  146. { 0x28, 0x1d }, { 0x29, 0x00 }, { 0x2a, 0x04 }, { 0x2b, 0xba },
  147. { 0x28, 0x1e }, { 0x29, 0x00 }, { 0x2a, 0x06 }, { 0x2b, 0x14 },
  148. { 0x50, 0x1e }, { 0x51, 0x5d },
  149. { 0x50, 0x22 }, { 0x51, 0x00 },
  150. { 0x50, 0x23 }, { 0x51, 0xc8 },
  151. { 0x50, 0x24 }, { 0x51, 0x00 },
  152. { 0x50, 0x25 }, { 0x51, 0xf0 },
  153. { 0x50, 0x26 }, { 0x51, 0x00 },
  154. { 0x50, 0x27 }, { 0x51, 0xc3 },
  155. { 0x50, 0x39 }, { 0x51, 0x02 },
  156. { 0x28, 0x6a }, { 0x29, 0x00 }, { 0x2a, 0x00 }, { 0x2b, 0x00 },
  157. { 0xd0, 0x00 },
  158. };
  159. static struct regdata mb86a20s_reset_reception[] = {
  160. { 0x70, 0xf0 },
  161. { 0x70, 0xff },
  162. { 0x08, 0x01 },
  163. { 0x08, 0x00 },
  164. };
  165. static struct regdata mb86a20s_vber_reset[] = {
  166. { 0x53, 0x00 }, /* VBER Counter reset */
  167. { 0x53, 0x07 },
  168. };
  169. static struct regdata mb86a20s_per_reset[] = {
  170. { 0x50, 0xb1 }, /* PER Counter reset */
  171. { 0x51, 0x07 },
  172. { 0x51, 0x00 },
  173. };
  174. /*
  175. * I2C read/write functions and macros
  176. */
  177. static int mb86a20s_i2c_writereg(struct mb86a20s_state *state,
  178. u8 i2c_addr, u8 reg, u8 data)
  179. {
  180. u8 buf[] = { reg, data };
  181. struct i2c_msg msg = {
  182. .addr = i2c_addr, .flags = 0, .buf = buf, .len = 2
  183. };
  184. int rc;
  185. rc = i2c_transfer(state->i2c, &msg, 1);
  186. if (rc != 1) {
  187. dev_err(&state->i2c->dev,
  188. "%s: writereg error (rc == %i, reg == 0x%02x, data == 0x%02x)\n",
  189. __func__, rc, reg, data);
  190. return rc;
  191. }
  192. return 0;
  193. }
  194. static int mb86a20s_i2c_writeregdata(struct mb86a20s_state *state,
  195. u8 i2c_addr, struct regdata *rd, int size)
  196. {
  197. int i, rc;
  198. for (i = 0; i < size; i++) {
  199. rc = mb86a20s_i2c_writereg(state, i2c_addr, rd[i].reg,
  200. rd[i].data);
  201. if (rc < 0)
  202. return rc;
  203. }
  204. return 0;
  205. }
  206. static int mb86a20s_i2c_readreg(struct mb86a20s_state *state,
  207. u8 i2c_addr, u8 reg)
  208. {
  209. u8 val;
  210. int rc;
  211. struct i2c_msg msg[] = {
  212. { .addr = i2c_addr, .flags = 0, .buf = &reg, .len = 1 },
  213. { .addr = i2c_addr, .flags = I2C_M_RD, .buf = &val, .len = 1 }
  214. };
  215. rc = i2c_transfer(state->i2c, msg, 2);
  216. if (rc != 2) {
  217. dev_err(&state->i2c->dev, "%s: reg=0x%x (error=%d)\n",
  218. __func__, reg, rc);
  219. return (rc < 0) ? rc : -EIO;
  220. }
  221. return val;
  222. }
  223. #define mb86a20s_readreg(state, reg) \
  224. mb86a20s_i2c_readreg(state, state->config->demod_address, reg)
  225. #define mb86a20s_writereg(state, reg, val) \
  226. mb86a20s_i2c_writereg(state, state->config->demod_address, reg, val)
  227. #define mb86a20s_writeregdata(state, regdata) \
  228. mb86a20s_i2c_writeregdata(state, state->config->demod_address, \
  229. regdata, ARRAY_SIZE(regdata))
  230. /*
  231. * Ancillary internal routines (likely compiled inlined)
  232. *
  233. * The functions below assume that gateway lock has already obtained
  234. */
  235. static int mb86a20s_read_status(struct dvb_frontend *fe, fe_status_t *status)
  236. {
  237. struct mb86a20s_state *state = fe->demodulator_priv;
  238. int val;
  239. *status = 0;
  240. val = mb86a20s_readreg(state, 0x0a) & 0xf;
  241. if (val < 0)
  242. return val;
  243. if (val >= 2)
  244. *status |= FE_HAS_SIGNAL;
  245. if (val >= 4)
  246. *status |= FE_HAS_CARRIER;
  247. if (val >= 5)
  248. *status |= FE_HAS_VITERBI;
  249. if (val >= 7)
  250. *status |= FE_HAS_SYNC;
  251. if (val >= 8) /* Maybe 9? */
  252. *status |= FE_HAS_LOCK;
  253. dev_dbg(&state->i2c->dev, "%s: Status = 0x%02x (state = %d)\n",
  254. __func__, *status, val);
  255. return 0;
  256. }
  257. static int mb86a20s_read_signal_strength(struct dvb_frontend *fe)
  258. {
  259. struct mb86a20s_state *state = fe->demodulator_priv;
  260. int rc;
  261. unsigned rf_max, rf_min, rf;
  262. /* Does a binary search to get RF strength */
  263. rf_max = 0xfff;
  264. rf_min = 0;
  265. do {
  266. rf = (rf_max + rf_min) / 2;
  267. rc = mb86a20s_writereg(state, 0x04, 0x1f);
  268. if (rc < 0)
  269. return rc;
  270. rc = mb86a20s_writereg(state, 0x05, rf >> 8);
  271. if (rc < 0)
  272. return rc;
  273. rc = mb86a20s_writereg(state, 0x04, 0x20);
  274. if (rc < 0)
  275. return rc;
  276. rc = mb86a20s_writereg(state, 0x04, rf);
  277. if (rc < 0)
  278. return rc;
  279. rc = mb86a20s_readreg(state, 0x02);
  280. if (rc < 0)
  281. return rc;
  282. if (rc & 0x08)
  283. rf_min = (rf_max + rf_min) / 2;
  284. else
  285. rf_max = (rf_max + rf_min) / 2;
  286. if (rf_max - rf_min < 4) {
  287. rf = (rf_max + rf_min) / 2;
  288. /* Rescale it from 2^12 (4096) to 2^16 */
  289. rf <<= (16 - 12);
  290. dev_dbg(&state->i2c->dev,
  291. "%s: signal strength = %d (%d < RF=%d < %d)\n",
  292. __func__, rf, rf_min, rf >> 4, rf_max);
  293. return rf;
  294. }
  295. } while (1);
  296. return 0;
  297. }
  298. static int mb86a20s_get_modulation(struct mb86a20s_state *state,
  299. unsigned layer)
  300. {
  301. int rc;
  302. static unsigned char reg[] = {
  303. [0] = 0x86, /* Layer A */
  304. [1] = 0x8a, /* Layer B */
  305. [2] = 0x8e, /* Layer C */
  306. };
  307. if (layer >= ARRAY_SIZE(reg))
  308. return -EINVAL;
  309. rc = mb86a20s_writereg(state, 0x6d, reg[layer]);
  310. if (rc < 0)
  311. return rc;
  312. rc = mb86a20s_readreg(state, 0x6e);
  313. if (rc < 0)
  314. return rc;
  315. switch ((rc >> 4) & 0x07) {
  316. case 0:
  317. return DQPSK;
  318. case 1:
  319. return QPSK;
  320. case 2:
  321. return QAM_16;
  322. case 3:
  323. return QAM_64;
  324. default:
  325. return QAM_AUTO;
  326. }
  327. }
  328. static int mb86a20s_get_fec(struct mb86a20s_state *state,
  329. unsigned layer)
  330. {
  331. int rc;
  332. static unsigned char reg[] = {
  333. [0] = 0x87, /* Layer A */
  334. [1] = 0x8b, /* Layer B */
  335. [2] = 0x8f, /* Layer C */
  336. };
  337. if (layer >= ARRAY_SIZE(reg))
  338. return -EINVAL;
  339. rc = mb86a20s_writereg(state, 0x6d, reg[layer]);
  340. if (rc < 0)
  341. return rc;
  342. rc = mb86a20s_readreg(state, 0x6e);
  343. if (rc < 0)
  344. return rc;
  345. switch ((rc >> 4) & 0x07) {
  346. case 0:
  347. return FEC_1_2;
  348. case 1:
  349. return FEC_2_3;
  350. case 2:
  351. return FEC_3_4;
  352. case 3:
  353. return FEC_5_6;
  354. case 4:
  355. return FEC_7_8;
  356. default:
  357. return FEC_AUTO;
  358. }
  359. }
  360. static int mb86a20s_get_interleaving(struct mb86a20s_state *state,
  361. unsigned layer)
  362. {
  363. int rc;
  364. static unsigned char reg[] = {
  365. [0] = 0x88, /* Layer A */
  366. [1] = 0x8c, /* Layer B */
  367. [2] = 0x90, /* Layer C */
  368. };
  369. if (layer >= ARRAY_SIZE(reg))
  370. return -EINVAL;
  371. rc = mb86a20s_writereg(state, 0x6d, reg[layer]);
  372. if (rc < 0)
  373. return rc;
  374. rc = mb86a20s_readreg(state, 0x6e);
  375. if (rc < 0)
  376. return rc;
  377. switch ((rc >> 4) & 0x07) {
  378. case 1:
  379. return GUARD_INTERVAL_1_4;
  380. case 2:
  381. return GUARD_INTERVAL_1_8;
  382. case 3:
  383. return GUARD_INTERVAL_1_16;
  384. case 4:
  385. return GUARD_INTERVAL_1_32;
  386. default:
  387. case 0:
  388. return GUARD_INTERVAL_AUTO;
  389. }
  390. }
  391. static int mb86a20s_get_segment_count(struct mb86a20s_state *state,
  392. unsigned layer)
  393. {
  394. int rc, count;
  395. static unsigned char reg[] = {
  396. [0] = 0x89, /* Layer A */
  397. [1] = 0x8d, /* Layer B */
  398. [2] = 0x91, /* Layer C */
  399. };
  400. dev_dbg(&state->i2c->dev, "%s called.\n", __func__);
  401. if (layer >= ARRAY_SIZE(reg))
  402. return -EINVAL;
  403. rc = mb86a20s_writereg(state, 0x6d, reg[layer]);
  404. if (rc < 0)
  405. return rc;
  406. rc = mb86a20s_readreg(state, 0x6e);
  407. if (rc < 0)
  408. return rc;
  409. count = (rc >> 4) & 0x0f;
  410. dev_dbg(&state->i2c->dev, "%s: segments: %d.\n", __func__, count);
  411. return count;
  412. }
  413. static void mb86a20s_reset_frontend_cache(struct dvb_frontend *fe)
  414. {
  415. struct mb86a20s_state *state = fe->demodulator_priv;
  416. struct dtv_frontend_properties *c = &fe->dtv_property_cache;
  417. dev_dbg(&state->i2c->dev, "%s called.\n", __func__);
  418. /* Fixed parameters */
  419. c->delivery_system = SYS_ISDBT;
  420. c->bandwidth_hz = 6000000;
  421. /* Initialize values that will be later autodetected */
  422. c->isdbt_layer_enabled = 0;
  423. c->transmission_mode = TRANSMISSION_MODE_AUTO;
  424. c->guard_interval = GUARD_INTERVAL_AUTO;
  425. c->isdbt_sb_mode = 0;
  426. c->isdbt_sb_segment_count = 0;
  427. }
  428. /*
  429. * Estimates the bit rate using the per-segment bit rate given by
  430. * ABNT/NBR 15601 spec (table 4).
  431. */
  432. static u32 isdbt_rate[3][5][4] = {
  433. { /* DQPSK/QPSK */
  434. { 280850, 312060, 330420, 340430 }, /* 1/2 */
  435. { 374470, 416080, 440560, 453910 }, /* 2/3 */
  436. { 421280, 468090, 495630, 510650 }, /* 3/4 */
  437. { 468090, 520100, 550700, 567390 }, /* 5/6 */
  438. { 491500, 546110, 578230, 595760 }, /* 7/8 */
  439. }, { /* QAM16 */
  440. { 561710, 624130, 660840, 680870 }, /* 1/2 */
  441. { 748950, 832170, 881120, 907820 }, /* 2/3 */
  442. { 842570, 936190, 991260, 1021300 }, /* 3/4 */
  443. { 936190, 1040210, 1101400, 1134780 }, /* 5/6 */
  444. { 983000, 1092220, 1156470, 1191520 }, /* 7/8 */
  445. }, { /* QAM64 */
  446. { 842570, 936190, 991260, 1021300 }, /* 1/2 */
  447. { 1123430, 1248260, 1321680, 1361740 }, /* 2/3 */
  448. { 1263860, 1404290, 1486900, 1531950 }, /* 3/4 */
  449. { 1404290, 1560320, 1652110, 1702170 }, /* 5/6 */
  450. { 1474500, 1638340, 1734710, 1787280 }, /* 7/8 */
  451. }
  452. };
  453. static void mb86a20s_layer_bitrate(struct dvb_frontend *fe, u32 layer,
  454. u32 modulation, u32 fec, u32 interleaving,
  455. u32 segment)
  456. {
  457. struct mb86a20s_state *state = fe->demodulator_priv;
  458. u32 rate;
  459. int m, f, i;
  460. /*
  461. * If modulation/fec/interleaving is not detected, the default is
  462. * to consider the lowest bit rate, to avoid taking too long time
  463. * to get BER.
  464. */
  465. switch (modulation) {
  466. case DQPSK:
  467. case QPSK:
  468. default:
  469. m = 0;
  470. break;
  471. case QAM_16:
  472. m = 1;
  473. break;
  474. case QAM_64:
  475. m = 2;
  476. break;
  477. }
  478. switch (fec) {
  479. default:
  480. case FEC_1_2:
  481. case FEC_AUTO:
  482. f = 0;
  483. break;
  484. case FEC_2_3:
  485. f = 1;
  486. break;
  487. case FEC_3_4:
  488. f = 2;
  489. break;
  490. case FEC_5_6:
  491. f = 3;
  492. break;
  493. case FEC_7_8:
  494. f = 4;
  495. break;
  496. }
  497. switch (interleaving) {
  498. default:
  499. case GUARD_INTERVAL_1_4:
  500. i = 0;
  501. break;
  502. case GUARD_INTERVAL_1_8:
  503. i = 1;
  504. break;
  505. case GUARD_INTERVAL_1_16:
  506. i = 2;
  507. break;
  508. case GUARD_INTERVAL_1_32:
  509. i = 3;
  510. break;
  511. }
  512. /* Samples BER at BER_SAMPLING_RATE seconds */
  513. rate = isdbt_rate[m][f][i] * segment * BER_SAMPLING_RATE;
  514. /* Avoids sampling too quickly or to overflow the register */
  515. if (rate < 256)
  516. rate = 256;
  517. else if (rate > (1 << 24) - 1)
  518. rate = (1 << 24) - 1;
  519. dev_dbg(&state->i2c->dev,
  520. "%s: layer %c bitrate: %d kbps; counter = %d (0x%06x)\n",
  521. __func__, 'A' + layer, segment * isdbt_rate[m][f][i]/1000,
  522. rate, rate);
  523. state->estimated_rate[i] = rate;
  524. }
  525. static int mb86a20s_get_frontend(struct dvb_frontend *fe)
  526. {
  527. struct mb86a20s_state *state = fe->demodulator_priv;
  528. struct dtv_frontend_properties *c = &fe->dtv_property_cache;
  529. int i, rc;
  530. dev_dbg(&state->i2c->dev, "%s called.\n", __func__);
  531. /* Reset frontend cache to default values */
  532. mb86a20s_reset_frontend_cache(fe);
  533. /* Check for partial reception */
  534. rc = mb86a20s_writereg(state, 0x6d, 0x85);
  535. if (rc < 0)
  536. return rc;
  537. rc = mb86a20s_readreg(state, 0x6e);
  538. if (rc < 0)
  539. return rc;
  540. c->isdbt_partial_reception = (rc & 0x10) ? 1 : 0;
  541. /* Get per-layer data */
  542. for (i = 0; i < 3; i++) {
  543. dev_dbg(&state->i2c->dev, "%s: getting data for layer %c.\n",
  544. __func__, 'A' + i);
  545. rc = mb86a20s_get_segment_count(state, i);
  546. if (rc < 0)
  547. goto noperlayer_error;
  548. if (rc >= 0 && rc < 14) {
  549. c->layer[i].segment_count = rc;
  550. } else {
  551. c->layer[i].segment_count = 0;
  552. state->estimated_rate[i] = 0;
  553. continue;
  554. }
  555. c->isdbt_layer_enabled |= 1 << i;
  556. rc = mb86a20s_get_modulation(state, i);
  557. if (rc < 0)
  558. goto noperlayer_error;
  559. dev_dbg(&state->i2c->dev, "%s: modulation %d.\n",
  560. __func__, rc);
  561. c->layer[i].modulation = rc;
  562. rc = mb86a20s_get_fec(state, i);
  563. if (rc < 0)
  564. goto noperlayer_error;
  565. dev_dbg(&state->i2c->dev, "%s: FEC %d.\n",
  566. __func__, rc);
  567. c->layer[i].fec = rc;
  568. rc = mb86a20s_get_interleaving(state, i);
  569. if (rc < 0)
  570. goto noperlayer_error;
  571. dev_dbg(&state->i2c->dev, "%s: interleaving %d.\n",
  572. __func__, rc);
  573. c->layer[i].interleaving = rc;
  574. mb86a20s_layer_bitrate(fe, i, c->layer[i].modulation,
  575. c->layer[i].fec,
  576. c->layer[i].interleaving,
  577. c->layer[i].segment_count);
  578. }
  579. rc = mb86a20s_writereg(state, 0x6d, 0x84);
  580. if (rc < 0)
  581. return rc;
  582. if ((rc & 0x60) == 0x20) {
  583. c->isdbt_sb_mode = 1;
  584. /* At least, one segment should exist */
  585. if (!c->isdbt_sb_segment_count)
  586. c->isdbt_sb_segment_count = 1;
  587. }
  588. /* Get transmission mode and guard interval */
  589. rc = mb86a20s_readreg(state, 0x07);
  590. if (rc < 0)
  591. return rc;
  592. if ((rc & 0x60) == 0x20) {
  593. switch (rc & 0x0c >> 2) {
  594. case 0:
  595. c->transmission_mode = TRANSMISSION_MODE_2K;
  596. break;
  597. case 1:
  598. c->transmission_mode = TRANSMISSION_MODE_4K;
  599. break;
  600. case 2:
  601. c->transmission_mode = TRANSMISSION_MODE_8K;
  602. break;
  603. }
  604. }
  605. if (!(rc & 0x10)) {
  606. switch (rc & 0x3) {
  607. case 0:
  608. c->guard_interval = GUARD_INTERVAL_1_4;
  609. break;
  610. case 1:
  611. c->guard_interval = GUARD_INTERVAL_1_8;
  612. break;
  613. case 2:
  614. c->guard_interval = GUARD_INTERVAL_1_16;
  615. break;
  616. }
  617. }
  618. return 0;
  619. noperlayer_error:
  620. /* per-layer info is incomplete; discard all per-layer */
  621. c->isdbt_layer_enabled = 0;
  622. return rc;
  623. }
  624. static int mb86a20s_reset_counters(struct dvb_frontend *fe)
  625. {
  626. struct mb86a20s_state *state = fe->demodulator_priv;
  627. struct dtv_frontend_properties *c = &fe->dtv_property_cache;
  628. int rc, val;
  629. dev_dbg(&state->i2c->dev, "%s called.\n", __func__);
  630. /* Reset the counters, if the channel changed */
  631. if (state->last_frequency != c->frequency) {
  632. memset(&c->strength, 0, sizeof(c->strength));
  633. memset(&c->cnr, 0, sizeof(c->cnr));
  634. memset(&c->pre_bit_error, 0, sizeof(c->pre_bit_error));
  635. memset(&c->pre_bit_count, 0, sizeof(c->pre_bit_count));
  636. memset(&c->block_error, 0, sizeof(c->block_error));
  637. memset(&c->block_count, 0, sizeof(c->block_count));
  638. state->last_frequency = c->frequency;
  639. }
  640. /* Clear status for most stats */
  641. /* BER counter reset */
  642. rc = mb86a20s_writeregdata(state, mb86a20s_vber_reset);
  643. if (rc < 0)
  644. goto err;
  645. /* MER, PER counter reset */
  646. rc = mb86a20s_writeregdata(state, mb86a20s_per_reset);
  647. if (rc < 0)
  648. goto err;
  649. /* CNR counter reset */
  650. rc = mb86a20s_readreg(state, 0x45);
  651. if (rc < 0)
  652. goto err;
  653. val = rc;
  654. rc = mb86a20s_writereg(state, 0x45, val | 0x10);
  655. if (rc < 0)
  656. goto err;
  657. rc = mb86a20s_writereg(state, 0x45, val & 0x6f);
  658. if (rc < 0)
  659. goto err;
  660. /* MER counter reset */
  661. rc = mb86a20s_writereg(state, 0x50, 0x50);
  662. if (rc < 0)
  663. goto err;
  664. rc = mb86a20s_readreg(state, 0x51);
  665. if (rc < 0)
  666. goto err;
  667. val = rc;
  668. rc = mb86a20s_writereg(state, 0x51, val | 0x01);
  669. if (rc < 0)
  670. goto err;
  671. rc = mb86a20s_writereg(state, 0x51, val & 0x06);
  672. if (rc < 0)
  673. goto err;
  674. goto ok;
  675. err:
  676. dev_err(&state->i2c->dev,
  677. "%s: Can't reset FE statistics (error %d).\n",
  678. __func__, rc);
  679. ok:
  680. return rc;
  681. }
  682. static int mb86a20s_get_ber_before_vterbi(struct dvb_frontend *fe,
  683. unsigned layer,
  684. u32 *error, u32 *count)
  685. {
  686. struct mb86a20s_state *state = fe->demodulator_priv;
  687. int rc;
  688. dev_dbg(&state->i2c->dev, "%s called.\n", __func__);
  689. if (layer >= 3)
  690. return -EINVAL;
  691. /* Check if the BER measures are already available */
  692. rc = mb86a20s_readreg(state, 0x54);
  693. if (rc < 0)
  694. return rc;
  695. /* Check if data is available for that layer */
  696. if (!(rc & (1 << layer))) {
  697. dev_dbg(&state->i2c->dev,
  698. "%s: BER for layer %c is not available yet.\n",
  699. __func__, 'A' + layer);
  700. return -EBUSY;
  701. }
  702. /* Read Bit Error Count */
  703. rc = mb86a20s_readreg(state, 0x55 + layer * 3);
  704. if (rc < 0)
  705. return rc;
  706. *error = rc << 16;
  707. rc = mb86a20s_readreg(state, 0x56 + layer * 3);
  708. if (rc < 0)
  709. return rc;
  710. *error |= rc << 8;
  711. rc = mb86a20s_readreg(state, 0x57 + layer * 3);
  712. if (rc < 0)
  713. return rc;
  714. *error |= rc;
  715. dev_dbg(&state->i2c->dev,
  716. "%s: bit error before Viterbi for layer %c: %d.\n",
  717. __func__, 'A' + layer, *error);
  718. /* Read Bit Count */
  719. rc = mb86a20s_writereg(state, 0x50, 0xa7 + layer * 3);
  720. if (rc < 0)
  721. return rc;
  722. rc = mb86a20s_readreg(state, 0x51);
  723. if (rc < 0)
  724. return rc;
  725. *count = rc << 16;
  726. rc = mb86a20s_writereg(state, 0x50, 0xa8 + 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 << 8;
  733. rc = mb86a20s_writereg(state, 0x50, 0xa9 + 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;
  740. dev_dbg(&state->i2c->dev,
  741. "%s: bit count before Viterbi for layer %c: %d.\n",
  742. __func__, 'A' + layer, *count);
  743. /*
  744. * As we get TMCC data from the frontend, we can better estimate the
  745. * BER bit counters, in order to do the BER measure during a longer
  746. * time. Use those data, if available, to update the bit count
  747. * measure.
  748. */
  749. if (state->estimated_rate[layer]
  750. && state->estimated_rate[layer] != *count) {
  751. dev_dbg(&state->i2c->dev,
  752. "%s: updating layer %c counter to %d.\n",
  753. __func__, 'A' + layer, state->estimated_rate[layer]);
  754. rc = mb86a20s_writereg(state, 0x50, 0xa7 + layer * 3);
  755. if (rc < 0)
  756. return rc;
  757. rc = mb86a20s_writereg(state, 0x51,
  758. state->estimated_rate[layer] >> 16);
  759. if (rc < 0)
  760. return rc;
  761. rc = mb86a20s_writereg(state, 0x50, 0xa8 + layer * 3);
  762. if (rc < 0)
  763. return rc;
  764. rc = mb86a20s_writereg(state, 0x51,
  765. state->estimated_rate[layer] >> 8);
  766. if (rc < 0)
  767. return rc;
  768. rc = mb86a20s_writereg(state, 0x50, 0xa9 + layer * 3);
  769. if (rc < 0)
  770. return rc;
  771. rc = mb86a20s_writereg(state, 0x51,
  772. state->estimated_rate[layer]);
  773. if (rc < 0)
  774. return rc;
  775. }
  776. /* Reset counter to collect new data */
  777. rc = mb86a20s_writereg(state, 0x53, 0x07 & ~(1 << layer));
  778. if (rc < 0)
  779. return rc;
  780. rc = mb86a20s_writereg(state, 0x53, 0x07);
  781. return 0;
  782. }
  783. static void mb86a20s_stats_not_ready(struct dvb_frontend *fe)
  784. {
  785. struct mb86a20s_state *state = fe->demodulator_priv;
  786. struct dtv_frontend_properties *c = &fe->dtv_property_cache;
  787. int i;
  788. dev_dbg(&state->i2c->dev, "%s called.\n", __func__);
  789. /* Fill the length of each status counter */
  790. /* Only global stats */
  791. c->strength.len = 1;
  792. /* Per-layer stats - 3 layers + global */
  793. c->cnr.len = 4;
  794. c->pre_bit_error.len = 4;
  795. c->pre_bit_count.len = 4;
  796. c->block_error.len = 4;
  797. c->block_count.len = 4;
  798. /* Signal is always available */
  799. c->strength.stat[0].scale = FE_SCALE_RELATIVE;
  800. c->strength.stat[0].uvalue = 0;
  801. /* Put all of them at FE_SCALE_NOT_AVAILABLE */
  802. for (i = 0; i < 4; i++) {
  803. c->cnr.stat[i].scale = FE_SCALE_NOT_AVAILABLE;
  804. c->pre_bit_error.stat[i].scale = FE_SCALE_NOT_AVAILABLE;
  805. c->pre_bit_count.stat[i].scale = FE_SCALE_NOT_AVAILABLE;
  806. c->block_error.stat[i].scale = FE_SCALE_NOT_AVAILABLE;
  807. c->block_count.stat[i].scale = FE_SCALE_NOT_AVAILABLE;
  808. }
  809. }
  810. static int mb86a20s_get_stats(struct dvb_frontend *fe)
  811. {
  812. struct mb86a20s_state *state = fe->demodulator_priv;
  813. struct dtv_frontend_properties *c = &fe->dtv_property_cache;
  814. int rc = 0, i;
  815. u32 bit_error = 0, bit_count = 0;
  816. u32 t_pre_bit_error = 0, t_pre_bit_count = 0;
  817. int active_layers = 0, ber_layers = 0;
  818. /* Get per-layer stats */
  819. for (i = 0; i < 3; i++) {
  820. if (c->isdbt_layer_enabled & (1 << i)) {
  821. /* Layer is active and has rc segments */
  822. active_layers++;
  823. /* Read per-layer BER */
  824. /* Handle BER before vterbi */
  825. rc = mb86a20s_get_ber_before_vterbi(fe, i,
  826. &bit_error,
  827. &bit_count);
  828. if (rc >= 0) {
  829. c->pre_bit_error.stat[1 + i].scale = FE_SCALE_COUNTER;
  830. c->pre_bit_error.stat[1 + i].uvalue += bit_error;
  831. c->pre_bit_count.stat[1 + i].scale = FE_SCALE_COUNTER;
  832. c->pre_bit_count.stat[1 + i].uvalue += bit_count;
  833. } else if (rc != -EBUSY) {
  834. /*
  835. * If an I/O error happened,
  836. * measures are now unavailable
  837. */
  838. c->pre_bit_error.stat[1 + i].scale = FE_SCALE_NOT_AVAILABLE;
  839. c->pre_bit_count.stat[1 + i].scale = FE_SCALE_NOT_AVAILABLE;
  840. dev_err(&state->i2c->dev,
  841. "%s: Can't get BER for layer %c (error %d).\n",
  842. __func__, 'A' + i, rc);
  843. }
  844. if (c->block_error.stat[1 + i].scale != FE_SCALE_NOT_AVAILABLE)
  845. ber_layers++;
  846. /* Update total BER */
  847. t_pre_bit_error += c->pre_bit_error.stat[1 + i].uvalue;
  848. t_pre_bit_count += c->pre_bit_count.stat[1 + i].uvalue;
  849. }
  850. }
  851. /*
  852. * Start showing global count if at least one error count is
  853. * available.
  854. */
  855. if (ber_layers) {
  856. /*
  857. * At least one per-layer BER measure was read. We can now
  858. * calculate the total BER
  859. *
  860. * Total Bit Error/Count is calculated as the sum of the
  861. * bit errors on all active layers.
  862. */
  863. c->pre_bit_error.stat[0].scale = FE_SCALE_COUNTER;
  864. c->pre_bit_error.stat[0].uvalue = t_pre_bit_error;
  865. c->pre_bit_count.stat[0].scale = FE_SCALE_COUNTER;
  866. c->pre_bit_count.stat[0].uvalue = t_pre_bit_count;
  867. }
  868. return rc;
  869. }
  870. /*
  871. * The functions below are called via DVB callbacks, so they need to
  872. * properly use the I2C gate control
  873. */
  874. static int mb86a20s_initfe(struct dvb_frontend *fe)
  875. {
  876. struct mb86a20s_state *state = fe->demodulator_priv;
  877. int rc;
  878. u8 regD5 = 1;
  879. dev_dbg(&state->i2c->dev, "%s called.\n", __func__);
  880. if (fe->ops.i2c_gate_ctrl)
  881. fe->ops.i2c_gate_ctrl(fe, 0);
  882. /* Initialize the frontend */
  883. rc = mb86a20s_writeregdata(state, mb86a20s_init);
  884. if (rc < 0)
  885. goto err;
  886. if (!state->config->is_serial) {
  887. regD5 &= ~1;
  888. rc = mb86a20s_writereg(state, 0x50, 0xd5);
  889. if (rc < 0)
  890. goto err;
  891. rc = mb86a20s_writereg(state, 0x51, regD5);
  892. if (rc < 0)
  893. goto err;
  894. }
  895. err:
  896. if (fe->ops.i2c_gate_ctrl)
  897. fe->ops.i2c_gate_ctrl(fe, 1);
  898. if (rc < 0) {
  899. state->need_init = true;
  900. dev_info(&state->i2c->dev,
  901. "mb86a20s: Init failed. Will try again later\n");
  902. } else {
  903. state->need_init = false;
  904. dev_dbg(&state->i2c->dev, "Initialization succeeded.\n");
  905. }
  906. return rc;
  907. }
  908. static int mb86a20s_set_frontend(struct dvb_frontend *fe)
  909. {
  910. struct mb86a20s_state *state = fe->demodulator_priv;
  911. int rc;
  912. #if 0
  913. /*
  914. * FIXME: Properly implement the set frontend properties
  915. */
  916. struct dtv_frontend_properties *c = &fe->dtv_property_cache;
  917. #endif
  918. dev_dbg(&state->i2c->dev, "%s called.\n", __func__);
  919. /*
  920. * Gate should already be opened, but it doesn't hurt to
  921. * double-check
  922. */
  923. if (fe->ops.i2c_gate_ctrl)
  924. fe->ops.i2c_gate_ctrl(fe, 1);
  925. fe->ops.tuner_ops.set_params(fe);
  926. /*
  927. * Make it more reliable: if, for some reason, the initial
  928. * device initialization doesn't happen, initialize it when
  929. * a SBTVD parameters are adjusted.
  930. *
  931. * Unfortunately, due to a hard to track bug at tda829x/tda18271,
  932. * the agc callback logic is not called during DVB attach time,
  933. * causing mb86a20s to not be initialized with Kworld SBTVD.
  934. * So, this hack is needed, in order to make Kworld SBTVD to work.
  935. */
  936. if (state->need_init)
  937. mb86a20s_initfe(fe);
  938. if (fe->ops.i2c_gate_ctrl)
  939. fe->ops.i2c_gate_ctrl(fe, 0);
  940. rc = mb86a20s_writeregdata(state, mb86a20s_reset_reception);
  941. mb86a20s_reset_counters(fe);
  942. if (fe->ops.i2c_gate_ctrl)
  943. fe->ops.i2c_gate_ctrl(fe, 1);
  944. return rc;
  945. }
  946. static int mb86a20s_read_status_and_stats(struct dvb_frontend *fe,
  947. fe_status_t *status)
  948. {
  949. struct mb86a20s_state *state = fe->demodulator_priv;
  950. struct dtv_frontend_properties *c = &fe->dtv_property_cache;
  951. int rc;
  952. dev_dbg(&state->i2c->dev, "%s called.\n", __func__);
  953. if (fe->ops.i2c_gate_ctrl)
  954. fe->ops.i2c_gate_ctrl(fe, 0);
  955. /* Get lock */
  956. rc = mb86a20s_read_status(fe, status);
  957. if (!(*status & FE_HAS_LOCK)) {
  958. mb86a20s_stats_not_ready(fe);
  959. mb86a20s_reset_frontend_cache(fe);
  960. }
  961. if (rc < 0) {
  962. dev_err(&state->i2c->dev,
  963. "%s: Can't read frontend lock status\n", __func__);
  964. goto error;
  965. }
  966. /* Get signal strength */
  967. rc = mb86a20s_read_signal_strength(fe);
  968. if (rc < 0) {
  969. dev_err(&state->i2c->dev,
  970. "%s: Can't reset VBER registers.\n", __func__);
  971. mb86a20s_stats_not_ready(fe);
  972. mb86a20s_reset_frontend_cache(fe);
  973. rc = 0; /* Status is OK */
  974. goto error;
  975. }
  976. /* Fill signal strength */
  977. c->strength.stat[0].uvalue = rc;
  978. if (*status & FE_HAS_LOCK) {
  979. /* Get TMCC info*/
  980. rc = mb86a20s_get_frontend(fe);
  981. if (rc < 0) {
  982. dev_err(&state->i2c->dev,
  983. "%s: Can't get FE TMCC data.\n", __func__);
  984. rc = 0; /* Status is OK */
  985. goto error;
  986. }
  987. /* Get statistics */
  988. rc = mb86a20s_get_stats(fe);
  989. if (rc < 0 && rc != -EBUSY) {
  990. dev_err(&state->i2c->dev,
  991. "%s: Can't get FE statistics.\n", __func__);
  992. rc = 0;
  993. goto error;
  994. }
  995. rc = 0; /* Don't return EBUSY to userspace */
  996. }
  997. goto ok;
  998. error:
  999. mb86a20s_stats_not_ready(fe);
  1000. ok:
  1001. if (fe->ops.i2c_gate_ctrl)
  1002. fe->ops.i2c_gate_ctrl(fe, 1);
  1003. return rc;
  1004. }
  1005. static int mb86a20s_read_signal_strength_from_cache(struct dvb_frontend *fe,
  1006. u16 *strength)
  1007. {
  1008. struct dtv_frontend_properties *c = &fe->dtv_property_cache;
  1009. *strength = c->strength.stat[0].uvalue;
  1010. return 0;
  1011. }
  1012. static int mb86a20s_get_frontend_dummy(struct dvb_frontend *fe)
  1013. {
  1014. /*
  1015. * get_frontend is now handled together with other stats
  1016. * retrival, when read_status() is called, as some statistics
  1017. * will depend on the layers detection.
  1018. */
  1019. return 0;
  1020. };
  1021. static int mb86a20s_tune(struct dvb_frontend *fe,
  1022. bool re_tune,
  1023. unsigned int mode_flags,
  1024. unsigned int *delay,
  1025. fe_status_t *status)
  1026. {
  1027. struct mb86a20s_state *state = fe->demodulator_priv;
  1028. int rc = 0;
  1029. dev_dbg(&state->i2c->dev, "%s called.\n", __func__);
  1030. if (re_tune)
  1031. rc = mb86a20s_set_frontend(fe);
  1032. if (!(mode_flags & FE_TUNE_MODE_ONESHOT))
  1033. mb86a20s_read_status_and_stats(fe, status);
  1034. return rc;
  1035. }
  1036. static void mb86a20s_release(struct dvb_frontend *fe)
  1037. {
  1038. struct mb86a20s_state *state = fe->demodulator_priv;
  1039. dev_dbg(&state->i2c->dev, "%s called.\n", __func__);
  1040. kfree(state);
  1041. }
  1042. static struct dvb_frontend_ops mb86a20s_ops;
  1043. struct dvb_frontend *mb86a20s_attach(const struct mb86a20s_config *config,
  1044. struct i2c_adapter *i2c)
  1045. {
  1046. struct mb86a20s_state *state;
  1047. u8 rev;
  1048. dev_dbg(&i2c->dev, "%s called.\n", __func__);
  1049. /* allocate memory for the internal state */
  1050. state = kzalloc(sizeof(struct mb86a20s_state), GFP_KERNEL);
  1051. if (state == NULL) {
  1052. dev_err(&i2c->dev,
  1053. "%s: unable to allocate memory for state\n", __func__);
  1054. goto error;
  1055. }
  1056. /* setup the state */
  1057. state->config = config;
  1058. state->i2c = i2c;
  1059. /* create dvb_frontend */
  1060. memcpy(&state->frontend.ops, &mb86a20s_ops,
  1061. sizeof(struct dvb_frontend_ops));
  1062. state->frontend.demodulator_priv = state;
  1063. /* Check if it is a mb86a20s frontend */
  1064. rev = mb86a20s_readreg(state, 0);
  1065. if (rev == 0x13) {
  1066. dev_info(&i2c->dev,
  1067. "Detected a Fujitsu mb86a20s frontend\n");
  1068. } else {
  1069. dev_dbg(&i2c->dev,
  1070. "Frontend revision %d is unknown - aborting.\n",
  1071. rev);
  1072. goto error;
  1073. }
  1074. return &state->frontend;
  1075. error:
  1076. kfree(state);
  1077. return NULL;
  1078. }
  1079. EXPORT_SYMBOL(mb86a20s_attach);
  1080. static struct dvb_frontend_ops mb86a20s_ops = {
  1081. .delsys = { SYS_ISDBT },
  1082. /* Use dib8000 values per default */
  1083. .info = {
  1084. .name = "Fujitsu mb86A20s",
  1085. .caps = FE_CAN_INVERSION_AUTO | FE_CAN_RECOVER |
  1086. FE_CAN_FEC_1_2 | FE_CAN_FEC_2_3 | FE_CAN_FEC_3_4 |
  1087. FE_CAN_FEC_5_6 | FE_CAN_FEC_7_8 | FE_CAN_FEC_AUTO |
  1088. FE_CAN_QPSK | FE_CAN_QAM_16 | FE_CAN_QAM_64 |
  1089. FE_CAN_TRANSMISSION_MODE_AUTO | FE_CAN_QAM_AUTO |
  1090. FE_CAN_GUARD_INTERVAL_AUTO | FE_CAN_HIERARCHY_AUTO,
  1091. /* Actually, those values depend on the used tuner */
  1092. .frequency_min = 45000000,
  1093. .frequency_max = 864000000,
  1094. .frequency_stepsize = 62500,
  1095. },
  1096. .release = mb86a20s_release,
  1097. .init = mb86a20s_initfe,
  1098. .set_frontend = mb86a20s_set_frontend,
  1099. .get_frontend = mb86a20s_get_frontend_dummy,
  1100. .read_status = mb86a20s_read_status_and_stats,
  1101. .read_signal_strength = mb86a20s_read_signal_strength_from_cache,
  1102. .tune = mb86a20s_tune,
  1103. };
  1104. MODULE_DESCRIPTION("DVB Frontend module for Fujitsu mb86A20s hardware");
  1105. MODULE_AUTHOR("Mauro Carvalho Chehab <mchehab@redhat.com>");
  1106. MODULE_LICENSE("GPL");