ves1820.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449
  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. return 0;
  164. }
  165. static int ves1820_set_parameters(struct dvb_frontend* fe, struct dvb_frontend_parameters *p)
  166. {
  167. struct ves1820_state* state = fe->demodulator_priv;
  168. static const u8 reg0x00[] = { 0x00, 0x04, 0x08, 0x0c, 0x10 };
  169. static const u8 reg0x01[] = { 140, 140, 106, 100, 92 };
  170. static const u8 reg0x05[] = { 135, 100, 70, 54, 38 };
  171. static const u8 reg0x08[] = { 162, 116, 67, 52, 35 };
  172. static const u8 reg0x09[] = { 145, 150, 106, 126, 107 };
  173. int real_qam = p->u.qam.modulation - QAM_16;
  174. if (real_qam < 0 || real_qam > 4)
  175. return -EINVAL;
  176. if (fe->ops->tuner_ops.set_params) {
  177. fe->ops->tuner_ops.set_params(fe, p);
  178. if (fe->ops->i2c_gate_ctrl) fe->ops->i2c_gate_ctrl(fe, 0);
  179. }
  180. ves1820_set_symbolrate(state, p->u.qam.symbol_rate);
  181. ves1820_writereg(state, 0x34, state->pwm);
  182. ves1820_writereg(state, 0x01, reg0x01[real_qam]);
  183. ves1820_writereg(state, 0x05, reg0x05[real_qam]);
  184. ves1820_writereg(state, 0x08, reg0x08[real_qam]);
  185. ves1820_writereg(state, 0x09, reg0x09[real_qam]);
  186. ves1820_setup_reg0(state, reg0x00[real_qam], p->inversion);
  187. ves1820_writereg(state, 2, ves1820_inittab[2] | (state->config->selagc ? 0x08 : 0));
  188. return 0;
  189. }
  190. static int ves1820_read_status(struct dvb_frontend* fe, fe_status_t* status)
  191. {
  192. struct ves1820_state* state = fe->demodulator_priv;
  193. int sync;
  194. *status = 0;
  195. sync = ves1820_readreg(state, 0x11);
  196. if (sync & 1)
  197. *status |= FE_HAS_SIGNAL;
  198. if (sync & 2)
  199. *status |= FE_HAS_CARRIER;
  200. if (sync & 2) /* XXX FIXME! */
  201. *status |= FE_HAS_VITERBI;
  202. if (sync & 4)
  203. *status |= FE_HAS_SYNC;
  204. if (sync & 8)
  205. *status |= FE_HAS_LOCK;
  206. return 0;
  207. }
  208. static int ves1820_read_ber(struct dvb_frontend* fe, u32* ber)
  209. {
  210. struct ves1820_state* state = fe->demodulator_priv;
  211. u32 _ber = ves1820_readreg(state, 0x14) |
  212. (ves1820_readreg(state, 0x15) << 8) |
  213. ((ves1820_readreg(state, 0x16) & 0x0f) << 16);
  214. *ber = 10 * _ber;
  215. return 0;
  216. }
  217. static int ves1820_read_signal_strength(struct dvb_frontend* fe, u16* strength)
  218. {
  219. struct ves1820_state* state = fe->demodulator_priv;
  220. u8 gain = ves1820_readreg(state, 0x17);
  221. *strength = (gain << 8) | gain;
  222. return 0;
  223. }
  224. static int ves1820_read_snr(struct dvb_frontend* fe, u16* snr)
  225. {
  226. struct ves1820_state* state = fe->demodulator_priv;
  227. u8 quality = ~ves1820_readreg(state, 0x18);
  228. *snr = (quality << 8) | quality;
  229. return 0;
  230. }
  231. static int ves1820_read_ucblocks(struct dvb_frontend* fe, u32* ucblocks)
  232. {
  233. struct ves1820_state* state = fe->demodulator_priv;
  234. *ucblocks = ves1820_readreg(state, 0x13) & 0x7f;
  235. if (*ucblocks == 0x7f)
  236. *ucblocks = 0xffffffff;
  237. /* reset uncorrected block counter */
  238. ves1820_writereg(state, 0x10, ves1820_inittab[0x10] & 0xdf);
  239. ves1820_writereg(state, 0x10, ves1820_inittab[0x10]);
  240. return 0;
  241. }
  242. static int ves1820_get_frontend(struct dvb_frontend* fe, struct dvb_frontend_parameters *p)
  243. {
  244. struct ves1820_state* state = fe->demodulator_priv;
  245. int sync;
  246. s8 afc = 0;
  247. sync = ves1820_readreg(state, 0x11);
  248. afc = ves1820_readreg(state, 0x19);
  249. if (verbose) {
  250. /* AFC only valid when carrier has been recovered */
  251. printk(sync & 2 ? "ves1820: AFC (%d) %dHz\n" :
  252. "ves1820: [AFC (%d) %dHz]\n", afc, -((s32) p->u.qam.symbol_rate * afc) >> 10);
  253. }
  254. if (!state->config->invert) {
  255. p->inversion = (state->reg0 & 0x20) ? INVERSION_ON : INVERSION_OFF;
  256. } else {
  257. p->inversion = (!(state->reg0 & 0x20)) ? INVERSION_ON : INVERSION_OFF;
  258. }
  259. p->u.qam.modulation = ((state->reg0 >> 2) & 7) + QAM_16;
  260. p->u.qam.fec_inner = FEC_NONE;
  261. p->frequency = ((p->frequency + 31250) / 62500) * 62500;
  262. if (sync & 2)
  263. p->frequency -= ((s32) p->u.qam.symbol_rate * afc) >> 10;
  264. return 0;
  265. }
  266. static int ves1820_sleep(struct dvb_frontend* fe)
  267. {
  268. struct ves1820_state* state = fe->demodulator_priv;
  269. ves1820_writereg(state, 0x1b, 0x02); /* pdown ADC */
  270. ves1820_writereg(state, 0x00, 0x80); /* standby */
  271. return 0;
  272. }
  273. static int ves1820_get_tune_settings(struct dvb_frontend* fe, struct dvb_frontend_tune_settings* fesettings)
  274. {
  275. fesettings->min_delay_ms = 200;
  276. fesettings->step_size = 0;
  277. fesettings->max_drift = 0;
  278. return 0;
  279. }
  280. static void ves1820_release(struct dvb_frontend* fe)
  281. {
  282. struct ves1820_state* state = fe->demodulator_priv;
  283. kfree(state);
  284. }
  285. static struct dvb_frontend_ops ves1820_ops;
  286. struct dvb_frontend* ves1820_attach(const struct ves1820_config* config,
  287. struct i2c_adapter* i2c,
  288. u8 pwm)
  289. {
  290. struct ves1820_state* state = NULL;
  291. /* allocate memory for the internal state */
  292. state = kmalloc(sizeof(struct ves1820_state), GFP_KERNEL);
  293. if (state == NULL)
  294. goto error;
  295. /* setup the state */
  296. memcpy(&state->ops, &ves1820_ops, sizeof(struct dvb_frontend_ops));
  297. state->reg0 = ves1820_inittab[0];
  298. state->config = config;
  299. state->i2c = i2c;
  300. state->pwm = pwm;
  301. /* check if the demod is there */
  302. if ((ves1820_readreg(state, 0x1a) & 0xf0) != 0x70)
  303. goto error;
  304. if (verbose)
  305. printk("ves1820: pwm=0x%02x\n", state->pwm);
  306. state->ops.info.symbol_rate_min = (state->config->xin / 2) / 64; /* SACLK/64 == (XIN/2)/64 */
  307. state->ops.info.symbol_rate_max = (state->config->xin / 2) / 4; /* SACLK/4 */
  308. /* create dvb_frontend */
  309. state->frontend.ops = &state->ops;
  310. state->frontend.demodulator_priv = state;
  311. return &state->frontend;
  312. error:
  313. kfree(state);
  314. return NULL;
  315. }
  316. static struct dvb_frontend_ops ves1820_ops = {
  317. .info = {
  318. .name = "VLSI VES1820 DVB-C",
  319. .type = FE_QAM,
  320. .frequency_stepsize = 62500,
  321. .frequency_min = 51000000,
  322. .frequency_max = 858000000,
  323. .caps = FE_CAN_QAM_16 |
  324. FE_CAN_QAM_32 |
  325. FE_CAN_QAM_64 |
  326. FE_CAN_QAM_128 |
  327. FE_CAN_QAM_256 |
  328. FE_CAN_FEC_AUTO
  329. },
  330. .release = ves1820_release,
  331. .init = ves1820_init,
  332. .sleep = ves1820_sleep,
  333. .set_frontend = ves1820_set_parameters,
  334. .get_frontend = ves1820_get_frontend,
  335. .get_tune_settings = ves1820_get_tune_settings,
  336. .read_status = ves1820_read_status,
  337. .read_ber = ves1820_read_ber,
  338. .read_signal_strength = ves1820_read_signal_strength,
  339. .read_snr = ves1820_read_snr,
  340. .read_ucblocks = ves1820_read_ucblocks,
  341. };
  342. module_param(verbose, int, 0644);
  343. MODULE_PARM_DESC(verbose, "print AFC offset after tuning for debugging the PWM setting");
  344. MODULE_DESCRIPTION("VLSI VES1820 DVB-C Demodulator driver");
  345. MODULE_AUTHOR("Ralph Metzler, Holger Waechtler");
  346. MODULE_LICENSE("GPL");
  347. EXPORT_SYMBOL(ves1820_attach);