cx24113.c 14 KB

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