cx24123.c 29 KB

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