cx24123.c 29 KB

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