ves1820.c 11 KB

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