cx24123.c 28 KB

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