ves1x93.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556
  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. enum fe_spectral_inversion 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,
  108. enum fe_spectral_inversion inversion)
  109. {
  110. u8 val;
  111. /*
  112. * inversion on/off are interchanged because i and q seem to
  113. * be swapped on the hardware
  114. */
  115. switch (inversion) {
  116. case INVERSION_OFF:
  117. val = 0xc0;
  118. break;
  119. case INVERSION_ON:
  120. val = 0x80;
  121. break;
  122. case INVERSION_AUTO:
  123. val = 0x00;
  124. break;
  125. default:
  126. return -EINVAL;
  127. }
  128. return ves1x93_writereg (state, 0x0c, (state->init_1x93_tab[0x0c] & 0x3f) | val);
  129. }
  130. static int ves1x93_set_fec(struct ves1x93_state *state, enum fe_code_rate fec)
  131. {
  132. if (fec == FEC_AUTO)
  133. return ves1x93_writereg (state, 0x0d, 0x08);
  134. else if (fec < FEC_1_2 || fec > FEC_8_9)
  135. return -EINVAL;
  136. else
  137. return ves1x93_writereg (state, 0x0d, fec - FEC_1_2);
  138. }
  139. static enum fe_code_rate ves1x93_get_fec(struct ves1x93_state *state)
  140. {
  141. return FEC_1_2 + ((ves1x93_readreg (state, 0x0d) >> 4) & 0x7);
  142. }
  143. static int ves1x93_set_symbolrate (struct ves1x93_state* state, u32 srate)
  144. {
  145. u32 BDR;
  146. u32 ratio;
  147. u8 ADCONF, FCONF, FNR, AGCR;
  148. u32 BDRI;
  149. u32 tmp;
  150. u32 FIN;
  151. dprintk("%s: srate == %d\n", __func__, (unsigned int) srate);
  152. if (srate > state->config->xin/2)
  153. srate = state->config->xin/2;
  154. if (srate < 500000)
  155. srate = 500000;
  156. #define MUL (1UL<<26)
  157. FIN = (state->config->xin + 6000) >> 4;
  158. tmp = srate << 6;
  159. ratio = tmp / FIN;
  160. tmp = (tmp % FIN) << 8;
  161. ratio = (ratio << 8) + tmp / FIN;
  162. tmp = (tmp % FIN) << 8;
  163. ratio = (ratio << 8) + tmp / FIN;
  164. FNR = 0xff;
  165. if (ratio < MUL/3) FNR = 0;
  166. if (ratio < (MUL*11)/50) FNR = 1;
  167. if (ratio < MUL/6) FNR = 2;
  168. if (ratio < MUL/9) FNR = 3;
  169. if (ratio < MUL/12) FNR = 4;
  170. if (ratio < (MUL*11)/200) FNR = 5;
  171. if (ratio < MUL/24) FNR = 6;
  172. if (ratio < (MUL*27)/1000) FNR = 7;
  173. if (ratio < MUL/48) FNR = 8;
  174. if (ratio < (MUL*137)/10000) FNR = 9;
  175. if (FNR == 0xff) {
  176. ADCONF = 0x89;
  177. FCONF = 0x80;
  178. FNR = 0;
  179. } else {
  180. ADCONF = 0x81;
  181. FCONF = 0x88 | (FNR >> 1) | ((FNR & 0x01) << 5);
  182. /*FCONF = 0x80 | ((FNR & 0x01) << 5) | (((FNR > 1) & 0x03) << 3) | ((FNR >> 1) & 0x07);*/
  183. }
  184. BDR = (( (ratio << (FNR >> 1)) >> 4) + 1) >> 1;
  185. BDRI = ( ((FIN << 8) / ((srate << (FNR >> 1)) >> 2)) + 1) >> 1;
  186. dprintk("FNR= %d\n", FNR);
  187. dprintk("ratio= %08x\n", (unsigned int) ratio);
  188. dprintk("BDR= %08x\n", (unsigned int) BDR);
  189. dprintk("BDRI= %02x\n", (unsigned int) BDRI);
  190. if (BDRI > 0xff)
  191. BDRI = 0xff;
  192. ves1x93_writereg (state, 0x06, 0xff & BDR);
  193. ves1x93_writereg (state, 0x07, 0xff & (BDR >> 8));
  194. ves1x93_writereg (state, 0x08, 0x0f & (BDR >> 16));
  195. ves1x93_writereg (state, 0x09, BDRI);
  196. ves1x93_writereg (state, 0x20, ADCONF);
  197. ves1x93_writereg (state, 0x21, FCONF);
  198. AGCR = state->init_1x93_tab[0x05];
  199. if (state->config->invert_pwm)
  200. AGCR |= 0x20;
  201. if (srate < 6000000)
  202. AGCR |= 0x80;
  203. else
  204. AGCR &= ~0x80;
  205. ves1x93_writereg (state, 0x05, AGCR);
  206. /* ves1993 hates this, will lose lock */
  207. if (state->demod_type != DEMOD_VES1993)
  208. ves1x93_clr_bit (state);
  209. return 0;
  210. }
  211. static int ves1x93_init (struct dvb_frontend* fe)
  212. {
  213. struct ves1x93_state* state = fe->demodulator_priv;
  214. int i;
  215. int val;
  216. dprintk("%s: init chip\n", __func__);
  217. for (i = 0; i < state->tab_size; i++) {
  218. if (state->init_1x93_wtab[i]) {
  219. val = state->init_1x93_tab[i];
  220. if (state->config->invert_pwm && (i == 0x05)) val |= 0x20; /* invert PWM */
  221. ves1x93_writereg (state, i, val);
  222. }
  223. }
  224. return 0;
  225. }
  226. static int ves1x93_set_voltage(struct dvb_frontend *fe,
  227. enum fe_sec_voltage voltage)
  228. {
  229. struct ves1x93_state* state = fe->demodulator_priv;
  230. switch (voltage) {
  231. case SEC_VOLTAGE_13:
  232. return ves1x93_writereg (state, 0x1f, 0x20);
  233. case SEC_VOLTAGE_18:
  234. return ves1x93_writereg (state, 0x1f, 0x30);
  235. case SEC_VOLTAGE_OFF:
  236. return ves1x93_writereg (state, 0x1f, 0x00);
  237. default:
  238. return -EINVAL;
  239. }
  240. }
  241. static int ves1x93_read_status(struct dvb_frontend *fe,
  242. enum fe_status *status)
  243. {
  244. struct ves1x93_state* state = fe->demodulator_priv;
  245. u8 sync = ves1x93_readreg (state, 0x0e);
  246. /*
  247. * The ves1893 sometimes returns sync values that make no sense,
  248. * because, e.g., the SIGNAL bit is 0, while some of the higher
  249. * bits are 1 (and how can there be a CARRIER w/o a SIGNAL?).
  250. * Tests showed that the VITERBI and SYNC bits are returned
  251. * reliably, while the SIGNAL and CARRIER bits ar sometimes wrong.
  252. * If such a case occurs, we read the value again, until we get a
  253. * valid value.
  254. */
  255. int maxtry = 10; /* just for safety - let's not get stuck here */
  256. while ((sync & 0x03) != 0x03 && (sync & 0x0c) && maxtry--) {
  257. msleep(10);
  258. sync = ves1x93_readreg (state, 0x0e);
  259. }
  260. *status = 0;
  261. if (sync & 1)
  262. *status |= FE_HAS_SIGNAL;
  263. if (sync & 2)
  264. *status |= FE_HAS_CARRIER;
  265. if (sync & 4)
  266. *status |= FE_HAS_VITERBI;
  267. if (sync & 8)
  268. *status |= FE_HAS_SYNC;
  269. if ((sync & 0x1f) == 0x1f)
  270. *status |= FE_HAS_LOCK;
  271. return 0;
  272. }
  273. static int ves1x93_read_ber(struct dvb_frontend* fe, u32* ber)
  274. {
  275. struct ves1x93_state* state = fe->demodulator_priv;
  276. *ber = ves1x93_readreg (state, 0x15);
  277. *ber |= (ves1x93_readreg (state, 0x16) << 8);
  278. *ber |= ((ves1x93_readreg (state, 0x17) & 0x0F) << 16);
  279. *ber *= 10;
  280. return 0;
  281. }
  282. static int ves1x93_read_signal_strength(struct dvb_frontend* fe, u16* strength)
  283. {
  284. struct ves1x93_state* state = fe->demodulator_priv;
  285. u8 signal = ~ves1x93_readreg (state, 0x0b);
  286. *strength = (signal << 8) | signal;
  287. return 0;
  288. }
  289. static int ves1x93_read_snr(struct dvb_frontend* fe, u16* snr)
  290. {
  291. struct ves1x93_state* state = fe->demodulator_priv;
  292. u8 _snr = ~ves1x93_readreg (state, 0x1c);
  293. *snr = (_snr << 8) | _snr;
  294. return 0;
  295. }
  296. static int ves1x93_read_ucblocks(struct dvb_frontend* fe, u32* ucblocks)
  297. {
  298. struct ves1x93_state* state = fe->demodulator_priv;
  299. *ucblocks = ves1x93_readreg (state, 0x18) & 0x7f;
  300. if (*ucblocks == 0x7f)
  301. *ucblocks = 0xffffffff; /* counter overflow... */
  302. ves1x93_writereg (state, 0x18, 0x00); /* reset the counter */
  303. ves1x93_writereg (state, 0x18, 0x80); /* dto. */
  304. return 0;
  305. }
  306. static int ves1x93_set_frontend(struct dvb_frontend *fe)
  307. {
  308. struct dtv_frontend_properties *p = &fe->dtv_property_cache;
  309. struct ves1x93_state* state = fe->demodulator_priv;
  310. if (fe->ops.tuner_ops.set_params) {
  311. fe->ops.tuner_ops.set_params(fe);
  312. if (fe->ops.i2c_gate_ctrl) fe->ops.i2c_gate_ctrl(fe, 0);
  313. }
  314. ves1x93_set_inversion (state, p->inversion);
  315. ves1x93_set_fec(state, p->fec_inner);
  316. ves1x93_set_symbolrate(state, p->symbol_rate);
  317. state->inversion = p->inversion;
  318. state->frequency = p->frequency;
  319. return 0;
  320. }
  321. static int ves1x93_get_frontend(struct dvb_frontend *fe)
  322. {
  323. struct dtv_frontend_properties *p = &fe->dtv_property_cache;
  324. struct ves1x93_state* state = fe->demodulator_priv;
  325. int afc;
  326. afc = ((int)((char)(ves1x93_readreg (state, 0x0a) << 1)))/2;
  327. afc = (afc * (int)(p->symbol_rate/1000/8))/16;
  328. p->frequency = state->frequency - afc;
  329. /*
  330. * inversion indicator is only valid
  331. * if auto inversion was used
  332. */
  333. if (state->inversion == INVERSION_AUTO)
  334. p->inversion = (ves1x93_readreg (state, 0x0f) & 2) ?
  335. INVERSION_OFF : INVERSION_ON;
  336. p->fec_inner = ves1x93_get_fec(state);
  337. /* XXX FIXME: timing offset !! */
  338. return 0;
  339. }
  340. static int ves1x93_sleep(struct dvb_frontend* fe)
  341. {
  342. struct ves1x93_state* state = fe->demodulator_priv;
  343. return ves1x93_writereg (state, 0x00, 0x08);
  344. }
  345. static void ves1x93_release(struct dvb_frontend* fe)
  346. {
  347. struct ves1x93_state* state = fe->demodulator_priv;
  348. kfree(state);
  349. }
  350. static int ves1x93_i2c_gate_ctrl(struct dvb_frontend* fe, int enable)
  351. {
  352. struct ves1x93_state* state = fe->demodulator_priv;
  353. if (enable) {
  354. return ves1x93_writereg(state, 0x00, 0x11);
  355. } else {
  356. return ves1x93_writereg(state, 0x00, 0x01);
  357. }
  358. }
  359. static struct dvb_frontend_ops ves1x93_ops;
  360. struct dvb_frontend* ves1x93_attach(const struct ves1x93_config* config,
  361. struct i2c_adapter* i2c)
  362. {
  363. struct ves1x93_state* state = NULL;
  364. u8 identity;
  365. /* allocate memory for the internal state */
  366. state = kzalloc(sizeof(struct ves1x93_state), GFP_KERNEL);
  367. if (state == NULL) goto error;
  368. /* setup the state */
  369. state->config = config;
  370. state->i2c = i2c;
  371. state->inversion = INVERSION_OFF;
  372. /* check if the demod is there + identify it */
  373. identity = ves1x93_readreg(state, 0x1e);
  374. switch (identity) {
  375. case 0xdc: /* VES1893A rev1 */
  376. printk("ves1x93: Detected ves1893a rev1\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 0xdd: /* VES1893A rev2 */
  383. printk("ves1x93: Detected ves1893a rev2\n");
  384. state->demod_type = DEMOD_VES1893;
  385. state->init_1x93_tab = init_1893_tab;
  386. state->init_1x93_wtab = init_1893_wtab;
  387. state->tab_size = sizeof(init_1893_tab);
  388. break;
  389. case 0xde: /* VES1993 */
  390. printk("ves1x93: Detected ves1993\n");
  391. state->demod_type = DEMOD_VES1993;
  392. state->init_1x93_tab = init_1993_tab;
  393. state->init_1x93_wtab = init_1993_wtab;
  394. state->tab_size = sizeof(init_1993_tab);
  395. break;
  396. default:
  397. goto error;
  398. }
  399. /* create dvb_frontend */
  400. memcpy(&state->frontend.ops, &ves1x93_ops, sizeof(struct dvb_frontend_ops));
  401. state->frontend.demodulator_priv = state;
  402. return &state->frontend;
  403. error:
  404. kfree(state);
  405. return NULL;
  406. }
  407. static struct dvb_frontend_ops ves1x93_ops = {
  408. .delsys = { SYS_DVBS },
  409. .info = {
  410. .name = "VLSI VES1x93 DVB-S",
  411. .frequency_min = 950000,
  412. .frequency_max = 2150000,
  413. .frequency_stepsize = 125, /* kHz for QPSK frontends */
  414. .frequency_tolerance = 29500,
  415. .symbol_rate_min = 1000000,
  416. .symbol_rate_max = 45000000,
  417. /* .symbol_rate_tolerance = ???,*/
  418. .caps = FE_CAN_INVERSION_AUTO |
  419. FE_CAN_FEC_1_2 | FE_CAN_FEC_2_3 | FE_CAN_FEC_3_4 |
  420. FE_CAN_FEC_5_6 | FE_CAN_FEC_7_8 | FE_CAN_FEC_AUTO |
  421. FE_CAN_QPSK
  422. },
  423. .release = ves1x93_release,
  424. .init = ves1x93_init,
  425. .sleep = ves1x93_sleep,
  426. .i2c_gate_ctrl = ves1x93_i2c_gate_ctrl,
  427. .set_frontend = ves1x93_set_frontend,
  428. .get_frontend = ves1x93_get_frontend,
  429. .read_status = ves1x93_read_status,
  430. .read_ber = ves1x93_read_ber,
  431. .read_signal_strength = ves1x93_read_signal_strength,
  432. .read_snr = ves1x93_read_snr,
  433. .read_ucblocks = ves1x93_read_ucblocks,
  434. .set_voltage = ves1x93_set_voltage,
  435. };
  436. module_param(debug, int, 0644);
  437. MODULE_DESCRIPTION("VLSI VES1x93 DVB-S Demodulator driver");
  438. MODULE_AUTHOR("Ralph Metzler");
  439. MODULE_LICENSE("GPL");
  440. EXPORT_SYMBOL(ves1x93_attach);