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/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,
  72. u8 reg0, enum fe_spectral_inversion inversion)
  73. {
  74. reg0 |= state->reg0 & 0x62;
  75. if (INVERSION_ON == inversion) {
  76. if (!state->config->invert) reg0 |= 0x20;
  77. else reg0 &= ~0x20;
  78. } else if (INVERSION_OFF == inversion) {
  79. if (!state->config->invert) reg0 &= ~0x20;
  80. else reg0 |= 0x20;
  81. }
  82. ves1820_writereg(state, 0x00, reg0 & 0xfe);
  83. ves1820_writereg(state, 0x00, reg0 | 0x01);
  84. state->reg0 = reg0;
  85. return 0;
  86. }
  87. static int ves1820_set_symbolrate(struct ves1820_state *state, u32 symbolrate)
  88. {
  89. s32 BDR;
  90. s32 BDRI;
  91. s16 SFIL = 0;
  92. u16 NDEC = 0;
  93. u32 ratio;
  94. u32 fin;
  95. u32 tmp;
  96. u64 fptmp;
  97. u64 fpxin;
  98. if (symbolrate > state->config->xin / 2)
  99. symbolrate = state->config->xin / 2;
  100. if (symbolrate < 500000)
  101. symbolrate = 500000;
  102. if (symbolrate < state->config->xin / 16)
  103. NDEC = 1;
  104. if (symbolrate < state->config->xin / 32)
  105. NDEC = 2;
  106. if (symbolrate < state->config->xin / 64)
  107. NDEC = 3;
  108. /* yeuch! */
  109. fpxin = state->config->xin * 10;
  110. fptmp = fpxin; do_div(fptmp, 123);
  111. if (symbolrate < fptmp)
  112. SFIL = 1;
  113. fptmp = fpxin; do_div(fptmp, 160);
  114. if (symbolrate < fptmp)
  115. SFIL = 0;
  116. fptmp = fpxin; do_div(fptmp, 246);
  117. if (symbolrate < fptmp)
  118. SFIL = 1;
  119. fptmp = fpxin; do_div(fptmp, 320);
  120. if (symbolrate < fptmp)
  121. SFIL = 0;
  122. fptmp = fpxin; do_div(fptmp, 492);
  123. if (symbolrate < fptmp)
  124. SFIL = 1;
  125. fptmp = fpxin; do_div(fptmp, 640);
  126. if (symbolrate < fptmp)
  127. SFIL = 0;
  128. fptmp = fpxin; do_div(fptmp, 984);
  129. if (symbolrate < fptmp)
  130. SFIL = 1;
  131. fin = state->config->xin >> 4;
  132. symbolrate <<= NDEC;
  133. ratio = (symbolrate << 4) / fin;
  134. tmp = ((symbolrate << 4) % fin) << 8;
  135. ratio = (ratio << 8) + tmp / fin;
  136. tmp = (tmp % fin) << 8;
  137. ratio = (ratio << 8) + DIV_ROUND_CLOSEST(tmp, fin);
  138. BDR = ratio;
  139. BDRI = (((state->config->xin << 5) / symbolrate) + 1) / 2;
  140. if (BDRI > 0xFF)
  141. BDRI = 0xFF;
  142. SFIL = (SFIL << 4) | ves1820_inittab[0x0E];
  143. NDEC = (NDEC << 6) | ves1820_inittab[0x03];
  144. ves1820_writereg(state, 0x03, NDEC);
  145. ves1820_writereg(state, 0x0a, BDR & 0xff);
  146. ves1820_writereg(state, 0x0b, (BDR >> 8) & 0xff);
  147. ves1820_writereg(state, 0x0c, (BDR >> 16) & 0x3f);
  148. ves1820_writereg(state, 0x0d, BDRI);
  149. ves1820_writereg(state, 0x0e, SFIL);
  150. return 0;
  151. }
  152. static int ves1820_init(struct dvb_frontend* fe)
  153. {
  154. struct ves1820_state* state = fe->demodulator_priv;
  155. int i;
  156. ves1820_writereg(state, 0, 0);
  157. for (i = 0; i < sizeof(ves1820_inittab); i++)
  158. ves1820_writereg(state, i, ves1820_inittab[i]);
  159. if (state->config->selagc)
  160. ves1820_writereg(state, 2, ves1820_inittab[2] | 0x08);
  161. ves1820_writereg(state, 0x34, state->pwm);
  162. return 0;
  163. }
  164. static int ves1820_set_parameters(struct dvb_frontend *fe)
  165. {
  166. struct dtv_frontend_properties *p = &fe->dtv_property_cache;
  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->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);
  178. if (fe->ops.i2c_gate_ctrl) fe->ops.i2c_gate_ctrl(fe, 0);
  179. }
  180. ves1820_set_symbolrate(state, p->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,
  191. enum fe_status *status)
  192. {
  193. struct ves1820_state* state = fe->demodulator_priv;
  194. int sync;
  195. *status = 0;
  196. sync = ves1820_readreg(state, 0x11);
  197. if (sync & 1)
  198. *status |= FE_HAS_SIGNAL;
  199. if (sync & 2)
  200. *status |= FE_HAS_CARRIER;
  201. if (sync & 2) /* XXX FIXME! */
  202. *status |= FE_HAS_VITERBI;
  203. if (sync & 4)
  204. *status |= FE_HAS_SYNC;
  205. if (sync & 8)
  206. *status |= FE_HAS_LOCK;
  207. return 0;
  208. }
  209. static int ves1820_read_ber(struct dvb_frontend* fe, u32* ber)
  210. {
  211. struct ves1820_state* state = fe->demodulator_priv;
  212. u32 _ber = ves1820_readreg(state, 0x14) |
  213. (ves1820_readreg(state, 0x15) << 8) |
  214. ((ves1820_readreg(state, 0x16) & 0x0f) << 16);
  215. *ber = 10 * _ber;
  216. return 0;
  217. }
  218. static int ves1820_read_signal_strength(struct dvb_frontend* fe, u16* strength)
  219. {
  220. struct ves1820_state* state = fe->demodulator_priv;
  221. u8 gain = ves1820_readreg(state, 0x17);
  222. *strength = (gain << 8) | gain;
  223. return 0;
  224. }
  225. static int ves1820_read_snr(struct dvb_frontend* fe, u16* snr)
  226. {
  227. struct ves1820_state* state = fe->demodulator_priv;
  228. u8 quality = ~ves1820_readreg(state, 0x18);
  229. *snr = (quality << 8) | quality;
  230. return 0;
  231. }
  232. static int ves1820_read_ucblocks(struct dvb_frontend* fe, u32* ucblocks)
  233. {
  234. struct ves1820_state* state = fe->demodulator_priv;
  235. *ucblocks = ves1820_readreg(state, 0x13) & 0x7f;
  236. if (*ucblocks == 0x7f)
  237. *ucblocks = 0xffffffff;
  238. /* reset uncorrected block counter */
  239. ves1820_writereg(state, 0x10, ves1820_inittab[0x10] & 0xdf);
  240. ves1820_writereg(state, 0x10, ves1820_inittab[0x10]);
  241. return 0;
  242. }
  243. static int ves1820_get_frontend(struct dvb_frontend *fe)
  244. {
  245. struct dtv_frontend_properties *p = &fe->dtv_property_cache;
  246. struct ves1820_state* state = fe->demodulator_priv;
  247. int sync;
  248. s8 afc = 0;
  249. sync = ves1820_readreg(state, 0x11);
  250. afc = ves1820_readreg(state, 0x19);
  251. if (verbose) {
  252. /* AFC only valid when carrier has been recovered */
  253. printk(sync & 2 ? "ves1820: AFC (%d) %dHz\n" :
  254. "ves1820: [AFC (%d) %dHz]\n", afc, -((s32) p->symbol_rate * afc) >> 10);
  255. }
  256. if (!state->config->invert) {
  257. p->inversion = (state->reg0 & 0x20) ? INVERSION_ON : INVERSION_OFF;
  258. } else {
  259. p->inversion = (!(state->reg0 & 0x20)) ? INVERSION_ON : INVERSION_OFF;
  260. }
  261. p->modulation = ((state->reg0 >> 2) & 7) + QAM_16;
  262. p->fec_inner = FEC_NONE;
  263. p->frequency = ((p->frequency + 31250) / 62500) * 62500;
  264. if (sync & 2)
  265. p->frequency -= ((s32) p->symbol_rate * afc) >> 10;
  266. return 0;
  267. }
  268. static int ves1820_sleep(struct dvb_frontend* fe)
  269. {
  270. struct ves1820_state* state = fe->demodulator_priv;
  271. ves1820_writereg(state, 0x1b, 0x02); /* pdown ADC */
  272. ves1820_writereg(state, 0x00, 0x80); /* standby */
  273. return 0;
  274. }
  275. static int ves1820_get_tune_settings(struct dvb_frontend* fe, struct dvb_frontend_tune_settings* fesettings)
  276. {
  277. fesettings->min_delay_ms = 200;
  278. fesettings->step_size = 0;
  279. fesettings->max_drift = 0;
  280. return 0;
  281. }
  282. static void ves1820_release(struct dvb_frontend* fe)
  283. {
  284. struct ves1820_state* state = fe->demodulator_priv;
  285. kfree(state);
  286. }
  287. static struct dvb_frontend_ops ves1820_ops;
  288. struct dvb_frontend* ves1820_attach(const struct ves1820_config* config,
  289. struct i2c_adapter* i2c,
  290. u8 pwm)
  291. {
  292. struct ves1820_state* state = NULL;
  293. /* allocate memory for the internal state */
  294. state = kzalloc(sizeof(struct ves1820_state), GFP_KERNEL);
  295. if (state == NULL)
  296. goto error;
  297. /* setup the state */
  298. state->reg0 = ves1820_inittab[0];
  299. state->config = config;
  300. state->i2c = i2c;
  301. state->pwm = pwm;
  302. /* check if the demod is there */
  303. if ((ves1820_readreg(state, 0x1a) & 0xf0) != 0x70)
  304. goto error;
  305. if (verbose)
  306. printk("ves1820: pwm=0x%02x\n", state->pwm);
  307. /* create dvb_frontend */
  308. memcpy(&state->frontend.ops, &ves1820_ops, sizeof(struct dvb_frontend_ops));
  309. state->frontend.ops.info.symbol_rate_min = (state->config->xin / 2) / 64; /* SACLK/64 == (XIN/2)/64 */
  310. state->frontend.ops.info.symbol_rate_max = (state->config->xin / 2) / 4; /* SACLK/4 */
  311. state->frontend.demodulator_priv = state;
  312. return &state->frontend;
  313. error:
  314. kfree(state);
  315. return NULL;
  316. }
  317. static struct dvb_frontend_ops ves1820_ops = {
  318. .delsys = { SYS_DVBC_ANNEX_A },
  319. .info = {
  320. .name = "VLSI VES1820 DVB-C",
  321. .frequency_stepsize = 62500,
  322. .frequency_min = 47000000,
  323. .frequency_max = 862000000,
  324. .caps = FE_CAN_QAM_16 |
  325. FE_CAN_QAM_32 |
  326. FE_CAN_QAM_64 |
  327. FE_CAN_QAM_128 |
  328. FE_CAN_QAM_256 |
  329. FE_CAN_FEC_AUTO
  330. },
  331. .release = ves1820_release,
  332. .init = ves1820_init,
  333. .sleep = ves1820_sleep,
  334. .set_frontend = ves1820_set_parameters,
  335. .get_frontend = ves1820_get_frontend,
  336. .get_tune_settings = ves1820_get_tune_settings,
  337. .read_status = ves1820_read_status,
  338. .read_ber = ves1820_read_ber,
  339. .read_signal_strength = ves1820_read_signal_strength,
  340. .read_snr = ves1820_read_snr,
  341. .read_ucblocks = ves1820_read_ucblocks,
  342. };
  343. module_param(verbose, int, 0644);
  344. MODULE_PARM_DESC(verbose, "print AFC offset after tuning for debugging the PWM setting");
  345. MODULE_DESCRIPTION("VLSI VES1820 DVB-C Demodulator driver");
  346. MODULE_AUTHOR("Ralph Metzler, Holger Waechtler");
  347. MODULE_LICENSE("GPL");
  348. EXPORT_SYMBOL(ves1820_attach);