cx24123.c 27 KB

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