mb86a20s.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762
  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. #define rc(args...) do { \
  24. printk(KERN_ERR "mb86a20s: " args); \
  25. } while (0)
  26. #define dprintk(args...) \
  27. do { \
  28. if (debug) { \
  29. printk(KERN_DEBUG "mb86a20s: %s: ", __func__); \
  30. printk(args); \
  31. } \
  32. } while (0)
  33. struct mb86a20s_state {
  34. struct i2c_adapter *i2c;
  35. const struct mb86a20s_config *config;
  36. struct dvb_frontend frontend;
  37. bool need_init;
  38. };
  39. struct regdata {
  40. u8 reg;
  41. u8 data;
  42. };
  43. /*
  44. * Initialization sequence: Use whatevere default values that PV SBTVD
  45. * does on its initialisation, obtained via USB snoop
  46. */
  47. static struct regdata mb86a20s_init[] = {
  48. { 0x70, 0x0f },
  49. { 0x70, 0xff },
  50. { 0x08, 0x01 },
  51. { 0x09, 0x3e },
  52. { 0x50, 0xd1 }, { 0x51, 0x22 },
  53. { 0x39, 0x01 },
  54. { 0x71, 0x00 },
  55. { 0x28, 0x2a }, { 0x29, 0x00 }, { 0x2a, 0xff }, { 0x2b, 0x80 },
  56. { 0x28, 0x20 }, { 0x29, 0x33 }, { 0x2a, 0xdf }, { 0x2b, 0xa9 },
  57. { 0x28, 0x22 }, { 0x29, 0x00 }, { 0x2a, 0x1f }, { 0x2b, 0xf0 },
  58. { 0x3b, 0x21 },
  59. { 0x3c, 0x3a },
  60. { 0x01, 0x0d },
  61. { 0x04, 0x08 }, { 0x05, 0x05 },
  62. { 0x04, 0x0e }, { 0x05, 0x00 },
  63. { 0x04, 0x0f }, { 0x05, 0x14 },
  64. { 0x04, 0x0b }, { 0x05, 0x8c },
  65. { 0x04, 0x00 }, { 0x05, 0x00 },
  66. { 0x04, 0x01 }, { 0x05, 0x07 },
  67. { 0x04, 0x02 }, { 0x05, 0x0f },
  68. { 0x04, 0x03 }, { 0x05, 0xa0 },
  69. { 0x04, 0x09 }, { 0x05, 0x00 },
  70. { 0x04, 0x0a }, { 0x05, 0xff },
  71. { 0x04, 0x27 }, { 0x05, 0x64 },
  72. { 0x04, 0x28 }, { 0x05, 0x00 },
  73. { 0x04, 0x1e }, { 0x05, 0xff },
  74. { 0x04, 0x29 }, { 0x05, 0x0a },
  75. { 0x04, 0x32 }, { 0x05, 0x0a },
  76. { 0x04, 0x14 }, { 0x05, 0x02 },
  77. { 0x04, 0x04 }, { 0x05, 0x00 },
  78. { 0x04, 0x05 }, { 0x05, 0x22 },
  79. { 0x04, 0x06 }, { 0x05, 0x0e },
  80. { 0x04, 0x07 }, { 0x05, 0xd8 },
  81. { 0x04, 0x12 }, { 0x05, 0x00 },
  82. { 0x04, 0x13 }, { 0x05, 0xff },
  83. { 0x04, 0x15 }, { 0x05, 0x4e },
  84. { 0x04, 0x16 }, { 0x05, 0x20 },
  85. { 0x52, 0x01 },
  86. { 0x50, 0xa7 }, { 0x51, 0xff },
  87. { 0x50, 0xa8 }, { 0x51, 0xff },
  88. { 0x50, 0xa9 }, { 0x51, 0xff },
  89. { 0x50, 0xaa }, { 0x51, 0xff },
  90. { 0x50, 0xab }, { 0x51, 0xff },
  91. { 0x50, 0xac }, { 0x51, 0xff },
  92. { 0x50, 0xad }, { 0x51, 0xff },
  93. { 0x50, 0xae }, { 0x51, 0xff },
  94. { 0x50, 0xaf }, { 0x51, 0xff },
  95. { 0x5e, 0x07 },
  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 },
  111. { 0x45, 0x04 },
  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. /*
  166. * I2C read/write functions and macros
  167. */
  168. static int mb86a20s_i2c_writereg(struct mb86a20s_state *state,
  169. u8 i2c_addr, int reg, int data)
  170. {
  171. u8 buf[] = { reg, data };
  172. struct i2c_msg msg = {
  173. .addr = i2c_addr, .flags = 0, .buf = buf, .len = 2
  174. };
  175. int rc;
  176. rc = i2c_transfer(state->i2c, &msg, 1);
  177. if (rc != 1) {
  178. printk("%s: writereg error (rc == %i, reg == 0x%02x,"
  179. " data == 0x%02x)\n", __func__, rc, reg, data);
  180. return rc;
  181. }
  182. return 0;
  183. }
  184. static int mb86a20s_i2c_writeregdata(struct mb86a20s_state *state,
  185. u8 i2c_addr, struct regdata *rd, int size)
  186. {
  187. int i, rc;
  188. for (i = 0; i < size; i++) {
  189. rc = mb86a20s_i2c_writereg(state, i2c_addr, rd[i].reg,
  190. rd[i].data);
  191. if (rc < 0)
  192. return rc;
  193. }
  194. return 0;
  195. }
  196. static int mb86a20s_i2c_readreg(struct mb86a20s_state *state,
  197. u8 i2c_addr, u8 reg)
  198. {
  199. u8 val;
  200. int rc;
  201. struct i2c_msg msg[] = {
  202. { .addr = i2c_addr, .flags = 0, .buf = &reg, .len = 1 },
  203. { .addr = i2c_addr, .flags = I2C_M_RD, .buf = &val, .len = 1 }
  204. };
  205. rc = i2c_transfer(state->i2c, msg, 2);
  206. if (rc != 2) {
  207. rc("%s: reg=0x%x (error=%d)\n", __func__, reg, rc);
  208. return rc;
  209. }
  210. return val;
  211. }
  212. #define mb86a20s_readreg(state, reg) \
  213. mb86a20s_i2c_readreg(state, state->config->demod_address, reg)
  214. #define mb86a20s_writereg(state, reg, val) \
  215. mb86a20s_i2c_writereg(state, state->config->demod_address, reg, val)
  216. #define mb86a20s_writeregdata(state, regdata) \
  217. mb86a20s_i2c_writeregdata(state, state->config->demod_address, \
  218. regdata, ARRAY_SIZE(regdata))
  219. static int mb86a20s_read_status(struct dvb_frontend *fe, fe_status_t *status)
  220. {
  221. struct mb86a20s_state *state = fe->demodulator_priv;
  222. int val;
  223. dprintk("\n");
  224. *status = 0;
  225. val = mb86a20s_readreg(state, 0x0a) & 0xf;
  226. if (val < 0)
  227. return val;
  228. if (val >= 2)
  229. *status |= FE_HAS_SIGNAL;
  230. if (val >= 4)
  231. *status |= FE_HAS_CARRIER;
  232. if (val >= 5)
  233. *status |= FE_HAS_VITERBI;
  234. if (val >= 7)
  235. *status |= FE_HAS_SYNC;
  236. if (val >= 8) /* Maybe 9? */
  237. *status |= FE_HAS_LOCK;
  238. dprintk("val = %d, status = 0x%02x\n", val, *status);
  239. return 0;
  240. }
  241. static int mb86a20s_read_signal_strength(struct dvb_frontend *fe, u16 *strength)
  242. {
  243. struct mb86a20s_state *state = fe->demodulator_priv;
  244. unsigned rf_max, rf_min, rf;
  245. u8 val;
  246. dprintk("\n");
  247. if (fe->ops.i2c_gate_ctrl)
  248. fe->ops.i2c_gate_ctrl(fe, 0);
  249. /* Does a binary search to get RF strength */
  250. rf_max = 0xfff;
  251. rf_min = 0;
  252. do {
  253. rf = (rf_max + rf_min) / 2;
  254. mb86a20s_writereg(state, 0x04, 0x1f);
  255. mb86a20s_writereg(state, 0x05, rf >> 8);
  256. mb86a20s_writereg(state, 0x04, 0x20);
  257. mb86a20s_writereg(state, 0x04, rf);
  258. val = mb86a20s_readreg(state, 0x02);
  259. if (val & 0x08)
  260. rf_min = (rf_max + rf_min) / 2;
  261. else
  262. rf_max = (rf_max + rf_min) / 2;
  263. if (rf_max - rf_min < 4) {
  264. *strength = (((rf_max + rf_min) / 2) * 65535) / 4095;
  265. break;
  266. }
  267. } while (1);
  268. dprintk("signal strength = %d\n", *strength);
  269. if (fe->ops.i2c_gate_ctrl)
  270. fe->ops.i2c_gate_ctrl(fe, 1);
  271. return 0;
  272. }
  273. static int mb86a20s_get_modulation(struct mb86a20s_state *state,
  274. unsigned layer)
  275. {
  276. int rc;
  277. static unsigned char reg[] = {
  278. [0] = 0x86, /* Layer A */
  279. [1] = 0x8a, /* Layer B */
  280. [2] = 0x8e, /* Layer C */
  281. };
  282. if (layer >= ARRAY_SIZE(reg))
  283. return -EINVAL;
  284. rc = mb86a20s_writereg(state, 0x6d, reg[layer]);
  285. if (rc < 0)
  286. return rc;
  287. rc = mb86a20s_readreg(state, 0x6e);
  288. if (rc < 0)
  289. return rc;
  290. switch ((rc >> 4) & 0x07) {
  291. case 0:
  292. return DQPSK;
  293. case 1:
  294. return QPSK;
  295. case 2:
  296. return QAM_16;
  297. case 3:
  298. return QAM_64;
  299. default:
  300. return QAM_AUTO;
  301. }
  302. }
  303. static int mb86a20s_get_fec(struct mb86a20s_state *state,
  304. unsigned layer)
  305. {
  306. int rc;
  307. static unsigned char reg[] = {
  308. [0] = 0x87, /* Layer A */
  309. [1] = 0x8b, /* Layer B */
  310. [2] = 0x8f, /* Layer C */
  311. };
  312. if (layer >= ARRAY_SIZE(reg))
  313. return -EINVAL;
  314. rc = mb86a20s_writereg(state, 0x6d, reg[layer]);
  315. if (rc < 0)
  316. return rc;
  317. rc = mb86a20s_readreg(state, 0x6e);
  318. if (rc < 0)
  319. return rc;
  320. switch ((rc >> 4) & 0x07) {
  321. case 0:
  322. return FEC_1_2;
  323. case 1:
  324. return FEC_2_3;
  325. case 2:
  326. return FEC_3_4;
  327. case 3:
  328. return FEC_5_6;
  329. case 4:
  330. return FEC_7_8;
  331. default:
  332. return FEC_AUTO;
  333. }
  334. }
  335. static int mb86a20s_get_interleaving(struct mb86a20s_state *state,
  336. unsigned layer)
  337. {
  338. int rc;
  339. static unsigned char reg[] = {
  340. [0] = 0x88, /* Layer A */
  341. [1] = 0x8c, /* Layer B */
  342. [2] = 0x90, /* 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 1:
  354. return GUARD_INTERVAL_1_4;
  355. case 2:
  356. return GUARD_INTERVAL_1_8;
  357. case 3:
  358. return GUARD_INTERVAL_1_16;
  359. case 4:
  360. return GUARD_INTERVAL_1_32;
  361. default:
  362. case 0:
  363. return GUARD_INTERVAL_AUTO;
  364. }
  365. }
  366. static int mb86a20s_get_segment_count(struct mb86a20s_state *state,
  367. unsigned layer)
  368. {
  369. int rc, count;
  370. static unsigned char reg[] = {
  371. [0] = 0x89, /* Layer A */
  372. [1] = 0x8d, /* Layer B */
  373. [2] = 0x91, /* Layer C */
  374. };
  375. if (layer >= ARRAY_SIZE(reg))
  376. return -EINVAL;
  377. rc = mb86a20s_writereg(state, 0x6d, reg[layer]);
  378. if (rc < 0)
  379. return rc;
  380. rc = mb86a20s_readreg(state, 0x6e);
  381. if (rc < 0)
  382. return rc;
  383. count = (rc >> 4) & 0x0f;
  384. return count;
  385. }
  386. static void mb86a20s_reset_frontend_cache(struct dvb_frontend *fe)
  387. {
  388. struct dtv_frontend_properties *c = &fe->dtv_property_cache;
  389. /* Fixed parameters */
  390. c->delivery_system = SYS_ISDBT;
  391. c->bandwidth_hz = 6000000;
  392. /* Initialize values that will be later autodetected */
  393. c->isdbt_layer_enabled = 0;
  394. c->transmission_mode = TRANSMISSION_MODE_AUTO;
  395. c->guard_interval = GUARD_INTERVAL_AUTO;
  396. c->isdbt_sb_mode = 0;
  397. c->isdbt_sb_segment_count = 0;
  398. }
  399. static int mb86a20s_get_frontend(struct dvb_frontend *fe)
  400. {
  401. struct mb86a20s_state *state = fe->demodulator_priv;
  402. struct dtv_frontend_properties *c = &fe->dtv_property_cache;
  403. int i, rc;
  404. /* Reset frontend cache to default values */
  405. mb86a20s_reset_frontend_cache(fe);
  406. if (fe->ops.i2c_gate_ctrl)
  407. fe->ops.i2c_gate_ctrl(fe, 0);
  408. /* Check for partial reception */
  409. rc = mb86a20s_writereg(state, 0x6d, 0x85);
  410. if (rc < 0)
  411. return rc;
  412. rc = mb86a20s_readreg(state, 0x6e);
  413. if (rc < 0)
  414. return rc;
  415. c->isdbt_partial_reception = (rc & 0x10) ? 1 : 0;
  416. /* Get per-layer data */
  417. for (i = 0; i < 3; i++) {
  418. rc = mb86a20s_get_segment_count(state, i);
  419. if (rc < 0)
  420. goto error;
  421. if (rc >= 0 && rc < 14)
  422. c->layer[i].segment_count = rc;
  423. else {
  424. c->layer[i].segment_count = 0;
  425. continue;
  426. }
  427. c->isdbt_layer_enabled |= 1 << i;
  428. rc = mb86a20s_get_modulation(state, i);
  429. if (rc < 0)
  430. goto error;
  431. c->layer[i].modulation = rc;
  432. rc = mb86a20s_get_fec(state, i);
  433. if (rc < 0)
  434. goto error;
  435. c->layer[i].fec = rc;
  436. rc = mb86a20s_get_interleaving(state, i);
  437. if (rc < 0)
  438. goto error;
  439. c->layer[i].interleaving = rc;
  440. }
  441. rc = mb86a20s_writereg(state, 0x6d, 0x84);
  442. if (rc < 0)
  443. return rc;
  444. if ((rc & 0x60) == 0x20) {
  445. c->isdbt_sb_mode = 1;
  446. /* At least, one segment should exist */
  447. if (!c->isdbt_sb_segment_count)
  448. c->isdbt_sb_segment_count = 1;
  449. }
  450. /* Get transmission mode and guard interval */
  451. rc = mb86a20s_readreg(state, 0x07);
  452. if (rc < 0)
  453. return rc;
  454. if ((rc & 0x60) == 0x20) {
  455. switch (rc & 0x0c >> 2) {
  456. case 0:
  457. c->transmission_mode = TRANSMISSION_MODE_2K;
  458. break;
  459. case 1:
  460. c->transmission_mode = TRANSMISSION_MODE_4K;
  461. break;
  462. case 2:
  463. c->transmission_mode = TRANSMISSION_MODE_8K;
  464. break;
  465. }
  466. }
  467. if (!(rc & 0x10)) {
  468. switch (rc & 0x3) {
  469. case 0:
  470. c->guard_interval = GUARD_INTERVAL_1_4;
  471. break;
  472. case 1:
  473. c->guard_interval = GUARD_INTERVAL_1_8;
  474. break;
  475. case 2:
  476. c->guard_interval = GUARD_INTERVAL_1_16;
  477. break;
  478. }
  479. }
  480. error:
  481. if (fe->ops.i2c_gate_ctrl)
  482. fe->ops.i2c_gate_ctrl(fe, 1);
  483. return rc;
  484. }
  485. static int mb86a20s_initfe(struct dvb_frontend *fe)
  486. {
  487. struct mb86a20s_state *state = fe->demodulator_priv;
  488. int rc;
  489. u8 regD5 = 1;
  490. dprintk("\n");
  491. if (fe->ops.i2c_gate_ctrl)
  492. fe->ops.i2c_gate_ctrl(fe, 0);
  493. /* Initialize the frontend */
  494. rc = mb86a20s_writeregdata(state, mb86a20s_init);
  495. if (rc < 0)
  496. goto err;
  497. if (!state->config->is_serial) {
  498. regD5 &= ~1;
  499. rc = mb86a20s_writereg(state, 0x50, 0xd5);
  500. if (rc < 0)
  501. goto err;
  502. rc = mb86a20s_writereg(state, 0x51, regD5);
  503. if (rc < 0)
  504. goto err;
  505. }
  506. err:
  507. if (fe->ops.i2c_gate_ctrl)
  508. fe->ops.i2c_gate_ctrl(fe, 1);
  509. if (rc < 0) {
  510. state->need_init = true;
  511. printk(KERN_INFO "mb86a20s: Init failed. Will try again later\n");
  512. } else {
  513. state->need_init = false;
  514. dprintk("Initialization succeeded.\n");
  515. }
  516. return rc;
  517. }
  518. static int mb86a20s_set_frontend(struct dvb_frontend *fe)
  519. {
  520. struct mb86a20s_state *state = fe->demodulator_priv;
  521. int rc;
  522. #if 0
  523. /*
  524. * FIXME: Properly implement the set frontend properties
  525. */
  526. struct dtv_frontend_properties *c = &fe->dtv_property_cache;
  527. #endif
  528. dprintk("\n");
  529. /*
  530. * Gate should already be opened, but it doesn't hurt to
  531. * double-check
  532. */
  533. if (fe->ops.i2c_gate_ctrl)
  534. fe->ops.i2c_gate_ctrl(fe, 1);
  535. dprintk("Calling tuner set parameters\n");
  536. fe->ops.tuner_ops.set_params(fe);
  537. /*
  538. * Make it more reliable: if, for some reason, the initial
  539. * device initialization doesn't happen, initialize it when
  540. * a SBTVD parameters are adjusted.
  541. *
  542. * Unfortunately, due to a hard to track bug at tda829x/tda18271,
  543. * the agc callback logic is not called during DVB attach time,
  544. * causing mb86a20s to not be initialized with Kworld SBTVD.
  545. * So, this hack is needed, in order to make Kworld SBTVD to work.
  546. */
  547. if (state->need_init)
  548. mb86a20s_initfe(fe);
  549. if (fe->ops.i2c_gate_ctrl)
  550. fe->ops.i2c_gate_ctrl(fe, 0);
  551. rc = mb86a20s_writeregdata(state, mb86a20s_reset_reception);
  552. if (fe->ops.i2c_gate_ctrl)
  553. fe->ops.i2c_gate_ctrl(fe, 1);
  554. return rc;
  555. }
  556. static int mb86a20s_read_status_gate(struct dvb_frontend *fe,
  557. fe_status_t *status)
  558. {
  559. int ret;
  560. dprintk("\n");
  561. *status = 0;
  562. if (fe->ops.i2c_gate_ctrl)
  563. fe->ops.i2c_gate_ctrl(fe, 0);
  564. ret = mb86a20s_read_status(fe, status);
  565. if (fe->ops.i2c_gate_ctrl)
  566. fe->ops.i2c_gate_ctrl(fe, 1);
  567. return ret;
  568. }
  569. static int mb86a20s_tune(struct dvb_frontend *fe,
  570. bool re_tune,
  571. unsigned int mode_flags,
  572. unsigned int *delay,
  573. fe_status_t *status)
  574. {
  575. int rc = 0;
  576. dprintk("\n");
  577. if (re_tune)
  578. rc = mb86a20s_set_frontend(fe);
  579. if (!(mode_flags & FE_TUNE_MODE_ONESHOT))
  580. mb86a20s_read_status_gate(fe, status);
  581. return rc;
  582. }
  583. static void mb86a20s_release(struct dvb_frontend *fe)
  584. {
  585. struct mb86a20s_state *state = fe->demodulator_priv;
  586. dprintk("\n");
  587. kfree(state);
  588. }
  589. static struct dvb_frontend_ops mb86a20s_ops;
  590. struct dvb_frontend *mb86a20s_attach(const struct mb86a20s_config *config,
  591. struct i2c_adapter *i2c)
  592. {
  593. u8 rev;
  594. /* allocate memory for the internal state */
  595. struct mb86a20s_state *state =
  596. kzalloc(sizeof(struct mb86a20s_state), GFP_KERNEL);
  597. dprintk("\n");
  598. if (state == NULL) {
  599. rc("Unable to kzalloc\n");
  600. goto error;
  601. }
  602. /* setup the state */
  603. state->config = config;
  604. state->i2c = i2c;
  605. /* create dvb_frontend */
  606. memcpy(&state->frontend.ops, &mb86a20s_ops,
  607. sizeof(struct dvb_frontend_ops));
  608. state->frontend.demodulator_priv = state;
  609. /* Check if it is a mb86a20s frontend */
  610. rev = mb86a20s_readreg(state, 0);
  611. if (rev == 0x13) {
  612. printk(KERN_INFO "Detected a Fujitsu mb86a20s frontend\n");
  613. } else {
  614. printk(KERN_ERR "Frontend revision %d is unknown - aborting.\n",
  615. rev);
  616. goto error;
  617. }
  618. return &state->frontend;
  619. error:
  620. kfree(state);
  621. return NULL;
  622. }
  623. EXPORT_SYMBOL(mb86a20s_attach);
  624. static struct dvb_frontend_ops mb86a20s_ops = {
  625. .delsys = { SYS_ISDBT },
  626. /* Use dib8000 values per default */
  627. .info = {
  628. .name = "Fujitsu mb86A20s",
  629. .caps = FE_CAN_INVERSION_AUTO | FE_CAN_RECOVER |
  630. FE_CAN_FEC_1_2 | FE_CAN_FEC_2_3 | FE_CAN_FEC_3_4 |
  631. FE_CAN_FEC_5_6 | FE_CAN_FEC_7_8 | FE_CAN_FEC_AUTO |
  632. FE_CAN_QPSK | FE_CAN_QAM_16 | FE_CAN_QAM_64 |
  633. FE_CAN_TRANSMISSION_MODE_AUTO | FE_CAN_QAM_AUTO |
  634. FE_CAN_GUARD_INTERVAL_AUTO | FE_CAN_HIERARCHY_AUTO,
  635. /* Actually, those values depend on the used tuner */
  636. .frequency_min = 45000000,
  637. .frequency_max = 864000000,
  638. .frequency_stepsize = 62500,
  639. },
  640. .release = mb86a20s_release,
  641. .init = mb86a20s_initfe,
  642. .set_frontend = mb86a20s_set_frontend,
  643. .get_frontend = mb86a20s_get_frontend,
  644. .read_status = mb86a20s_read_status_gate,
  645. .read_signal_strength = mb86a20s_read_signal_strength,
  646. .tune = mb86a20s_tune,
  647. };
  648. MODULE_DESCRIPTION("DVB Frontend module for Fujitsu mb86A20s hardware");
  649. MODULE_AUTHOR("Mauro Carvalho Chehab <mchehab@redhat.com>");
  650. MODULE_LICENSE("GPL");