cx24123.c 27 KB

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