mb86a20s.c 18 KB

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