mb86a20s.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615
  1. /*
  2. * Fujitu mb86a20s ISDB-T/ISDB-Tsb Module driver
  3. *
  4. * Copyright (C) 2010 Mauro Carvalho Chehab <mchehab@redhat.com>
  5. * Copyright (C) 2009-2010 Douglas Landgraf <dougsland@redhat.com>
  6. *
  7. * FIXME: Need to port to DVB v5.2 API
  8. *
  9. * This program is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU General Public License as
  11. * published by the Free Software Foundation version 2.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * General Public License for more details.
  17. */
  18. #include <linux/kernel.h>
  19. #include <asm/div64.h>
  20. #include "dvb_frontend.h"
  21. #include "mb86a20s.h"
  22. static int debug = 1;
  23. module_param(debug, int, 0644);
  24. MODULE_PARM_DESC(debug, "Activates frontend debugging (default:0)");
  25. #define rc(args...) do { \
  26. printk(KERN_ERR "mb86a20s: " args); \
  27. } while (0)
  28. #define dprintk(args...) \
  29. do { \
  30. if (debug) { \
  31. printk(KERN_DEBUG "mb86a20s: %s: ", __func__); \
  32. printk(args); \
  33. } \
  34. } while (0)
  35. struct mb86a20s_state {
  36. struct i2c_adapter *i2c;
  37. const struct mb86a20s_config *config;
  38. struct dvb_frontend frontend;
  39. };
  40. struct regdata {
  41. u8 reg;
  42. u8 data;
  43. };
  44. /*
  45. * Initialization sequence: Use whatevere default values that PV SBTVD
  46. * does on its initialisation, obtained via USB snoop
  47. */
  48. static struct regdata mb86a20s_init[] = {
  49. { 0x70, 0x0f },
  50. { 0x70, 0xff },
  51. { 0x08, 0x01 },
  52. { 0x09, 0x3e },
  53. { 0x50, 0xd1 },
  54. { 0x51, 0x22 },
  55. { 0x39, 0x01 },
  56. { 0x71, 0x00 },
  57. { 0x28, 0x2a },
  58. { 0x29, 0x00 },
  59. { 0x2a, 0xff },
  60. { 0x2b, 0x80 },
  61. { 0x28, 0x20 },
  62. { 0x29, 0x33 },
  63. { 0x2a, 0xdf },
  64. { 0x2b, 0xa9 },
  65. { 0x3b, 0x21 },
  66. { 0x3c, 0x3a },
  67. { 0x01, 0x0d },
  68. { 0x04, 0x08 },
  69. { 0x05, 0x05 },
  70. { 0x04, 0x0e },
  71. { 0x05, 0x00 },
  72. { 0x04, 0x0f },
  73. { 0x05, 0x14 },
  74. { 0x04, 0x0b },
  75. { 0x05, 0x8c },
  76. { 0x04, 0x00 },
  77. { 0x05, 0x00 },
  78. { 0x04, 0x01 },
  79. { 0x05, 0x07 },
  80. { 0x04, 0x02 },
  81. { 0x05, 0x0f },
  82. { 0x04, 0x03 },
  83. { 0x05, 0xa0 },
  84. { 0x04, 0x09 },
  85. { 0x05, 0x00 },
  86. { 0x04, 0x0a },
  87. { 0x05, 0xff },
  88. { 0x04, 0x27 },
  89. { 0x05, 0x64 },
  90. { 0x04, 0x28 },
  91. { 0x05, 0x00 },
  92. { 0x04, 0x1e },
  93. { 0x05, 0xff },
  94. { 0x04, 0x29 },
  95. { 0x05, 0x0a },
  96. { 0x04, 0x32 },
  97. { 0x05, 0x0a },
  98. { 0x04, 0x14 },
  99. { 0x05, 0x02 },
  100. { 0x04, 0x04 },
  101. { 0x05, 0x00 },
  102. { 0x04, 0x05 },
  103. { 0x05, 0x22 },
  104. { 0x04, 0x06 },
  105. { 0x05, 0x0e },
  106. { 0x04, 0x07 },
  107. { 0x05, 0xd8 },
  108. { 0x04, 0x12 },
  109. { 0x05, 0x00 },
  110. { 0x04, 0x13 },
  111. { 0x05, 0xff },
  112. { 0x52, 0x01 },
  113. { 0x50, 0xa7 },
  114. { 0x51, 0x00 },
  115. { 0x50, 0xa8 },
  116. { 0x51, 0xff },
  117. { 0x50, 0xa9 },
  118. { 0x51, 0xff },
  119. { 0x50, 0xaa },
  120. { 0x51, 0x00 },
  121. { 0x50, 0xab },
  122. { 0x51, 0xff },
  123. { 0x50, 0xac },
  124. { 0x51, 0xff },
  125. { 0x50, 0xad },
  126. { 0x51, 0x00 },
  127. { 0x50, 0xae },
  128. { 0x51, 0xff },
  129. { 0x50, 0xaf },
  130. { 0x51, 0xff },
  131. { 0x5e, 0x07 },
  132. { 0x50, 0xdc },
  133. { 0x51, 0x01 },
  134. { 0x50, 0xdd },
  135. { 0x51, 0xf4 },
  136. { 0x50, 0xde },
  137. { 0x51, 0x01 },
  138. { 0x50, 0xdf },
  139. { 0x51, 0xf4 },
  140. { 0x50, 0xe0 },
  141. { 0x51, 0x01 },
  142. { 0x50, 0xe1 },
  143. { 0x51, 0xf4 },
  144. { 0x50, 0xb0 },
  145. { 0x51, 0x07 },
  146. { 0x50, 0xb2 },
  147. { 0x51, 0xff },
  148. { 0x50, 0xb3 },
  149. { 0x51, 0xff },
  150. { 0x50, 0xb4 },
  151. { 0x51, 0xff },
  152. { 0x50, 0xb5 },
  153. { 0x51, 0xff },
  154. { 0x50, 0xb6 },
  155. { 0x51, 0xff },
  156. { 0x50, 0xb7 },
  157. { 0x51, 0xff },
  158. { 0x50, 0x50 },
  159. { 0x51, 0x02 },
  160. { 0x50, 0x51 },
  161. { 0x51, 0x04 },
  162. { 0x45, 0x04 },
  163. { 0x48, 0x04 },
  164. { 0x50, 0xd5 },
  165. { 0x51, 0x01 }, /* Serial */
  166. { 0x50, 0xd6 },
  167. { 0x51, 0x1f },
  168. { 0x50, 0xd2 },
  169. { 0x51, 0x03 },
  170. { 0x50, 0xd7 },
  171. { 0x51, 0x3f },
  172. { 0x1c, 0x01 },
  173. { 0x28, 0x06 },
  174. { 0x29, 0x00 },
  175. { 0x2a, 0x00 },
  176. { 0x2b, 0x03 },
  177. { 0x28, 0x07 },
  178. { 0x29, 0x00 },
  179. { 0x2a, 0x00 },
  180. { 0x2b, 0x0d },
  181. { 0x28, 0x08 },
  182. { 0x29, 0x00 },
  183. { 0x2a, 0x00 },
  184. { 0x2b, 0x02 },
  185. { 0x28, 0x09 },
  186. { 0x29, 0x00 },
  187. { 0x2a, 0x00 },
  188. { 0x2b, 0x01 },
  189. { 0x28, 0x0a },
  190. { 0x29, 0x00 },
  191. { 0x2a, 0x00 },
  192. { 0x2b, 0x21 },
  193. { 0x28, 0x0b },
  194. { 0x29, 0x00 },
  195. { 0x2a, 0x00 },
  196. { 0x2b, 0x29 },
  197. { 0x28, 0x0c },
  198. { 0x29, 0x00 },
  199. { 0x2a, 0x00 },
  200. { 0x2b, 0x16 },
  201. { 0x28, 0x0d },
  202. { 0x29, 0x00 },
  203. { 0x2a, 0x00 },
  204. { 0x2b, 0x31 },
  205. { 0x28, 0x0e },
  206. { 0x29, 0x00 },
  207. { 0x2a, 0x00 },
  208. { 0x2b, 0x0e },
  209. { 0x28, 0x0f },
  210. { 0x29, 0x00 },
  211. { 0x2a, 0x00 },
  212. { 0x2b, 0x4e },
  213. { 0x28, 0x10 },
  214. { 0x29, 0x00 },
  215. { 0x2a, 0x00 },
  216. { 0x2b, 0x46 },
  217. { 0x28, 0x11 },
  218. { 0x29, 0x00 },
  219. { 0x2a, 0x00 },
  220. { 0x2b, 0x0f },
  221. { 0x28, 0x12 },
  222. { 0x29, 0x00 },
  223. { 0x2a, 0x00 },
  224. { 0x2b, 0x56 },
  225. { 0x28, 0x13 },
  226. { 0x29, 0x00 },
  227. { 0x2a, 0x00 },
  228. { 0x2b, 0x35 },
  229. { 0x28, 0x14 },
  230. { 0x29, 0x00 },
  231. { 0x2a, 0x01 },
  232. { 0x2b, 0xbe },
  233. { 0x28, 0x15 },
  234. { 0x29, 0x00 },
  235. { 0x2a, 0x01 },
  236. { 0x2b, 0x84 },
  237. { 0x28, 0x16 },
  238. { 0x29, 0x00 },
  239. { 0x2a, 0x03 },
  240. { 0x2b, 0xee },
  241. { 0x28, 0x17 },
  242. { 0x29, 0x00 },
  243. { 0x2a, 0x00 },
  244. { 0x2b, 0x98 },
  245. { 0x28, 0x18 },
  246. { 0x29, 0x00 },
  247. { 0x2a, 0x00 },
  248. { 0x2b, 0x9f },
  249. { 0x28, 0x19 },
  250. { 0x29, 0x00 },
  251. { 0x2a, 0x07 },
  252. { 0x2b, 0xb2 },
  253. { 0x28, 0x1a },
  254. { 0x29, 0x00 },
  255. { 0x2a, 0x06 },
  256. { 0x2b, 0xc2 },
  257. { 0x28, 0x1b },
  258. { 0x29, 0x00 },
  259. { 0x2a, 0x07 },
  260. { 0x2b, 0x4a },
  261. { 0x28, 0x1c },
  262. { 0x29, 0x00 },
  263. { 0x2a, 0x01 },
  264. { 0x2b, 0xbc },
  265. { 0x28, 0x1d },
  266. { 0x29, 0x00 },
  267. { 0x2a, 0x04 },
  268. { 0x2b, 0xba },
  269. { 0x28, 0x1e },
  270. { 0x29, 0x00 },
  271. { 0x2a, 0x06 },
  272. { 0x2b, 0x14 },
  273. { 0x50, 0x1e },
  274. { 0x51, 0x5d },
  275. { 0x50, 0x22 },
  276. { 0x51, 0x00 },
  277. { 0x50, 0x23 },
  278. { 0x51, 0xc8 },
  279. { 0x50, 0x24 },
  280. { 0x51, 0x00 },
  281. { 0x50, 0x25 },
  282. { 0x51, 0xf0 },
  283. { 0x50, 0x26 },
  284. { 0x51, 0x00 },
  285. { 0x50, 0x27 },
  286. { 0x51, 0xc3 },
  287. { 0x50, 0x39 },
  288. { 0x51, 0x02 },
  289. { 0x50, 0xd5 },
  290. { 0x51, 0x01 },
  291. { 0xd0, 0x00 },
  292. };
  293. static struct regdata mb86a20s_reset_reception[] = {
  294. { 0x70, 0xf0 },
  295. { 0x70, 0xff },
  296. { 0x08, 0x01 },
  297. { 0x08, 0x00 },
  298. };
  299. static int mb86a20s_i2c_writereg(struct mb86a20s_state *state,
  300. u8 i2c_addr, int reg, int data)
  301. {
  302. u8 buf[] = { reg, data };
  303. struct i2c_msg msg = {
  304. .addr = i2c_addr, .flags = 0, .buf = buf, .len = 2
  305. };
  306. int rc;
  307. rc = i2c_transfer(state->i2c, &msg, 1);
  308. if (rc != 1) {
  309. printk("%s: writereg rcor(rc == %i, reg == 0x%02x,"
  310. " data == 0x%02x)\n", __func__, rc, reg, data);
  311. return rc;
  312. }
  313. return 0;
  314. }
  315. static int mb86a20s_i2c_writeregdata(struct mb86a20s_state *state,
  316. u8 i2c_addr, struct regdata *rd, int size)
  317. {
  318. int i, rc;
  319. for (i = 0; i < size; i++) {
  320. rc = mb86a20s_i2c_writereg(state, i2c_addr, rd[i].reg,
  321. rd[i].data);
  322. if (rc < 0)
  323. return rc;
  324. }
  325. return 0;
  326. }
  327. static int mb86a20s_i2c_readreg(struct mb86a20s_state *state,
  328. u8 i2c_addr, u8 reg)
  329. {
  330. u8 val;
  331. int rc;
  332. struct i2c_msg msg[] = {
  333. { .addr = i2c_addr, .flags = 0, .buf = &reg, .len = 1 },
  334. { .addr = i2c_addr, .flags = I2C_M_RD, .buf = &val, .len = 1 }
  335. };
  336. rc = i2c_transfer(state->i2c, msg, 2);
  337. if (rc != 2) {
  338. rc("%s: reg=0x%x (rcor=%d)\n", __func__, reg, rc);
  339. return rc;
  340. }
  341. return val;
  342. }
  343. #define mb86a20s_readreg(state, reg) \
  344. mb86a20s_i2c_readreg(state, state->config->demod_address, reg)
  345. #define mb86a20s_writereg(state, reg, val) \
  346. mb86a20s_i2c_writereg(state, state->config->demod_address, reg, val)
  347. #define mb86a20s_writeregdata(state, regdata) \
  348. mb86a20s_i2c_writeregdata(state, state->config->demod_address, \
  349. regdata, ARRAY_SIZE(regdata))
  350. static int mb86a20s_initfe(struct dvb_frontend *fe)
  351. {
  352. struct mb86a20s_state *state = fe->demodulator_priv;
  353. int rc;
  354. u8 regD5 = 1;
  355. dprintk("\n");
  356. if (fe->ops.i2c_gate_ctrl)
  357. fe->ops.i2c_gate_ctrl(fe, 0);
  358. /* Initialize the frontend */
  359. rc = mb86a20s_writeregdata(state, mb86a20s_init);
  360. if (rc < 0)
  361. return rc;
  362. if (!state->config->is_serial) {
  363. regD5 &= ~1;
  364. rc = mb86a20s_writereg(state, 0x50, 0xd5);
  365. if (rc < 0)
  366. return rc;
  367. rc = mb86a20s_writereg(state, 0x51, regD5);
  368. if (rc < 0)
  369. return rc;
  370. }
  371. if (fe->ops.i2c_gate_ctrl)
  372. fe->ops.i2c_gate_ctrl(fe, 1);
  373. return 0;
  374. }
  375. static int mb86a20s_read_signal_strength(struct dvb_frontend *fe, u16 *strength)
  376. {
  377. struct mb86a20s_state *state = fe->demodulator_priv;
  378. unsigned rf_max, rf_min, rf;
  379. u8 val;
  380. dprintk("\n");
  381. if (fe->ops.i2c_gate_ctrl)
  382. fe->ops.i2c_gate_ctrl(fe, 0);
  383. /* Does a binary search to get RF strength */
  384. rf_max = 0xfff;
  385. rf_min = 0;
  386. do {
  387. rf = (rf_max + rf_min) / 2;
  388. mb86a20s_writereg(state, 0x04, 0x1f);
  389. mb86a20s_writereg(state, 0x05, rf >> 8);
  390. mb86a20s_writereg(state, 0x04, 0x20);
  391. mb86a20s_writereg(state, 0x04, rf);
  392. val = mb86a20s_readreg(state, 0x02);
  393. if (val & 0x08)
  394. rf_min = (rf_max + rf_min) / 2;
  395. else
  396. rf_max = (rf_max + rf_min) / 2;
  397. if (rf_max - rf_min < 4) {
  398. *strength = (((rf_max + rf_min) / 2) * 65535) / 4095;
  399. break;
  400. }
  401. } while (1);
  402. dprintk("signal strength = %d\n", *strength);
  403. if (fe->ops.i2c_gate_ctrl)
  404. fe->ops.i2c_gate_ctrl(fe, 1);
  405. return 0;
  406. }
  407. static int mb86a20s_read_status(struct dvb_frontend *fe, fe_status_t *status)
  408. {
  409. struct mb86a20s_state *state = fe->demodulator_priv;
  410. u8 val;
  411. dprintk("\n");
  412. *status = 0;
  413. if (fe->ops.i2c_gate_ctrl)
  414. fe->ops.i2c_gate_ctrl(fe, 0);
  415. val = mb86a20s_readreg(state, 0x0a) & 0xf;
  416. if (fe->ops.i2c_gate_ctrl)
  417. fe->ops.i2c_gate_ctrl(fe, 1);
  418. if (val >= 2)
  419. *status |= FE_HAS_SIGNAL;
  420. if (val >= 4)
  421. *status |= FE_HAS_CARRIER;
  422. if (val >= 5)
  423. *status |= FE_HAS_VITERBI;
  424. if (val >= 7)
  425. *status |= FE_HAS_SYNC;
  426. if (val >= 8) /* Maybe 9? */
  427. *status |= FE_HAS_LOCK;
  428. dprintk("val = %d, status = 0x%02x\n", val, *status);
  429. return 0;
  430. }
  431. static int mb86a20s_set_frontend(struct dvb_frontend *fe,
  432. struct dvb_frontend_parameters *p)
  433. {
  434. struct mb86a20s_state *state = fe->demodulator_priv;
  435. int rc;
  436. dprintk("\n");
  437. if (fe->ops.i2c_gate_ctrl)
  438. fe->ops.i2c_gate_ctrl(fe, 1);
  439. fe->ops.tuner_ops.set_params(fe, p);
  440. if (fe->ops.i2c_gate_ctrl)
  441. fe->ops.i2c_gate_ctrl(fe, 0);
  442. rc = mb86a20s_writeregdata(state, mb86a20s_reset_reception);
  443. if (fe->ops.i2c_gate_ctrl)
  444. fe->ops.i2c_gate_ctrl(fe, 1);
  445. return rc;
  446. }
  447. static int mb86a20s_get_frontend(struct dvb_frontend *fe,
  448. struct dvb_frontend_parameters *p)
  449. {
  450. /* FIXME: For now, it does nothing */
  451. fe->dtv_property_cache.bandwidth_hz = 6000000;
  452. fe->dtv_property_cache.transmission_mode = TRANSMISSION_MODE_AUTO;
  453. fe->dtv_property_cache.guard_interval = GUARD_INTERVAL_AUTO;
  454. fe->dtv_property_cache.isdbt_partial_reception = 0;
  455. return 0;
  456. }
  457. static int mb86a20s_tune(struct dvb_frontend *fe,
  458. struct dvb_frontend_parameters *params,
  459. unsigned int mode_flags,
  460. unsigned int *delay,
  461. fe_status_t *status)
  462. {
  463. int rc = 0;
  464. dprintk("\n");
  465. if (params != NULL)
  466. rc = mb86a20s_set_frontend(fe, params);
  467. if (!(mode_flags & FE_TUNE_MODE_ONESHOT))
  468. mb86a20s_read_status(fe, status);
  469. return rc;
  470. }
  471. static void mb86a20s_release(struct dvb_frontend *fe)
  472. {
  473. struct mb86a20s_state *state = fe->demodulator_priv;
  474. dprintk("\n");
  475. kfree(state);
  476. }
  477. static struct dvb_frontend_ops mb86a20s_ops;
  478. struct dvb_frontend *mb86a20s_attach(const struct mb86a20s_config *config,
  479. struct i2c_adapter *i2c)
  480. {
  481. u8 rev;
  482. /* allocate memory for the internal state */
  483. struct mb86a20s_state *state =
  484. kzalloc(sizeof(struct mb86a20s_state), GFP_KERNEL);
  485. dprintk("\n");
  486. if (state == NULL) {
  487. rc("Unable to kzalloc\n");
  488. goto error;
  489. }
  490. /* setup the state */
  491. state->config = config;
  492. state->i2c = i2c;
  493. /* create dvb_frontend */
  494. memcpy(&state->frontend.ops, &mb86a20s_ops,
  495. sizeof(struct dvb_frontend_ops));
  496. state->frontend.demodulator_priv = state;
  497. /* Check if it is a mb86a20s frontend */
  498. rev = mb86a20s_readreg(state, 0);
  499. if (rev == 0x13) {
  500. printk(KERN_INFO "Detected a Fujitsu mb86a20s frontend\n");
  501. } else {
  502. printk(KERN_ERR "Frontend revision %d is unknown - aborting.\n",
  503. rev);
  504. goto error;
  505. }
  506. return &state->frontend;
  507. error:
  508. kfree(state);
  509. return NULL;
  510. }
  511. EXPORT_SYMBOL(mb86a20s_attach);
  512. static struct dvb_frontend_ops mb86a20s_ops = {
  513. /* Use dib8000 values per default */
  514. .info = {
  515. .name = "Fujitsu mb86A20s",
  516. .type = FE_OFDM,
  517. .caps = FE_CAN_INVERSION_AUTO | FE_CAN_RECOVER |
  518. FE_CAN_FEC_1_2 | FE_CAN_FEC_2_3 | FE_CAN_FEC_3_4 |
  519. FE_CAN_FEC_5_6 | FE_CAN_FEC_7_8 | FE_CAN_FEC_AUTO |
  520. FE_CAN_QPSK | FE_CAN_QAM_16 | FE_CAN_QAM_64 |
  521. FE_CAN_TRANSMISSION_MODE_AUTO | FE_CAN_QAM_AUTO |
  522. FE_CAN_GUARD_INTERVAL_AUTO | FE_CAN_HIERARCHY_AUTO,
  523. /* Actually, those values depend on the used tuner */
  524. .frequency_min = 45000000,
  525. .frequency_max = 864000000,
  526. .frequency_stepsize = 62500,
  527. },
  528. .release = mb86a20s_release,
  529. .init = mb86a20s_initfe,
  530. .set_frontend = mb86a20s_set_frontend,
  531. .get_frontend = mb86a20s_get_frontend,
  532. .read_status = mb86a20s_read_status,
  533. .read_signal_strength = mb86a20s_read_signal_strength,
  534. .tune = mb86a20s_tune,
  535. };
  536. MODULE_DESCRIPTION("DVB Frontend module for Fujitsu mb86A20s hardware");
  537. MODULE_AUTHOR("Mauro Carvalho Chehab <mchehab@redhat.com>");
  538. MODULE_LICENSE("GPL");