ves1x93.c 14 KB

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