cx24123.c 29 KB

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