ves1x93.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553
  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. u32 frequency;
  39. };
  40. static int debug;
  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", __func__, 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", __func__, (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", __func__);
  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. return 0;
  224. }
  225. static int ves1x93_set_voltage (struct dvb_frontend* fe, fe_sec_voltage_t voltage)
  226. {
  227. struct ves1x93_state* state = fe->demodulator_priv;
  228. switch (voltage) {
  229. case SEC_VOLTAGE_13:
  230. return ves1x93_writereg (state, 0x1f, 0x20);
  231. case SEC_VOLTAGE_18:
  232. return ves1x93_writereg (state, 0x1f, 0x30);
  233. case SEC_VOLTAGE_OFF:
  234. return ves1x93_writereg (state, 0x1f, 0x00);
  235. default:
  236. return -EINVAL;
  237. }
  238. }
  239. static int ves1x93_read_status(struct dvb_frontend* fe, fe_status_t* status)
  240. {
  241. struct ves1x93_state* state = fe->demodulator_priv;
  242. u8 sync = ves1x93_readreg (state, 0x0e);
  243. /*
  244. * The ves1893 sometimes returns sync values that make no sense,
  245. * because, e.g., the SIGNAL bit is 0, while some of the higher
  246. * bits are 1 (and how can there be a CARRIER w/o a SIGNAL?).
  247. * Tests showed that the VITERBI and SYNC bits are returned
  248. * reliably, while the SIGNAL and CARRIER bits ar sometimes wrong.
  249. * If such a case occurs, we read the value again, until we get a
  250. * valid value.
  251. */
  252. int maxtry = 10; /* just for safety - let's not get stuck here */
  253. while ((sync & 0x03) != 0x03 && (sync & 0x0c) && maxtry--) {
  254. msleep(10);
  255. sync = ves1x93_readreg (state, 0x0e);
  256. }
  257. *status = 0;
  258. if (sync & 1)
  259. *status |= FE_HAS_SIGNAL;
  260. if (sync & 2)
  261. *status |= FE_HAS_CARRIER;
  262. if (sync & 4)
  263. *status |= FE_HAS_VITERBI;
  264. if (sync & 8)
  265. *status |= FE_HAS_SYNC;
  266. if ((sync & 0x1f) == 0x1f)
  267. *status |= FE_HAS_LOCK;
  268. return 0;
  269. }
  270. static int ves1x93_read_ber(struct dvb_frontend* fe, u32* ber)
  271. {
  272. struct ves1x93_state* state = fe->demodulator_priv;
  273. *ber = ves1x93_readreg (state, 0x15);
  274. *ber |= (ves1x93_readreg (state, 0x16) << 8);
  275. *ber |= ((ves1x93_readreg (state, 0x17) & 0x0F) << 16);
  276. *ber *= 10;
  277. return 0;
  278. }
  279. static int ves1x93_read_signal_strength(struct dvb_frontend* fe, u16* strength)
  280. {
  281. struct ves1x93_state* state = fe->demodulator_priv;
  282. u8 signal = ~ves1x93_readreg (state, 0x0b);
  283. *strength = (signal << 8) | signal;
  284. return 0;
  285. }
  286. static int ves1x93_read_snr(struct dvb_frontend* fe, u16* snr)
  287. {
  288. struct ves1x93_state* state = fe->demodulator_priv;
  289. u8 _snr = ~ves1x93_readreg (state, 0x1c);
  290. *snr = (_snr << 8) | _snr;
  291. return 0;
  292. }
  293. static int ves1x93_read_ucblocks(struct dvb_frontend* fe, u32* ucblocks)
  294. {
  295. struct ves1x93_state* state = fe->demodulator_priv;
  296. *ucblocks = ves1x93_readreg (state, 0x18) & 0x7f;
  297. if (*ucblocks == 0x7f)
  298. *ucblocks = 0xffffffff; /* counter overflow... */
  299. ves1x93_writereg (state, 0x18, 0x00); /* reset the counter */
  300. ves1x93_writereg (state, 0x18, 0x80); /* dto. */
  301. return 0;
  302. }
  303. static int ves1x93_set_frontend(struct dvb_frontend *fe)
  304. {
  305. struct dtv_frontend_properties *p = &fe->dtv_property_cache;
  306. struct ves1x93_state* state = fe->demodulator_priv;
  307. if (fe->ops.tuner_ops.set_params) {
  308. fe->ops.tuner_ops.set_params(fe);
  309. if (fe->ops.i2c_gate_ctrl) fe->ops.i2c_gate_ctrl(fe, 0);
  310. }
  311. ves1x93_set_inversion (state, p->inversion);
  312. ves1x93_set_fec(state, p->fec_inner);
  313. ves1x93_set_symbolrate(state, p->symbol_rate);
  314. state->inversion = p->inversion;
  315. state->frequency = p->frequency;
  316. return 0;
  317. }
  318. static int ves1x93_get_frontend(struct dvb_frontend *fe)
  319. {
  320. struct dtv_frontend_properties *p = &fe->dtv_property_cache;
  321. struct ves1x93_state* state = fe->demodulator_priv;
  322. int afc;
  323. afc = ((int)((char)(ves1x93_readreg (state, 0x0a) << 1)))/2;
  324. afc = (afc * (int)(p->symbol_rate/1000/8))/16;
  325. p->frequency = state->frequency - afc;
  326. /*
  327. * inversion indicator is only valid
  328. * if auto inversion was used
  329. */
  330. if (state->inversion == INVERSION_AUTO)
  331. p->inversion = (ves1x93_readreg (state, 0x0f) & 2) ?
  332. INVERSION_OFF : INVERSION_ON;
  333. p->fec_inner = ves1x93_get_fec(state);
  334. /* XXX FIXME: timing offset !! */
  335. return 0;
  336. }
  337. static int ves1x93_sleep(struct dvb_frontend* fe)
  338. {
  339. struct ves1x93_state* state = fe->demodulator_priv;
  340. return ves1x93_writereg (state, 0x00, 0x08);
  341. }
  342. static void ves1x93_release(struct dvb_frontend* fe)
  343. {
  344. struct ves1x93_state* state = fe->demodulator_priv;
  345. kfree(state);
  346. }
  347. static int ves1x93_i2c_gate_ctrl(struct dvb_frontend* fe, int enable)
  348. {
  349. struct ves1x93_state* state = fe->demodulator_priv;
  350. if (enable) {
  351. return ves1x93_writereg(state, 0x00, 0x11);
  352. } else {
  353. return ves1x93_writereg(state, 0x00, 0x01);
  354. }
  355. }
  356. static struct dvb_frontend_ops ves1x93_ops;
  357. struct dvb_frontend* ves1x93_attach(const struct ves1x93_config* config,
  358. struct i2c_adapter* i2c)
  359. {
  360. struct ves1x93_state* state = NULL;
  361. u8 identity;
  362. /* allocate memory for the internal state */
  363. state = kzalloc(sizeof(struct ves1x93_state), GFP_KERNEL);
  364. if (state == NULL) goto error;
  365. /* setup the state */
  366. state->config = config;
  367. state->i2c = i2c;
  368. state->inversion = INVERSION_OFF;
  369. /* check if the demod is there + identify it */
  370. identity = ves1x93_readreg(state, 0x1e);
  371. switch (identity) {
  372. case 0xdc: /* VES1893A rev1 */
  373. printk("ves1x93: Detected ves1893a rev1\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 0xdd: /* VES1893A rev2 */
  380. printk("ves1x93: Detected ves1893a rev2\n");
  381. state->demod_type = DEMOD_VES1893;
  382. state->init_1x93_tab = init_1893_tab;
  383. state->init_1x93_wtab = init_1893_wtab;
  384. state->tab_size = sizeof(init_1893_tab);
  385. break;
  386. case 0xde: /* VES1993 */
  387. printk("ves1x93: Detected ves1993\n");
  388. state->demod_type = DEMOD_VES1993;
  389. state->init_1x93_tab = init_1993_tab;
  390. state->init_1x93_wtab = init_1993_wtab;
  391. state->tab_size = sizeof(init_1993_tab);
  392. break;
  393. default:
  394. goto error;
  395. }
  396. /* create dvb_frontend */
  397. memcpy(&state->frontend.ops, &ves1x93_ops, sizeof(struct dvb_frontend_ops));
  398. state->frontend.demodulator_priv = state;
  399. return &state->frontend;
  400. error:
  401. kfree(state);
  402. return NULL;
  403. }
  404. static struct dvb_frontend_ops ves1x93_ops = {
  405. .delsys = { SYS_DVBS },
  406. .info = {
  407. .name = "VLSI VES1x93 DVB-S",
  408. .frequency_min = 950000,
  409. .frequency_max = 2150000,
  410. .frequency_stepsize = 125, /* kHz for QPSK frontends */
  411. .frequency_tolerance = 29500,
  412. .symbol_rate_min = 1000000,
  413. .symbol_rate_max = 45000000,
  414. /* .symbol_rate_tolerance = ???,*/
  415. .caps = FE_CAN_INVERSION_AUTO |
  416. FE_CAN_FEC_1_2 | FE_CAN_FEC_2_3 | FE_CAN_FEC_3_4 |
  417. FE_CAN_FEC_5_6 | FE_CAN_FEC_7_8 | FE_CAN_FEC_AUTO |
  418. FE_CAN_QPSK
  419. },
  420. .release = ves1x93_release,
  421. .init = ves1x93_init,
  422. .sleep = ves1x93_sleep,
  423. .i2c_gate_ctrl = ves1x93_i2c_gate_ctrl,
  424. .set_frontend = ves1x93_set_frontend,
  425. .get_frontend = ves1x93_get_frontend,
  426. .read_status = ves1x93_read_status,
  427. .read_ber = ves1x93_read_ber,
  428. .read_signal_strength = ves1x93_read_signal_strength,
  429. .read_snr = ves1x93_read_snr,
  430. .read_ucblocks = ves1x93_read_ucblocks,
  431. .set_voltage = ves1x93_set_voltage,
  432. };
  433. module_param(debug, int, 0644);
  434. MODULE_DESCRIPTION("VLSI VES1x93 DVB-S Demodulator driver");
  435. MODULE_AUTHOR("Ralph Metzler");
  436. MODULE_LICENSE("GPL");
  437. EXPORT_SYMBOL(ves1x93_attach);