mt352.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618
  1. /*
  2. * Driver for Zarlink DVB-T MT352 demodulator
  3. *
  4. * Written by Holger Waechtler <holger@qanu.de>
  5. * and Daniel Mack <daniel@qanu.de>
  6. *
  7. * AVerMedia AVerTV DVB-T 771 support by
  8. * Wolfram Joost <dbox2@frokaschwei.de>
  9. *
  10. * Support for Samsung TDTC9251DH01C(M) tuner
  11. * Copyright (C) 2004 Antonio Mancuso <antonio.mancuso@digitaltelevision.it>
  12. * Amauri Celani <acelani@essegi.net>
  13. *
  14. * DVICO FusionHDTV DVB-T1 and DVICO FusionHDTV DVB-T Lite support by
  15. * Christopher Pascoe <c.pascoe@itee.uq.edu.au>
  16. *
  17. * This program is free software; you can redistribute it and/or modify
  18. * it under the terms of the GNU General Public License as published by
  19. * the Free Software Foundation; either version 2 of the License, or
  20. * (at your option) any later version.
  21. *
  22. * This program is distributed in the hope that it will be useful,
  23. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  24. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  25. *
  26. * GNU General Public License for more details.
  27. *
  28. * You should have received a copy of the GNU General Public License
  29. * along with this program; if not, write to the Free Software
  30. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.=
  31. */
  32. #include <linux/kernel.h>
  33. #include <linux/module.h>
  34. #include <linux/moduleparam.h>
  35. #include <linux/init.h>
  36. #include <linux/delay.h>
  37. #include <linux/string.h>
  38. #include <linux/slab.h>
  39. #include "dvb_frontend.h"
  40. #include "mt352_priv.h"
  41. #include "mt352.h"
  42. struct mt352_state {
  43. struct i2c_adapter* i2c;
  44. struct dvb_frontend frontend;
  45. struct dvb_frontend_ops ops;
  46. /* configuration settings */
  47. struct mt352_config config;
  48. };
  49. static int debug;
  50. #define dprintk(args...) \
  51. do { \
  52. if (debug) printk(KERN_DEBUG "mt352: " args); \
  53. } while (0)
  54. static int mt352_single_write(struct dvb_frontend *fe, u8 reg, u8 val)
  55. {
  56. struct mt352_state* state = fe->demodulator_priv;
  57. u8 buf[2] = { reg, val };
  58. struct i2c_msg msg = { .addr = state->config.demod_address, .flags = 0,
  59. .buf = buf, .len = 2 };
  60. int err = i2c_transfer(state->i2c, &msg, 1);
  61. if (err != 1) {
  62. printk("mt352_write() to reg %x failed (err = %d)!\n", reg, err);
  63. return err;
  64. }
  65. return 0;
  66. }
  67. int mt352_write(struct dvb_frontend* fe, u8* ibuf, int ilen)
  68. {
  69. int err,i;
  70. for (i=0; i < ilen-1; i++)
  71. if ((err = mt352_single_write(fe,ibuf[0]+i,ibuf[i+1])))
  72. return err;
  73. return 0;
  74. }
  75. static int mt352_read_register(struct mt352_state* state, u8 reg)
  76. {
  77. int ret;
  78. u8 b0 [] = { reg };
  79. u8 b1 [] = { 0 };
  80. struct i2c_msg msg [] = { { .addr = state->config.demod_address,
  81. .flags = 0,
  82. .buf = b0, .len = 1 },
  83. { .addr = state->config.demod_address,
  84. .flags = I2C_M_RD,
  85. .buf = b1, .len = 1 } };
  86. ret = i2c_transfer(state->i2c, msg, 2);
  87. if (ret != 2) {
  88. printk("%s: readreg error (reg=%d, ret==%i)\n",
  89. __FUNCTION__, reg, ret);
  90. return ret;
  91. }
  92. return b1[0];
  93. }
  94. static int mt352_sleep(struct dvb_frontend* fe)
  95. {
  96. static u8 mt352_softdown[] = { CLOCK_CTL, 0x20, 0x08 };
  97. mt352_write(fe, mt352_softdown, sizeof(mt352_softdown));
  98. return 0;
  99. }
  100. static void mt352_calc_nominal_rate(struct mt352_state* state,
  101. enum fe_bandwidth bandwidth,
  102. unsigned char *buf)
  103. {
  104. u32 adc_clock = 20480; /* 20.340 MHz */
  105. u32 bw,value;
  106. switch (bandwidth) {
  107. case BANDWIDTH_6_MHZ:
  108. bw = 6;
  109. break;
  110. case BANDWIDTH_7_MHZ:
  111. bw = 7;
  112. break;
  113. case BANDWIDTH_8_MHZ:
  114. default:
  115. bw = 8;
  116. break;
  117. }
  118. if (state->config.adc_clock)
  119. adc_clock = state->config.adc_clock;
  120. value = 64 * bw * (1<<16) / (7 * 8);
  121. value = value * 1000 / adc_clock;
  122. dprintk("%s: bw %d, adc_clock %d => 0x%x\n",
  123. __FUNCTION__, bw, adc_clock, value);
  124. buf[0] = msb(value);
  125. buf[1] = lsb(value);
  126. }
  127. static void mt352_calc_input_freq(struct mt352_state* state,
  128. unsigned char *buf)
  129. {
  130. int adc_clock = 20480; /* 20.480000 MHz */
  131. int if2 = 36167; /* 36.166667 MHz */
  132. int ife,value;
  133. if (state->config.adc_clock)
  134. adc_clock = state->config.adc_clock;
  135. if (state->config.if2)
  136. if2 = state->config.if2;
  137. ife = (2*adc_clock - if2);
  138. value = -16374 * ife / adc_clock;
  139. dprintk("%s: if2 %d, ife %d, adc_clock %d => %d / 0x%x\n",
  140. __FUNCTION__, if2, ife, adc_clock, value, value & 0x3fff);
  141. buf[0] = msb(value);
  142. buf[1] = lsb(value);
  143. }
  144. static int mt352_set_parameters(struct dvb_frontend* fe,
  145. struct dvb_frontend_parameters *param)
  146. {
  147. struct mt352_state* state = fe->demodulator_priv;
  148. unsigned char buf[13];
  149. static unsigned char tuner_go[] = { 0x5d, 0x01 };
  150. static unsigned char fsm_go[] = { 0x5e, 0x01 };
  151. unsigned int tps = 0;
  152. struct dvb_ofdm_parameters *op = &param->u.ofdm;
  153. switch (op->code_rate_HP) {
  154. case FEC_2_3:
  155. tps |= (1 << 7);
  156. break;
  157. case FEC_3_4:
  158. tps |= (2 << 7);
  159. break;
  160. case FEC_5_6:
  161. tps |= (3 << 7);
  162. break;
  163. case FEC_7_8:
  164. tps |= (4 << 7);
  165. break;
  166. case FEC_1_2:
  167. case FEC_AUTO:
  168. break;
  169. default:
  170. return -EINVAL;
  171. }
  172. switch (op->code_rate_LP) {
  173. case FEC_2_3:
  174. tps |= (1 << 4);
  175. break;
  176. case FEC_3_4:
  177. tps |= (2 << 4);
  178. break;
  179. case FEC_5_6:
  180. tps |= (3 << 4);
  181. break;
  182. case FEC_7_8:
  183. tps |= (4 << 4);
  184. break;
  185. case FEC_1_2:
  186. case FEC_AUTO:
  187. break;
  188. case FEC_NONE:
  189. if (op->hierarchy_information == HIERARCHY_AUTO ||
  190. op->hierarchy_information == HIERARCHY_NONE)
  191. break;
  192. default:
  193. return -EINVAL;
  194. }
  195. switch (op->constellation) {
  196. case QPSK:
  197. break;
  198. case QAM_AUTO:
  199. case QAM_16:
  200. tps |= (1 << 13);
  201. break;
  202. case QAM_64:
  203. tps |= (2 << 13);
  204. break;
  205. default:
  206. return -EINVAL;
  207. }
  208. switch (op->transmission_mode) {
  209. case TRANSMISSION_MODE_2K:
  210. case TRANSMISSION_MODE_AUTO:
  211. break;
  212. case TRANSMISSION_MODE_8K:
  213. tps |= (1 << 0);
  214. break;
  215. default:
  216. return -EINVAL;
  217. }
  218. switch (op->guard_interval) {
  219. case GUARD_INTERVAL_1_32:
  220. case GUARD_INTERVAL_AUTO:
  221. break;
  222. case GUARD_INTERVAL_1_16:
  223. tps |= (1 << 2);
  224. break;
  225. case GUARD_INTERVAL_1_8:
  226. tps |= (2 << 2);
  227. break;
  228. case GUARD_INTERVAL_1_4:
  229. tps |= (3 << 2);
  230. break;
  231. default:
  232. return -EINVAL;
  233. }
  234. switch (op->hierarchy_information) {
  235. case HIERARCHY_AUTO:
  236. case HIERARCHY_NONE:
  237. break;
  238. case HIERARCHY_1:
  239. tps |= (1 << 10);
  240. break;
  241. case HIERARCHY_2:
  242. tps |= (2 << 10);
  243. break;
  244. case HIERARCHY_4:
  245. tps |= (3 << 10);
  246. break;
  247. default:
  248. return -EINVAL;
  249. }
  250. buf[0] = TPS_GIVEN_1; /* TPS_GIVEN_1 and following registers */
  251. buf[1] = msb(tps); /* TPS_GIVEN_(1|0) */
  252. buf[2] = lsb(tps);
  253. buf[3] = 0x50; // old
  254. // buf[3] = 0xf4; // pinnacle
  255. mt352_calc_nominal_rate(state, op->bandwidth, buf+4);
  256. mt352_calc_input_freq(state, buf+6);
  257. // if there is no secondary tuner, call set_params to set up a potential
  258. // tuner attached elsewhere
  259. if (state->config.no_tuner) {
  260. if (fe->ops->tuner_ops.set_params) {
  261. fe->ops->tuner_ops.set_params(fe, param);
  262. if (fe->ops->i2c_gate_ctrl) fe->ops->i2c_gate_ctrl(fe, 0);
  263. }
  264. /* start decoding only */
  265. mt352_write(fe, fsm_go, 2);
  266. }
  267. // retrieve the pllbuf - we do this even if there is no
  268. // secondary tuner simply so we have a record of what was sent for
  269. // debugging.
  270. if (fe->ops->tuner_ops.pllbuf) {
  271. fe->ops->tuner_ops.pllbuf(fe, param, buf+8, 5);
  272. buf[8] <<= 1;
  273. mt352_write(fe, buf, sizeof(buf));
  274. }
  275. // send PLL and start tuning and then decoding
  276. if (!state->config.no_tuner) {
  277. mt352_write(fe, tuner_go, 2);
  278. }
  279. return 0;
  280. }
  281. static int mt352_get_parameters(struct dvb_frontend* fe,
  282. struct dvb_frontend_parameters *param)
  283. {
  284. struct mt352_state* state = fe->demodulator_priv;
  285. u16 tps;
  286. u16 div;
  287. u8 trl;
  288. struct dvb_ofdm_parameters *op = &param->u.ofdm;
  289. static const u8 tps_fec_to_api[8] =
  290. {
  291. FEC_1_2,
  292. FEC_2_3,
  293. FEC_3_4,
  294. FEC_5_6,
  295. FEC_7_8,
  296. FEC_AUTO,
  297. FEC_AUTO,
  298. FEC_AUTO
  299. };
  300. if ( (mt352_read_register(state,0x00) & 0xC0) != 0xC0 )
  301. return -EINVAL;
  302. /* Use TPS_RECEIVED-registers, not the TPS_CURRENT-registers because
  303. * the mt352 sometimes works with the wrong parameters
  304. */
  305. tps = (mt352_read_register(state, TPS_RECEIVED_1) << 8) | mt352_read_register(state, TPS_RECEIVED_0);
  306. div = (mt352_read_register(state, CHAN_START_1) << 8) | mt352_read_register(state, CHAN_START_0);
  307. trl = mt352_read_register(state, TRL_NOMINAL_RATE_1);
  308. op->code_rate_HP = tps_fec_to_api[(tps >> 7) & 7];
  309. op->code_rate_LP = tps_fec_to_api[(tps >> 4) & 7];
  310. switch ( (tps >> 13) & 3)
  311. {
  312. case 0:
  313. op->constellation = QPSK;
  314. break;
  315. case 1:
  316. op->constellation = QAM_16;
  317. break;
  318. case 2:
  319. op->constellation = QAM_64;
  320. break;
  321. default:
  322. op->constellation = QAM_AUTO;
  323. break;
  324. }
  325. op->transmission_mode = (tps & 0x01) ? TRANSMISSION_MODE_8K : TRANSMISSION_MODE_2K;
  326. switch ( (tps >> 2) & 3)
  327. {
  328. case 0:
  329. op->guard_interval = GUARD_INTERVAL_1_32;
  330. break;
  331. case 1:
  332. op->guard_interval = GUARD_INTERVAL_1_16;
  333. break;
  334. case 2:
  335. op->guard_interval = GUARD_INTERVAL_1_8;
  336. break;
  337. case 3:
  338. op->guard_interval = GUARD_INTERVAL_1_4;
  339. break;
  340. default:
  341. op->guard_interval = GUARD_INTERVAL_AUTO;
  342. break;
  343. }
  344. switch ( (tps >> 10) & 7)
  345. {
  346. case 0:
  347. op->hierarchy_information = HIERARCHY_NONE;
  348. break;
  349. case 1:
  350. op->hierarchy_information = HIERARCHY_1;
  351. break;
  352. case 2:
  353. op->hierarchy_information = HIERARCHY_2;
  354. break;
  355. case 3:
  356. op->hierarchy_information = HIERARCHY_4;
  357. break;
  358. default:
  359. op->hierarchy_information = HIERARCHY_AUTO;
  360. break;
  361. }
  362. param->frequency = ( 500 * (div - IF_FREQUENCYx6) ) / 3 * 1000;
  363. if (trl == 0x72)
  364. op->bandwidth = BANDWIDTH_8_MHZ;
  365. else if (trl == 0x64)
  366. op->bandwidth = BANDWIDTH_7_MHZ;
  367. else
  368. op->bandwidth = BANDWIDTH_6_MHZ;
  369. if (mt352_read_register(state, STATUS_2) & 0x02)
  370. param->inversion = INVERSION_OFF;
  371. else
  372. param->inversion = INVERSION_ON;
  373. return 0;
  374. }
  375. static int mt352_read_status(struct dvb_frontend* fe, fe_status_t* status)
  376. {
  377. struct mt352_state* state = fe->demodulator_priv;
  378. int s0, s1, s3;
  379. /* FIXME:
  380. *
  381. * The MT352 design manual from Zarlink states (page 46-47):
  382. *
  383. * Notes about the TUNER_GO register:
  384. *
  385. * If the Read_Tuner_Byte (bit-1) is activated, then the tuner status
  386. * byte is copied from the tuner to the STATUS_3 register and
  387. * completion of the read operation is indicated by bit-5 of the
  388. * INTERRUPT_3 register.
  389. */
  390. if ((s0 = mt352_read_register(state, STATUS_0)) < 0)
  391. return -EREMOTEIO;
  392. if ((s1 = mt352_read_register(state, STATUS_1)) < 0)
  393. return -EREMOTEIO;
  394. if ((s3 = mt352_read_register(state, STATUS_3)) < 0)
  395. return -EREMOTEIO;
  396. *status = 0;
  397. if (s0 & (1 << 4))
  398. *status |= FE_HAS_CARRIER;
  399. if (s0 & (1 << 1))
  400. *status |= FE_HAS_VITERBI;
  401. if (s0 & (1 << 5))
  402. *status |= FE_HAS_LOCK;
  403. if (s1 & (1 << 1))
  404. *status |= FE_HAS_SYNC;
  405. if (s3 & (1 << 6))
  406. *status |= FE_HAS_SIGNAL;
  407. if ((*status & (FE_HAS_CARRIER | FE_HAS_VITERBI | FE_HAS_SYNC)) !=
  408. (FE_HAS_CARRIER | FE_HAS_VITERBI | FE_HAS_SYNC))
  409. *status &= ~FE_HAS_LOCK;
  410. return 0;
  411. }
  412. static int mt352_read_ber(struct dvb_frontend* fe, u32* ber)
  413. {
  414. struct mt352_state* state = fe->demodulator_priv;
  415. *ber = (mt352_read_register (state, RS_ERR_CNT_2) << 16) |
  416. (mt352_read_register (state, RS_ERR_CNT_1) << 8) |
  417. (mt352_read_register (state, RS_ERR_CNT_0));
  418. return 0;
  419. }
  420. static int mt352_read_signal_strength(struct dvb_frontend* fe, u16* strength)
  421. {
  422. struct mt352_state* state = fe->demodulator_priv;
  423. /* align the 12 bit AGC gain with the most significant bits */
  424. u16 signal = ((mt352_read_register(state, AGC_GAIN_1) & 0x0f) << 12) |
  425. (mt352_read_register(state, AGC_GAIN_0) << 4);
  426. /* inverse of gain is signal strength */
  427. *strength = ~signal;
  428. return 0;
  429. }
  430. static int mt352_read_snr(struct dvb_frontend* fe, u16* snr)
  431. {
  432. struct mt352_state* state = fe->demodulator_priv;
  433. u8 _snr = mt352_read_register (state, SNR);
  434. *snr = (_snr << 8) | _snr;
  435. return 0;
  436. }
  437. static int mt352_read_ucblocks(struct dvb_frontend* fe, u32* ucblocks)
  438. {
  439. struct mt352_state* state = fe->demodulator_priv;
  440. *ucblocks = (mt352_read_register (state, RS_UBC_1) << 8) |
  441. (mt352_read_register (state, RS_UBC_0));
  442. return 0;
  443. }
  444. static int mt352_get_tune_settings(struct dvb_frontend* fe, struct dvb_frontend_tune_settings* fe_tune_settings)
  445. {
  446. fe_tune_settings->min_delay_ms = 800;
  447. fe_tune_settings->step_size = 0;
  448. fe_tune_settings->max_drift = 0;
  449. return 0;
  450. }
  451. static int mt352_init(struct dvb_frontend* fe)
  452. {
  453. struct mt352_state* state = fe->demodulator_priv;
  454. static u8 mt352_reset_attach [] = { RESET, 0xC0 };
  455. dprintk("%s: hello\n",__FUNCTION__);
  456. if ((mt352_read_register(state, CLOCK_CTL) & 0x10) == 0 ||
  457. (mt352_read_register(state, CONFIG) & 0x20) == 0) {
  458. /* Do a "hard" reset */
  459. mt352_write(fe, mt352_reset_attach, sizeof(mt352_reset_attach));
  460. return state->config.demod_init(fe);
  461. }
  462. return 0;
  463. }
  464. static void mt352_release(struct dvb_frontend* fe)
  465. {
  466. struct mt352_state* state = fe->demodulator_priv;
  467. kfree(state);
  468. }
  469. static struct dvb_frontend_ops mt352_ops;
  470. struct dvb_frontend* mt352_attach(const struct mt352_config* config,
  471. struct i2c_adapter* i2c)
  472. {
  473. struct mt352_state* state = NULL;
  474. /* allocate memory for the internal state */
  475. state = kzalloc(sizeof(struct mt352_state), GFP_KERNEL);
  476. if (state == NULL) goto error;
  477. /* setup the state */
  478. state->i2c = i2c;
  479. memcpy(&state->config,config,sizeof(struct mt352_config));
  480. memcpy(&state->ops, &mt352_ops, sizeof(struct dvb_frontend_ops));
  481. /* check if the demod is there */
  482. if (mt352_read_register(state, CHIP_ID) != ID_MT352) goto error;
  483. /* create dvb_frontend */
  484. state->frontend.ops = &state->ops;
  485. state->frontend.demodulator_priv = state;
  486. return &state->frontend;
  487. error:
  488. kfree(state);
  489. return NULL;
  490. }
  491. static struct dvb_frontend_ops mt352_ops = {
  492. .info = {
  493. .name = "Zarlink MT352 DVB-T",
  494. .type = FE_OFDM,
  495. .frequency_min = 174000000,
  496. .frequency_max = 862000000,
  497. .frequency_stepsize = 166667,
  498. .frequency_tolerance = 0,
  499. .caps = FE_CAN_FEC_1_2 | FE_CAN_FEC_2_3 |
  500. FE_CAN_FEC_3_4 | FE_CAN_FEC_5_6 | FE_CAN_FEC_7_8 |
  501. FE_CAN_FEC_AUTO |
  502. FE_CAN_QPSK | FE_CAN_QAM_16 | FE_CAN_QAM_64 | FE_CAN_QAM_AUTO |
  503. FE_CAN_TRANSMISSION_MODE_AUTO | FE_CAN_GUARD_INTERVAL_AUTO |
  504. FE_CAN_HIERARCHY_AUTO | FE_CAN_RECOVER |
  505. FE_CAN_MUTE_TS
  506. },
  507. .release = mt352_release,
  508. .init = mt352_init,
  509. .sleep = mt352_sleep,
  510. .set_frontend = mt352_set_parameters,
  511. .get_frontend = mt352_get_parameters,
  512. .get_tune_settings = mt352_get_tune_settings,
  513. .read_status = mt352_read_status,
  514. .read_ber = mt352_read_ber,
  515. .read_signal_strength = mt352_read_signal_strength,
  516. .read_snr = mt352_read_snr,
  517. .read_ucblocks = mt352_read_ucblocks,
  518. };
  519. module_param(debug, int, 0644);
  520. MODULE_PARM_DESC(debug, "Turn on/off frontend debugging (default:off).");
  521. MODULE_DESCRIPTION("Zarlink MT352 DVB-T Demodulator driver");
  522. MODULE_AUTHOR("Holger Waechtler, Daniel Mack, Antonio Mancuso");
  523. MODULE_LICENSE("GPL");
  524. EXPORT_SYMBOL(mt352_attach);
  525. EXPORT_SYMBOL(mt352_write);