ves1820.c 11 KB

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