tda10021.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529
  1. /*
  2. TDA10021 - Single Chip Cable Channel Receiver driver module
  3. used on the Siemens DVB-C cards
  4. Copyright (C) 1999 Convergence Integrated Media GmbH <ralph@convergence.de>
  5. Copyright (C) 2004 Markus Schulz <msc@antzsystem.de>
  6. Support for TDA10021
  7. This program is free software; you can redistribute it and/or modify
  8. it under the terms of the GNU General Public License as published by
  9. the Free Software Foundation; either version 2 of the License, or
  10. (at your option) any later version.
  11. This program is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. GNU General Public License for more details.
  15. You should have received a copy of the GNU General Public License
  16. along with this program; if not, write to the Free Software
  17. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  18. */
  19. #include <linux/delay.h>
  20. #include <linux/errno.h>
  21. #include <linux/init.h>
  22. #include <linux/kernel.h>
  23. #include <linux/module.h>
  24. #include <linux/string.h>
  25. #include <linux/slab.h>
  26. #include "dvb_frontend.h"
  27. #include "tda1002x.h"
  28. struct tda10021_state {
  29. struct i2c_adapter* i2c;
  30. /* configuration settings */
  31. const struct tda1002x_config* config;
  32. struct dvb_frontend frontend;
  33. u8 pwm;
  34. u8 reg0;
  35. };
  36. #if 0
  37. #define dprintk(x...) printk(x)
  38. #else
  39. #define dprintk(x...)
  40. #endif
  41. static int verbose;
  42. #define XIN 57840000UL
  43. #define FIN (XIN >> 4)
  44. static int tda10021_inittab_size = 0x40;
  45. static u8 tda10021_inittab[0x40]=
  46. {
  47. 0x73, 0x6a, 0x23, 0x0a, 0x02, 0x37, 0x77, 0x1a,
  48. 0x37, 0x6a, 0x17, 0x8a, 0x1e, 0x86, 0x43, 0x40,
  49. 0xb8, 0x3f, 0xa1, 0x00, 0xcd, 0x01, 0x00, 0xff,
  50. 0x11, 0x00, 0x7c, 0x31, 0x30, 0x20, 0x00, 0x00,
  51. 0x02, 0x00, 0x00, 0x7d, 0x00, 0x00, 0x00, 0x00,
  52. 0x07, 0x00, 0x33, 0x11, 0x0d, 0x95, 0x08, 0x58,
  53. 0x00, 0x00, 0x80, 0x00, 0x80, 0xff, 0x00, 0x00,
  54. 0x04, 0x2d, 0x2f, 0xff, 0x00, 0x00, 0x00, 0x00,
  55. };
  56. static int _tda10021_writereg (struct tda10021_state* state, u8 reg, u8 data)
  57. {
  58. u8 buf[] = { reg, data };
  59. struct i2c_msg msg = { .addr = state->config->demod_address, .flags = 0, .buf = buf, .len = 2 };
  60. int ret;
  61. ret = i2c_transfer (state->i2c, &msg, 1);
  62. if (ret != 1)
  63. printk("DVB: TDA10021(%d): %s, writereg error "
  64. "(reg == 0x%02x, val == 0x%02x, ret == %i)\n",
  65. state->frontend.dvb->num, __func__, reg, data, ret);
  66. msleep(10);
  67. return (ret != 1) ? -EREMOTEIO : 0;
  68. }
  69. static u8 tda10021_readreg (struct tda10021_state* state, u8 reg)
  70. {
  71. u8 b0 [] = { reg };
  72. u8 b1 [] = { 0 };
  73. struct i2c_msg msg [] = { { .addr = state->config->demod_address, .flags = 0, .buf = b0, .len = 1 },
  74. { .addr = state->config->demod_address, .flags = I2C_M_RD, .buf = b1, .len = 1 } };
  75. int ret;
  76. ret = i2c_transfer (state->i2c, msg, 2);
  77. // Don't print an error message if the id is read.
  78. if (ret != 2 && reg != 0x1a)
  79. printk("DVB: TDA10021: %s: readreg error (ret == %i)\n",
  80. __func__, ret);
  81. return b1[0];
  82. }
  83. //get access to tuner
  84. static int lock_tuner(struct tda10021_state* state)
  85. {
  86. u8 buf[2] = { 0x0f, tda10021_inittab[0x0f] | 0x80 };
  87. struct i2c_msg msg = {.addr=state->config->demod_address, .flags=0, .buf=buf, .len=2};
  88. if(i2c_transfer(state->i2c, &msg, 1) != 1)
  89. {
  90. printk("tda10021: lock tuner fails\n");
  91. return -EREMOTEIO;
  92. }
  93. return 0;
  94. }
  95. //release access from tuner
  96. static int unlock_tuner(struct tda10021_state* state)
  97. {
  98. u8 buf[2] = { 0x0f, tda10021_inittab[0x0f] & 0x7f };
  99. struct i2c_msg msg_post={.addr=state->config->demod_address, .flags=0, .buf=buf, .len=2};
  100. if(i2c_transfer(state->i2c, &msg_post, 1) != 1)
  101. {
  102. printk("tda10021: unlock tuner fails\n");
  103. return -EREMOTEIO;
  104. }
  105. return 0;
  106. }
  107. static int tda10021_setup_reg0(struct tda10021_state *state, u8 reg0,
  108. enum fe_spectral_inversion inversion)
  109. {
  110. reg0 |= state->reg0 & 0x63;
  111. if ((INVERSION_ON == inversion) ^ (state->config->invert == 0))
  112. reg0 &= ~0x20;
  113. else
  114. reg0 |= 0x20;
  115. _tda10021_writereg (state, 0x00, reg0 & 0xfe);
  116. _tda10021_writereg (state, 0x00, reg0 | 0x01);
  117. state->reg0 = reg0;
  118. return 0;
  119. }
  120. static int tda10021_set_symbolrate (struct tda10021_state* state, u32 symbolrate)
  121. {
  122. s32 BDR;
  123. s32 BDRI;
  124. s16 SFIL=0;
  125. u16 NDEC = 0;
  126. u32 tmp, ratio;
  127. if (symbolrate > XIN/2)
  128. symbolrate = XIN/2;
  129. if (symbolrate < 500000)
  130. symbolrate = 500000;
  131. if (symbolrate < XIN/16) NDEC = 1;
  132. if (symbolrate < XIN/32) NDEC = 2;
  133. if (symbolrate < XIN/64) NDEC = 3;
  134. if (symbolrate < (u32)(XIN/12.3)) SFIL = 1;
  135. if (symbolrate < (u32)(XIN/16)) SFIL = 0;
  136. if (symbolrate < (u32)(XIN/24.6)) SFIL = 1;
  137. if (symbolrate < (u32)(XIN/32)) SFIL = 0;
  138. if (symbolrate < (u32)(XIN/49.2)) SFIL = 1;
  139. if (symbolrate < (u32)(XIN/64)) SFIL = 0;
  140. if (symbolrate < (u32)(XIN/98.4)) SFIL = 1;
  141. symbolrate <<= NDEC;
  142. ratio = (symbolrate << 4) / FIN;
  143. tmp = ((symbolrate << 4) % FIN) << 8;
  144. ratio = (ratio << 8) + tmp / FIN;
  145. tmp = (tmp % FIN) << 8;
  146. ratio = (ratio << 8) + DIV_ROUND_CLOSEST(tmp, FIN);
  147. BDR = ratio;
  148. BDRI = (((XIN << 5) / symbolrate) + 1) / 2;
  149. if (BDRI > 0xFF)
  150. BDRI = 0xFF;
  151. SFIL = (SFIL << 4) | tda10021_inittab[0x0E];
  152. NDEC = (NDEC << 6) | tda10021_inittab[0x03];
  153. _tda10021_writereg (state, 0x03, NDEC);
  154. _tda10021_writereg (state, 0x0a, BDR&0xff);
  155. _tda10021_writereg (state, 0x0b, (BDR>> 8)&0xff);
  156. _tda10021_writereg (state, 0x0c, (BDR>>16)&0x3f);
  157. _tda10021_writereg (state, 0x0d, BDRI);
  158. _tda10021_writereg (state, 0x0e, SFIL);
  159. return 0;
  160. }
  161. static int tda10021_init (struct dvb_frontend *fe)
  162. {
  163. struct tda10021_state* state = fe->demodulator_priv;
  164. int i;
  165. dprintk("DVB: TDA10021(%d): init chip\n", fe->adapter->num);
  166. //_tda10021_writereg (fe, 0, 0);
  167. for (i=0; i<tda10021_inittab_size; i++)
  168. _tda10021_writereg (state, i, tda10021_inittab[i]);
  169. _tda10021_writereg (state, 0x34, state->pwm);
  170. //Comment by markus
  171. //0x2A[3-0] == PDIV -> P multiplaying factor (P=PDIV+1)(default 0)
  172. //0x2A[4] == BYPPLL -> Power down mode (default 1)
  173. //0x2A[5] == LCK -> PLL Lock Flag
  174. //0x2A[6] == POLAXIN -> Polarity of the input reference clock (default 0)
  175. //Activate PLL
  176. _tda10021_writereg(state, 0x2a, tda10021_inittab[0x2a] & 0xef);
  177. return 0;
  178. }
  179. struct qam_params {
  180. u8 conf, agcref, lthr, mseth, aref;
  181. };
  182. static int tda10021_set_parameters(struct dvb_frontend *fe)
  183. {
  184. struct dtv_frontend_properties *c = &fe->dtv_property_cache;
  185. u32 delsys = c->delivery_system;
  186. unsigned qam = c->modulation;
  187. bool is_annex_c;
  188. u32 reg0x3d;
  189. struct tda10021_state* state = fe->demodulator_priv;
  190. static const struct qam_params qam_params[] = {
  191. /* Modulation Conf AGCref LTHR MSETH AREF */
  192. [QPSK] = { 0x14, 0x78, 0x78, 0x8c, 0x96 },
  193. [QAM_16] = { 0x00, 0x8c, 0x87, 0xa2, 0x91 },
  194. [QAM_32] = { 0x04, 0x8c, 0x64, 0x74, 0x96 },
  195. [QAM_64] = { 0x08, 0x6a, 0x46, 0x43, 0x6a },
  196. [QAM_128] = { 0x0c, 0x78, 0x36, 0x34, 0x7e },
  197. [QAM_256] = { 0x10, 0x5c, 0x26, 0x23, 0x6b },
  198. };
  199. switch (delsys) {
  200. case SYS_DVBC_ANNEX_A:
  201. is_annex_c = false;
  202. break;
  203. case SYS_DVBC_ANNEX_C:
  204. is_annex_c = true;
  205. break;
  206. default:
  207. return -EINVAL;
  208. }
  209. /*
  210. * gcc optimizes the code below the same way as it would code:
  211. * "if (qam > 5) return -EINVAL;"
  212. * Yet, the code is clearer, as it shows what QAM standards are
  213. * supported by the driver, and avoids the usage of magic numbers on
  214. * it.
  215. */
  216. switch (qam) {
  217. case QPSK:
  218. case QAM_16:
  219. case QAM_32:
  220. case QAM_64:
  221. case QAM_128:
  222. case QAM_256:
  223. break;
  224. default:
  225. return -EINVAL;
  226. }
  227. if (c->inversion != INVERSION_ON && c->inversion != INVERSION_OFF)
  228. return -EINVAL;
  229. /*printk("tda10021: set frequency to %d qam=%d symrate=%d\n", p->frequency,qam,p->symbol_rate);*/
  230. if (fe->ops.tuner_ops.set_params) {
  231. fe->ops.tuner_ops.set_params(fe);
  232. if (fe->ops.i2c_gate_ctrl) fe->ops.i2c_gate_ctrl(fe, 0);
  233. }
  234. tda10021_set_symbolrate(state, c->symbol_rate);
  235. _tda10021_writereg(state, 0x34, state->pwm);
  236. _tda10021_writereg(state, 0x01, qam_params[qam].agcref);
  237. _tda10021_writereg(state, 0x05, qam_params[qam].lthr);
  238. _tda10021_writereg(state, 0x08, qam_params[qam].mseth);
  239. _tda10021_writereg(state, 0x09, qam_params[qam].aref);
  240. /*
  241. * Bit 0 == 0 means roll-off = 0.15 (Annex A)
  242. * == 1 means roll-off = 0.13 (Annex C)
  243. */
  244. reg0x3d = tda10021_readreg (state, 0x3d);
  245. if (is_annex_c)
  246. _tda10021_writereg (state, 0x3d, 0x01 | reg0x3d);
  247. else
  248. _tda10021_writereg (state, 0x3d, 0xfe & reg0x3d);
  249. tda10021_setup_reg0(state, qam_params[qam].conf, c->inversion);
  250. return 0;
  251. }
  252. static int tda10021_read_status(struct dvb_frontend *fe,
  253. enum fe_status *status)
  254. {
  255. struct tda10021_state* state = fe->demodulator_priv;
  256. int sync;
  257. *status = 0;
  258. //0x11[0] == EQALGO -> Equalizer algorithms state
  259. //0x11[1] == CARLOCK -> Carrier locked
  260. //0x11[2] == FSYNC -> Frame synchronisation
  261. //0x11[3] == FEL -> Front End locked
  262. //0x11[6] == NODVB -> DVB Mode Information
  263. sync = tda10021_readreg (state, 0x11);
  264. if (sync & 2)
  265. *status |= FE_HAS_SIGNAL|FE_HAS_CARRIER;
  266. if (sync & 4)
  267. *status |= FE_HAS_SYNC|FE_HAS_VITERBI;
  268. if (sync & 8)
  269. *status |= FE_HAS_LOCK;
  270. return 0;
  271. }
  272. static int tda10021_read_ber(struct dvb_frontend* fe, u32* ber)
  273. {
  274. struct tda10021_state* state = fe->demodulator_priv;
  275. u32 _ber = tda10021_readreg(state, 0x14) |
  276. (tda10021_readreg(state, 0x15) << 8) |
  277. ((tda10021_readreg(state, 0x16) & 0x0f) << 16);
  278. _tda10021_writereg(state, 0x10, (tda10021_readreg(state, 0x10) & ~0xc0)
  279. | (tda10021_inittab[0x10] & 0xc0));
  280. *ber = 10 * _ber;
  281. return 0;
  282. }
  283. static int tda10021_read_signal_strength(struct dvb_frontend* fe, u16* strength)
  284. {
  285. struct tda10021_state* state = fe->demodulator_priv;
  286. u8 config = tda10021_readreg(state, 0x02);
  287. u8 gain = tda10021_readreg(state, 0x17);
  288. if (config & 0x02)
  289. /* the agc value is inverted */
  290. gain = ~gain;
  291. *strength = (gain << 8) | gain;
  292. return 0;
  293. }
  294. static int tda10021_read_snr(struct dvb_frontend* fe, u16* snr)
  295. {
  296. struct tda10021_state* state = fe->demodulator_priv;
  297. u8 quality = ~tda10021_readreg(state, 0x18);
  298. *snr = (quality << 8) | quality;
  299. return 0;
  300. }
  301. static int tda10021_read_ucblocks(struct dvb_frontend* fe, u32* ucblocks)
  302. {
  303. struct tda10021_state* state = fe->demodulator_priv;
  304. *ucblocks = tda10021_readreg (state, 0x13) & 0x7f;
  305. if (*ucblocks == 0x7f)
  306. *ucblocks = 0xffffffff;
  307. /* reset uncorrected block counter */
  308. _tda10021_writereg (state, 0x10, tda10021_inittab[0x10] & 0xdf);
  309. _tda10021_writereg (state, 0x10, tda10021_inittab[0x10]);
  310. return 0;
  311. }
  312. static int tda10021_get_frontend(struct dvb_frontend *fe)
  313. {
  314. struct dtv_frontend_properties *p = &fe->dtv_property_cache;
  315. struct tda10021_state* state = fe->demodulator_priv;
  316. int sync;
  317. s8 afc = 0;
  318. sync = tda10021_readreg(state, 0x11);
  319. afc = tda10021_readreg(state, 0x19);
  320. if (verbose) {
  321. /* AFC only valid when carrier has been recovered */
  322. printk(sync & 2 ? "DVB: TDA10021(%d): AFC (%d) %dHz\n" :
  323. "DVB: TDA10021(%d): [AFC (%d) %dHz]\n",
  324. state->frontend.dvb->num, afc,
  325. -((s32)p->symbol_rate * afc) >> 10);
  326. }
  327. p->inversion = ((state->reg0 & 0x20) == 0x20) ^ (state->config->invert != 0) ? INVERSION_ON : INVERSION_OFF;
  328. p->modulation = ((state->reg0 >> 2) & 7) + QAM_16;
  329. p->fec_inner = FEC_NONE;
  330. p->frequency = ((p->frequency + 31250) / 62500) * 62500;
  331. if (sync & 2)
  332. p->frequency -= ((s32)p->symbol_rate * afc) >> 10;
  333. return 0;
  334. }
  335. static int tda10021_i2c_gate_ctrl(struct dvb_frontend* fe, int enable)
  336. {
  337. struct tda10021_state* state = fe->demodulator_priv;
  338. if (enable) {
  339. lock_tuner(state);
  340. } else {
  341. unlock_tuner(state);
  342. }
  343. return 0;
  344. }
  345. static int tda10021_sleep(struct dvb_frontend* fe)
  346. {
  347. struct tda10021_state* state = fe->demodulator_priv;
  348. _tda10021_writereg (state, 0x1b, 0x02); /* pdown ADC */
  349. _tda10021_writereg (state, 0x00, 0x80); /* standby */
  350. return 0;
  351. }
  352. static void tda10021_release(struct dvb_frontend* fe)
  353. {
  354. struct tda10021_state* state = fe->demodulator_priv;
  355. kfree(state);
  356. }
  357. static struct dvb_frontend_ops tda10021_ops;
  358. struct dvb_frontend* tda10021_attach(const struct tda1002x_config* config,
  359. struct i2c_adapter* i2c,
  360. u8 pwm)
  361. {
  362. struct tda10021_state* state = NULL;
  363. u8 id;
  364. /* allocate memory for the internal state */
  365. state = kzalloc(sizeof(struct tda10021_state), GFP_KERNEL);
  366. if (state == NULL) goto error;
  367. /* setup the state */
  368. state->config = config;
  369. state->i2c = i2c;
  370. state->pwm = pwm;
  371. state->reg0 = tda10021_inittab[0];
  372. /* check if the demod is there */
  373. id = tda10021_readreg(state, 0x1a);
  374. if ((id & 0xf0) != 0x70) goto error;
  375. /* Don't claim TDA10023 */
  376. if (id == 0x7d)
  377. goto error;
  378. printk("TDA10021: i2c-addr = 0x%02x, id = 0x%02x\n",
  379. state->config->demod_address, id);
  380. /* create dvb_frontend */
  381. memcpy(&state->frontend.ops, &tda10021_ops, sizeof(struct dvb_frontend_ops));
  382. state->frontend.demodulator_priv = state;
  383. return &state->frontend;
  384. error:
  385. kfree(state);
  386. return NULL;
  387. }
  388. static struct dvb_frontend_ops tda10021_ops = {
  389. .delsys = { SYS_DVBC_ANNEX_A, SYS_DVBC_ANNEX_C },
  390. .info = {
  391. .name = "Philips TDA10021 DVB-C",
  392. .frequency_stepsize = 62500,
  393. .frequency_min = 47000000,
  394. .frequency_max = 862000000,
  395. .symbol_rate_min = (XIN/2)/64, /* SACLK/64 == (XIN/2)/64 */
  396. .symbol_rate_max = (XIN/2)/4, /* SACLK/4 */
  397. #if 0
  398. .frequency_tolerance = ???,
  399. .symbol_rate_tolerance = ???, /* ppm */ /* == 8% (spec p. 5) */
  400. #endif
  401. .caps = 0x400 | //FE_CAN_QAM_4
  402. FE_CAN_QAM_16 | FE_CAN_QAM_32 | FE_CAN_QAM_64 |
  403. FE_CAN_QAM_128 | FE_CAN_QAM_256 |
  404. FE_CAN_FEC_AUTO
  405. },
  406. .release = tda10021_release,
  407. .init = tda10021_init,
  408. .sleep = tda10021_sleep,
  409. .i2c_gate_ctrl = tda10021_i2c_gate_ctrl,
  410. .set_frontend = tda10021_set_parameters,
  411. .get_frontend = tda10021_get_frontend,
  412. .read_status = tda10021_read_status,
  413. .read_ber = tda10021_read_ber,
  414. .read_signal_strength = tda10021_read_signal_strength,
  415. .read_snr = tda10021_read_snr,
  416. .read_ucblocks = tda10021_read_ucblocks,
  417. };
  418. module_param(verbose, int, 0644);
  419. MODULE_PARM_DESC(verbose, "print AFC offset after tuning for debugging the PWM setting");
  420. MODULE_DESCRIPTION("Philips TDA10021 DVB-C demodulator driver");
  421. MODULE_AUTHOR("Ralph Metzler, Holger Waechtler, Markus Schulz");
  422. MODULE_LICENSE("GPL");
  423. EXPORT_SYMBOL(tda10021_attach);