mt352.c 14 KB

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