ves1x93.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. Driver for VES1893 and VES1993 QPSK Demodulators
  4. Copyright (C) 1999 Convergence Integrated Media GmbH <ralph@convergence.de>
  5. Copyright (C) 2001 Ronny Strutz <3des@elitedvb.de>
  6. Copyright (C) 2002 Dennis Noermann <dennis.noermann@noernet.de>
  7. Copyright (C) 2002-2003 Andreas Oberritter <obi@linuxtv.org>
  8. */
  9. #include <linux/kernel.h>
  10. #include <linux/module.h>
  11. #include <linux/init.h>
  12. #include <linux/string.h>
  13. #include <linux/slab.h>
  14. #include <linux/delay.h>
  15. #include <media/dvb_frontend.h>
  16. #include "ves1x93.h"
  17. struct ves1x93_state {
  18. struct i2c_adapter* i2c;
  19. /* configuration settings */
  20. const struct ves1x93_config* config;
  21. struct dvb_frontend frontend;
  22. /* previous uncorrected block counter */
  23. enum fe_spectral_inversion inversion;
  24. u8 *init_1x93_tab;
  25. u8 *init_1x93_wtab;
  26. u8 tab_size;
  27. u8 demod_type;
  28. u32 frequency;
  29. };
  30. static int debug;
  31. #define dprintk if (debug) printk
  32. #define DEMOD_VES1893 0
  33. #define DEMOD_VES1993 1
  34. static u8 init_1893_tab [] = {
  35. 0x01, 0xa4, 0x35, 0x80, 0x2a, 0x0b, 0x55, 0xc4,
  36. 0x09, 0x69, 0x00, 0x86, 0x4c, 0x28, 0x7f, 0x00,
  37. 0x00, 0x81, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  38. 0x80, 0x00, 0x21, 0xb0, 0x14, 0x00, 0xdc, 0x00,
  39. 0x81, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  40. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  41. 0x00, 0x55, 0x00, 0x00, 0x7f, 0x00
  42. };
  43. static u8 init_1993_tab [] = {
  44. 0x00, 0x9c, 0x35, 0x80, 0x6a, 0x09, 0x72, 0x8c,
  45. 0x09, 0x6b, 0x00, 0x00, 0x4c, 0x08, 0x00, 0x00,
  46. 0x00, 0x81, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  47. 0x80, 0x40, 0x21, 0xb0, 0x00, 0x00, 0x00, 0x10,
  48. 0x81, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  49. 0x00, 0x00, 0x80, 0x80, 0x00, 0x00, 0x00, 0x00,
  50. 0x00, 0x55, 0x03, 0x00, 0x00, 0x00, 0x00, 0x03,
  51. 0x00, 0x00, 0x0e, 0x80, 0x00
  52. };
  53. static u8 init_1893_wtab[] =
  54. {
  55. 1,1,1,1,1,1,1,1, 1,1,0,0,1,1,0,0,
  56. 0,1,0,0,0,0,0,0, 1,0,1,1,0,0,0,1,
  57. 1,1,1,0,0,0,0,0, 0,0,1,1,0,0,0,0,
  58. 1,1,1,0,1,1
  59. };
  60. static u8 init_1993_wtab[] =
  61. {
  62. 1,1,1,1,1,1,1,1, 1,1,0,0,1,1,0,0,
  63. 0,1,0,0,0,0,0,0, 1,1,1,1,0,0,0,1,
  64. 1,1,1,0,0,0,0,0, 0,0,1,1,0,0,0,0,
  65. 1,1,1,0,1,1,1,1, 1,1,1,1,1
  66. };
  67. static int ves1x93_writereg (struct ves1x93_state* state, u8 reg, u8 data)
  68. {
  69. u8 buf [] = { 0x00, reg, data };
  70. struct i2c_msg msg = { .addr = state->config->demod_address, .flags = 0, .buf = buf, .len = 3 };
  71. int err;
  72. if ((err = i2c_transfer (state->i2c, &msg, 1)) != 1) {
  73. dprintk ("%s: writereg error (err == %i, reg == 0x%02x, data == 0x%02x)\n", __func__, err, reg, data);
  74. return -EREMOTEIO;
  75. }
  76. return 0;
  77. }
  78. static u8 ves1x93_readreg (struct ves1x93_state* state, u8 reg)
  79. {
  80. int ret;
  81. u8 b0 [] = { 0x00, reg };
  82. u8 b1 [] = { 0 };
  83. struct i2c_msg msg [] = { { .addr = state->config->demod_address, .flags = 0, .buf = b0, .len = 2 },
  84. { .addr = state->config->demod_address, .flags = I2C_M_RD, .buf = b1, .len = 1 } };
  85. ret = i2c_transfer (state->i2c, msg, 2);
  86. if (ret != 2) return ret;
  87. return b1[0];
  88. }
  89. static int ves1x93_clr_bit (struct ves1x93_state* state)
  90. {
  91. msleep(10);
  92. ves1x93_writereg (state, 0, state->init_1x93_tab[0] & 0xfe);
  93. ves1x93_writereg (state, 0, state->init_1x93_tab[0]);
  94. msleep(50);
  95. return 0;
  96. }
  97. static int ves1x93_set_inversion(struct ves1x93_state *state,
  98. enum fe_spectral_inversion inversion)
  99. {
  100. u8 val;
  101. /*
  102. * inversion on/off are interchanged because i and q seem to
  103. * be swapped on the hardware
  104. */
  105. switch (inversion) {
  106. case INVERSION_OFF:
  107. val = 0xc0;
  108. break;
  109. case INVERSION_ON:
  110. val = 0x80;
  111. break;
  112. case INVERSION_AUTO:
  113. val = 0x00;
  114. break;
  115. default:
  116. return -EINVAL;
  117. }
  118. return ves1x93_writereg (state, 0x0c, (state->init_1x93_tab[0x0c] & 0x3f) | val);
  119. }
  120. static int ves1x93_set_fec(struct ves1x93_state *state, enum fe_code_rate fec)
  121. {
  122. if (fec == FEC_AUTO)
  123. return ves1x93_writereg (state, 0x0d, 0x08);
  124. else if (fec < FEC_1_2 || fec > FEC_8_9)
  125. return -EINVAL;
  126. else
  127. return ves1x93_writereg (state, 0x0d, fec - FEC_1_2);
  128. }
  129. static enum fe_code_rate ves1x93_get_fec(struct ves1x93_state *state)
  130. {
  131. return FEC_1_2 + ((ves1x93_readreg (state, 0x0d) >> 4) & 0x7);
  132. }
  133. static int ves1x93_set_symbolrate (struct ves1x93_state* state, u32 srate)
  134. {
  135. u32 BDR;
  136. u32 ratio;
  137. u8 ADCONF, FCONF, FNR, AGCR;
  138. u32 BDRI;
  139. u32 tmp;
  140. u32 FIN;
  141. dprintk("%s: srate == %d\n", __func__, (unsigned int) srate);
  142. if (srate > state->config->xin/2)
  143. srate = state->config->xin/2;
  144. if (srate < 500000)
  145. srate = 500000;
  146. #define MUL (1UL<<26)
  147. FIN = (state->config->xin + 6000) >> 4;
  148. tmp = srate << 6;
  149. ratio = tmp / FIN;
  150. tmp = (tmp % FIN) << 8;
  151. ratio = (ratio << 8) + tmp / FIN;
  152. tmp = (tmp % FIN) << 8;
  153. ratio = (ratio << 8) + tmp / FIN;
  154. FNR = 0xff;
  155. if (ratio < MUL/3) FNR = 0;
  156. if (ratio < (MUL*11)/50) FNR = 1;
  157. if (ratio < MUL/6) FNR = 2;
  158. if (ratio < MUL/9) FNR = 3;
  159. if (ratio < MUL/12) FNR = 4;
  160. if (ratio < (MUL*11)/200) FNR = 5;
  161. if (ratio < MUL/24) FNR = 6;
  162. if (ratio < (MUL*27)/1000) FNR = 7;
  163. if (ratio < MUL/48) FNR = 8;
  164. if (ratio < (MUL*137)/10000) FNR = 9;
  165. if (FNR == 0xff) {
  166. ADCONF = 0x89;
  167. FCONF = 0x80;
  168. FNR = 0;
  169. } else {
  170. ADCONF = 0x81;
  171. FCONF = 0x88 | (FNR >> 1) | ((FNR & 0x01) << 5);
  172. /*FCONF = 0x80 | ((FNR & 0x01) << 5) | (((FNR > 1) & 0x03) << 3) | ((FNR >> 1) & 0x07);*/
  173. }
  174. BDR = (( (ratio << (FNR >> 1)) >> 4) + 1) >> 1;
  175. BDRI = ( ((FIN << 8) / ((srate << (FNR >> 1)) >> 2)) + 1) >> 1;
  176. dprintk("FNR= %d\n", FNR);
  177. dprintk("ratio= %08x\n", (unsigned int) ratio);
  178. dprintk("BDR= %08x\n", (unsigned int) BDR);
  179. dprintk("BDRI= %02x\n", (unsigned int) BDRI);
  180. if (BDRI > 0xff)
  181. BDRI = 0xff;
  182. ves1x93_writereg (state, 0x06, 0xff & BDR);
  183. ves1x93_writereg (state, 0x07, 0xff & (BDR >> 8));
  184. ves1x93_writereg (state, 0x08, 0x0f & (BDR >> 16));
  185. ves1x93_writereg (state, 0x09, BDRI);
  186. ves1x93_writereg (state, 0x20, ADCONF);
  187. ves1x93_writereg (state, 0x21, FCONF);
  188. AGCR = state->init_1x93_tab[0x05];
  189. if (state->config->invert_pwm)
  190. AGCR |= 0x20;
  191. if (srate < 6000000)
  192. AGCR |= 0x80;
  193. else
  194. AGCR &= ~0x80;
  195. ves1x93_writereg (state, 0x05, AGCR);
  196. /* ves1993 hates this, will lose lock */
  197. if (state->demod_type != DEMOD_VES1993)
  198. ves1x93_clr_bit (state);
  199. return 0;
  200. }
  201. static int ves1x93_init (struct dvb_frontend* fe)
  202. {
  203. struct ves1x93_state* state = fe->demodulator_priv;
  204. int i;
  205. int val;
  206. dprintk("%s: init chip\n", __func__);
  207. for (i = 0; i < state->tab_size; i++) {
  208. if (state->init_1x93_wtab[i]) {
  209. val = state->init_1x93_tab[i];
  210. if (state->config->invert_pwm && (i == 0x05)) val |= 0x20; /* invert PWM */
  211. ves1x93_writereg (state, i, val);
  212. }
  213. }
  214. return 0;
  215. }
  216. static int ves1x93_set_voltage(struct dvb_frontend *fe,
  217. enum fe_sec_voltage voltage)
  218. {
  219. struct ves1x93_state* state = fe->demodulator_priv;
  220. switch (voltage) {
  221. case SEC_VOLTAGE_13:
  222. return ves1x93_writereg (state, 0x1f, 0x20);
  223. case SEC_VOLTAGE_18:
  224. return ves1x93_writereg (state, 0x1f, 0x30);
  225. case SEC_VOLTAGE_OFF:
  226. return ves1x93_writereg (state, 0x1f, 0x00);
  227. default:
  228. return -EINVAL;
  229. }
  230. }
  231. static int ves1x93_read_status(struct dvb_frontend *fe,
  232. enum fe_status *status)
  233. {
  234. struct ves1x93_state* state = fe->demodulator_priv;
  235. u8 sync = ves1x93_readreg (state, 0x0e);
  236. /*
  237. * The ves1893 sometimes returns sync values that make no sense,
  238. * because, e.g., the SIGNAL bit is 0, while some of the higher
  239. * bits are 1 (and how can there be a CARRIER w/o a SIGNAL?).
  240. * Tests showed that the VITERBI and SYNC bits are returned
  241. * reliably, while the SIGNAL and CARRIER bits ar sometimes wrong.
  242. * If such a case occurs, we read the value again, until we get a
  243. * valid value.
  244. */
  245. int maxtry = 10; /* just for safety - let's not get stuck here */
  246. while ((sync & 0x03) != 0x03 && (sync & 0x0c) && maxtry--) {
  247. msleep(10);
  248. sync = ves1x93_readreg (state, 0x0e);
  249. }
  250. *status = 0;
  251. if (sync & 1)
  252. *status |= FE_HAS_SIGNAL;
  253. if (sync & 2)
  254. *status |= FE_HAS_CARRIER;
  255. if (sync & 4)
  256. *status |= FE_HAS_VITERBI;
  257. if (sync & 8)
  258. *status |= FE_HAS_SYNC;
  259. if ((sync & 0x1f) == 0x1f)
  260. *status |= FE_HAS_LOCK;
  261. return 0;
  262. }
  263. static int ves1x93_read_ber(struct dvb_frontend* fe, u32* ber)
  264. {
  265. struct ves1x93_state* state = fe->demodulator_priv;
  266. *ber = ves1x93_readreg (state, 0x15);
  267. *ber |= (ves1x93_readreg (state, 0x16) << 8);
  268. *ber |= ((ves1x93_readreg (state, 0x17) & 0x0F) << 16);
  269. *ber *= 10;
  270. return 0;
  271. }
  272. static int ves1x93_read_signal_strength(struct dvb_frontend* fe, u16* strength)
  273. {
  274. struct ves1x93_state* state = fe->demodulator_priv;
  275. u8 signal = ~ves1x93_readreg (state, 0x0b);
  276. *strength = (signal << 8) | signal;
  277. return 0;
  278. }
  279. static int ves1x93_read_snr(struct dvb_frontend* fe, u16* snr)
  280. {
  281. struct ves1x93_state* state = fe->demodulator_priv;
  282. u8 _snr = ~ves1x93_readreg (state, 0x1c);
  283. *snr = (_snr << 8) | _snr;
  284. return 0;
  285. }
  286. static int ves1x93_read_ucblocks(struct dvb_frontend* fe, u32* ucblocks)
  287. {
  288. struct ves1x93_state* state = fe->demodulator_priv;
  289. *ucblocks = ves1x93_readreg (state, 0x18) & 0x7f;
  290. if (*ucblocks == 0x7f)
  291. *ucblocks = 0xffffffff; /* counter overflow... */
  292. ves1x93_writereg (state, 0x18, 0x00); /* reset the counter */
  293. ves1x93_writereg (state, 0x18, 0x80); /* dto. */
  294. return 0;
  295. }
  296. static int ves1x93_set_frontend(struct dvb_frontend *fe)
  297. {
  298. struct dtv_frontend_properties *p = &fe->dtv_property_cache;
  299. struct ves1x93_state* state = fe->demodulator_priv;
  300. if (fe->ops.tuner_ops.set_params) {
  301. fe->ops.tuner_ops.set_params(fe);
  302. if (fe->ops.i2c_gate_ctrl) fe->ops.i2c_gate_ctrl(fe, 0);
  303. }
  304. ves1x93_set_inversion (state, p->inversion);
  305. ves1x93_set_fec(state, p->fec_inner);
  306. ves1x93_set_symbolrate(state, p->symbol_rate);
  307. state->inversion = p->inversion;
  308. state->frequency = p->frequency;
  309. return 0;
  310. }
  311. static int ves1x93_get_frontend(struct dvb_frontend *fe,
  312. struct dtv_frontend_properties *p)
  313. {
  314. struct ves1x93_state* state = fe->demodulator_priv;
  315. int afc;
  316. afc = ((int)((char)(ves1x93_readreg (state, 0x0a) << 1)))/2;
  317. afc = (afc * (int)(p->symbol_rate/1000/8))/16;
  318. p->frequency = state->frequency - afc;
  319. /*
  320. * inversion indicator is only valid
  321. * if auto inversion was used
  322. */
  323. if (state->inversion == INVERSION_AUTO)
  324. p->inversion = (ves1x93_readreg (state, 0x0f) & 2) ?
  325. INVERSION_OFF : INVERSION_ON;
  326. p->fec_inner = ves1x93_get_fec(state);
  327. /* XXX FIXME: timing offset !! */
  328. return 0;
  329. }
  330. static int ves1x93_sleep(struct dvb_frontend* fe)
  331. {
  332. struct ves1x93_state* state = fe->demodulator_priv;
  333. return ves1x93_writereg (state, 0x00, 0x08);
  334. }
  335. static void ves1x93_release(struct dvb_frontend* fe)
  336. {
  337. struct ves1x93_state* state = fe->demodulator_priv;
  338. kfree(state);
  339. }
  340. static int ves1x93_i2c_gate_ctrl(struct dvb_frontend* fe, int enable)
  341. {
  342. struct ves1x93_state* state = fe->demodulator_priv;
  343. if (enable) {
  344. return ves1x93_writereg(state, 0x00, 0x11);
  345. } else {
  346. return ves1x93_writereg(state, 0x00, 0x01);
  347. }
  348. }
  349. static const struct dvb_frontend_ops ves1x93_ops;
  350. struct dvb_frontend* ves1x93_attach(const struct ves1x93_config* config,
  351. struct i2c_adapter* i2c)
  352. {
  353. struct ves1x93_state* state = NULL;
  354. u8 identity;
  355. /* allocate memory for the internal state */
  356. state = kzalloc(sizeof(struct ves1x93_state), GFP_KERNEL);
  357. if (state == NULL) goto error;
  358. /* setup the state */
  359. state->config = config;
  360. state->i2c = i2c;
  361. state->inversion = INVERSION_OFF;
  362. /* check if the demod is there + identify it */
  363. identity = ves1x93_readreg(state, 0x1e);
  364. switch (identity) {
  365. case 0xdc: /* VES1893A rev1 */
  366. printk("ves1x93: Detected ves1893a rev1\n");
  367. state->demod_type = DEMOD_VES1893;
  368. state->init_1x93_tab = init_1893_tab;
  369. state->init_1x93_wtab = init_1893_wtab;
  370. state->tab_size = sizeof(init_1893_tab);
  371. break;
  372. case 0xdd: /* VES1893A rev2 */
  373. printk("ves1x93: Detected ves1893a rev2\n");
  374. state->demod_type = DEMOD_VES1893;
  375. state->init_1x93_tab = init_1893_tab;
  376. state->init_1x93_wtab = init_1893_wtab;
  377. state->tab_size = sizeof(init_1893_tab);
  378. break;
  379. case 0xde: /* VES1993 */
  380. printk("ves1x93: Detected ves1993\n");
  381. state->demod_type = DEMOD_VES1993;
  382. state->init_1x93_tab = init_1993_tab;
  383. state->init_1x93_wtab = init_1993_wtab;
  384. state->tab_size = sizeof(init_1993_tab);
  385. break;
  386. default:
  387. goto error;
  388. }
  389. /* create dvb_frontend */
  390. memcpy(&state->frontend.ops, &ves1x93_ops, sizeof(struct dvb_frontend_ops));
  391. state->frontend.demodulator_priv = state;
  392. return &state->frontend;
  393. error:
  394. kfree(state);
  395. return NULL;
  396. }
  397. static const struct dvb_frontend_ops ves1x93_ops = {
  398. .delsys = { SYS_DVBS },
  399. .info = {
  400. .name = "VLSI VES1x93 DVB-S",
  401. .frequency_min_hz = 950 * MHz,
  402. .frequency_max_hz = 2150 * MHz,
  403. .frequency_stepsize_hz = 125 * kHz,
  404. .frequency_tolerance_hz = 29500 * kHz,
  405. .symbol_rate_min = 1000000,
  406. .symbol_rate_max = 45000000,
  407. /* .symbol_rate_tolerance = ???,*/
  408. .caps = FE_CAN_INVERSION_AUTO |
  409. FE_CAN_FEC_1_2 | FE_CAN_FEC_2_3 | FE_CAN_FEC_3_4 |
  410. FE_CAN_FEC_5_6 | FE_CAN_FEC_7_8 | FE_CAN_FEC_AUTO |
  411. FE_CAN_QPSK
  412. },
  413. .release = ves1x93_release,
  414. .init = ves1x93_init,
  415. .sleep = ves1x93_sleep,
  416. .i2c_gate_ctrl = ves1x93_i2c_gate_ctrl,
  417. .set_frontend = ves1x93_set_frontend,
  418. .get_frontend = ves1x93_get_frontend,
  419. .read_status = ves1x93_read_status,
  420. .read_ber = ves1x93_read_ber,
  421. .read_signal_strength = ves1x93_read_signal_strength,
  422. .read_snr = ves1x93_read_snr,
  423. .read_ucblocks = ves1x93_read_ucblocks,
  424. .set_voltage = ves1x93_set_voltage,
  425. };
  426. module_param(debug, int, 0644);
  427. MODULE_DESCRIPTION("VLSI VES1x93 DVB-S Demodulator driver");
  428. MODULE_AUTHOR("Ralph Metzler");
  429. MODULE_LICENSE("GPL");
  430. EXPORT_SYMBOL(ves1x93_attach);