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