tda10021.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484
  1. /*
  2. TDA10021 - Single Chip Cable Channel Receiver driver module
  3. used on the 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 "tda10021.h"
  28. struct tda10021_state {
  29. struct i2c_adapter* i2c;
  30. /* configuration settings */
  31. const struct tda10021_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 DISABLE_INVERSION(reg0) do { reg0 |= 0x20; } while (0)
  44. #define ENABLE_INVERSION(reg0) do { reg0 &= ~0x20; } while (0)
  45. #define HAS_INVERSION(reg0) (!(reg0 & 0x20))
  46. #define FIN (XIN >> 4)
  47. static int tda10021_inittab_size = 0x40;
  48. static u8 tda10021_inittab[0x40]=
  49. {
  50. 0x73, 0x6a, 0x23, 0x0a, 0x02, 0x37, 0x77, 0x1a,
  51. 0x37, 0x6a, 0x17, 0x8a, 0x1e, 0x86, 0x43, 0x40,
  52. 0xb8, 0x3f, 0xa0, 0x00, 0xcd, 0x01, 0x00, 0xff,
  53. 0x11, 0x00, 0x7c, 0x31, 0x30, 0x20, 0x00, 0x00,
  54. 0x02, 0x00, 0x00, 0x7d, 0x00, 0x00, 0x00, 0x00,
  55. 0x07, 0x00, 0x33, 0x11, 0x0d, 0x95, 0x08, 0x58,
  56. 0x00, 0x00, 0x80, 0x00, 0x80, 0xff, 0x00, 0x00,
  57. 0x04, 0x2d, 0x2f, 0xff, 0x00, 0x00, 0x00, 0x00,
  58. };
  59. static int _tda10021_writereg (struct tda10021_state* state, u8 reg, u8 data)
  60. {
  61. u8 buf[] = { reg, data };
  62. struct i2c_msg msg = { .addr = state->config->demod_address, .flags = 0, .buf = buf, .len = 2 };
  63. int ret;
  64. ret = i2c_transfer (state->i2c, &msg, 1);
  65. if (ret != 1)
  66. printk("DVB: TDA10021(%d): %s, writereg error "
  67. "(reg == 0x%02x, val == 0x%02x, ret == %i)\n",
  68. state->frontend.dvb->num, __FUNCTION__, reg, data, ret);
  69. msleep(10);
  70. return (ret != 1) ? -EREMOTEIO : 0;
  71. }
  72. static u8 tda10021_readreg (struct tda10021_state* state, u8 reg)
  73. {
  74. u8 b0 [] = { reg };
  75. u8 b1 [] = { 0 };
  76. struct i2c_msg msg [] = { { .addr = state->config->demod_address, .flags = 0, .buf = b0, .len = 1 },
  77. { .addr = state->config->demod_address, .flags = I2C_M_RD, .buf = b1, .len = 1 } };
  78. int ret;
  79. ret = i2c_transfer (state->i2c, msg, 2);
  80. if (ret != 2)
  81. printk("DVB: TDA10021: %s: readreg error (ret == %i)\n",
  82. __FUNCTION__, ret);
  83. return b1[0];
  84. }
  85. //get access to tuner
  86. static int lock_tuner(struct tda10021_state* state)
  87. {
  88. u8 buf[2] = { 0x0f, tda10021_inittab[0x0f] | 0x80 };
  89. struct i2c_msg msg = {.addr=state->config->demod_address, .flags=0, .buf=buf, .len=2};
  90. if(i2c_transfer(state->i2c, &msg, 1) != 1)
  91. {
  92. printk("tda10021: lock tuner fails\n");
  93. return -EREMOTEIO;
  94. }
  95. return 0;
  96. }
  97. //release access from tuner
  98. static int unlock_tuner(struct tda10021_state* state)
  99. {
  100. u8 buf[2] = { 0x0f, tda10021_inittab[0x0f] & 0x7f };
  101. struct i2c_msg msg_post={.addr=state->config->demod_address, .flags=0, .buf=buf, .len=2};
  102. if(i2c_transfer(state->i2c, &msg_post, 1) != 1)
  103. {
  104. printk("tda10021: unlock tuner fails\n");
  105. return -EREMOTEIO;
  106. }
  107. return 0;
  108. }
  109. static int tda10021_setup_reg0 (struct tda10021_state* state, u8 reg0,
  110. fe_spectral_inversion_t inversion)
  111. {
  112. reg0 |= state->reg0 & 0x63;
  113. if (INVERSION_ON == inversion)
  114. ENABLE_INVERSION(reg0);
  115. else if (INVERSION_OFF == inversion)
  116. DISABLE_INVERSION(reg0);
  117. _tda10021_writereg (state, 0x00, reg0 & 0xfe);
  118. _tda10021_writereg (state, 0x00, reg0 | 0x01);
  119. state->reg0 = reg0;
  120. return 0;
  121. }
  122. static int tda10021_set_symbolrate (struct tda10021_state* state, u32 symbolrate)
  123. {
  124. s32 BDR;
  125. s32 BDRI;
  126. s16 SFIL=0;
  127. u16 NDEC = 0;
  128. u32 tmp, ratio;
  129. if (symbolrate > XIN/2)
  130. symbolrate = XIN/2;
  131. if (symbolrate < 500000)
  132. symbolrate = 500000;
  133. if (symbolrate < XIN/16) NDEC = 1;
  134. if (symbolrate < XIN/32) NDEC = 2;
  135. if (symbolrate < XIN/64) NDEC = 3;
  136. if (symbolrate < (u32)(XIN/12.3)) SFIL = 1;
  137. if (symbolrate < (u32)(XIN/16)) SFIL = 0;
  138. if (symbolrate < (u32)(XIN/24.6)) SFIL = 1;
  139. if (symbolrate < (u32)(XIN/32)) SFIL = 0;
  140. if (symbolrate < (u32)(XIN/49.2)) SFIL = 1;
  141. if (symbolrate < (u32)(XIN/64)) SFIL = 0;
  142. if (symbolrate < (u32)(XIN/98.4)) SFIL = 1;
  143. symbolrate <<= NDEC;
  144. ratio = (symbolrate << 4) / FIN;
  145. tmp = ((symbolrate << 4) % FIN) << 8;
  146. ratio = (ratio << 8) + tmp / FIN;
  147. tmp = (tmp % FIN) << 8;
  148. ratio = (ratio << 8) + (tmp + FIN/2) / FIN;
  149. BDR = ratio;
  150. BDRI = (((XIN << 5) / symbolrate) + 1) / 2;
  151. if (BDRI > 0xFF)
  152. BDRI = 0xFF;
  153. SFIL = (SFIL << 4) | tda10021_inittab[0x0E];
  154. NDEC = (NDEC << 6) | tda10021_inittab[0x03];
  155. _tda10021_writereg (state, 0x03, NDEC);
  156. _tda10021_writereg (state, 0x0a, BDR&0xff);
  157. _tda10021_writereg (state, 0x0b, (BDR>> 8)&0xff);
  158. _tda10021_writereg (state, 0x0c, (BDR>>16)&0x3f);
  159. _tda10021_writereg (state, 0x0d, BDRI);
  160. _tda10021_writereg (state, 0x0e, SFIL);
  161. return 0;
  162. }
  163. int tda10021_write(struct dvb_frontend* fe, u8 *buf, int len)
  164. {
  165. struct tda10021_state* state = fe->demodulator_priv;
  166. if (len != 2)
  167. return -EINVAL;
  168. return _tda10021_writereg(state, buf[0], buf[1]);
  169. }
  170. static int tda10021_init (struct dvb_frontend *fe)
  171. {
  172. struct tda10021_state* state = fe->demodulator_priv;
  173. int i;
  174. dprintk("DVB: TDA10021(%d): init chip\n", fe->adapter->num);
  175. //_tda10021_writereg (fe, 0, 0);
  176. for (i=0; i<tda10021_inittab_size; i++)
  177. _tda10021_writereg (state, i, tda10021_inittab[i]);
  178. _tda10021_writereg (state, 0x34, state->pwm);
  179. //Comment by markus
  180. //0x2A[3-0] == PDIV -> P multiplaying factor (P=PDIV+1)(default 0)
  181. //0x2A[4] == BYPPLL -> Power down mode (default 1)
  182. //0x2A[5] == LCK -> PLL Lock Flag
  183. //0x2A[6] == POLAXIN -> Polarity of the input reference clock (default 0)
  184. //Activate PLL
  185. _tda10021_writereg(state, 0x2a, tda10021_inittab[0x2a] & 0xef);
  186. return 0;
  187. }
  188. static int tda10021_set_parameters (struct dvb_frontend *fe,
  189. struct dvb_frontend_parameters *p)
  190. {
  191. struct tda10021_state* state = fe->demodulator_priv;
  192. //table for QAM4-QAM256 ready QAM4 QAM16 QAM32 QAM64 QAM128 QAM256
  193. //CONF
  194. static const u8 reg0x00 [] = { 0x14, 0x00, 0x04, 0x08, 0x0c, 0x10 };
  195. //AGCREF value
  196. static const u8 reg0x01 [] = { 0x78, 0x8c, 0x8c, 0x6a, 0x78, 0x5c };
  197. //LTHR value
  198. static const u8 reg0x05 [] = { 0x78, 0x87, 0x64, 0x46, 0x36, 0x26 };
  199. //MSETH
  200. static const u8 reg0x08 [] = { 0x8c, 0xa2, 0x74, 0x43, 0x34, 0x23 };
  201. //AREF
  202. static const u8 reg0x09 [] = { 0x96, 0x91, 0x96, 0x6a, 0x7e, 0x6b };
  203. int qam = p->u.qam.modulation;
  204. if (qam < 0 || qam > 5)
  205. return -EINVAL;
  206. //printk("tda10021: set frequency to %d qam=%d symrate=%d\n", p->frequency,qam,p->u.qam.symbol_rate);
  207. if (fe->ops.tuner_ops.set_params) {
  208. fe->ops.tuner_ops.set_params(fe, p);
  209. if (fe->ops.i2c_gate_ctrl) fe->ops.i2c_gate_ctrl(fe, 0);
  210. }
  211. tda10021_set_symbolrate (state, p->u.qam.symbol_rate);
  212. _tda10021_writereg (state, 0x34, state->pwm);
  213. _tda10021_writereg (state, 0x01, reg0x01[qam]);
  214. _tda10021_writereg (state, 0x05, reg0x05[qam]);
  215. _tda10021_writereg (state, 0x08, reg0x08[qam]);
  216. _tda10021_writereg (state, 0x09, reg0x09[qam]);
  217. tda10021_setup_reg0 (state, reg0x00[qam], p->inversion);
  218. return 0;
  219. }
  220. static int tda10021_read_status(struct dvb_frontend* fe, fe_status_t* status)
  221. {
  222. struct tda10021_state* state = fe->demodulator_priv;
  223. int sync;
  224. *status = 0;
  225. //0x11[0] == EQALGO -> Equalizer algorithms state
  226. //0x11[1] == CARLOCK -> Carrier locked
  227. //0x11[2] == FSYNC -> Frame synchronisation
  228. //0x11[3] == FEL -> Front End locked
  229. //0x11[6] == NODVB -> DVB Mode Information
  230. sync = tda10021_readreg (state, 0x11);
  231. if (sync & 2)
  232. *status |= FE_HAS_SIGNAL|FE_HAS_CARRIER;
  233. if (sync & 4)
  234. *status |= FE_HAS_SYNC|FE_HAS_VITERBI;
  235. if (sync & 8)
  236. *status |= FE_HAS_LOCK;
  237. return 0;
  238. }
  239. static int tda10021_read_ber(struct dvb_frontend* fe, u32* ber)
  240. {
  241. struct tda10021_state* state = fe->demodulator_priv;
  242. u32 _ber = tda10021_readreg(state, 0x14) |
  243. (tda10021_readreg(state, 0x15) << 8) |
  244. ((tda10021_readreg(state, 0x16) & 0x0f) << 16);
  245. *ber = 10 * _ber;
  246. return 0;
  247. }
  248. static int tda10021_read_signal_strength(struct dvb_frontend* fe, u16* strength)
  249. {
  250. struct tda10021_state* state = fe->demodulator_priv;
  251. u8 gain = tda10021_readreg(state, 0x17);
  252. *strength = (gain << 8) | gain;
  253. return 0;
  254. }
  255. static int tda10021_read_snr(struct dvb_frontend* fe, u16* snr)
  256. {
  257. struct tda10021_state* state = fe->demodulator_priv;
  258. u8 quality = ~tda10021_readreg(state, 0x18);
  259. *snr = (quality << 8) | quality;
  260. return 0;
  261. }
  262. static int tda10021_read_ucblocks(struct dvb_frontend* fe, u32* ucblocks)
  263. {
  264. struct tda10021_state* state = fe->demodulator_priv;
  265. *ucblocks = tda10021_readreg (state, 0x13) & 0x7f;
  266. if (*ucblocks == 0x7f)
  267. *ucblocks = 0xffffffff;
  268. /* reset uncorrected block counter */
  269. _tda10021_writereg (state, 0x10, tda10021_inittab[0x10] & 0xdf);
  270. _tda10021_writereg (state, 0x10, tda10021_inittab[0x10]);
  271. return 0;
  272. }
  273. static int tda10021_get_frontend(struct dvb_frontend* fe, struct dvb_frontend_parameters *p)
  274. {
  275. struct tda10021_state* state = fe->demodulator_priv;
  276. int sync;
  277. s8 afc = 0;
  278. sync = tda10021_readreg(state, 0x11);
  279. afc = tda10021_readreg(state, 0x19);
  280. if (verbose) {
  281. /* AFC only valid when carrier has been recovered */
  282. printk(sync & 2 ? "DVB: TDA10021(%d): AFC (%d) %dHz\n" :
  283. "DVB: TDA10021(%d): [AFC (%d) %dHz]\n",
  284. state->frontend.dvb->num, afc,
  285. -((s32)p->u.qam.symbol_rate * afc) >> 10);
  286. }
  287. p->inversion = HAS_INVERSION(state->reg0) ? INVERSION_ON : INVERSION_OFF;
  288. p->u.qam.modulation = ((state->reg0 >> 2) & 7) + QAM_16;
  289. p->u.qam.fec_inner = FEC_NONE;
  290. p->frequency = ((p->frequency + 31250) / 62500) * 62500;
  291. if (sync & 2)
  292. p->frequency -= ((s32)p->u.qam.symbol_rate * afc) >> 10;
  293. return 0;
  294. }
  295. static int tda10021_i2c_gate_ctrl(struct dvb_frontend* fe, int enable)
  296. {
  297. struct tda10021_state* state = fe->demodulator_priv;
  298. if (enable) {
  299. lock_tuner(state);
  300. } else {
  301. unlock_tuner(state);
  302. }
  303. return 0;
  304. }
  305. static int tda10021_sleep(struct dvb_frontend* fe)
  306. {
  307. struct tda10021_state* state = fe->demodulator_priv;
  308. _tda10021_writereg (state, 0x1b, 0x02); /* pdown ADC */
  309. _tda10021_writereg (state, 0x00, 0x80); /* standby */
  310. return 0;
  311. }
  312. static void tda10021_release(struct dvb_frontend* fe)
  313. {
  314. struct tda10021_state* state = fe->demodulator_priv;
  315. kfree(state);
  316. }
  317. static struct dvb_frontend_ops tda10021_ops;
  318. struct dvb_frontend* tda10021_attach(const struct tda10021_config* config,
  319. struct i2c_adapter* i2c,
  320. u8 pwm)
  321. {
  322. struct tda10021_state* state = NULL;
  323. /* allocate memory for the internal state */
  324. state = kmalloc(sizeof(struct tda10021_state), GFP_KERNEL);
  325. if (state == NULL) goto error;
  326. /* setup the state */
  327. state->config = config;
  328. state->i2c = i2c;
  329. state->pwm = pwm;
  330. state->reg0 = tda10021_inittab[0];
  331. /* check if the demod is there */
  332. if ((tda10021_readreg(state, 0x1a) & 0xf0) != 0x70) goto error;
  333. /* create dvb_frontend */
  334. memcpy(&state->frontend.ops, &tda10021_ops, sizeof(struct dvb_frontend_ops));
  335. state->frontend.demodulator_priv = state;
  336. return &state->frontend;
  337. error:
  338. kfree(state);
  339. return NULL;
  340. }
  341. static struct dvb_frontend_ops tda10021_ops = {
  342. .info = {
  343. .name = "Philips TDA10021 DVB-C",
  344. .type = FE_QAM,
  345. .frequency_stepsize = 62500,
  346. .frequency_min = 51000000,
  347. .frequency_max = 858000000,
  348. .symbol_rate_min = (XIN/2)/64, /* SACLK/64 == (XIN/2)/64 */
  349. .symbol_rate_max = (XIN/2)/4, /* SACLK/4 */
  350. #if 0
  351. .frequency_tolerance = ???,
  352. .symbol_rate_tolerance = ???, /* ppm */ /* == 8% (spec p. 5) */
  353. #endif
  354. .caps = 0x400 | //FE_CAN_QAM_4
  355. FE_CAN_QAM_16 | FE_CAN_QAM_32 | FE_CAN_QAM_64 |
  356. FE_CAN_QAM_128 | FE_CAN_QAM_256 |
  357. FE_CAN_FEC_AUTO
  358. },
  359. .release = tda10021_release,
  360. .init = tda10021_init,
  361. .sleep = tda10021_sleep,
  362. .write = tda10021_write,
  363. .i2c_gate_ctrl = tda10021_i2c_gate_ctrl,
  364. .set_frontend = tda10021_set_parameters,
  365. .get_frontend = tda10021_get_frontend,
  366. .read_status = tda10021_read_status,
  367. .read_ber = tda10021_read_ber,
  368. .read_signal_strength = tda10021_read_signal_strength,
  369. .read_snr = tda10021_read_snr,
  370. .read_ucblocks = tda10021_read_ucblocks,
  371. };
  372. module_param(verbose, int, 0644);
  373. MODULE_PARM_DESC(verbose, "print AFC offset after tuning for debugging the PWM setting");
  374. MODULE_DESCRIPTION("Philips TDA10021 DVB-C demodulator driver");
  375. MODULE_AUTHOR("Ralph Metzler, Holger Waechtler, Markus Schulz");
  376. MODULE_LICENSE("GPL");
  377. EXPORT_SYMBOL(tda10021_attach);