cx24123.c 29 KB

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