tda10021.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526
  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 <[email protected]>
  6. Copyright (C) 2004 Markus Schulz <[email protected]>
  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. else if (symbolrate < 500000)
  119. symbolrate = 500000;
  120. if (symbolrate < XIN / 16)
  121. NDEC = 1;
  122. if (symbolrate < XIN / 32)
  123. NDEC = 2;
  124. if (symbolrate < XIN / 64)
  125. NDEC = 3;
  126. if (symbolrate < XIN * 10 / 123)
  127. SFIL = 1;
  128. if (symbolrate < XIN * 10 / 160)
  129. SFIL = 0;
  130. if (symbolrate < XIN * 10 / 246)
  131. SFIL = 1;
  132. if (symbolrate < XIN * 10 / 320)
  133. SFIL = 0;
  134. if (symbolrate < XIN * 10 / 492)
  135. SFIL = 1;
  136. if (symbolrate < XIN * 10 / 640)
  137. SFIL = 0;
  138. if (symbolrate < XIN * 10 / 984)
  139. SFIL = 1;
  140. symbolrate <<= NDEC;
  141. ratio = (symbolrate << 4) / FIN;
  142. tmp = ((symbolrate << 4) % FIN) << 8;
  143. ratio = (ratio << 8) + tmp / FIN;
  144. tmp = (tmp % FIN) << 8;
  145. ratio = (ratio << 8) + DIV_ROUND_CLOSEST(tmp, FIN);
  146. BDR = ratio;
  147. BDRI = (((XIN << 5) / symbolrate) + 1) / 2;
  148. if (BDRI > 0xFF)
  149. BDRI = 0xFF;
  150. SFIL = (SFIL << 4) | tda10021_inittab[0x0E];
  151. NDEC = (NDEC << 6) | tda10021_inittab[0x03];
  152. _tda10021_writereg (state, 0x03, NDEC);
  153. _tda10021_writereg (state, 0x0a, BDR&0xff);
  154. _tda10021_writereg (state, 0x0b, (BDR>> 8)&0xff);
  155. _tda10021_writereg (state, 0x0c, (BDR>>16)&0x3f);
  156. _tda10021_writereg (state, 0x0d, BDRI);
  157. _tda10021_writereg (state, 0x0e, SFIL);
  158. return 0;
  159. }
  160. static int tda10021_init (struct dvb_frontend *fe)
  161. {
  162. struct tda10021_state* state = fe->demodulator_priv;
  163. int i;
  164. dprintk("DVB: TDA10021(%d): init chip\n", fe->adapter->num);
  165. //_tda10021_writereg (fe, 0, 0);
  166. for (i=0; i<tda10021_inittab_size; i++)
  167. _tda10021_writereg (state, i, tda10021_inittab[i]);
  168. _tda10021_writereg (state, 0x34, state->pwm);
  169. //Comment by markus
  170. //0x2A[3-0] == PDIV -> P multiplaying factor (P=PDIV+1)(default 0)
  171. //0x2A[4] == BYPPLL -> Power down mode (default 1)
  172. //0x2A[5] == LCK -> PLL Lock Flag
  173. //0x2A[6] == POLAXIN -> Polarity of the input reference clock (default 0)
  174. //Activate PLL
  175. _tda10021_writereg(state, 0x2a, tda10021_inittab[0x2a] & 0xef);
  176. return 0;
  177. }
  178. struct qam_params {
  179. u8 conf, agcref, lthr, mseth, aref;
  180. };
  181. static int tda10021_set_parameters(struct dvb_frontend *fe)
  182. {
  183. struct dtv_frontend_properties *c = &fe->dtv_property_cache;
  184. u32 delsys = c->delivery_system;
  185. unsigned qam = c->modulation;
  186. bool is_annex_c;
  187. u32 reg0x3d;
  188. struct tda10021_state* state = fe->demodulator_priv;
  189. static const struct qam_params qam_params[] = {
  190. /* Modulation Conf AGCref LTHR MSETH AREF */
  191. [QPSK] = { 0x14, 0x78, 0x78, 0x8c, 0x96 },
  192. [QAM_16] = { 0x00, 0x8c, 0x87, 0xa2, 0x91 },
  193. [QAM_32] = { 0x04, 0x8c, 0x64, 0x74, 0x96 },
  194. [QAM_64] = { 0x08, 0x6a, 0x46, 0x43, 0x6a },
  195. [QAM_128] = { 0x0c, 0x78, 0x36, 0x34, 0x7e },
  196. [QAM_256] = { 0x10, 0x5c, 0x26, 0x23, 0x6b },
  197. };
  198. switch (delsys) {
  199. case SYS_DVBC_ANNEX_A:
  200. is_annex_c = false;
  201. break;
  202. case SYS_DVBC_ANNEX_C:
  203. is_annex_c = true;
  204. break;
  205. default:
  206. return -EINVAL;
  207. }
  208. /*
  209. * gcc optimizes the code below the same way as it would code:
  210. * "if (qam > 5) return -EINVAL;"
  211. * Yet, the code is clearer, as it shows what QAM standards are
  212. * supported by the driver, and avoids the usage of magic numbers on
  213. * it.
  214. */
  215. switch (qam) {
  216. case QPSK:
  217. case QAM_16:
  218. case QAM_32:
  219. case QAM_64:
  220. case QAM_128:
  221. case QAM_256:
  222. break;
  223. default:
  224. return -EINVAL;
  225. }
  226. if (c->inversion != INVERSION_ON && c->inversion != INVERSION_OFF)
  227. return -EINVAL;
  228. /*printk("tda10021: set frequency to %d qam=%d symrate=%d\n", p->frequency,qam,p->symbol_rate);*/
  229. if (fe->ops.tuner_ops.set_params) {
  230. fe->ops.tuner_ops.set_params(fe);
  231. if (fe->ops.i2c_gate_ctrl) fe->ops.i2c_gate_ctrl(fe, 0);
  232. }
  233. tda10021_set_symbolrate(state, c->symbol_rate);
  234. _tda10021_writereg(state, 0x34, state->pwm);
  235. _tda10021_writereg(state, 0x01, qam_params[qam].agcref);
  236. _tda10021_writereg(state, 0x05, qam_params[qam].lthr);
  237. _tda10021_writereg(state, 0x08, qam_params[qam].mseth);
  238. _tda10021_writereg(state, 0x09, qam_params[qam].aref);
  239. /*
  240. * Bit 0 == 0 means roll-off = 0.15 (Annex A)
  241. * == 1 means roll-off = 0.13 (Annex C)
  242. */
  243. reg0x3d = tda10021_readreg (state, 0x3d);
  244. if (is_annex_c)
  245. _tda10021_writereg (state, 0x3d, 0x01 | reg0x3d);
  246. else
  247. _tda10021_writereg (state, 0x3d, 0xfe & reg0x3d);
  248. tda10021_setup_reg0(state, qam_params[qam].conf, c->inversion);
  249. return 0;
  250. }
  251. static int tda10021_read_status(struct dvb_frontend *fe,
  252. enum fe_status *status)
  253. {
  254. struct tda10021_state* state = fe->demodulator_priv;
  255. int sync;
  256. *status = 0;
  257. //0x11[0] == EQALGO -> Equalizer algorithms state
  258. //0x11[1] == CARLOCK -> Carrier locked
  259. //0x11[2] == FSYNC -> Frame synchronisation
  260. //0x11[3] == FEL -> Front End locked
  261. //0x11[6] == NODVB -> DVB Mode Information
  262. sync = tda10021_readreg (state, 0x11);
  263. if (sync & 2)
  264. *status |= FE_HAS_SIGNAL|FE_HAS_CARRIER;
  265. if (sync & 4)
  266. *status |= FE_HAS_SYNC|FE_HAS_VITERBI;
  267. if (sync & 8)
  268. *status |= FE_HAS_LOCK;
  269. return 0;
  270. }
  271. static int tda10021_read_ber(struct dvb_frontend* fe, u32* ber)
  272. {
  273. struct tda10021_state* state = fe->demodulator_priv;
  274. u32 _ber = tda10021_readreg(state, 0x14) |
  275. (tda10021_readreg(state, 0x15) << 8) |
  276. ((tda10021_readreg(state, 0x16) & 0x0f) << 16);
  277. _tda10021_writereg(state, 0x10, (tda10021_readreg(state, 0x10) & ~0xc0)
  278. | (tda10021_inittab[0x10] & 0xc0));
  279. *ber = 10 * _ber;
  280. return 0;
  281. }
  282. static int tda10021_read_signal_strength(struct dvb_frontend* fe, u16* strength)
  283. {
  284. struct tda10021_state* state = fe->demodulator_priv;
  285. u8 config = tda10021_readreg(state, 0x02);
  286. u8 gain = tda10021_readreg(state, 0x17);
  287. if (config & 0x02)
  288. /* the agc value is inverted */
  289. gain = ~gain;
  290. *strength = (gain << 8) | gain;
  291. return 0;
  292. }
  293. static int tda10021_read_snr(struct dvb_frontend* fe, u16* snr)
  294. {
  295. struct tda10021_state* state = fe->demodulator_priv;
  296. u8 quality = ~tda10021_readreg(state, 0x18);
  297. *snr = (quality << 8) | quality;
  298. return 0;
  299. }
  300. static int tda10021_read_ucblocks(struct dvb_frontend* fe, u32* ucblocks)
  301. {
  302. struct tda10021_state* state = fe->demodulator_priv;
  303. *ucblocks = tda10021_readreg (state, 0x13) & 0x7f;
  304. if (*ucblocks == 0x7f)
  305. *ucblocks = 0xffffffff;
  306. /* reset uncorrected block counter */
  307. _tda10021_writereg (state, 0x10, tda10021_inittab[0x10] & 0xdf);
  308. _tda10021_writereg (state, 0x10, tda10021_inittab[0x10]);
  309. return 0;
  310. }
  311. static int tda10021_get_frontend(struct dvb_frontend *fe,
  312. struct dtv_frontend_properties *p)
  313. {
  314. struct tda10021_state* state = fe->demodulator_priv;
  315. int sync;
  316. s8 afc = 0;
  317. sync = tda10021_readreg(state, 0x11);
  318. afc = tda10021_readreg(state, 0x19);
  319. if (verbose) {
  320. /* AFC only valid when carrier has been recovered */
  321. printk(sync & 2 ? "DVB: TDA10021(%d): AFC (%d) %dHz\n" :
  322. "DVB: TDA10021(%d): [AFC (%d) %dHz]\n",
  323. state->frontend.dvb->num, afc,
  324. -((s32)p->symbol_rate * afc) >> 10);
  325. }
  326. p->inversion = ((state->reg0 & 0x20) == 0x20) ^ (state->config->invert != 0) ? INVERSION_ON : INVERSION_OFF;
  327. p->modulation = ((state->reg0 >> 2) & 7) + QAM_16;
  328. p->fec_inner = FEC_NONE;
  329. p->frequency = ((p->frequency + 31250) / 62500) * 62500;
  330. if (sync & 2)
  331. p->frequency -= ((s32)p->symbol_rate * afc) >> 10;
  332. return 0;
  333. }
  334. static int tda10021_i2c_gate_ctrl(struct dvb_frontend* fe, int enable)
  335. {
  336. struct tda10021_state* state = fe->demodulator_priv;
  337. if (enable) {
  338. lock_tuner(state);
  339. } else {
  340. unlock_tuner(state);
  341. }
  342. return 0;
  343. }
  344. static int tda10021_sleep(struct dvb_frontend* fe)
  345. {
  346. struct tda10021_state* state = fe->demodulator_priv;
  347. _tda10021_writereg (state, 0x1b, 0x02); /* pdown ADC */
  348. _tda10021_writereg (state, 0x00, 0x80); /* standby */
  349. return 0;
  350. }
  351. static void tda10021_release(struct dvb_frontend* fe)
  352. {
  353. struct tda10021_state* state = fe->demodulator_priv;
  354. kfree(state);
  355. }
  356. static const struct dvb_frontend_ops tda10021_ops;
  357. struct dvb_frontend* tda10021_attach(const struct tda1002x_config* config,
  358. struct i2c_adapter* i2c,
  359. u8 pwm)
  360. {
  361. struct tda10021_state* state = NULL;
  362. u8 id;
  363. /* allocate memory for the internal state */
  364. state = kzalloc(sizeof(struct tda10021_state), GFP_KERNEL);
  365. if (state == NULL) goto error;
  366. /* setup the state */
  367. state->config = config;
  368. state->i2c = i2c;
  369. state->pwm = pwm;
  370. state->reg0 = tda10021_inittab[0];
  371. /* check if the demod is there */
  372. id = tda10021_readreg(state, 0x1a);
  373. if ((id & 0xf0) != 0x70) goto error;
  374. /* Don't claim TDA10023 */
  375. if (id == 0x7d)
  376. goto error;
  377. printk("TDA10021: i2c-addr = 0x%02x, id = 0x%02x\n",
  378. state->config->demod_address, id);
  379. /* create dvb_frontend */
  380. memcpy(&state->frontend.ops, &tda10021_ops, sizeof(struct dvb_frontend_ops));
  381. state->frontend.demodulator_priv = state;
  382. return &state->frontend;
  383. error:
  384. kfree(state);
  385. return NULL;
  386. }
  387. static const struct dvb_frontend_ops tda10021_ops = {
  388. .delsys = { SYS_DVBC_ANNEX_A, SYS_DVBC_ANNEX_C },
  389. .info = {
  390. .name = "Philips TDA10021 DVB-C",
  391. .frequency_min_hz = 47 * MHz,
  392. .frequency_max_hz = 862 * MHz,
  393. .frequency_stepsize_hz = 62500,
  394. .symbol_rate_min = (XIN / 2) / 64, /* SACLK/64 == (XIN/2)/64 */
  395. .symbol_rate_max = (XIN / 2) / 4, /* SACLK/4 */
  396. #if 0
  397. .frequency_tolerance = ???,
  398. .symbol_rate_tolerance = ???, /* ppm */ /* == 8% (spec p. 5) */
  399. #endif
  400. .caps = 0x400 | //FE_CAN_QAM_4
  401. FE_CAN_QAM_16 | FE_CAN_QAM_32 | FE_CAN_QAM_64 |
  402. FE_CAN_QAM_128 | FE_CAN_QAM_256 |
  403. FE_CAN_FEC_AUTO
  404. },
  405. .release = tda10021_release,
  406. .init = tda10021_init,
  407. .sleep = tda10021_sleep,
  408. .i2c_gate_ctrl = tda10021_i2c_gate_ctrl,
  409. .set_frontend = tda10021_set_parameters,
  410. .get_frontend = tda10021_get_frontend,
  411. .read_status = tda10021_read_status,
  412. .read_ber = tda10021_read_ber,
  413. .read_signal_strength = tda10021_read_signal_strength,
  414. .read_snr = tda10021_read_snr,
  415. .read_ucblocks = tda10021_read_ucblocks,
  416. };
  417. module_param(verbose, int, 0644);
  418. MODULE_PARM_DESC(verbose, "print AFC offset after tuning for debugging the PWM setting");
  419. MODULE_DESCRIPTION("Philips TDA10021 DVB-C demodulator driver");
  420. MODULE_AUTHOR("Ralph Metzler, Holger Waechtler, Markus Schulz");
  421. MODULE_LICENSE("GPL");
  422. EXPORT_SYMBOL_GPL(tda10021_attach);