tda10021.c 13 KB

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