mt352.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601
  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. state->config.pll_set(fe, param, buf+8);
  258. mt352_write(fe, buf, sizeof(buf));
  259. if (state->config.no_tuner) {
  260. /* start decoding */
  261. mt352_write(fe, fsm_go, 2);
  262. } else {
  263. /* start tuning */
  264. mt352_write(fe, tuner_go, 2);
  265. }
  266. return 0;
  267. }
  268. static int mt352_get_parameters(struct dvb_frontend* fe,
  269. struct dvb_frontend_parameters *param)
  270. {
  271. struct mt352_state* state = fe->demodulator_priv;
  272. u16 tps;
  273. u16 div;
  274. u8 trl;
  275. struct dvb_ofdm_parameters *op = &param->u.ofdm;
  276. static const u8 tps_fec_to_api[8] =
  277. {
  278. FEC_1_2,
  279. FEC_2_3,
  280. FEC_3_4,
  281. FEC_5_6,
  282. FEC_7_8,
  283. FEC_AUTO,
  284. FEC_AUTO,
  285. FEC_AUTO
  286. };
  287. if ( (mt352_read_register(state,0x00) & 0xC0) != 0xC0 )
  288. return -EINVAL;
  289. /* Use TPS_RECEIVED-registers, not the TPS_CURRENT-registers because
  290. * the mt352 sometimes works with the wrong parameters
  291. */
  292. tps = (mt352_read_register(state, TPS_RECEIVED_1) << 8) | mt352_read_register(state, TPS_RECEIVED_0);
  293. div = (mt352_read_register(state, CHAN_START_1) << 8) | mt352_read_register(state, CHAN_START_0);
  294. trl = mt352_read_register(state, TRL_NOMINAL_RATE_1);
  295. op->code_rate_HP = tps_fec_to_api[(tps >> 7) & 7];
  296. op->code_rate_LP = tps_fec_to_api[(tps >> 4) & 7];
  297. switch ( (tps >> 13) & 3)
  298. {
  299. case 0:
  300. op->constellation = QPSK;
  301. break;
  302. case 1:
  303. op->constellation = QAM_16;
  304. break;
  305. case 2:
  306. op->constellation = QAM_64;
  307. break;
  308. default:
  309. op->constellation = QAM_AUTO;
  310. break;
  311. }
  312. op->transmission_mode = (tps & 0x01) ? TRANSMISSION_MODE_8K : TRANSMISSION_MODE_2K;
  313. switch ( (tps >> 2) & 3)
  314. {
  315. case 0:
  316. op->guard_interval = GUARD_INTERVAL_1_32;
  317. break;
  318. case 1:
  319. op->guard_interval = GUARD_INTERVAL_1_16;
  320. break;
  321. case 2:
  322. op->guard_interval = GUARD_INTERVAL_1_8;
  323. break;
  324. case 3:
  325. op->guard_interval = GUARD_INTERVAL_1_4;
  326. break;
  327. default:
  328. op->guard_interval = GUARD_INTERVAL_AUTO;
  329. break;
  330. }
  331. switch ( (tps >> 10) & 7)
  332. {
  333. case 0:
  334. op->hierarchy_information = HIERARCHY_NONE;
  335. break;
  336. case 1:
  337. op->hierarchy_information = HIERARCHY_1;
  338. break;
  339. case 2:
  340. op->hierarchy_information = HIERARCHY_2;
  341. break;
  342. case 3:
  343. op->hierarchy_information = HIERARCHY_4;
  344. break;
  345. default:
  346. op->hierarchy_information = HIERARCHY_AUTO;
  347. break;
  348. }
  349. param->frequency = ( 500 * (div - IF_FREQUENCYx6) ) / 3 * 1000;
  350. if (trl == 0x72)
  351. op->bandwidth = BANDWIDTH_8_MHZ;
  352. else if (trl == 0x64)
  353. op->bandwidth = BANDWIDTH_7_MHZ;
  354. else
  355. op->bandwidth = BANDWIDTH_6_MHZ;
  356. if (mt352_read_register(state, STATUS_2) & 0x02)
  357. param->inversion = INVERSION_OFF;
  358. else
  359. param->inversion = INVERSION_ON;
  360. return 0;
  361. }
  362. static int mt352_read_status(struct dvb_frontend* fe, fe_status_t* status)
  363. {
  364. struct mt352_state* state = fe->demodulator_priv;
  365. int s0, s1, s3;
  366. /* FIXME:
  367. *
  368. * The MT352 design manual from Zarlink states (page 46-47):
  369. *
  370. * Notes about the TUNER_GO register:
  371. *
  372. * If the Read_Tuner_Byte (bit-1) is activated, then the tuner status
  373. * byte is copied from the tuner to the STATUS_3 register and
  374. * completion of the read operation is indicated by bit-5 of the
  375. * INTERRUPT_3 register.
  376. */
  377. if ((s0 = mt352_read_register(state, STATUS_0)) < 0)
  378. return -EREMOTEIO;
  379. if ((s1 = mt352_read_register(state, STATUS_1)) < 0)
  380. return -EREMOTEIO;
  381. if ((s3 = mt352_read_register(state, STATUS_3)) < 0)
  382. return -EREMOTEIO;
  383. *status = 0;
  384. if (s0 & (1 << 4))
  385. *status |= FE_HAS_CARRIER;
  386. if (s0 & (1 << 1))
  387. *status |= FE_HAS_VITERBI;
  388. if (s0 & (1 << 5))
  389. *status |= FE_HAS_LOCK;
  390. if (s1 & (1 << 1))
  391. *status |= FE_HAS_SYNC;
  392. if (s3 & (1 << 6))
  393. *status |= FE_HAS_SIGNAL;
  394. if ((*status & (FE_HAS_CARRIER | FE_HAS_VITERBI | FE_HAS_SYNC)) !=
  395. (FE_HAS_CARRIER | FE_HAS_VITERBI | FE_HAS_SYNC))
  396. *status &= ~FE_HAS_LOCK;
  397. return 0;
  398. }
  399. static int mt352_read_ber(struct dvb_frontend* fe, u32* ber)
  400. {
  401. struct mt352_state* state = fe->demodulator_priv;
  402. *ber = (mt352_read_register (state, RS_ERR_CNT_2) << 16) |
  403. (mt352_read_register (state, RS_ERR_CNT_1) << 8) |
  404. (mt352_read_register (state, RS_ERR_CNT_0));
  405. return 0;
  406. }
  407. static int mt352_read_signal_strength(struct dvb_frontend* fe, u16* strength)
  408. {
  409. struct mt352_state* state = fe->demodulator_priv;
  410. /* align the 12 bit AGC gain with the most significant bits */
  411. u16 signal = ((mt352_read_register(state, AGC_GAIN_1) & 0x0f) << 12) |
  412. (mt352_read_register(state, AGC_GAIN_0) << 4);
  413. /* inverse of gain is signal strength */
  414. *strength = ~signal;
  415. return 0;
  416. }
  417. static int mt352_read_snr(struct dvb_frontend* fe, u16* snr)
  418. {
  419. struct mt352_state* state = fe->demodulator_priv;
  420. u8 _snr = mt352_read_register (state, SNR);
  421. *snr = (_snr << 8) | _snr;
  422. return 0;
  423. }
  424. static int mt352_read_ucblocks(struct dvb_frontend* fe, u32* ucblocks)
  425. {
  426. struct mt352_state* state = fe->demodulator_priv;
  427. *ucblocks = (mt352_read_register (state, RS_UBC_1) << 8) |
  428. (mt352_read_register (state, RS_UBC_0));
  429. return 0;
  430. }
  431. static int mt352_get_tune_settings(struct dvb_frontend* fe, struct dvb_frontend_tune_settings* fe_tune_settings)
  432. {
  433. fe_tune_settings->min_delay_ms = 800;
  434. fe_tune_settings->step_size = 0;
  435. fe_tune_settings->max_drift = 0;
  436. return 0;
  437. }
  438. static int mt352_init(struct dvb_frontend* fe)
  439. {
  440. struct mt352_state* state = fe->demodulator_priv;
  441. static u8 mt352_reset_attach [] = { RESET, 0xC0 };
  442. dprintk("%s: hello\n",__FUNCTION__);
  443. if ((mt352_read_register(state, CLOCK_CTL) & 0x10) == 0 ||
  444. (mt352_read_register(state, CONFIG) & 0x20) == 0) {
  445. /* Do a "hard" reset */
  446. mt352_write(fe, mt352_reset_attach, sizeof(mt352_reset_attach));
  447. return state->config.demod_init(fe);
  448. }
  449. return 0;
  450. }
  451. static void mt352_release(struct dvb_frontend* fe)
  452. {
  453. struct mt352_state* state = fe->demodulator_priv;
  454. kfree(state);
  455. }
  456. static struct dvb_frontend_ops mt352_ops;
  457. struct dvb_frontend* mt352_attach(const struct mt352_config* config,
  458. struct i2c_adapter* i2c)
  459. {
  460. struct mt352_state* state = NULL;
  461. /* allocate memory for the internal state */
  462. state = kzalloc(sizeof(struct mt352_state), GFP_KERNEL);
  463. if (state == NULL) goto error;
  464. /* setup the state */
  465. state->i2c = i2c;
  466. memcpy(&state->config,config,sizeof(struct mt352_config));
  467. memcpy(&state->ops, &mt352_ops, sizeof(struct dvb_frontend_ops));
  468. /* check if the demod is there */
  469. if (mt352_read_register(state, CHIP_ID) != ID_MT352) goto error;
  470. /* create dvb_frontend */
  471. state->frontend.ops = &state->ops;
  472. state->frontend.demodulator_priv = state;
  473. return &state->frontend;
  474. error:
  475. kfree(state);
  476. return NULL;
  477. }
  478. static struct dvb_frontend_ops mt352_ops = {
  479. .info = {
  480. .name = "Zarlink MT352 DVB-T",
  481. .type = FE_OFDM,
  482. .frequency_min = 174000000,
  483. .frequency_max = 862000000,
  484. .frequency_stepsize = 166667,
  485. .frequency_tolerance = 0,
  486. .caps = FE_CAN_FEC_1_2 | FE_CAN_FEC_2_3 |
  487. FE_CAN_FEC_3_4 | FE_CAN_FEC_5_6 | FE_CAN_FEC_7_8 |
  488. FE_CAN_FEC_AUTO |
  489. FE_CAN_QPSK | FE_CAN_QAM_16 | FE_CAN_QAM_64 | FE_CAN_QAM_AUTO |
  490. FE_CAN_TRANSMISSION_MODE_AUTO | FE_CAN_GUARD_INTERVAL_AUTO |
  491. FE_CAN_HIERARCHY_AUTO | FE_CAN_RECOVER |
  492. FE_CAN_MUTE_TS
  493. },
  494. .release = mt352_release,
  495. .init = mt352_init,
  496. .sleep = mt352_sleep,
  497. .set_frontend = mt352_set_parameters,
  498. .get_frontend = mt352_get_parameters,
  499. .get_tune_settings = mt352_get_tune_settings,
  500. .read_status = mt352_read_status,
  501. .read_ber = mt352_read_ber,
  502. .read_signal_strength = mt352_read_signal_strength,
  503. .read_snr = mt352_read_snr,
  504. .read_ucblocks = mt352_read_ucblocks,
  505. };
  506. module_param(debug, int, 0644);
  507. MODULE_PARM_DESC(debug, "Turn on/off frontend debugging (default:off).");
  508. MODULE_DESCRIPTION("Zarlink MT352 DVB-T Demodulator driver");
  509. MODULE_AUTHOR("Holger Waechtler, Daniel Mack, Antonio Mancuso");
  510. MODULE_LICENSE("GPL");
  511. EXPORT_SYMBOL(mt352_attach);
  512. EXPORT_SYMBOL(mt352_write);