cx24123.c 29 KB

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