ves1820.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448
  1. /*
  2. VES1820 - Single Chip Cable Channel Receiver driver module
  3. Copyright (C) 1999 Convergence Integrated Media GmbH <ralph@convergence.de>
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  15. */
  16. #include <linux/config.h>
  17. #include <linux/delay.h>
  18. #include <linux/errno.h>
  19. #include <linux/init.h>
  20. #include <linux/kernel.h>
  21. #include <linux/module.h>
  22. #include <linux/string.h>
  23. #include <linux/slab.h>
  24. #include <asm/div64.h>
  25. #include "dvb_frontend.h"
  26. #include "ves1820.h"
  27. struct ves1820_state {
  28. struct i2c_adapter* i2c;
  29. struct dvb_frontend_ops ops;
  30. /* configuration settings */
  31. const struct ves1820_config* config;
  32. struct dvb_frontend frontend;
  33. /* private demodulator data */
  34. u8 reg0;
  35. u8 pwm;
  36. };
  37. static int verbose;
  38. static u8 ves1820_inittab[] = {
  39. 0x69, 0x6A, 0x93, 0x12, 0x12, 0x46, 0x26, 0x1A,
  40. 0x43, 0x6A, 0xAA, 0xAA, 0x1E, 0x85, 0x43, 0x20,
  41. 0xE0, 0x00, 0xA1, 0x00, 0x00, 0x00, 0x00, 0x00,
  42. 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00,
  43. 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  44. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  45. 0x00, 0x00, 0x00, 0x00, 0x40
  46. };
  47. static int ves1820_writereg(struct ves1820_state *state, u8 reg, u8 data)
  48. {
  49. u8 buf[] = { 0x00, reg, data };
  50. struct i2c_msg msg = {.addr = state->config->demod_address,.flags = 0,.buf = buf,.len = 3 };
  51. int ret;
  52. ret = i2c_transfer(state->i2c, &msg, 1);
  53. if (ret != 1)
  54. printk("ves1820: %s(): writereg error (reg == 0x%02x,"
  55. "val == 0x%02x, ret == %i)\n", __FUNCTION__, reg, data, ret);
  56. return (ret != 1) ? -EREMOTEIO : 0;
  57. }
  58. static u8 ves1820_readreg(struct ves1820_state *state, u8 reg)
  59. {
  60. u8 b0[] = { 0x00, reg };
  61. u8 b1[] = { 0 };
  62. struct i2c_msg msg[] = {
  63. {.addr = state->config->demod_address,.flags = 0,.buf = b0,.len = 2},
  64. {.addr = state->config->demod_address,.flags = I2C_M_RD,.buf = b1,.len = 1}
  65. };
  66. int ret;
  67. ret = i2c_transfer(state->i2c, msg, 2);
  68. if (ret != 2)
  69. printk("ves1820: %s(): readreg error (reg == 0x%02x,"
  70. "ret == %i)\n", __FUNCTION__, reg, ret);
  71. return b1[0];
  72. }
  73. static int ves1820_setup_reg0(struct ves1820_state *state, u8 reg0, fe_spectral_inversion_t inversion)
  74. {
  75. reg0 |= state->reg0 & 0x62;
  76. if (INVERSION_ON == inversion) {
  77. if (!state->config->invert) reg0 |= 0x20;
  78. else reg0 &= ~0x20;
  79. } else if (INVERSION_OFF == inversion) {
  80. if (!state->config->invert) reg0 &= ~0x20;
  81. else reg0 |= 0x20;
  82. }
  83. ves1820_writereg(state, 0x00, reg0 & 0xfe);
  84. ves1820_writereg(state, 0x00, reg0 | 0x01);
  85. state->reg0 = reg0;
  86. return 0;
  87. }
  88. static int ves1820_set_symbolrate(struct ves1820_state *state, u32 symbolrate)
  89. {
  90. s32 BDR;
  91. s32 BDRI;
  92. s16 SFIL = 0;
  93. u16 NDEC = 0;
  94. u32 ratio;
  95. u32 fin;
  96. u32 tmp;
  97. u64 fptmp;
  98. u64 fpxin;
  99. if (symbolrate > state->config->xin / 2)
  100. symbolrate = state->config->xin / 2;
  101. if (symbolrate < 500000)
  102. symbolrate = 500000;
  103. if (symbolrate < state->config->xin / 16)
  104. NDEC = 1;
  105. if (symbolrate < state->config->xin / 32)
  106. NDEC = 2;
  107. if (symbolrate < state->config->xin / 64)
  108. NDEC = 3;
  109. /* yeuch! */
  110. fpxin = state->config->xin * 10;
  111. fptmp = fpxin; do_div(fptmp, 123);
  112. if (symbolrate < fptmp)
  113. SFIL = 1;
  114. fptmp = fpxin; do_div(fptmp, 160);
  115. if (symbolrate < fptmp)
  116. SFIL = 0;
  117. fptmp = fpxin; do_div(fptmp, 246);
  118. if (symbolrate < fptmp)
  119. SFIL = 1;
  120. fptmp = fpxin; do_div(fptmp, 320);
  121. if (symbolrate < fptmp)
  122. SFIL = 0;
  123. fptmp = fpxin; do_div(fptmp, 492);
  124. if (symbolrate < fptmp)
  125. SFIL = 1;
  126. fptmp = fpxin; do_div(fptmp, 640);
  127. if (symbolrate < fptmp)
  128. SFIL = 0;
  129. fptmp = fpxin; do_div(fptmp, 984);
  130. if (symbolrate < fptmp)
  131. SFIL = 1;
  132. fin = state->config->xin >> 4;
  133. symbolrate <<= NDEC;
  134. ratio = (symbolrate << 4) / fin;
  135. tmp = ((symbolrate << 4) % fin) << 8;
  136. ratio = (ratio << 8) + tmp / fin;
  137. tmp = (tmp % fin) << 8;
  138. ratio = (ratio << 8) + (tmp + fin / 2) / fin;
  139. BDR = ratio;
  140. BDRI = (((state->config->xin << 5) / symbolrate) + 1) / 2;
  141. if (BDRI > 0xFF)
  142. BDRI = 0xFF;
  143. SFIL = (SFIL << 4) | ves1820_inittab[0x0E];
  144. NDEC = (NDEC << 6) | ves1820_inittab[0x03];
  145. ves1820_writereg(state, 0x03, NDEC);
  146. ves1820_writereg(state, 0x0a, BDR & 0xff);
  147. ves1820_writereg(state, 0x0b, (BDR >> 8) & 0xff);
  148. ves1820_writereg(state, 0x0c, (BDR >> 16) & 0x3f);
  149. ves1820_writereg(state, 0x0d, BDRI);
  150. ves1820_writereg(state, 0x0e, SFIL);
  151. return 0;
  152. }
  153. static int ves1820_init(struct dvb_frontend* fe)
  154. {
  155. struct ves1820_state* state = fe->demodulator_priv;
  156. int i;
  157. ves1820_writereg(state, 0, 0);
  158. for (i = 0; i < sizeof(ves1820_inittab); i++)
  159. ves1820_writereg(state, i, ves1820_inittab[i]);
  160. if (state->config->selagc)
  161. ves1820_writereg(state, 2, ves1820_inittab[2] | 0x08);
  162. ves1820_writereg(state, 0x34, state->pwm);
  163. if (state->config->pll_init)
  164. state->config->pll_init(fe);
  165. return 0;
  166. }
  167. static int ves1820_set_parameters(struct dvb_frontend* fe, struct dvb_frontend_parameters *p)
  168. {
  169. struct ves1820_state* state = fe->demodulator_priv;
  170. static const u8 reg0x00[] = { 0x00, 0x04, 0x08, 0x0c, 0x10 };
  171. static const u8 reg0x01[] = { 140, 140, 106, 100, 92 };
  172. static const u8 reg0x05[] = { 135, 100, 70, 54, 38 };
  173. static const u8 reg0x08[] = { 162, 116, 67, 52, 35 };
  174. static const u8 reg0x09[] = { 145, 150, 106, 126, 107 };
  175. int real_qam = p->u.qam.modulation - QAM_16;
  176. if (real_qam < 0 || real_qam > 4)
  177. return -EINVAL;
  178. state->config->pll_set(fe, p);
  179. ves1820_set_symbolrate(state, p->u.qam.symbol_rate);
  180. ves1820_writereg(state, 0x34, state->pwm);
  181. ves1820_writereg(state, 0x01, reg0x01[real_qam]);
  182. ves1820_writereg(state, 0x05, reg0x05[real_qam]);
  183. ves1820_writereg(state, 0x08, reg0x08[real_qam]);
  184. ves1820_writereg(state, 0x09, reg0x09[real_qam]);
  185. ves1820_setup_reg0(state, reg0x00[real_qam], p->inversion);
  186. ves1820_writereg(state, 2, ves1820_inittab[2] | (state->config->selagc ? 0x08 : 0));
  187. return 0;
  188. }
  189. static int ves1820_read_status(struct dvb_frontend* fe, fe_status_t* status)
  190. {
  191. struct ves1820_state* state = fe->demodulator_priv;
  192. int sync;
  193. *status = 0;
  194. sync = ves1820_readreg(state, 0x11);
  195. if (sync & 1)
  196. *status |= FE_HAS_SIGNAL;
  197. if (sync & 2)
  198. *status |= FE_HAS_CARRIER;
  199. if (sync & 2) /* XXX FIXME! */
  200. *status |= FE_HAS_VITERBI;
  201. if (sync & 4)
  202. *status |= FE_HAS_SYNC;
  203. if (sync & 8)
  204. *status |= FE_HAS_LOCK;
  205. return 0;
  206. }
  207. static int ves1820_read_ber(struct dvb_frontend* fe, u32* ber)
  208. {
  209. struct ves1820_state* state = fe->demodulator_priv;
  210. u32 _ber = ves1820_readreg(state, 0x14) |
  211. (ves1820_readreg(state, 0x15) << 8) |
  212. ((ves1820_readreg(state, 0x16) & 0x0f) << 16);
  213. *ber = 10 * _ber;
  214. return 0;
  215. }
  216. static int ves1820_read_signal_strength(struct dvb_frontend* fe, u16* strength)
  217. {
  218. struct ves1820_state* state = fe->demodulator_priv;
  219. u8 gain = ves1820_readreg(state, 0x17);
  220. *strength = (gain << 8) | gain;
  221. return 0;
  222. }
  223. static int ves1820_read_snr(struct dvb_frontend* fe, u16* snr)
  224. {
  225. struct ves1820_state* state = fe->demodulator_priv;
  226. u8 quality = ~ves1820_readreg(state, 0x18);
  227. *snr = (quality << 8) | quality;
  228. return 0;
  229. }
  230. static int ves1820_read_ucblocks(struct dvb_frontend* fe, u32* ucblocks)
  231. {
  232. struct ves1820_state* state = fe->demodulator_priv;
  233. *ucblocks = ves1820_readreg(state, 0x13) & 0x7f;
  234. if (*ucblocks == 0x7f)
  235. *ucblocks = 0xffffffff;
  236. /* reset uncorrected block counter */
  237. ves1820_writereg(state, 0x10, ves1820_inittab[0x10] & 0xdf);
  238. ves1820_writereg(state, 0x10, ves1820_inittab[0x10]);
  239. return 0;
  240. }
  241. static int ves1820_get_frontend(struct dvb_frontend* fe, struct dvb_frontend_parameters *p)
  242. {
  243. struct ves1820_state* state = fe->demodulator_priv;
  244. int sync;
  245. s8 afc = 0;
  246. sync = ves1820_readreg(state, 0x11);
  247. afc = ves1820_readreg(state, 0x19);
  248. if (verbose) {
  249. /* AFC only valid when carrier has been recovered */
  250. printk(sync & 2 ? "ves1820: AFC (%d) %dHz\n" :
  251. "ves1820: [AFC (%d) %dHz]\n", afc, -((s32) p->u.qam.symbol_rate * afc) >> 10);
  252. }
  253. if (!state->config->invert) {
  254. p->inversion = (state->reg0 & 0x20) ? INVERSION_ON : INVERSION_OFF;
  255. } else {
  256. p->inversion = (!(state->reg0 & 0x20)) ? INVERSION_ON : INVERSION_OFF;
  257. }
  258. p->u.qam.modulation = ((state->reg0 >> 2) & 7) + QAM_16;
  259. p->u.qam.fec_inner = FEC_NONE;
  260. p->frequency = ((p->frequency + 31250) / 62500) * 62500;
  261. if (sync & 2)
  262. p->frequency -= ((s32) p->u.qam.symbol_rate * afc) >> 10;
  263. return 0;
  264. }
  265. static int ves1820_sleep(struct dvb_frontend* fe)
  266. {
  267. struct ves1820_state* state = fe->demodulator_priv;
  268. ves1820_writereg(state, 0x1b, 0x02); /* pdown ADC */
  269. ves1820_writereg(state, 0x00, 0x80); /* standby */
  270. return 0;
  271. }
  272. static int ves1820_get_tune_settings(struct dvb_frontend* fe, struct dvb_frontend_tune_settings* fesettings)
  273. {
  274. fesettings->min_delay_ms = 200;
  275. fesettings->step_size = 0;
  276. fesettings->max_drift = 0;
  277. return 0;
  278. }
  279. static void ves1820_release(struct dvb_frontend* fe)
  280. {
  281. struct ves1820_state* state = fe->demodulator_priv;
  282. kfree(state);
  283. }
  284. static struct dvb_frontend_ops ves1820_ops;
  285. struct dvb_frontend* ves1820_attach(const struct ves1820_config* config,
  286. struct i2c_adapter* i2c,
  287. u8 pwm)
  288. {
  289. struct ves1820_state* state = NULL;
  290. /* allocate memory for the internal state */
  291. state = kmalloc(sizeof(struct ves1820_state), GFP_KERNEL);
  292. if (state == NULL)
  293. goto error;
  294. /* setup the state */
  295. memcpy(&state->ops, &ves1820_ops, sizeof(struct dvb_frontend_ops));
  296. state->reg0 = ves1820_inittab[0];
  297. state->config = config;
  298. state->i2c = i2c;
  299. state->pwm = pwm;
  300. /* check if the demod is there */
  301. if ((ves1820_readreg(state, 0x1a) & 0xf0) != 0x70)
  302. goto error;
  303. if (verbose)
  304. printk("ves1820: pwm=0x%02x\n", state->pwm);
  305. state->ops.info.symbol_rate_min = (state->config->xin / 2) / 64; /* SACLK/64 == (XIN/2)/64 */
  306. state->ops.info.symbol_rate_max = (state->config->xin / 2) / 4; /* SACLK/4 */
  307. /* create dvb_frontend */
  308. state->frontend.ops = &state->ops;
  309. state->frontend.demodulator_priv = state;
  310. return &state->frontend;
  311. error:
  312. kfree(state);
  313. return NULL;
  314. }
  315. static struct dvb_frontend_ops ves1820_ops = {
  316. .info = {
  317. .name = "VLSI VES1820 DVB-C",
  318. .type = FE_QAM,
  319. .frequency_stepsize = 62500,
  320. .frequency_min = 51000000,
  321. .frequency_max = 858000000,
  322. .caps = FE_CAN_QAM_16 |
  323. FE_CAN_QAM_32 |
  324. FE_CAN_QAM_64 |
  325. FE_CAN_QAM_128 |
  326. FE_CAN_QAM_256 |
  327. FE_CAN_FEC_AUTO
  328. },
  329. .release = ves1820_release,
  330. .init = ves1820_init,
  331. .sleep = ves1820_sleep,
  332. .set_frontend = ves1820_set_parameters,
  333. .get_frontend = ves1820_get_frontend,
  334. .get_tune_settings = ves1820_get_tune_settings,
  335. .read_status = ves1820_read_status,
  336. .read_ber = ves1820_read_ber,
  337. .read_signal_strength = ves1820_read_signal_strength,
  338. .read_snr = ves1820_read_snr,
  339. .read_ucblocks = ves1820_read_ucblocks,
  340. };
  341. module_param(verbose, int, 0644);
  342. MODULE_PARM_DESC(verbose, "print AFC offset after tuning for debugging the PWM setting");
  343. MODULE_DESCRIPTION("VLSI VES1820 DVB-C Demodulator driver");
  344. MODULE_AUTHOR("Ralph Metzler, Holger Waechtler");
  345. MODULE_LICENSE("GPL");
  346. EXPORT_SYMBOL(ves1820_attach);