cx24123.c 27 KB

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