cx24123.c 27 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036
  1. /*
  2. Conexant cx24123/cx24109 - DVB QPSK Satellite demod/tuner driver
  3. Copyright (C) 2005 Steven Toth <stoth@hauppauge.com>
  4. Support for KWorld DVB-S 100 by Vadim Catana <skystar@moldova.cc>
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program; if not, write to the Free Software
  15. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  16. */
  17. #include <linux/slab.h>
  18. #include <linux/kernel.h>
  19. #include <linux/module.h>
  20. #include <linux/init.h>
  21. #include "dvb_frontend.h"
  22. #include "cx24123.h"
  23. #define XTAL 10111000
  24. static int force_band;
  25. static int debug;
  26. #define dprintk(args...) \
  27. do { \
  28. if (debug) printk (KERN_DEBUG "cx24123: " args); \
  29. } while (0)
  30. struct cx24123_state
  31. {
  32. struct i2c_adapter* i2c;
  33. const struct cx24123_config* config;
  34. struct dvb_frontend frontend;
  35. /* Some PLL specifics for tuning */
  36. u32 VCAarg;
  37. u32 VGAarg;
  38. u32 bandselectarg;
  39. u32 pllarg;
  40. u32 FILTune;
  41. /* The Demod/Tuner can't easily provide these, we cache them */
  42. u32 currentfreq;
  43. u32 currentsymbolrate;
  44. };
  45. /* Various tuner defaults need to be established for a given symbol rate Sps */
  46. static struct
  47. {
  48. u32 symbolrate_low;
  49. u32 symbolrate_high;
  50. u32 VCAprogdata;
  51. u32 VGAprogdata;
  52. u32 FILTune;
  53. } cx24123_AGC_vals[] =
  54. {
  55. {
  56. .symbolrate_low = 1000000,
  57. .symbolrate_high = 4999999,
  58. /* the specs recommend other values for VGA offsets,
  59. but tests show they are wrong */
  60. .VGAprogdata = (1 << 19) | (0x180 << 9) | 0x1e0,
  61. .VCAprogdata = (2 << 19) | (0x07 << 9) | 0x07,
  62. .FILTune = 0x27f /* 0.41 V */
  63. },
  64. {
  65. .symbolrate_low = 5000000,
  66. .symbolrate_high = 14999999,
  67. .VGAprogdata = (1 << 19) | (0x180 << 9) | 0x1e0,
  68. .VCAprogdata = (2 << 19) | (0x07 << 9) | 0x1f,
  69. .FILTune = 0x317 /* 0.90 V */
  70. },
  71. {
  72. .symbolrate_low = 15000000,
  73. .symbolrate_high = 45000000,
  74. .VGAprogdata = (1 << 19) | (0x100 << 9) | 0x180,
  75. .VCAprogdata = (2 << 19) | (0x07 << 9) | 0x3f,
  76. .FILTune = 0x145 /* 2.70 V */
  77. },
  78. };
  79. /*
  80. * Various tuner defaults need to be established for a given frequency kHz.
  81. * fixme: The bounds on the bands do not match the doc in real life.
  82. * fixme: Some of them have been moved, other might need adjustment.
  83. */
  84. static struct
  85. {
  86. u32 freq_low;
  87. u32 freq_high;
  88. u32 VCOdivider;
  89. u32 progdata;
  90. } cx24123_bandselect_vals[] =
  91. {
  92. /* band 1 */
  93. {
  94. .freq_low = 950000,
  95. .freq_high = 1074999,
  96. .VCOdivider = 4,
  97. .progdata = (0 << 19) | (0 << 9) | 0x40,
  98. },
  99. /* band 2 */
  100. {
  101. .freq_low = 1075000,
  102. .freq_high = 1177999,
  103. .VCOdivider = 4,
  104. .progdata = (0 << 19) | (0 << 9) | 0x80,
  105. },
  106. /* band 3 */
  107. {
  108. .freq_low = 1178000,
  109. .freq_high = 1295999,
  110. .VCOdivider = 2,
  111. .progdata = (0 << 19) | (1 << 9) | 0x01,
  112. },
  113. /* band 4 */
  114. {
  115. .freq_low = 1296000,
  116. .freq_high = 1431999,
  117. .VCOdivider = 2,
  118. .progdata = (0 << 19) | (1 << 9) | 0x02,
  119. },
  120. /* band 5 */
  121. {
  122. .freq_low = 1432000,
  123. .freq_high = 1575999,
  124. .VCOdivider = 2,
  125. .progdata = (0 << 19) | (1 << 9) | 0x04,
  126. },
  127. /* band 6 */
  128. {
  129. .freq_low = 1576000,
  130. .freq_high = 1717999,
  131. .VCOdivider = 2,
  132. .progdata = (0 << 19) | (1 << 9) | 0x08,
  133. },
  134. /* band 7 */
  135. {
  136. .freq_low = 1718000,
  137. .freq_high = 1855999,
  138. .VCOdivider = 2,
  139. .progdata = (0 << 19) | (1 << 9) | 0x10,
  140. },
  141. /* band 8 */
  142. {
  143. .freq_low = 1856000,
  144. .freq_high = 2035999,
  145. .VCOdivider = 2,
  146. .progdata = (0 << 19) | (1 << 9) | 0x20,
  147. },
  148. /* band 9 */
  149. {
  150. .freq_low = 2036000,
  151. .freq_high = 2150000,
  152. .VCOdivider = 2,
  153. .progdata = (0 << 19) | (1 << 9) | 0x40,
  154. },
  155. };
  156. static struct {
  157. u8 reg;
  158. u8 data;
  159. } cx24123_regdata[] =
  160. {
  161. {0x00, 0x03}, /* Reset system */
  162. {0x00, 0x00}, /* Clear reset */
  163. {0x03, 0x07}, /* QPSK, DVB, Auto Acquisition (default) */
  164. {0x04, 0x10}, /* MPEG */
  165. {0x05, 0x04}, /* MPEG */
  166. {0x06, 0x31}, /* MPEG (default) */
  167. {0x0b, 0x00}, /* Freq search start point (default) */
  168. {0x0c, 0x00}, /* Demodulator sample gain (default) */
  169. {0x0d, 0x7f}, /* Force driver to shift until the maximum (+-10 MHz) */
  170. {0x0e, 0x03}, /* Default non-inverted, FEC 3/4 (default) */
  171. {0x0f, 0xfe}, /* FEC search mask (all supported codes) */
  172. {0x10, 0x01}, /* Default search inversion, no repeat (default) */
  173. {0x16, 0x00}, /* Enable reading of frequency */
  174. {0x17, 0x01}, /* Enable EsNO Ready Counter */
  175. {0x1c, 0x80}, /* Enable error counter */
  176. {0x20, 0x00}, /* Tuner burst clock rate = 500KHz */
  177. {0x21, 0x15}, /* Tuner burst mode, word length = 0x15 */
  178. {0x28, 0x00}, /* Enable FILTERV with positive pol., DiSEqC 2.x off */
  179. {0x29, 0x00}, /* DiSEqC LNB_DC off */
  180. {0x2a, 0xb0}, /* DiSEqC Parameters (default) */
  181. {0x2b, 0x73}, /* DiSEqC Tone Frequency (default) */
  182. {0x2c, 0x00}, /* DiSEqC Message (0x2c - 0x31) */
  183. {0x2d, 0x00},
  184. {0x2e, 0x00},
  185. {0x2f, 0x00},
  186. {0x30, 0x00},
  187. {0x31, 0x00},
  188. {0x32, 0x8c}, /* DiSEqC Parameters (default) */
  189. {0x33, 0x00}, /* Interrupts off (0x33 - 0x34) */
  190. {0x34, 0x00},
  191. {0x35, 0x03}, /* DiSEqC Tone Amplitude (default) */
  192. {0x36, 0x02}, /* DiSEqC Parameters (default) */
  193. {0x37, 0x3a}, /* DiSEqC Parameters (default) */
  194. {0x3a, 0x00}, /* Enable AGC accumulator (for signal strength) */
  195. {0x44, 0x00}, /* Constellation (default) */
  196. {0x45, 0x00}, /* Symbol count (default) */
  197. {0x46, 0x0d}, /* Symbol rate estimator on (default) */
  198. {0x56, 0xc1}, /* Error Counter = Viterbi BER */
  199. {0x57, 0xff}, /* Error Counter Window (default) */
  200. {0x5c, 0x20}, /* Acquisition AFC Expiration window (default is 0x10) */
  201. {0x67, 0x83}, /* Non-DCII symbol clock */
  202. };
  203. static int cx24123_writereg(struct cx24123_state* state, int reg, int data)
  204. {
  205. u8 buf[] = { reg, data };
  206. struct i2c_msg msg = { .addr = state->config->demod_address, .flags = 0, .buf = buf, .len = 2 };
  207. int err;
  208. if (debug>1)
  209. printk("cx24123: %s: write reg 0x%02x, value 0x%02x\n",
  210. __FUNCTION__,reg, data);
  211. if ((err = i2c_transfer(state->i2c, &msg, 1)) != 1) {
  212. printk("%s: writereg error(err == %i, reg == 0x%02x,"
  213. " data == 0x%02x)\n", __FUNCTION__, err, reg, data);
  214. return -EREMOTEIO;
  215. }
  216. return 0;
  217. }
  218. static int cx24123_readreg(struct cx24123_state* state, u8 reg)
  219. {
  220. int ret;
  221. u8 b0[] = { reg };
  222. u8 b1[] = { 0 };
  223. struct i2c_msg msg[] = {
  224. { .addr = state->config->demod_address, .flags = 0, .buf = b0, .len = 1 },
  225. { .addr = state->config->demod_address, .flags = I2C_M_RD, .buf = b1, .len = 1 }
  226. };
  227. ret = i2c_transfer(state->i2c, msg, 2);
  228. if (ret != 2) {
  229. printk("%s: reg=0x%x (error=%d)\n", __FUNCTION__, reg, ret);
  230. return ret;
  231. }
  232. if (debug>1)
  233. printk("cx24123: read reg 0x%02x, value 0x%02x\n",reg, ret);
  234. return b1[0];
  235. }
  236. static int cx24123_set_inversion(struct cx24123_state* state, fe_spectral_inversion_t inversion)
  237. {
  238. u8 nom_reg = cx24123_readreg(state, 0x0e);
  239. u8 auto_reg = cx24123_readreg(state, 0x10);
  240. switch (inversion) {
  241. case INVERSION_OFF:
  242. dprintk("%s: inversion off\n",__FUNCTION__);
  243. cx24123_writereg(state, 0x0e, nom_reg & ~0x80);
  244. cx24123_writereg(state, 0x10, auto_reg | 0x80);
  245. break;
  246. case INVERSION_ON:
  247. dprintk("%s: inversion on\n",__FUNCTION__);
  248. cx24123_writereg(state, 0x0e, nom_reg | 0x80);
  249. cx24123_writereg(state, 0x10, auto_reg | 0x80);
  250. break;
  251. case INVERSION_AUTO:
  252. dprintk("%s: inversion auto\n",__FUNCTION__);
  253. cx24123_writereg(state, 0x10, auto_reg & ~0x80);
  254. break;
  255. default:
  256. return -EINVAL;
  257. }
  258. return 0;
  259. }
  260. static int cx24123_get_inversion(struct cx24123_state* state, fe_spectral_inversion_t *inversion)
  261. {
  262. u8 val;
  263. val = cx24123_readreg(state, 0x1b) >> 7;
  264. if (val == 0) {
  265. dprintk("%s: read inversion off\n",__FUNCTION__);
  266. *inversion = INVERSION_OFF;
  267. } else {
  268. dprintk("%s: read inversion on\n",__FUNCTION__);
  269. *inversion = INVERSION_ON;
  270. }
  271. return 0;
  272. }
  273. static int cx24123_set_fec(struct cx24123_state* state, fe_code_rate_t fec)
  274. {
  275. u8 nom_reg = cx24123_readreg(state, 0x0e) & ~0x07;
  276. if ( (fec < FEC_NONE) || (fec > FEC_AUTO) )
  277. fec = FEC_AUTO;
  278. /* Set the soft decision threshold */
  279. if(fec == FEC_1_2)
  280. cx24123_writereg(state, 0x43, cx24123_readreg(state, 0x43) | 0x01);
  281. else
  282. cx24123_writereg(state, 0x43, cx24123_readreg(state, 0x43) & ~0x01);
  283. switch (fec) {
  284. case FEC_1_2:
  285. dprintk("%s: set FEC to 1/2\n",__FUNCTION__);
  286. cx24123_writereg(state, 0x0e, nom_reg | 0x01);
  287. cx24123_writereg(state, 0x0f, 0x02);
  288. break;
  289. case FEC_2_3:
  290. dprintk("%s: set FEC to 2/3\n",__FUNCTION__);
  291. cx24123_writereg(state, 0x0e, nom_reg | 0x02);
  292. cx24123_writereg(state, 0x0f, 0x04);
  293. break;
  294. case FEC_3_4:
  295. dprintk("%s: set FEC to 3/4\n",__FUNCTION__);
  296. cx24123_writereg(state, 0x0e, nom_reg | 0x03);
  297. cx24123_writereg(state, 0x0f, 0x08);
  298. break;
  299. case FEC_4_5:
  300. dprintk("%s: set FEC to 4/5\n",__FUNCTION__);
  301. cx24123_writereg(state, 0x0e, nom_reg | 0x04);
  302. cx24123_writereg(state, 0x0f, 0x10);
  303. break;
  304. case FEC_5_6:
  305. dprintk("%s: set FEC to 5/6\n",__FUNCTION__);
  306. cx24123_writereg(state, 0x0e, nom_reg | 0x05);
  307. cx24123_writereg(state, 0x0f, 0x20);
  308. break;
  309. case FEC_6_7:
  310. dprintk("%s: set FEC to 6/7\n",__FUNCTION__);
  311. cx24123_writereg(state, 0x0e, nom_reg | 0x06);
  312. cx24123_writereg(state, 0x0f, 0x40);
  313. break;
  314. case FEC_7_8:
  315. dprintk("%s: set FEC to 7/8\n",__FUNCTION__);
  316. cx24123_writereg(state, 0x0e, nom_reg | 0x07);
  317. cx24123_writereg(state, 0x0f, 0x80);
  318. break;
  319. case FEC_AUTO:
  320. dprintk("%s: set FEC to auto\n",__FUNCTION__);
  321. cx24123_writereg(state, 0x0f, 0xfe);
  322. break;
  323. default:
  324. return -EOPNOTSUPP;
  325. }
  326. return 0;
  327. }
  328. static int cx24123_get_fec(struct cx24123_state* state, fe_code_rate_t *fec)
  329. {
  330. int ret;
  331. ret = cx24123_readreg (state, 0x1b);
  332. if (ret < 0)
  333. return ret;
  334. ret = ret & 0x07;
  335. switch (ret) {
  336. case 1:
  337. *fec = FEC_1_2;
  338. break;
  339. case 2:
  340. *fec = FEC_2_3;
  341. break;
  342. case 3:
  343. *fec = FEC_3_4;
  344. break;
  345. case 4:
  346. *fec = FEC_4_5;
  347. break;
  348. case 5:
  349. *fec = FEC_5_6;
  350. break;
  351. case 6:
  352. *fec = FEC_6_7;
  353. break;
  354. case 7:
  355. *fec = FEC_7_8;
  356. break;
  357. default:
  358. /* this can happen when there's no lock */
  359. *fec = FEC_NONE;
  360. }
  361. return 0;
  362. }
  363. /* Approximation of closest integer of log2(a/b). It actually gives the
  364. lowest integer i such that 2^i >= round(a/b) */
  365. static u32 cx24123_int_log2(u32 a, u32 b)
  366. {
  367. u32 exp, nearest = 0;
  368. u32 div = a / b;
  369. if(a % b >= b / 2) ++div;
  370. if(div < (1 << 31))
  371. {
  372. for(exp = 1; div > exp; nearest++)
  373. exp += exp;
  374. }
  375. return nearest;
  376. }
  377. static int cx24123_set_symbolrate(struct cx24123_state* state, u32 srate)
  378. {
  379. u32 tmp, sample_rate, ratio, sample_gain;
  380. u8 pll_mult;
  381. /* check if symbol rate is within limits */
  382. if ((srate > state->frontend.ops.info.symbol_rate_max) ||
  383. (srate < state->frontend.ops.info.symbol_rate_min))
  384. return -EOPNOTSUPP;;
  385. /* choose the sampling rate high enough for the required operation,
  386. while optimizing the power consumed by the demodulator */
  387. if (srate < (XTAL*2)/2)
  388. pll_mult = 2;
  389. else if (srate < (XTAL*3)/2)
  390. pll_mult = 3;
  391. else if (srate < (XTAL*4)/2)
  392. pll_mult = 4;
  393. else if (srate < (XTAL*5)/2)
  394. pll_mult = 5;
  395. else if (srate < (XTAL*6)/2)
  396. pll_mult = 6;
  397. else if (srate < (XTAL*7)/2)
  398. pll_mult = 7;
  399. else if (srate < (XTAL*8)/2)
  400. pll_mult = 8;
  401. else
  402. pll_mult = 9;
  403. sample_rate = pll_mult * XTAL;
  404. /*
  405. SYSSymbolRate[21:0] = (srate << 23) / sample_rate
  406. We have to use 32 bit unsigned arithmetic without precision loss.
  407. The maximum srate is 45000000 or 0x02AEA540. This number has
  408. only 6 clear bits on top, hence we can shift it left only 6 bits
  409. at a time. Borrowed from cx24110.c
  410. */
  411. tmp = srate << 6;
  412. ratio = tmp / sample_rate;
  413. tmp = (tmp % sample_rate) << 6;
  414. ratio = (ratio << 6) + (tmp / sample_rate);
  415. tmp = (tmp % sample_rate) << 6;
  416. ratio = (ratio << 6) + (tmp / sample_rate);
  417. tmp = (tmp % sample_rate) << 5;
  418. ratio = (ratio << 5) + (tmp / sample_rate);
  419. cx24123_writereg(state, 0x01, pll_mult * 6);
  420. cx24123_writereg(state, 0x08, (ratio >> 16) & 0x3f );
  421. cx24123_writereg(state, 0x09, (ratio >> 8) & 0xff );
  422. cx24123_writereg(state, 0x0a, (ratio ) & 0xff );
  423. /* also set the demodulator sample gain */
  424. sample_gain = cx24123_int_log2(sample_rate, srate);
  425. tmp = cx24123_readreg(state, 0x0c) & ~0xe0;
  426. cx24123_writereg(state, 0x0c, tmp | sample_gain << 5);
  427. dprintk("%s: srate=%d, ratio=0x%08x, sample_rate=%i sample_gain=%d\n", __FUNCTION__, srate, ratio, sample_rate, sample_gain);
  428. return 0;
  429. }
  430. /*
  431. * Based on the required frequency and symbolrate, the tuner AGC has to be configured
  432. * and the correct band selected. Calculate those values
  433. */
  434. static int cx24123_pll_calculate(struct dvb_frontend* fe, struct dvb_frontend_parameters *p)
  435. {
  436. struct cx24123_state *state = fe->demodulator_priv;
  437. u32 ndiv = 0, adiv = 0, vco_div = 0;
  438. int i = 0;
  439. int pump = 2;
  440. int band = 0;
  441. int num_bands = ARRAY_SIZE(cx24123_bandselect_vals);
  442. /* Defaults for low freq, low rate */
  443. state->VCAarg = cx24123_AGC_vals[0].VCAprogdata;
  444. state->VGAarg = cx24123_AGC_vals[0].VGAprogdata;
  445. state->bandselectarg = cx24123_bandselect_vals[0].progdata;
  446. vco_div = cx24123_bandselect_vals[0].VCOdivider;
  447. /* For the given symbol rate, determine the VCA, VGA and FILTUNE programming bits */
  448. for (i = 0; i < ARRAY_SIZE(cx24123_AGC_vals); i++)
  449. {
  450. if ((cx24123_AGC_vals[i].symbolrate_low <= p->u.qpsk.symbol_rate) &&
  451. (cx24123_AGC_vals[i].symbolrate_high >= p->u.qpsk.symbol_rate) ) {
  452. state->VCAarg = cx24123_AGC_vals[i].VCAprogdata;
  453. state->VGAarg = cx24123_AGC_vals[i].VGAprogdata;
  454. state->FILTune = cx24123_AGC_vals[i].FILTune;
  455. }
  456. }
  457. /* determine the band to use */
  458. if(force_band < 1 || force_band > num_bands)
  459. {
  460. for (i = 0; i < num_bands; i++)
  461. {
  462. if ((cx24123_bandselect_vals[i].freq_low <= p->frequency) &&
  463. (cx24123_bandselect_vals[i].freq_high >= p->frequency) )
  464. band = i;
  465. }
  466. }
  467. else
  468. band = force_band - 1;
  469. state->bandselectarg = cx24123_bandselect_vals[band].progdata;
  470. vco_div = cx24123_bandselect_vals[band].VCOdivider;
  471. /* determine the charge pump current */
  472. if ( p->frequency < (cx24123_bandselect_vals[band].freq_low + cx24123_bandselect_vals[band].freq_high)/2 )
  473. pump = 0x01;
  474. else
  475. pump = 0x02;
  476. /* Determine the N/A dividers for the requested lband freq (in kHz). */
  477. /* Note: the reference divider R=10, frequency is in KHz, XTAL is in Hz */
  478. ndiv = ( ((p->frequency * vco_div * 10) / (2 * XTAL / 1000)) / 32) & 0x1ff;
  479. adiv = ( ((p->frequency * vco_div * 10) / (2 * XTAL / 1000)) % 32) & 0x1f;
  480. if (adiv == 0 && ndiv > 0)
  481. ndiv--;
  482. /* control bits 11, refdiv 11, charge pump polarity 1, charge pump current, ndiv, adiv */
  483. state->pllarg = (3 << 19) | (3 << 17) | (1 << 16) | (pump << 14) | (ndiv << 5) | adiv;
  484. return 0;
  485. }
  486. /*
  487. * Tuner data is 21 bits long, must be left-aligned in data.
  488. * Tuner cx24109 is written through a dedicated 3wire interface on the demod chip.
  489. */
  490. static int cx24123_pll_writereg(struct dvb_frontend* fe, struct dvb_frontend_parameters *p, u32 data)
  491. {
  492. struct cx24123_state *state = fe->demodulator_priv;
  493. unsigned long timeout;
  494. dprintk("%s: pll writereg called, data=0x%08x\n",__FUNCTION__,data);
  495. /* align the 21 bytes into to bit23 boundary */
  496. data = data << 3;
  497. /* Reset the demod pll word length to 0x15 bits */
  498. cx24123_writereg(state, 0x21, 0x15);
  499. /* write the msb 8 bits, wait for the send to be completed */
  500. timeout = jiffies + msecs_to_jiffies(40);
  501. cx24123_writereg(state, 0x22, (data >> 16) & 0xff);
  502. while ((cx24123_readreg(state, 0x20) & 0x40) == 0) {
  503. if (time_after(jiffies, timeout)) {
  504. printk("%s: demodulator is not responding, possibly hung, aborting.\n", __FUNCTION__);
  505. return -EREMOTEIO;
  506. }
  507. msleep(10);
  508. }
  509. /* send another 8 bytes, wait for the send to be completed */
  510. timeout = jiffies + msecs_to_jiffies(40);
  511. cx24123_writereg(state, 0x22, (data>>8) & 0xff );
  512. while ((cx24123_readreg(state, 0x20) & 0x40) == 0) {
  513. if (time_after(jiffies, timeout)) {
  514. printk("%s: demodulator is not responding, possibly hung, aborting.\n", __FUNCTION__);
  515. return -EREMOTEIO;
  516. }
  517. msleep(10);
  518. }
  519. /* send the lower 5 bits of this byte, padded with 3 LBB, wait for the send to be completed */
  520. timeout = jiffies + msecs_to_jiffies(40);
  521. cx24123_writereg(state, 0x22, (data) & 0xff );
  522. while ((cx24123_readreg(state, 0x20) & 0x80)) {
  523. if (time_after(jiffies, timeout)) {
  524. printk("%s: demodulator is not responding, possibly hung, aborting.\n", __FUNCTION__);
  525. return -EREMOTEIO;
  526. }
  527. msleep(10);
  528. }
  529. /* Trigger the demod to configure the tuner */
  530. cx24123_writereg(state, 0x20, cx24123_readreg(state, 0x20) | 2);
  531. cx24123_writereg(state, 0x20, cx24123_readreg(state, 0x20) & 0xfd);
  532. return 0;
  533. }
  534. static int cx24123_pll_tune(struct dvb_frontend* fe, struct dvb_frontend_parameters *p)
  535. {
  536. struct cx24123_state *state = fe->demodulator_priv;
  537. u8 val;
  538. dprintk("frequency=%i\n", p->frequency);
  539. if (cx24123_pll_calculate(fe, p) != 0) {
  540. printk("%s: cx24123_pll_calcutate failed\n",__FUNCTION__);
  541. return -EINVAL;
  542. }
  543. /* Write the new VCO/VGA */
  544. cx24123_pll_writereg(fe, p, state->VCAarg);
  545. cx24123_pll_writereg(fe, p, state->VGAarg);
  546. /* Write the new bandselect and pll args */
  547. cx24123_pll_writereg(fe, p, state->bandselectarg);
  548. cx24123_pll_writereg(fe, p, state->pllarg);
  549. /* set the FILTUNE voltage */
  550. val = cx24123_readreg(state, 0x28) & ~0x3;
  551. cx24123_writereg(state, 0x27, state->FILTune >> 2);
  552. cx24123_writereg(state, 0x28, val | (state->FILTune & 0x3));
  553. dprintk("%s: pll tune VCA=%d, band=%d, pll=%d\n",__FUNCTION__,state->VCAarg,
  554. state->bandselectarg,state->pllarg);
  555. return 0;
  556. }
  557. static int cx24123_initfe(struct dvb_frontend* fe)
  558. {
  559. struct cx24123_state *state = fe->demodulator_priv;
  560. int i;
  561. dprintk("%s: init frontend\n",__FUNCTION__);
  562. /* Configure the demod to a good set of defaults */
  563. for (i = 0; i < ARRAY_SIZE(cx24123_regdata); i++)
  564. cx24123_writereg(state, cx24123_regdata[i].reg, cx24123_regdata[i].data);
  565. /* Set the LNB polarity */
  566. if(state->config->lnb_polarity)
  567. cx24123_writereg(state, 0x32, cx24123_readreg(state, 0x32) | 0x02);
  568. return 0;
  569. }
  570. static int cx24123_set_voltage(struct dvb_frontend* fe, fe_sec_voltage_t voltage)
  571. {
  572. struct cx24123_state *state = fe->demodulator_priv;
  573. u8 val;
  574. val = cx24123_readreg(state, 0x29) & ~0x40;
  575. switch (voltage) {
  576. case SEC_VOLTAGE_13:
  577. dprintk("%s: setting voltage 13V\n", __FUNCTION__);
  578. return cx24123_writereg(state, 0x29, val & 0x7f);
  579. case SEC_VOLTAGE_18:
  580. dprintk("%s: setting voltage 18V\n", __FUNCTION__);
  581. return cx24123_writereg(state, 0x29, val | 0x80);
  582. case SEC_VOLTAGE_OFF:
  583. /* already handled in cx88-dvb */
  584. return 0;
  585. default:
  586. return -EINVAL;
  587. };
  588. return 0;
  589. }
  590. /* wait for diseqc queue to become ready (or timeout) */
  591. static void cx24123_wait_for_diseqc(struct cx24123_state *state)
  592. {
  593. unsigned long timeout = jiffies + msecs_to_jiffies(200);
  594. while (!(cx24123_readreg(state, 0x29) & 0x40)) {
  595. if(time_after(jiffies, timeout)) {
  596. printk("%s: diseqc queue not ready, command may be lost.\n", __FUNCTION__);
  597. break;
  598. }
  599. msleep(10);
  600. }
  601. }
  602. static int cx24123_send_diseqc_msg(struct dvb_frontend* fe, struct dvb_diseqc_master_cmd *cmd)
  603. {
  604. struct cx24123_state *state = fe->demodulator_priv;
  605. int i, val, tone;
  606. dprintk("%s:\n",__FUNCTION__);
  607. /* stop continuous tone if enabled */
  608. tone = cx24123_readreg(state, 0x29);
  609. if (tone & 0x10)
  610. cx24123_writereg(state, 0x29, tone & ~0x50);
  611. /* wait for diseqc queue ready */
  612. cx24123_wait_for_diseqc(state);
  613. /* select tone mode */
  614. cx24123_writereg(state, 0x2a, cx24123_readreg(state, 0x2a) & 0xfb);
  615. for (i = 0; i < cmd->msg_len; i++)
  616. cx24123_writereg(state, 0x2C + i, cmd->msg[i]);
  617. val = cx24123_readreg(state, 0x29);
  618. cx24123_writereg(state, 0x29, ((val & 0x90) | 0x40) | ((cmd->msg_len-3) & 3));
  619. /* wait for diseqc message to finish sending */
  620. cx24123_wait_for_diseqc(state);
  621. /* restart continuous tone if enabled */
  622. if (tone & 0x10) {
  623. cx24123_writereg(state, 0x29, tone & ~0x40);
  624. }
  625. return 0;
  626. }
  627. static int cx24123_diseqc_send_burst(struct dvb_frontend* fe, fe_sec_mini_cmd_t burst)
  628. {
  629. struct cx24123_state *state = fe->demodulator_priv;
  630. int val, tone;
  631. dprintk("%s:\n", __FUNCTION__);
  632. /* stop continuous tone if enabled */
  633. tone = cx24123_readreg(state, 0x29);
  634. if (tone & 0x10)
  635. cx24123_writereg(state, 0x29, tone & ~0x50);
  636. /* wait for diseqc queue ready */
  637. cx24123_wait_for_diseqc(state);
  638. /* select tone mode */
  639. cx24123_writereg(state, 0x2a, cx24123_readreg(state, 0x2a) | 0x4);
  640. msleep(30);
  641. val = cx24123_readreg(state, 0x29);
  642. if (burst == SEC_MINI_A)
  643. cx24123_writereg(state, 0x29, ((val & 0x90) | 0x40 | 0x00));
  644. else if (burst == SEC_MINI_B)
  645. cx24123_writereg(state, 0x29, ((val & 0x90) | 0x40 | 0x08));
  646. else
  647. return -EINVAL;
  648. cx24123_wait_for_diseqc(state);
  649. cx24123_writereg(state, 0x2a, cx24123_readreg(state, 0x2a) & 0xfb);
  650. /* restart continuous tone if enabled */
  651. if (tone & 0x10) {
  652. cx24123_writereg(state, 0x29, tone & ~0x40);
  653. }
  654. return 0;
  655. }
  656. static int cx24123_read_status(struct dvb_frontend* fe, fe_status_t* status)
  657. {
  658. struct cx24123_state *state = fe->demodulator_priv;
  659. int sync = cx24123_readreg(state, 0x14);
  660. int lock = cx24123_readreg(state, 0x20);
  661. *status = 0;
  662. if (lock & 0x01)
  663. *status |= FE_HAS_SIGNAL;
  664. if (sync & 0x02)
  665. *status |= FE_HAS_CARRIER; /* Phase locked */
  666. if (sync & 0x04)
  667. *status |= FE_HAS_VITERBI;
  668. /* Reed-Solomon Status */
  669. if (sync & 0x08)
  670. *status |= FE_HAS_SYNC;
  671. if (sync & 0x80)
  672. *status |= FE_HAS_LOCK; /*Full Sync */
  673. return 0;
  674. }
  675. /*
  676. * Configured to return the measurement of errors in blocks, because no UCBLOCKS value
  677. * is available, so this value doubles up to satisfy both measurements
  678. */
  679. static int cx24123_read_ber(struct dvb_frontend* fe, u32* ber)
  680. {
  681. struct cx24123_state *state = fe->demodulator_priv;
  682. /* The true bit error rate is this value divided by
  683. the window size (set as 256 * 255) */
  684. *ber = ((cx24123_readreg(state, 0x1c) & 0x3f) << 16) |
  685. (cx24123_readreg(state, 0x1d) << 8 |
  686. cx24123_readreg(state, 0x1e));
  687. dprintk("%s: BER = %d\n",__FUNCTION__,*ber);
  688. return 0;
  689. }
  690. static int cx24123_read_signal_strength(struct dvb_frontend* fe, u16* signal_strength)
  691. {
  692. struct cx24123_state *state = fe->demodulator_priv;
  693. *signal_strength = cx24123_readreg(state, 0x3b) << 8; /* larger = better */
  694. dprintk("%s: Signal strength = %d\n",__FUNCTION__,*signal_strength);
  695. return 0;
  696. }
  697. static int cx24123_read_snr(struct dvb_frontend* fe, u16* snr)
  698. {
  699. struct cx24123_state *state = fe->demodulator_priv;
  700. /* Inverted raw Es/N0 count, totally bogus but better than the
  701. BER threshold. */
  702. *snr = 65535 - (((u16)cx24123_readreg(state, 0x18) << 8) |
  703. (u16)cx24123_readreg(state, 0x19));
  704. dprintk("%s: read S/N index = %d\n",__FUNCTION__,*snr);
  705. return 0;
  706. }
  707. static int cx24123_set_frontend(struct dvb_frontend* fe, struct dvb_frontend_parameters *p)
  708. {
  709. struct cx24123_state *state = fe->demodulator_priv;
  710. dprintk("%s: set_frontend\n",__FUNCTION__);
  711. if (state->config->set_ts_params)
  712. state->config->set_ts_params(fe, 0);
  713. state->currentfreq=p->frequency;
  714. state->currentsymbolrate = p->u.qpsk.symbol_rate;
  715. cx24123_set_inversion(state, p->inversion);
  716. cx24123_set_fec(state, p->u.qpsk.fec_inner);
  717. cx24123_set_symbolrate(state, p->u.qpsk.symbol_rate);
  718. cx24123_pll_tune(fe, p);
  719. /* Enable automatic aquisition and reset cycle */
  720. cx24123_writereg(state, 0x03, (cx24123_readreg(state, 0x03) | 0x07));
  721. cx24123_writereg(state, 0x00, 0x10);
  722. cx24123_writereg(state, 0x00, 0);
  723. return 0;
  724. }
  725. static int cx24123_get_frontend(struct dvb_frontend* fe, struct dvb_frontend_parameters *p)
  726. {
  727. struct cx24123_state *state = fe->demodulator_priv;
  728. dprintk("%s: get_frontend\n",__FUNCTION__);
  729. if (cx24123_get_inversion(state, &p->inversion) != 0) {
  730. printk("%s: Failed to get inversion status\n",__FUNCTION__);
  731. return -EREMOTEIO;
  732. }
  733. if (cx24123_get_fec(state, &p->u.qpsk.fec_inner) != 0) {
  734. printk("%s: Failed to get fec status\n",__FUNCTION__);
  735. return -EREMOTEIO;
  736. }
  737. p->frequency = state->currentfreq;
  738. p->u.qpsk.symbol_rate = state->currentsymbolrate;
  739. return 0;
  740. }
  741. static int cx24123_set_tone(struct dvb_frontend* fe, fe_sec_tone_mode_t tone)
  742. {
  743. struct cx24123_state *state = fe->demodulator_priv;
  744. u8 val;
  745. /* wait for diseqc queue ready */
  746. cx24123_wait_for_diseqc(state);
  747. val = cx24123_readreg(state, 0x29) & ~0x40;
  748. switch (tone) {
  749. case SEC_TONE_ON:
  750. dprintk("%s: setting tone on\n", __FUNCTION__);
  751. return cx24123_writereg(state, 0x29, val | 0x10);
  752. case SEC_TONE_OFF:
  753. dprintk("%s: setting tone off\n",__FUNCTION__);
  754. return cx24123_writereg(state, 0x29, val & 0xef);
  755. default:
  756. printk("%s: CASE reached default with tone=%d\n", __FUNCTION__, tone);
  757. return -EINVAL;
  758. }
  759. return 0;
  760. }
  761. static int cx24123_tune(struct dvb_frontend* fe,
  762. struct dvb_frontend_parameters* params,
  763. unsigned int mode_flags,
  764. unsigned int *delay,
  765. fe_status_t *status)
  766. {
  767. int retval = 0;
  768. if (params != NULL)
  769. retval = cx24123_set_frontend(fe, params);
  770. if (!(mode_flags & FE_TUNE_MODE_ONESHOT))
  771. cx24123_read_status(fe, status);
  772. *delay = HZ/10;
  773. return retval;
  774. }
  775. static int cx24123_get_algo(struct dvb_frontend *fe)
  776. {
  777. return 1; //FE_ALGO_HW
  778. }
  779. static void cx24123_release(struct dvb_frontend* fe)
  780. {
  781. struct cx24123_state* state = fe->demodulator_priv;
  782. dprintk("%s\n",__FUNCTION__);
  783. kfree(state);
  784. }
  785. static struct dvb_frontend_ops cx24123_ops;
  786. struct dvb_frontend* cx24123_attach(const struct cx24123_config* config,
  787. struct i2c_adapter* i2c)
  788. {
  789. struct cx24123_state* state = NULL;
  790. int ret;
  791. dprintk("%s\n",__FUNCTION__);
  792. /* allocate memory for the internal state */
  793. state = kmalloc(sizeof(struct cx24123_state), GFP_KERNEL);
  794. if (state == NULL) {
  795. printk("Unable to kmalloc\n");
  796. goto error;
  797. }
  798. /* setup the state */
  799. state->config = config;
  800. state->i2c = i2c;
  801. state->VCAarg = 0;
  802. state->VGAarg = 0;
  803. state->bandselectarg = 0;
  804. state->pllarg = 0;
  805. state->currentfreq = 0;
  806. state->currentsymbolrate = 0;
  807. /* check if the demod is there */
  808. ret = cx24123_readreg(state, 0x00);
  809. if ((ret != 0xd1) && (ret != 0xe1)) {
  810. printk("Version != d1 or e1\n");
  811. goto error;
  812. }
  813. /* create dvb_frontend */
  814. memcpy(&state->frontend.ops, &cx24123_ops, sizeof(struct dvb_frontend_ops));
  815. state->frontend.demodulator_priv = state;
  816. return &state->frontend;
  817. error:
  818. kfree(state);
  819. return NULL;
  820. }
  821. static struct dvb_frontend_ops cx24123_ops = {
  822. .info = {
  823. .name = "Conexant CX24123/CX24109",
  824. .type = FE_QPSK,
  825. .frequency_min = 950000,
  826. .frequency_max = 2150000,
  827. .frequency_stepsize = 1011, /* kHz for QPSK frontends */
  828. .frequency_tolerance = 5000,
  829. .symbol_rate_min = 1000000,
  830. .symbol_rate_max = 45000000,
  831. .caps = FE_CAN_INVERSION_AUTO |
  832. FE_CAN_FEC_1_2 | FE_CAN_FEC_2_3 | FE_CAN_FEC_3_4 |
  833. FE_CAN_FEC_4_5 | FE_CAN_FEC_5_6 | FE_CAN_FEC_6_7 |
  834. FE_CAN_FEC_7_8 | FE_CAN_FEC_AUTO |
  835. FE_CAN_QPSK | FE_CAN_RECOVER
  836. },
  837. .release = cx24123_release,
  838. .init = cx24123_initfe,
  839. .set_frontend = cx24123_set_frontend,
  840. .get_frontend = cx24123_get_frontend,
  841. .read_status = cx24123_read_status,
  842. .read_ber = cx24123_read_ber,
  843. .read_signal_strength = cx24123_read_signal_strength,
  844. .read_snr = cx24123_read_snr,
  845. .diseqc_send_master_cmd = cx24123_send_diseqc_msg,
  846. .diseqc_send_burst = cx24123_diseqc_send_burst,
  847. .set_tone = cx24123_set_tone,
  848. .set_voltage = cx24123_set_voltage,
  849. .tune = cx24123_tune,
  850. .get_frontend_algo = cx24123_get_algo,
  851. };
  852. module_param(debug, int, 0644);
  853. MODULE_PARM_DESC(debug, "Activates frontend debugging (default:0)");
  854. module_param(force_band, int, 0644);
  855. MODULE_PARM_DESC(force_band, "Force a specific band select (1-9, default:off).");
  856. MODULE_DESCRIPTION("DVB Frontend module for Conexant cx24123/cx24109 hardware");
  857. MODULE_AUTHOR("Steven Toth");
  858. MODULE_LICENSE("GPL");
  859. EXPORT_SYMBOL(cx24123_attach);