mt352.c 14 KB

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