cx24123.c 29 KB

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