mt352.c 14 KB

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