mt352.c 14 KB

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