cx24113.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613
  1. /*
  2. * Driver for Conexant CX24113/CX24128 Tuner (Satellite)
  3. *
  4. * Copyright (C) 2007-8 Patrick Boettcher <pb@linuxtv.org>
  5. *
  6. * Developed for BBTI / Technisat
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. *
  17. * GNU General Public License for more details.
  18. */
  19. #include <linux/slab.h>
  20. #include <linux/kernel.h>
  21. #include <linux/module.h>
  22. #include <linux/init.h>
  23. #include "dvb_frontend.h"
  24. #include "cx24113.h"
  25. static int debug;
  26. #define cx_info(args...) do { printk(KERN_INFO "CX24113: " args); } while (0)
  27. #define cx_err(args...) do { printk(KERN_ERR "CX24113: " args); } while (0)
  28. #define dprintk(args...) \
  29. do { \
  30. if (debug) { \
  31. printk(KERN_DEBUG "CX24113: %s: ", __func__); \
  32. printk(args); \
  33. } \
  34. } while (0)
  35. struct cx24113_state {
  36. struct i2c_adapter *i2c;
  37. const struct cx24113_config *config;
  38. #define REV_CX24113 0x23
  39. u8 rev;
  40. u8 ver;
  41. u8 icp_mode:1;
  42. #define ICP_LEVEL1 0
  43. #define ICP_LEVEL2 1
  44. #define ICP_LEVEL3 2
  45. #define ICP_LEVEL4 3
  46. u8 icp_man:2;
  47. u8 icp_auto_low:2;
  48. u8 icp_auto_mlow:2;
  49. u8 icp_auto_mhi:2;
  50. u8 icp_auto_hi:2;
  51. u8 icp_dig;
  52. #define LNA_MIN_GAIN 0
  53. #define LNA_MID_GAIN 1
  54. #define LNA_MAX_GAIN 2
  55. u8 lna_gain:2;
  56. u8 acp_on:1;
  57. u8 vco_mode:2;
  58. u8 vco_shift:1;
  59. #define VCOBANDSEL_6 0x80
  60. #define VCOBANDSEL_5 0x01
  61. #define VCOBANDSEL_4 0x02
  62. #define VCOBANDSEL_3 0x04
  63. #define VCOBANDSEL_2 0x08
  64. #define VCOBANDSEL_1 0x10
  65. u8 vco_band;
  66. #define VCODIV4 4
  67. #define VCODIV2 2
  68. u8 vcodiv;
  69. u8 bs_delay:4;
  70. u16 bs_freqcnt:13;
  71. u16 bs_rdiv;
  72. u8 prescaler_mode:1;
  73. u8 rfvga_bias_ctrl;
  74. s16 tuner_gain_thres;
  75. u8 gain_level;
  76. u32 frequency;
  77. u8 refdiv;
  78. u8 Fwindow_enabled;
  79. };
  80. static int cx24113_writereg(struct cx24113_state *state, int reg, int data)
  81. {
  82. u8 buf[] = { reg, data };
  83. struct i2c_msg msg = { .addr = state->config->i2c_addr,
  84. .flags = 0, .buf = buf, .len = 2 };
  85. int err = i2c_transfer(state->i2c, &msg, 1);
  86. if (err != 1) {
  87. printk(KERN_DEBUG "%s: writereg error(err == %i, reg == 0x%02x, data == 0x%02x)\n",
  88. __func__, err, reg, data);
  89. return err;
  90. }
  91. return 0;
  92. }
  93. static int cx24113_readreg(struct cx24113_state *state, u8 reg)
  94. {
  95. int ret;
  96. u8 b;
  97. struct i2c_msg msg[] = {
  98. { .addr = state->config->i2c_addr,
  99. .flags = 0, .buf = &reg, .len = 1 },
  100. { .addr = state->config->i2c_addr,
  101. .flags = I2C_M_RD, .buf = &b, .len = 1 }
  102. };
  103. ret = i2c_transfer(state->i2c, msg, 2);
  104. if (ret != 2) {
  105. printk(KERN_DEBUG "%s: reg=0x%x (error=%d)\n",
  106. __func__, reg, ret);
  107. return ret;
  108. }
  109. return b;
  110. }
  111. static void cx24113_set_parameters(struct cx24113_state *state)
  112. {
  113. u8 r;
  114. r = cx24113_readreg(state, 0x10) & 0x82;
  115. r |= state->icp_mode;
  116. r |= state->icp_man << 4;
  117. r |= state->icp_dig << 2;
  118. r |= state->prescaler_mode << 5;
  119. cx24113_writereg(state, 0x10, r);
  120. r = (state->icp_auto_low << 0) | (state->icp_auto_mlow << 2)
  121. | (state->icp_auto_mhi << 4) | (state->icp_auto_hi << 6);
  122. cx24113_writereg(state, 0x11, r);
  123. if (state->rev == REV_CX24113) {
  124. r = cx24113_readreg(state, 0x20) & 0xec;
  125. r |= state->lna_gain;
  126. r |= state->rfvga_bias_ctrl << 4;
  127. cx24113_writereg(state, 0x20, r);
  128. }
  129. r = cx24113_readreg(state, 0x12) & 0x03;
  130. r |= state->acp_on << 2;
  131. r |= state->bs_delay << 4;
  132. cx24113_writereg(state, 0x12, r);
  133. r = cx24113_readreg(state, 0x18) & 0x40;
  134. r |= state->vco_shift;
  135. if (state->vco_band == VCOBANDSEL_6)
  136. r |= (1 << 7);
  137. else
  138. r |= (state->vco_band << 1);
  139. cx24113_writereg(state, 0x18, r);
  140. r = cx24113_readreg(state, 0x14) & 0x20;
  141. r |= (state->vco_mode << 6) | ((state->bs_freqcnt >> 8) & 0x1f);
  142. cx24113_writereg(state, 0x14, r);
  143. cx24113_writereg(state, 0x15, (state->bs_freqcnt & 0xff));
  144. cx24113_writereg(state, 0x16, (state->bs_rdiv >> 4) & 0xff);
  145. r = (cx24113_readreg(state, 0x17) & 0x0f) |
  146. ((state->bs_rdiv & 0x0f) << 4);
  147. cx24113_writereg(state, 0x17, r);
  148. }
  149. #define VGA_0 0x00
  150. #define VGA_1 0x04
  151. #define VGA_2 0x02
  152. #define VGA_3 0x06
  153. #define VGA_4 0x01
  154. #define VGA_5 0x05
  155. #define VGA_6 0x03
  156. #define VGA_7 0x07
  157. #define RFVGA_0 0x00
  158. #define RFVGA_1 0x01
  159. #define RFVGA_2 0x02
  160. #define RFVGA_3 0x03
  161. static int cx24113_set_gain_settings(struct cx24113_state *state,
  162. s16 power_estimation)
  163. {
  164. u8 ampout = cx24113_readreg(state, 0x1d) & 0xf0,
  165. vga = cx24113_readreg(state, 0x1f) & 0x3f,
  166. rfvga = cx24113_readreg(state, 0x20) & 0xf3;
  167. u8 gain_level = power_estimation >= state->tuner_gain_thres;
  168. dprintk("power estimation: %d, thres: %d, gain_level: %d/%d\n",
  169. power_estimation, state->tuner_gain_thres,
  170. state->gain_level, gain_level);
  171. if (gain_level == state->gain_level)
  172. return 0; /* nothing to be done */
  173. ampout |= 0xf;
  174. if (gain_level) {
  175. rfvga |= RFVGA_0 << 2;
  176. vga |= (VGA_7 << 3) | VGA_7;
  177. } else {
  178. rfvga |= RFVGA_2 << 2;
  179. vga |= (VGA_6 << 3) | VGA_2;
  180. }
  181. state->gain_level = gain_level;
  182. cx24113_writereg(state, 0x1d, ampout);
  183. cx24113_writereg(state, 0x1f, vga);
  184. cx24113_writereg(state, 0x20, rfvga);
  185. return 1; /* did something */
  186. }
  187. static int cx24113_set_Fref(struct cx24113_state *state, u8 high)
  188. {
  189. u8 xtal = cx24113_readreg(state, 0x02);
  190. if (state->rev == 0x43 && state->vcodiv == VCODIV4)
  191. high = 1;
  192. xtal &= ~0x2;
  193. if (high)
  194. xtal |= high << 1;
  195. return cx24113_writereg(state, 0x02, xtal);
  196. }
  197. static int cx24113_enable(struct cx24113_state *state, u8 enable)
  198. {
  199. u8 r21 = (cx24113_readreg(state, 0x21) & 0xc0) | enable;
  200. if (state->rev == REV_CX24113)
  201. r21 |= (1 << 1);
  202. return cx24113_writereg(state, 0x21, r21);
  203. }
  204. static int cx24113_set_bandwidth(struct cx24113_state *state, u32 bandwidth_khz)
  205. {
  206. u8 r;
  207. if (bandwidth_khz <= 19000)
  208. r = 0x03 << 6;
  209. else if (bandwidth_khz <= 25000)
  210. r = 0x02 << 6;
  211. else
  212. r = 0x01 << 6;
  213. dprintk("bandwidth to be set: %d\n", bandwidth_khz);
  214. bandwidth_khz *= 10;
  215. bandwidth_khz -= 10000;
  216. bandwidth_khz /= 1000;
  217. bandwidth_khz += 5;
  218. bandwidth_khz /= 10;
  219. dprintk("bandwidth: %d %d\n", r >> 6, bandwidth_khz);
  220. r |= bandwidth_khz & 0x3f;
  221. return cx24113_writereg(state, 0x1e, r);
  222. }
  223. static int cx24113_set_clk_inversion(struct cx24113_state *state, u8 on)
  224. {
  225. u8 r = (cx24113_readreg(state, 0x10) & 0x7f) | ((on & 0x1) << 7);
  226. return cx24113_writereg(state, 0x10, r);
  227. }
  228. static int cx24113_get_status(struct dvb_frontend *fe, u32 *status)
  229. {
  230. struct cx24113_state *state = fe->tuner_priv;
  231. u8 r = (cx24113_readreg(state, 0x10) & 0x02) >> 1;
  232. if (r)
  233. *status |= TUNER_STATUS_LOCKED;
  234. dprintk("PLL locked: %d\n", r);
  235. return 0;
  236. }
  237. static u8 cx24113_set_ref_div(struct cx24113_state *state, u8 refdiv)
  238. {
  239. if (state->rev == 0x43 && state->vcodiv == VCODIV4)
  240. refdiv = 2;
  241. return state->refdiv = refdiv;
  242. }
  243. static void cx24113_calc_pll_nf(struct cx24113_state *state, u16 *n, s32 *f)
  244. {
  245. s32 N;
  246. s64 F;
  247. u64 dividend;
  248. u8 R, r;
  249. u8 vcodiv;
  250. u8 factor;
  251. s32 freq_hz = state->frequency * 1000;
  252. if (state->config->xtal_khz < 20000)
  253. factor = 1;
  254. else
  255. factor = 2;
  256. if (state->rev == REV_CX24113) {
  257. if (state->frequency >= 1100000)
  258. vcodiv = VCODIV2;
  259. else
  260. vcodiv = VCODIV4;
  261. } else {
  262. if (state->frequency >= 1165000)
  263. vcodiv = VCODIV2;
  264. else
  265. vcodiv = VCODIV4;
  266. }
  267. state->vcodiv = vcodiv;
  268. dprintk("calculating N/F for %dHz with vcodiv %d\n", freq_hz, vcodiv);
  269. R = 0;
  270. do {
  271. R = cx24113_set_ref_div(state, R + 1);
  272. /* calculate tuner PLL settings: */
  273. N = (freq_hz / 100 * vcodiv) * R;
  274. N /= (state->config->xtal_khz) * factor * 2;
  275. N += 5; /* For round up. */
  276. N /= 10;
  277. N -= 32;
  278. } while (N < 6 && R < 3);
  279. if (N < 6) {
  280. cx_err("strange frequency: N < 6\n");
  281. return;
  282. }
  283. F = freq_hz;
  284. F *= (u64) (R * vcodiv * 262144);
  285. dprintk("1 N: %d, F: %lld, R: %d\n", N, (long long)F, R);
  286. /* do_div needs an u64 as first argument */
  287. dividend = F;
  288. do_div(dividend, state->config->xtal_khz * 1000 * factor * 2);
  289. F = dividend;
  290. dprintk("2 N: %d, F: %lld, R: %d\n", N, (long long)F, R);
  291. F -= (N + 32) * 262144;
  292. dprintk("3 N: %d, F: %lld, R: %d\n", N, (long long)F, R);
  293. if (state->Fwindow_enabled) {
  294. if (F > (262144 / 2 - 1638))
  295. F = 262144 / 2 - 1638;
  296. if (F < (-262144 / 2 + 1638))
  297. F = -262144 / 2 + 1638;
  298. if ((F < 3277 && F > 0) || (F > -3277 && F < 0)) {
  299. F = 0;
  300. r = cx24113_readreg(state, 0x10);
  301. cx24113_writereg(state, 0x10, r | (1 << 6));
  302. }
  303. }
  304. dprintk("4 N: %d, F: %lld, R: %d\n", N, (long long)F, R);
  305. *n = (u16) N;
  306. *f = (s32) F;
  307. }
  308. static void cx24113_set_nfr(struct cx24113_state *state, u16 n, s32 f, u8 r)
  309. {
  310. u8 reg;
  311. cx24113_writereg(state, 0x19, (n >> 1) & 0xff);
  312. reg = ((n & 0x1) << 7) | ((f >> 11) & 0x7f);
  313. cx24113_writereg(state, 0x1a, reg);
  314. cx24113_writereg(state, 0x1b, (f >> 3) & 0xff);
  315. reg = cx24113_readreg(state, 0x1c) & 0x1f;
  316. cx24113_writereg(state, 0x1c, reg | ((f & 0x7) << 5));
  317. cx24113_set_Fref(state, r - 1);
  318. }
  319. static int cx24113_set_frequency(struct cx24113_state *state, u32 frequency)
  320. {
  321. u8 r = 1; /* or 2 */
  322. u16 n = 6;
  323. s32 f = 0;
  324. r = cx24113_readreg(state, 0x14);
  325. cx24113_writereg(state, 0x14, r & 0x3f);
  326. r = cx24113_readreg(state, 0x10);
  327. cx24113_writereg(state, 0x10, r & 0xbf);
  328. state->frequency = frequency;
  329. dprintk("tuning to frequency: %d\n", frequency);
  330. cx24113_calc_pll_nf(state, &n, &f);
  331. cx24113_set_nfr(state, n, f, state->refdiv);
  332. r = cx24113_readreg(state, 0x18) & 0xbf;
  333. if (state->vcodiv != VCODIV2)
  334. r |= 1 << 6;
  335. cx24113_writereg(state, 0x18, r);
  336. /* The need for this sleep is not clear. But helps in some cases */
  337. msleep(5);
  338. r = cx24113_readreg(state, 0x1c) & 0xef;
  339. cx24113_writereg(state, 0x1c, r | (1 << 4));
  340. return 0;
  341. }
  342. static int cx24113_init(struct dvb_frontend *fe)
  343. {
  344. struct cx24113_state *state = fe->tuner_priv;
  345. int ret;
  346. state->tuner_gain_thres = -50;
  347. state->gain_level = 255; /* to force a gain-setting initialization */
  348. state->icp_mode = 0;
  349. if (state->config->xtal_khz < 11000) {
  350. state->icp_auto_hi = ICP_LEVEL4;
  351. state->icp_auto_mhi = ICP_LEVEL4;
  352. state->icp_auto_mlow = ICP_LEVEL3;
  353. state->icp_auto_low = ICP_LEVEL3;
  354. } else {
  355. state->icp_auto_hi = ICP_LEVEL4;
  356. state->icp_auto_mhi = ICP_LEVEL4;
  357. state->icp_auto_mlow = ICP_LEVEL3;
  358. state->icp_auto_low = ICP_LEVEL2;
  359. }
  360. state->icp_dig = ICP_LEVEL3;
  361. state->icp_man = ICP_LEVEL1;
  362. state->acp_on = 1;
  363. state->vco_mode = 0;
  364. state->vco_shift = 0;
  365. state->vco_band = VCOBANDSEL_1;
  366. state->bs_delay = 8;
  367. state->bs_freqcnt = 0x0fff;
  368. state->bs_rdiv = 0x0fff;
  369. state->prescaler_mode = 0;
  370. state->lna_gain = LNA_MAX_GAIN;
  371. state->rfvga_bias_ctrl = 1;
  372. state->Fwindow_enabled = 1;
  373. cx24113_set_Fref(state, 0);
  374. cx24113_enable(state, 0x3d);
  375. cx24113_set_parameters(state);
  376. cx24113_set_gain_settings(state, -30);
  377. cx24113_set_bandwidth(state, 18025);
  378. cx24113_set_clk_inversion(state, 1);
  379. if (state->config->xtal_khz >= 40000)
  380. ret = cx24113_writereg(state, 0x02,
  381. (cx24113_readreg(state, 0x02) & 0xfb) | (1 << 2));
  382. else
  383. ret = cx24113_writereg(state, 0x02,
  384. (cx24113_readreg(state, 0x02) & 0xfb) | (0 << 2));
  385. return ret;
  386. }
  387. static int cx24113_set_params(struct dvb_frontend *fe)
  388. {
  389. struct dtv_frontend_properties *c = &fe->dtv_property_cache;
  390. struct cx24113_state *state = fe->tuner_priv;
  391. /* for a ROLL-OFF factor of 0.35, 0.2: 600, 0.25: 625 */
  392. u32 roll_off = 675;
  393. u32 bw;
  394. bw = ((c->symbol_rate/100) * roll_off) / 1000;
  395. bw += (10000000/100) + 5;
  396. bw /= 10;
  397. bw += 1000;
  398. cx24113_set_bandwidth(state, bw);
  399. cx24113_set_frequency(state, c->frequency);
  400. msleep(5);
  401. return cx24113_get_status(fe, &bw);
  402. }
  403. static s8 cx24113_agc_table[2][10] = {
  404. {-54, -41, -35, -30, -25, -21, -16, -10, -6, -2},
  405. {-39, -35, -30, -25, -19, -15, -11, -5, 1, 9},
  406. };
  407. void cx24113_agc_callback(struct dvb_frontend *fe)
  408. {
  409. struct cx24113_state *state = fe->tuner_priv;
  410. s16 s, i;
  411. if (!fe->ops.read_signal_strength)
  412. return;
  413. do {
  414. /* this only works with the current CX24123 implementation */
  415. fe->ops.read_signal_strength(fe, (u16 *) &s);
  416. s >>= 8;
  417. dprintk("signal strength: %d\n", s);
  418. for (i = 0; i < sizeof(cx24113_agc_table[0]); i++)
  419. if (cx24113_agc_table[state->gain_level][i] > s)
  420. break;
  421. s = -25 - i*5;
  422. } while (cx24113_set_gain_settings(state, s));
  423. }
  424. EXPORT_SYMBOL(cx24113_agc_callback);
  425. static int cx24113_get_frequency(struct dvb_frontend *fe, u32 *frequency)
  426. {
  427. struct cx24113_state *state = fe->tuner_priv;
  428. *frequency = state->frequency;
  429. return 0;
  430. }
  431. static void cx24113_release(struct dvb_frontend *fe)
  432. {
  433. struct cx24113_state *state = fe->tuner_priv;
  434. dprintk("\n");
  435. fe->tuner_priv = NULL;
  436. kfree(state);
  437. }
  438. static const struct dvb_tuner_ops cx24113_tuner_ops = {
  439. .info = {
  440. .name = "Conexant CX24113",
  441. .frequency_min = 950000,
  442. .frequency_max = 2150000,
  443. .frequency_step = 125,
  444. },
  445. .release = cx24113_release,
  446. .init = cx24113_init,
  447. .set_params = cx24113_set_params,
  448. .get_frequency = cx24113_get_frequency,
  449. .get_status = cx24113_get_status,
  450. };
  451. struct dvb_frontend *cx24113_attach(struct dvb_frontend *fe,
  452. const struct cx24113_config *config, struct i2c_adapter *i2c)
  453. {
  454. /* allocate memory for the internal state */
  455. struct cx24113_state *state =
  456. kzalloc(sizeof(struct cx24113_state), GFP_KERNEL);
  457. int rc;
  458. if (state == NULL) {
  459. cx_err("Unable to kzalloc\n");
  460. goto error;
  461. }
  462. /* setup the state */
  463. state->config = config;
  464. state->i2c = i2c;
  465. cx_info("trying to detect myself\n");
  466. /* making a dummy read, because of some expected troubles
  467. * after power on */
  468. cx24113_readreg(state, 0x00);
  469. rc = cx24113_readreg(state, 0x00);
  470. if (rc < 0) {
  471. cx_info("CX24113 not found.\n");
  472. goto error;
  473. }
  474. state->rev = rc;
  475. switch (rc) {
  476. case 0x43:
  477. cx_info("detected CX24113 variant\n");
  478. break;
  479. case REV_CX24113:
  480. cx_info("successfully detected\n");
  481. break;
  482. default:
  483. cx_err("unsupported device id: %x\n", state->rev);
  484. goto error;
  485. }
  486. state->ver = cx24113_readreg(state, 0x01);
  487. cx_info("version: %x\n", state->ver);
  488. /* create dvb_frontend */
  489. memcpy(&fe->ops.tuner_ops, &cx24113_tuner_ops,
  490. sizeof(struct dvb_tuner_ops));
  491. fe->tuner_priv = state;
  492. return fe;
  493. error:
  494. kfree(state);
  495. return NULL;
  496. }
  497. EXPORT_SYMBOL(cx24113_attach);
  498. module_param(debug, int, 0644);
  499. MODULE_PARM_DESC(debug, "Activates frontend debugging (default:0)");
  500. MODULE_AUTHOR("Patrick Boettcher <pb@linuxtv.org>");
  501. MODULE_DESCRIPTION("DVB Frontend module for Conexant CX24113/CX24128hardware");
  502. MODULE_LICENSE("GPL");