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/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)
  164. {
  165. struct dtv_frontend_properties *p = &fe->dtv_property_cache;
  166. struct ves1820_state* state = fe->demodulator_priv;
  167. static const u8 reg0x00[] = { 0x00, 0x04, 0x08, 0x0c, 0x10 };
  168. static const u8 reg0x01[] = { 140, 140, 106, 100, 92 };
  169. static const u8 reg0x05[] = { 135, 100, 70, 54, 38 };
  170. static const u8 reg0x08[] = { 162, 116, 67, 52, 35 };
  171. static const u8 reg0x09[] = { 145, 150, 106, 126, 107 };
  172. int real_qam = p->modulation - QAM_16;
  173. if (real_qam < 0 || real_qam > 4)
  174. return -EINVAL;
  175. if (fe->ops.tuner_ops.set_params) {
  176. fe->ops.tuner_ops.set_params(fe);
  177. if (fe->ops.i2c_gate_ctrl) fe->ops.i2c_gate_ctrl(fe, 0);
  178. }
  179. ves1820_set_symbolrate(state, p->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)
  242. {
  243. struct dtv_frontend_properties *p = &fe->dtv_property_cache;
  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->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->modulation = ((state->reg0 >> 2) & 7) + QAM_16;
  260. p->fec_inner = FEC_NONE;
  261. p->frequency = ((p->frequency + 31250) / 62500) * 62500;
  262. if (sync & 2)
  263. p->frequency -= ((s32) p->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 = kzalloc(sizeof(struct ves1820_state), GFP_KERNEL);
  293. if (state == NULL)
  294. goto error;
  295. /* setup the state */
  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. /* create dvb_frontend */
  306. memcpy(&state->frontend.ops, &ves1820_ops, sizeof(struct dvb_frontend_ops));
  307. state->frontend.ops.info.symbol_rate_min = (state->config->xin / 2) / 64; /* SACLK/64 == (XIN/2)/64 */
  308. state->frontend.ops.info.symbol_rate_max = (state->config->xin / 2) / 4; /* SACLK/4 */
  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. .delsys = { SYS_DVBC_ANNEX_A },
  317. .info = {
  318. .name = "VLSI VES1820 DVB-C",
  319. .frequency_stepsize = 62500,
  320. .frequency_min = 47000000,
  321. .frequency_max = 862000000,
  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);