cx24123.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023
  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. switch (fec) {
  281. case FEC_1_2:
  282. dprintk("%s: set FEC to 1/2\n",__FUNCTION__);
  283. cx24123_writereg(state, 0x0e, nom_reg | 0x01);
  284. cx24123_writereg(state, 0x0f, 0x02);
  285. break;
  286. case FEC_2_3:
  287. dprintk("%s: set FEC to 2/3\n",__FUNCTION__);
  288. cx24123_writereg(state, 0x0e, nom_reg | 0x02);
  289. cx24123_writereg(state, 0x0f, 0x04);
  290. break;
  291. case FEC_3_4:
  292. dprintk("%s: set FEC to 3/4\n",__FUNCTION__);
  293. cx24123_writereg(state, 0x0e, nom_reg | 0x03);
  294. cx24123_writereg(state, 0x0f, 0x08);
  295. break;
  296. case FEC_4_5:
  297. dprintk("%s: set FEC to 4/5\n",__FUNCTION__);
  298. cx24123_writereg(state, 0x0e, nom_reg | 0x04);
  299. cx24123_writereg(state, 0x0f, 0x10);
  300. break;
  301. case FEC_5_6:
  302. dprintk("%s: set FEC to 5/6\n",__FUNCTION__);
  303. cx24123_writereg(state, 0x0e, nom_reg | 0x05);
  304. cx24123_writereg(state, 0x0f, 0x20);
  305. break;
  306. case FEC_6_7:
  307. dprintk("%s: set FEC to 6/7\n",__FUNCTION__);
  308. cx24123_writereg(state, 0x0e, nom_reg | 0x06);
  309. cx24123_writereg(state, 0x0f, 0x40);
  310. break;
  311. case FEC_7_8:
  312. dprintk("%s: set FEC to 7/8\n",__FUNCTION__);
  313. cx24123_writereg(state, 0x0e, nom_reg | 0x07);
  314. cx24123_writereg(state, 0x0f, 0x80);
  315. break;
  316. case FEC_AUTO:
  317. dprintk("%s: set FEC to auto\n",__FUNCTION__);
  318. cx24123_writereg(state, 0x0f, 0xfe);
  319. break;
  320. default:
  321. return -EOPNOTSUPP;
  322. }
  323. return 0;
  324. }
  325. static int cx24123_get_fec(struct cx24123_state* state, fe_code_rate_t *fec)
  326. {
  327. int ret;
  328. ret = cx24123_readreg (state, 0x1b);
  329. if (ret < 0)
  330. return ret;
  331. ret = ret & 0x07;
  332. switch (ret) {
  333. case 1:
  334. *fec = FEC_1_2;
  335. break;
  336. case 2:
  337. *fec = FEC_2_3;
  338. break;
  339. case 3:
  340. *fec = FEC_3_4;
  341. break;
  342. case 4:
  343. *fec = FEC_4_5;
  344. break;
  345. case 5:
  346. *fec = FEC_5_6;
  347. break;
  348. case 6:
  349. *fec = FEC_6_7;
  350. break;
  351. case 7:
  352. *fec = FEC_7_8;
  353. break;
  354. default:
  355. /* this can happen when there's no lock */
  356. *fec = FEC_NONE;
  357. }
  358. return 0;
  359. }
  360. /* Approximation of closest integer of log2(a/b). It actually gives the
  361. lowest integer i such that 2^i >= round(a/b) */
  362. static u32 cx24123_int_log2(u32 a, u32 b)
  363. {
  364. u32 exp, nearest = 0;
  365. u32 div = a / b;
  366. if(a % b >= b / 2) ++div;
  367. if(div < (1 << 31))
  368. {
  369. for(exp = 1; div > exp; nearest++)
  370. exp += exp;
  371. }
  372. return nearest;
  373. }
  374. static int cx24123_set_symbolrate(struct cx24123_state* state, u32 srate)
  375. {
  376. u32 tmp, sample_rate, ratio, sample_gain;
  377. u8 pll_mult;
  378. /* check if symbol rate is within limits */
  379. if ((srate > state->frontend.ops.info.symbol_rate_max) ||
  380. (srate < state->frontend.ops.info.symbol_rate_min))
  381. return -EOPNOTSUPP;;
  382. /* choose the sampling rate high enough for the required operation,
  383. while optimizing the power consumed by the demodulator */
  384. if (srate < (XTAL*2)/2)
  385. pll_mult = 2;
  386. else if (srate < (XTAL*3)/2)
  387. pll_mult = 3;
  388. else if (srate < (XTAL*4)/2)
  389. pll_mult = 4;
  390. else if (srate < (XTAL*5)/2)
  391. pll_mult = 5;
  392. else if (srate < (XTAL*6)/2)
  393. pll_mult = 6;
  394. else if (srate < (XTAL*7)/2)
  395. pll_mult = 7;
  396. else if (srate < (XTAL*8)/2)
  397. pll_mult = 8;
  398. else
  399. pll_mult = 9;
  400. sample_rate = pll_mult * XTAL;
  401. /*
  402. SYSSymbolRate[21:0] = (srate << 23) / sample_rate
  403. We have to use 32 bit unsigned arithmetic without precision loss.
  404. The maximum srate is 45000000 or 0x02AEA540. This number has
  405. only 6 clear bits on top, hence we can shift it left only 6 bits
  406. at a time. Borrowed from cx24110.c
  407. */
  408. tmp = srate << 6;
  409. ratio = tmp / sample_rate;
  410. tmp = (tmp % sample_rate) << 6;
  411. ratio = (ratio << 6) + (tmp / sample_rate);
  412. tmp = (tmp % sample_rate) << 6;
  413. ratio = (ratio << 6) + (tmp / sample_rate);
  414. tmp = (tmp % sample_rate) << 5;
  415. ratio = (ratio << 5) + (tmp / sample_rate);
  416. cx24123_writereg(state, 0x01, pll_mult * 6);
  417. cx24123_writereg(state, 0x08, (ratio >> 16) & 0x3f );
  418. cx24123_writereg(state, 0x09, (ratio >> 8) & 0xff );
  419. cx24123_writereg(state, 0x0a, (ratio ) & 0xff );
  420. /* also set the demodulator sample gain */
  421. sample_gain = cx24123_int_log2(sample_rate, srate);
  422. tmp = cx24123_readreg(state, 0x0c) & ~0xe0;
  423. cx24123_writereg(state, 0x0c, tmp | sample_gain << 5);
  424. dprintk("%s: srate=%d, ratio=0x%08x, sample_rate=%i sample_gain=%d\n", __FUNCTION__, srate, ratio, sample_rate, sample_gain);
  425. return 0;
  426. }
  427. /*
  428. * Based on the required frequency and symbolrate, the tuner AGC has to be configured
  429. * and the correct band selected. Calculate those values
  430. */
  431. static int cx24123_pll_calculate(struct dvb_frontend* fe, struct dvb_frontend_parameters *p)
  432. {
  433. struct cx24123_state *state = fe->demodulator_priv;
  434. u32 ndiv = 0, adiv = 0, vco_div = 0;
  435. int i = 0;
  436. int pump = 2;
  437. int band = 0;
  438. int num_bands = sizeof(cx24123_bandselect_vals) / sizeof(cx24123_bandselect_vals[0]);
  439. /* Defaults for low freq, low rate */
  440. state->VCAarg = cx24123_AGC_vals[0].VCAprogdata;
  441. state->VGAarg = cx24123_AGC_vals[0].VGAprogdata;
  442. state->bandselectarg = cx24123_bandselect_vals[0].progdata;
  443. vco_div = cx24123_bandselect_vals[0].VCOdivider;
  444. /* For the given symbol rate, determine the VCA, VGA and FILTUNE programming bits */
  445. for (i = 0; i < sizeof(cx24123_AGC_vals) / sizeof(cx24123_AGC_vals[0]); i++)
  446. {
  447. if ((cx24123_AGC_vals[i].symbolrate_low <= p->u.qpsk.symbol_rate) &&
  448. (cx24123_AGC_vals[i].symbolrate_high >= p->u.qpsk.symbol_rate) ) {
  449. state->VCAarg = cx24123_AGC_vals[i].VCAprogdata;
  450. state->VGAarg = cx24123_AGC_vals[i].VGAprogdata;
  451. state->FILTune = cx24123_AGC_vals[i].FILTune;
  452. }
  453. }
  454. /* determine the band to use */
  455. if(force_band < 1 || force_band > num_bands)
  456. {
  457. for (i = 0; i < num_bands; i++)
  458. {
  459. if ((cx24123_bandselect_vals[i].freq_low <= p->frequency) &&
  460. (cx24123_bandselect_vals[i].freq_high >= p->frequency) )
  461. band = i;
  462. }
  463. }
  464. else
  465. band = force_band - 1;
  466. state->bandselectarg = cx24123_bandselect_vals[band].progdata;
  467. vco_div = cx24123_bandselect_vals[band].VCOdivider;
  468. /* determine the charge pump current */
  469. if ( p->frequency < (cx24123_bandselect_vals[band].freq_low + cx24123_bandselect_vals[band].freq_high)/2 )
  470. pump = 0x01;
  471. else
  472. pump = 0x02;
  473. /* Determine the N/A dividers for the requested lband freq (in kHz). */
  474. /* Note: the reference divider R=10, frequency is in KHz, XTAL is in Hz */
  475. ndiv = ( ((p->frequency * vco_div * 10) / (2 * XTAL / 1000)) / 32) & 0x1ff;
  476. adiv = ( ((p->frequency * vco_div * 10) / (2 * XTAL / 1000)) % 32) & 0x1f;
  477. if (adiv == 0)
  478. ndiv++;
  479. /* control bits 11, refdiv 11, charge pump polarity 1, charge pump current, ndiv, adiv */
  480. state->pllarg = (3 << 19) | (3 << 17) | (1 << 16) | (pump << 14) | (ndiv << 5) | adiv;
  481. return 0;
  482. }
  483. /*
  484. * Tuner data is 21 bits long, must be left-aligned in data.
  485. * Tuner cx24109 is written through a dedicated 3wire interface on the demod chip.
  486. */
  487. static int cx24123_pll_writereg(struct dvb_frontend* fe, struct dvb_frontend_parameters *p, u32 data)
  488. {
  489. struct cx24123_state *state = fe->demodulator_priv;
  490. unsigned long timeout;
  491. dprintk("%s: pll writereg called, data=0x%08x\n",__FUNCTION__,data);
  492. /* align the 21 bytes into to bit23 boundary */
  493. data = data << 3;
  494. /* Reset the demod pll word length to 0x15 bits */
  495. cx24123_writereg(state, 0x21, 0x15);
  496. /* write the msb 8 bits, wait for the send to be completed */
  497. timeout = jiffies + msecs_to_jiffies(40);
  498. cx24123_writereg(state, 0x22, (data >> 16) & 0xff);
  499. while ((cx24123_readreg(state, 0x20) & 0x40) == 0) {
  500. if (time_after(jiffies, timeout)) {
  501. printk("%s: demodulator is not responding, possibly hung, aborting.\n", __FUNCTION__);
  502. return -EREMOTEIO;
  503. }
  504. msleep(10);
  505. }
  506. /* send another 8 bytes, wait for the send to be completed */
  507. timeout = jiffies + msecs_to_jiffies(40);
  508. cx24123_writereg(state, 0x22, (data>>8) & 0xff );
  509. while ((cx24123_readreg(state, 0x20) & 0x40) == 0) {
  510. if (time_after(jiffies, timeout)) {
  511. printk("%s: demodulator is not responding, possibly hung, aborting.\n", __FUNCTION__);
  512. return -EREMOTEIO;
  513. }
  514. msleep(10);
  515. }
  516. /* send the lower 5 bits of this byte, padded with 3 LBB, wait for the send to be completed */
  517. timeout = jiffies + msecs_to_jiffies(40);
  518. cx24123_writereg(state, 0x22, (data) & 0xff );
  519. while ((cx24123_readreg(state, 0x20) & 0x80)) {
  520. if (time_after(jiffies, timeout)) {
  521. printk("%s: demodulator is not responding, possibly hung, aborting.\n", __FUNCTION__);
  522. return -EREMOTEIO;
  523. }
  524. msleep(10);
  525. }
  526. /* Trigger the demod to configure the tuner */
  527. cx24123_writereg(state, 0x20, cx24123_readreg(state, 0x20) | 2);
  528. cx24123_writereg(state, 0x20, cx24123_readreg(state, 0x20) & 0xfd);
  529. return 0;
  530. }
  531. static int cx24123_pll_tune(struct dvb_frontend* fe, struct dvb_frontend_parameters *p)
  532. {
  533. struct cx24123_state *state = fe->demodulator_priv;
  534. u8 val;
  535. dprintk("frequency=%i\n", p->frequency);
  536. if (cx24123_pll_calculate(fe, p) != 0) {
  537. printk("%s: cx24123_pll_calcutate failed\n",__FUNCTION__);
  538. return -EINVAL;
  539. }
  540. /* Write the new VCO/VGA */
  541. cx24123_pll_writereg(fe, p, state->VCAarg);
  542. cx24123_pll_writereg(fe, p, state->VGAarg);
  543. /* Write the new bandselect and pll args */
  544. cx24123_pll_writereg(fe, p, state->bandselectarg);
  545. cx24123_pll_writereg(fe, p, state->pllarg);
  546. /* set the FILTUNE voltage */
  547. val = cx24123_readreg(state, 0x28) & ~0x3;
  548. cx24123_writereg(state, 0x27, state->FILTune >> 2);
  549. cx24123_writereg(state, 0x28, val | (state->FILTune & 0x3));
  550. dprintk("%s: pll tune VCA=%d, band=%d, pll=%d\n",__FUNCTION__,state->VCAarg,
  551. state->bandselectarg,state->pllarg);
  552. return 0;
  553. }
  554. static int cx24123_initfe(struct dvb_frontend* fe)
  555. {
  556. struct cx24123_state *state = fe->demodulator_priv;
  557. int i;
  558. dprintk("%s: init frontend\n",__FUNCTION__);
  559. /* Configure the demod to a good set of defaults */
  560. for (i = 0; i < sizeof(cx24123_regdata) / sizeof(cx24123_regdata[0]); i++)
  561. cx24123_writereg(state, cx24123_regdata[i].reg, cx24123_regdata[i].data);
  562. return 0;
  563. }
  564. static int cx24123_set_voltage(struct dvb_frontend* fe, fe_sec_voltage_t voltage)
  565. {
  566. struct cx24123_state *state = fe->demodulator_priv;
  567. u8 val;
  568. val = cx24123_readreg(state, 0x29) & ~0x40;
  569. switch (voltage) {
  570. case SEC_VOLTAGE_13:
  571. dprintk("%s: setting voltage 13V\n", __FUNCTION__);
  572. return cx24123_writereg(state, 0x29, val & 0x7f);
  573. case SEC_VOLTAGE_18:
  574. dprintk("%s: setting voltage 18V\n", __FUNCTION__);
  575. return cx24123_writereg(state, 0x29, val | 0x80);
  576. default:
  577. return -EINVAL;
  578. };
  579. return 0;
  580. }
  581. /* wait for diseqc queue to become ready (or timeout) */
  582. static void cx24123_wait_for_diseqc(struct cx24123_state *state)
  583. {
  584. unsigned long timeout = jiffies + msecs_to_jiffies(200);
  585. while (!(cx24123_readreg(state, 0x29) & 0x40)) {
  586. if(time_after(jiffies, timeout)) {
  587. printk("%s: diseqc queue not ready, command may be lost.\n", __FUNCTION__);
  588. break;
  589. }
  590. msleep(10);
  591. }
  592. }
  593. static int cx24123_send_diseqc_msg(struct dvb_frontend* fe, struct dvb_diseqc_master_cmd *cmd)
  594. {
  595. struct cx24123_state *state = fe->demodulator_priv;
  596. int i, val, tone;
  597. dprintk("%s:\n",__FUNCTION__);
  598. /* stop continuous tone if enabled */
  599. tone = cx24123_readreg(state, 0x29);
  600. if (tone & 0x10)
  601. cx24123_writereg(state, 0x29, tone & ~0x50);
  602. /* wait for diseqc queue ready */
  603. cx24123_wait_for_diseqc(state);
  604. /* select tone mode */
  605. cx24123_writereg(state, 0x2a, cx24123_readreg(state, 0x2a) & 0xfb);
  606. for (i = 0; i < cmd->msg_len; i++)
  607. cx24123_writereg(state, 0x2C + i, cmd->msg[i]);
  608. val = cx24123_readreg(state, 0x29);
  609. cx24123_writereg(state, 0x29, ((val & 0x90) | 0x40) | ((cmd->msg_len-3) & 3));
  610. /* wait for diseqc message to finish sending */
  611. cx24123_wait_for_diseqc(state);
  612. /* restart continuous tone if enabled */
  613. if (tone & 0x10) {
  614. cx24123_writereg(state, 0x29, tone & ~0x40);
  615. }
  616. return 0;
  617. }
  618. static int cx24123_diseqc_send_burst(struct dvb_frontend* fe, fe_sec_mini_cmd_t burst)
  619. {
  620. struct cx24123_state *state = fe->demodulator_priv;
  621. int val, tone;
  622. dprintk("%s:\n", __FUNCTION__);
  623. /* stop continuous tone if enabled */
  624. tone = cx24123_readreg(state, 0x29);
  625. if (tone & 0x10)
  626. cx24123_writereg(state, 0x29, tone & ~0x50);
  627. /* wait for diseqc queue ready */
  628. cx24123_wait_for_diseqc(state);
  629. /* select tone mode */
  630. cx24123_writereg(state, 0x2a, cx24123_readreg(state, 0x2a) | 0x4);
  631. msleep(30);
  632. val = cx24123_readreg(state, 0x29);
  633. if (burst == SEC_MINI_A)
  634. cx24123_writereg(state, 0x29, ((val & 0x90) | 0x40 | 0x00));
  635. else if (burst == SEC_MINI_B)
  636. cx24123_writereg(state, 0x29, ((val & 0x90) | 0x40 | 0x08));
  637. else
  638. return -EINVAL;
  639. cx24123_wait_for_diseqc(state);
  640. cx24123_writereg(state, 0x2a, cx24123_readreg(state, 0x2a) & 0xfb);
  641. /* restart continuous tone if enabled */
  642. if (tone & 0x10) {
  643. cx24123_writereg(state, 0x29, tone & ~0x40);
  644. }
  645. return 0;
  646. }
  647. static int cx24123_read_status(struct dvb_frontend* fe, fe_status_t* status)
  648. {
  649. struct cx24123_state *state = fe->demodulator_priv;
  650. int sync = cx24123_readreg(state, 0x14);
  651. int lock = cx24123_readreg(state, 0x20);
  652. *status = 0;
  653. if (lock & 0x01)
  654. *status |= FE_HAS_SIGNAL;
  655. if (sync & 0x02)
  656. *status |= FE_HAS_CARRIER;
  657. if (sync & 0x04)
  658. *status |= FE_HAS_VITERBI;
  659. if (sync & 0x08)
  660. *status |= FE_HAS_SYNC;
  661. if (sync & 0x80)
  662. *status |= FE_HAS_LOCK;
  663. return 0;
  664. }
  665. /*
  666. * Configured to return the measurement of errors in blocks, because no UCBLOCKS value
  667. * is available, so this value doubles up to satisfy both measurements
  668. */
  669. static int cx24123_read_ber(struct dvb_frontend* fe, u32* ber)
  670. {
  671. struct cx24123_state *state = fe->demodulator_priv;
  672. state->lastber =
  673. ((cx24123_readreg(state, 0x1c) & 0x3f) << 16) |
  674. (cx24123_readreg(state, 0x1d) << 8 |
  675. cx24123_readreg(state, 0x1e));
  676. /* Do the signal quality processing here, it's derived from the BER. */
  677. /* Scale the BER from a 24bit to a SNR 16 bit where higher = better */
  678. if (state->lastber < 5000)
  679. state->snr = 655*100;
  680. else if ( (state->lastber >= 5000) && (state->lastber < 55000) )
  681. state->snr = 655*90;
  682. else if ( (state->lastber >= 55000) && (state->lastber < 150000) )
  683. state->snr = 655*80;
  684. else if ( (state->lastber >= 150000) && (state->lastber < 250000) )
  685. state->snr = 655*70;
  686. else if ( (state->lastber >= 250000) && (state->lastber < 450000) )
  687. state->snr = 655*65;
  688. else
  689. state->snr = 0;
  690. dprintk("%s: BER = %d, S/N index = %d\n",__FUNCTION__,state->lastber, state->snr);
  691. *ber = state->lastber;
  692. return 0;
  693. }
  694. static int cx24123_read_signal_strength(struct dvb_frontend* fe, u16* signal_strength)
  695. {
  696. struct cx24123_state *state = fe->demodulator_priv;
  697. *signal_strength = cx24123_readreg(state, 0x3b) << 8; /* larger = better */
  698. dprintk("%s: Signal strength = %d\n",__FUNCTION__,*signal_strength);
  699. return 0;
  700. }
  701. static int cx24123_read_snr(struct dvb_frontend* fe, u16* snr)
  702. {
  703. struct cx24123_state *state = fe->demodulator_priv;
  704. *snr = state->snr;
  705. dprintk("%s: read S/N index = %d\n",__FUNCTION__,*snr);
  706. return 0;
  707. }
  708. static int cx24123_read_ucblocks(struct dvb_frontend* fe, u32* ucblocks)
  709. {
  710. struct cx24123_state *state = fe->demodulator_priv;
  711. *ucblocks = state->lastber;
  712. dprintk("%s: ucblocks (ber) = %d\n",__FUNCTION__,*ucblocks);
  713. return 0;
  714. }
  715. static int cx24123_set_frontend(struct dvb_frontend* fe, struct dvb_frontend_parameters *p)
  716. {
  717. struct cx24123_state *state = fe->demodulator_priv;
  718. dprintk("%s: set_frontend\n",__FUNCTION__);
  719. if (state->config->set_ts_params)
  720. state->config->set_ts_params(fe, 0);
  721. state->currentfreq=p->frequency;
  722. state->currentsymbolrate = p->u.qpsk.symbol_rate;
  723. cx24123_set_inversion(state, p->inversion);
  724. cx24123_set_fec(state, p->u.qpsk.fec_inner);
  725. cx24123_set_symbolrate(state, p->u.qpsk.symbol_rate);
  726. cx24123_pll_tune(fe, p);
  727. /* Enable automatic aquisition and reset cycle */
  728. cx24123_writereg(state, 0x03, (cx24123_readreg(state, 0x03) | 0x07));
  729. cx24123_writereg(state, 0x00, 0x10);
  730. cx24123_writereg(state, 0x00, 0);
  731. return 0;
  732. }
  733. static int cx24123_get_frontend(struct dvb_frontend* fe, struct dvb_frontend_parameters *p)
  734. {
  735. struct cx24123_state *state = fe->demodulator_priv;
  736. dprintk("%s: get_frontend\n",__FUNCTION__);
  737. if (cx24123_get_inversion(state, &p->inversion) != 0) {
  738. printk("%s: Failed to get inversion status\n",__FUNCTION__);
  739. return -EREMOTEIO;
  740. }
  741. if (cx24123_get_fec(state, &p->u.qpsk.fec_inner) != 0) {
  742. printk("%s: Failed to get fec status\n",__FUNCTION__);
  743. return -EREMOTEIO;
  744. }
  745. p->frequency = state->currentfreq;
  746. p->u.qpsk.symbol_rate = state->currentsymbolrate;
  747. return 0;
  748. }
  749. static int cx24123_set_tone(struct dvb_frontend* fe, fe_sec_tone_mode_t tone)
  750. {
  751. struct cx24123_state *state = fe->demodulator_priv;
  752. u8 val;
  753. /* wait for diseqc queue ready */
  754. cx24123_wait_for_diseqc(state);
  755. val = cx24123_readreg(state, 0x29) & ~0x40;
  756. switch (tone) {
  757. case SEC_TONE_ON:
  758. dprintk("%s: setting tone on\n", __FUNCTION__);
  759. return cx24123_writereg(state, 0x29, val | 0x10);
  760. case SEC_TONE_OFF:
  761. dprintk("%s: setting tone off\n",__FUNCTION__);
  762. return cx24123_writereg(state, 0x29, val & 0xef);
  763. default:
  764. printk("%s: CASE reached default with tone=%d\n", __FUNCTION__, tone);
  765. return -EINVAL;
  766. }
  767. return 0;
  768. }
  769. static void cx24123_release(struct dvb_frontend* fe)
  770. {
  771. struct cx24123_state* state = fe->demodulator_priv;
  772. dprintk("%s\n",__FUNCTION__);
  773. kfree(state);
  774. }
  775. static struct dvb_frontend_ops cx24123_ops;
  776. struct dvb_frontend* cx24123_attach(const struct cx24123_config* config,
  777. struct i2c_adapter* i2c)
  778. {
  779. struct cx24123_state* state = NULL;
  780. int ret;
  781. dprintk("%s\n",__FUNCTION__);
  782. /* allocate memory for the internal state */
  783. state = kmalloc(sizeof(struct cx24123_state), GFP_KERNEL);
  784. if (state == NULL) {
  785. printk("Unable to kmalloc\n");
  786. goto error;
  787. }
  788. /* setup the state */
  789. state->config = config;
  790. state->i2c = i2c;
  791. state->lastber = 0;
  792. state->snr = 0;
  793. state->VCAarg = 0;
  794. state->VGAarg = 0;
  795. state->bandselectarg = 0;
  796. state->pllarg = 0;
  797. state->currentfreq = 0;
  798. state->currentsymbolrate = 0;
  799. /* check if the demod is there */
  800. ret = cx24123_readreg(state, 0x00);
  801. if ((ret != 0xd1) && (ret != 0xe1)) {
  802. printk("Version != d1 or e1\n");
  803. goto error;
  804. }
  805. /* create dvb_frontend */
  806. memcpy(&state->frontend.ops, &cx24123_ops, sizeof(struct dvb_frontend_ops));
  807. state->frontend.demodulator_priv = state;
  808. return &state->frontend;
  809. error:
  810. kfree(state);
  811. return NULL;
  812. }
  813. static struct dvb_frontend_ops cx24123_ops = {
  814. .info = {
  815. .name = "Conexant CX24123/CX24109",
  816. .type = FE_QPSK,
  817. .frequency_min = 950000,
  818. .frequency_max = 2150000,
  819. .frequency_stepsize = 1011, /* kHz for QPSK frontends */
  820. .frequency_tolerance = 5000,
  821. .symbol_rate_min = 1000000,
  822. .symbol_rate_max = 45000000,
  823. .caps = FE_CAN_INVERSION_AUTO |
  824. FE_CAN_FEC_1_2 | FE_CAN_FEC_2_3 | FE_CAN_FEC_3_4 |
  825. FE_CAN_FEC_4_5 | FE_CAN_FEC_5_6 | FE_CAN_FEC_6_7 |
  826. FE_CAN_FEC_7_8 | FE_CAN_FEC_AUTO |
  827. FE_CAN_QPSK | FE_CAN_RECOVER
  828. },
  829. .release = cx24123_release,
  830. .init = cx24123_initfe,
  831. .set_frontend = cx24123_set_frontend,
  832. .get_frontend = cx24123_get_frontend,
  833. .read_status = cx24123_read_status,
  834. .read_ber = cx24123_read_ber,
  835. .read_signal_strength = cx24123_read_signal_strength,
  836. .read_snr = cx24123_read_snr,
  837. .read_ucblocks = cx24123_read_ucblocks,
  838. .diseqc_send_master_cmd = cx24123_send_diseqc_msg,
  839. .diseqc_send_burst = cx24123_diseqc_send_burst,
  840. .set_tone = cx24123_set_tone,
  841. .set_voltage = cx24123_set_voltage,
  842. };
  843. module_param(debug, int, 0644);
  844. MODULE_PARM_DESC(debug, "Activates frontend debugging (default:0)");
  845. module_param(force_band, int, 0644);
  846. MODULE_PARM_DESC(force_band, "Force a specific band select (1-9, default:off).");
  847. MODULE_DESCRIPTION("DVB Frontend module for Conexant cx24123/cx24109 hardware");
  848. MODULE_AUTHOR("Steven Toth");
  849. MODULE_LICENSE("GPL");
  850. EXPORT_SYMBOL(cx24123_attach);