ves1x93.c 14 KB

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