fc0012.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513
  1. /*
  2. * Fitipower FC0012 tuner driver
  3. *
  4. * Copyright (C) 2012 Hans-Frieder Vogt <hfvogt@gmx.net>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. */
  16. #include "fc0012.h"
  17. #include "fc0012-priv.h"
  18. static int fc0012_writereg(struct fc0012_priv *priv, u8 reg, u8 val)
  19. {
  20. u8 buf[2] = {reg, val};
  21. struct i2c_msg msg = {
  22. .addr = priv->cfg->i2c_address, .flags = 0, .buf = buf, .len = 2
  23. };
  24. if (i2c_transfer(priv->i2c, &msg, 1) != 1) {
  25. dev_err(&priv->i2c->dev,
  26. "%s: I2C write reg failed, reg: %02x, val: %02x\n",
  27. KBUILD_MODNAME, reg, val);
  28. return -EREMOTEIO;
  29. }
  30. return 0;
  31. }
  32. static int fc0012_readreg(struct fc0012_priv *priv, u8 reg, u8 *val)
  33. {
  34. struct i2c_msg msg[2] = {
  35. { .addr = priv->cfg->i2c_address, .flags = 0,
  36. .buf = &reg, .len = 1 },
  37. { .addr = priv->cfg->i2c_address, .flags = I2C_M_RD,
  38. .buf = val, .len = 1 },
  39. };
  40. if (i2c_transfer(priv->i2c, msg, 2) != 2) {
  41. dev_err(&priv->i2c->dev,
  42. "%s: I2C read reg failed, reg: %02x\n",
  43. KBUILD_MODNAME, reg);
  44. return -EREMOTEIO;
  45. }
  46. return 0;
  47. }
  48. static void fc0012_release(struct dvb_frontend *fe)
  49. {
  50. kfree(fe->tuner_priv);
  51. fe->tuner_priv = NULL;
  52. }
  53. static int fc0012_init(struct dvb_frontend *fe)
  54. {
  55. struct fc0012_priv *priv = fe->tuner_priv;
  56. int i, ret = 0;
  57. unsigned char reg[] = {
  58. 0x00, /* dummy reg. 0 */
  59. 0x05, /* reg. 0x01 */
  60. 0x10, /* reg. 0x02 */
  61. 0x00, /* reg. 0x03 */
  62. 0x00, /* reg. 0x04 */
  63. 0x0f, /* reg. 0x05: may also be 0x0a */
  64. 0x00, /* reg. 0x06: divider 2, VCO slow */
  65. 0x00, /* reg. 0x07: may also be 0x0f */
  66. 0xff, /* reg. 0x08: AGC Clock divide by 256, AGC gain 1/256,
  67. Loop Bw 1/8 */
  68. 0x6e, /* reg. 0x09: Disable LoopThrough, Enable LoopThrough: 0x6f */
  69. 0xb8, /* reg. 0x0a: Disable LO Test Buffer */
  70. 0x82, /* reg. 0x0b: Output Clock is same as clock frequency,
  71. may also be 0x83 */
  72. 0xfc, /* reg. 0x0c: depending on AGC Up-Down mode, may need 0xf8 */
  73. 0x02, /* reg. 0x0d: AGC Not Forcing & LNA Forcing, 0x02 for DVB-T */
  74. 0x00, /* reg. 0x0e */
  75. 0x00, /* reg. 0x0f */
  76. 0x00, /* reg. 0x10: may also be 0x0d */
  77. 0x00, /* reg. 0x11 */
  78. 0x1f, /* reg. 0x12: Set to maximum gain */
  79. 0x08, /* reg. 0x13: Set to Middle Gain: 0x08,
  80. Low Gain: 0x00, High Gain: 0x10, enable IX2: 0x80 */
  81. 0x00, /* reg. 0x14 */
  82. 0x04, /* reg. 0x15: Enable LNA COMPS */
  83. };
  84. switch (priv->cfg->xtal_freq) {
  85. case FC_XTAL_27_MHZ:
  86. case FC_XTAL_28_8_MHZ:
  87. reg[0x07] |= 0x20;
  88. break;
  89. case FC_XTAL_36_MHZ:
  90. default:
  91. break;
  92. }
  93. if (priv->cfg->dual_master)
  94. reg[0x0c] |= 0x02;
  95. if (priv->cfg->loop_through)
  96. reg[0x09] |= 0x01;
  97. if (fe->ops.i2c_gate_ctrl)
  98. fe->ops.i2c_gate_ctrl(fe, 1); /* open I2C-gate */
  99. for (i = 1; i < sizeof(reg); i++) {
  100. ret = fc0012_writereg(priv, i, reg[i]);
  101. if (ret)
  102. break;
  103. }
  104. if (fe->ops.i2c_gate_ctrl)
  105. fe->ops.i2c_gate_ctrl(fe, 0); /* close I2C-gate */
  106. if (ret)
  107. dev_err(&priv->i2c->dev, "%s: fc0012_writereg failed: %d\n",
  108. KBUILD_MODNAME, ret);
  109. return ret;
  110. }
  111. static int fc0012_set_params(struct dvb_frontend *fe)
  112. {
  113. struct fc0012_priv *priv = fe->tuner_priv;
  114. int i, ret = 0;
  115. struct dtv_frontend_properties *p = &fe->dtv_property_cache;
  116. u32 freq = p->frequency / 1000;
  117. u32 delsys = p->delivery_system;
  118. unsigned char reg[7], am, pm, multi, tmp;
  119. unsigned long f_vco;
  120. unsigned short xtal_freq_khz_2, xin, xdiv;
  121. bool vco_select = false;
  122. if (fe->callback) {
  123. ret = fe->callback(priv->i2c, DVB_FRONTEND_COMPONENT_TUNER,
  124. FC_FE_CALLBACK_VHF_ENABLE, (freq > 300000 ? 0 : 1));
  125. if (ret)
  126. goto exit;
  127. }
  128. switch (priv->cfg->xtal_freq) {
  129. case FC_XTAL_27_MHZ:
  130. xtal_freq_khz_2 = 27000 / 2;
  131. break;
  132. case FC_XTAL_36_MHZ:
  133. xtal_freq_khz_2 = 36000 / 2;
  134. break;
  135. case FC_XTAL_28_8_MHZ:
  136. default:
  137. xtal_freq_khz_2 = 28800 / 2;
  138. break;
  139. }
  140. /* select frequency divider and the frequency of VCO */
  141. if (freq < 37084) { /* freq * 96 < 3560000 */
  142. multi = 96;
  143. reg[5] = 0x82;
  144. reg[6] = 0x00;
  145. } else if (freq < 55625) { /* freq * 64 < 3560000 */
  146. multi = 64;
  147. reg[5] = 0x82;
  148. reg[6] = 0x02;
  149. } else if (freq < 74167) { /* freq * 48 < 3560000 */
  150. multi = 48;
  151. reg[5] = 0x42;
  152. reg[6] = 0x00;
  153. } else if (freq < 111250) { /* freq * 32 < 3560000 */
  154. multi = 32;
  155. reg[5] = 0x42;
  156. reg[6] = 0x02;
  157. } else if (freq < 148334) { /* freq * 24 < 3560000 */
  158. multi = 24;
  159. reg[5] = 0x22;
  160. reg[6] = 0x00;
  161. } else if (freq < 222500) { /* freq * 16 < 3560000 */
  162. multi = 16;
  163. reg[5] = 0x22;
  164. reg[6] = 0x02;
  165. } else if (freq < 296667) { /* freq * 12 < 3560000 */
  166. multi = 12;
  167. reg[5] = 0x12;
  168. reg[6] = 0x00;
  169. } else if (freq < 445000) { /* freq * 8 < 3560000 */
  170. multi = 8;
  171. reg[5] = 0x12;
  172. reg[6] = 0x02;
  173. } else if (freq < 593334) { /* freq * 6 < 3560000 */
  174. multi = 6;
  175. reg[5] = 0x0a;
  176. reg[6] = 0x00;
  177. } else {
  178. multi = 4;
  179. reg[5] = 0x0a;
  180. reg[6] = 0x02;
  181. }
  182. f_vco = freq * multi;
  183. if (f_vco >= 3060000) {
  184. reg[6] |= 0x08;
  185. vco_select = true;
  186. }
  187. if (freq >= 45000) {
  188. /* From divided value (XDIV) determined the FA and FP value */
  189. xdiv = (unsigned short)(f_vco / xtal_freq_khz_2);
  190. if ((f_vco - xdiv * xtal_freq_khz_2) >= (xtal_freq_khz_2 / 2))
  191. xdiv++;
  192. pm = (unsigned char)(xdiv / 8);
  193. am = (unsigned char)(xdiv - (8 * pm));
  194. if (am < 2) {
  195. reg[1] = am + 8;
  196. reg[2] = pm - 1;
  197. } else {
  198. reg[1] = am;
  199. reg[2] = pm;
  200. }
  201. } else {
  202. /* fix for frequency less than 45 MHz */
  203. reg[1] = 0x06;
  204. reg[2] = 0x11;
  205. }
  206. /* fix clock out */
  207. reg[6] |= 0x20;
  208. /* From VCO frequency determines the XIN ( fractional part of Delta
  209. Sigma PLL) and divided value (XDIV) */
  210. xin = (unsigned short)(f_vco - (f_vco / xtal_freq_khz_2) * xtal_freq_khz_2);
  211. xin = (xin << 15) / xtal_freq_khz_2;
  212. if (xin >= 16384)
  213. xin += 32768;
  214. reg[3] = xin >> 8; /* xin with 9 bit resolution */
  215. reg[4] = xin & 0xff;
  216. if (delsys == SYS_DVBT) {
  217. reg[6] &= 0x3f; /* bits 6 and 7 describe the bandwidth */
  218. switch (p->bandwidth_hz) {
  219. case 6000000:
  220. reg[6] |= 0x80;
  221. break;
  222. case 7000000:
  223. reg[6] |= 0x40;
  224. break;
  225. case 8000000:
  226. default:
  227. break;
  228. }
  229. } else {
  230. dev_err(&priv->i2c->dev, "%s: modulation type not supported!\n",
  231. KBUILD_MODNAME);
  232. return -EINVAL;
  233. }
  234. /* modified for Realtek demod */
  235. reg[5] |= 0x07;
  236. if (fe->ops.i2c_gate_ctrl)
  237. fe->ops.i2c_gate_ctrl(fe, 1); /* open I2C-gate */
  238. for (i = 1; i <= 6; i++) {
  239. ret = fc0012_writereg(priv, i, reg[i]);
  240. if (ret)
  241. goto exit;
  242. }
  243. /* VCO Calibration */
  244. ret = fc0012_writereg(priv, 0x0e, 0x80);
  245. if (!ret)
  246. ret = fc0012_writereg(priv, 0x0e, 0x00);
  247. /* VCO Re-Calibration if needed */
  248. if (!ret)
  249. ret = fc0012_writereg(priv, 0x0e, 0x00);
  250. if (!ret) {
  251. msleep(10);
  252. ret = fc0012_readreg(priv, 0x0e, &tmp);
  253. }
  254. if (ret)
  255. goto exit;
  256. /* vco selection */
  257. tmp &= 0x3f;
  258. if (vco_select) {
  259. if (tmp > 0x3c) {
  260. reg[6] &= ~0x08;
  261. ret = fc0012_writereg(priv, 0x06, reg[6]);
  262. if (!ret)
  263. ret = fc0012_writereg(priv, 0x0e, 0x80);
  264. if (!ret)
  265. ret = fc0012_writereg(priv, 0x0e, 0x00);
  266. }
  267. } else {
  268. if (tmp < 0x02) {
  269. reg[6] |= 0x08;
  270. ret = fc0012_writereg(priv, 0x06, reg[6]);
  271. if (!ret)
  272. ret = fc0012_writereg(priv, 0x0e, 0x80);
  273. if (!ret)
  274. ret = fc0012_writereg(priv, 0x0e, 0x00);
  275. }
  276. }
  277. priv->frequency = p->frequency;
  278. priv->bandwidth = p->bandwidth_hz;
  279. exit:
  280. if (fe->ops.i2c_gate_ctrl)
  281. fe->ops.i2c_gate_ctrl(fe, 0); /* close I2C-gate */
  282. if (ret)
  283. dev_warn(&priv->i2c->dev, "%s: %s failed: %d\n",
  284. KBUILD_MODNAME, __func__, ret);
  285. return ret;
  286. }
  287. static int fc0012_get_frequency(struct dvb_frontend *fe, u32 *frequency)
  288. {
  289. struct fc0012_priv *priv = fe->tuner_priv;
  290. *frequency = priv->frequency;
  291. return 0;
  292. }
  293. static int fc0012_get_if_frequency(struct dvb_frontend *fe, u32 *frequency)
  294. {
  295. *frequency = 0; /* Zero-IF */
  296. return 0;
  297. }
  298. static int fc0012_get_bandwidth(struct dvb_frontend *fe, u32 *bandwidth)
  299. {
  300. struct fc0012_priv *priv = fe->tuner_priv;
  301. *bandwidth = priv->bandwidth;
  302. return 0;
  303. }
  304. #define INPUT_ADC_LEVEL -8
  305. static int fc0012_get_rf_strength(struct dvb_frontend *fe, u16 *strength)
  306. {
  307. struct fc0012_priv *priv = fe->tuner_priv;
  308. int ret;
  309. unsigned char tmp;
  310. int int_temp, lna_gain, int_lna, tot_agc_gain, power;
  311. static const int fc0012_lna_gain_table[] = {
  312. /* low gain */
  313. -63, -58, -99, -73,
  314. -63, -65, -54, -60,
  315. /* middle gain */
  316. 71, 70, 68, 67,
  317. 65, 63, 61, 58,
  318. /* high gain */
  319. 197, 191, 188, 186,
  320. 184, 182, 181, 179,
  321. };
  322. if (fe->ops.i2c_gate_ctrl)
  323. fe->ops.i2c_gate_ctrl(fe, 1); /* open I2C-gate */
  324. ret = fc0012_writereg(priv, 0x12, 0x00);
  325. if (ret)
  326. goto err;
  327. ret = fc0012_readreg(priv, 0x12, &tmp);
  328. if (ret)
  329. goto err;
  330. int_temp = tmp;
  331. ret = fc0012_readreg(priv, 0x13, &tmp);
  332. if (ret)
  333. goto err;
  334. lna_gain = tmp & 0x1f;
  335. if (fe->ops.i2c_gate_ctrl)
  336. fe->ops.i2c_gate_ctrl(fe, 0); /* close I2C-gate */
  337. if (lna_gain < ARRAY_SIZE(fc0012_lna_gain_table)) {
  338. int_lna = fc0012_lna_gain_table[lna_gain];
  339. tot_agc_gain = (abs((int_temp >> 5) - 7) - 2 +
  340. (int_temp & 0x1f)) * 2;
  341. power = INPUT_ADC_LEVEL - tot_agc_gain - int_lna / 10;
  342. if (power >= 45)
  343. *strength = 255; /* 100% */
  344. else if (power < -95)
  345. *strength = 0;
  346. else
  347. *strength = (power + 95) * 255 / 140;
  348. *strength |= *strength << 8;
  349. } else {
  350. ret = -1;
  351. }
  352. goto exit;
  353. err:
  354. if (fe->ops.i2c_gate_ctrl)
  355. fe->ops.i2c_gate_ctrl(fe, 0); /* close I2C-gate */
  356. exit:
  357. if (ret)
  358. dev_warn(&priv->i2c->dev, "%s: %s failed: %d\n",
  359. KBUILD_MODNAME, __func__, ret);
  360. return ret;
  361. }
  362. static const struct dvb_tuner_ops fc0012_tuner_ops = {
  363. .info = {
  364. .name = "Fitipower FC0012",
  365. .frequency_min_hz = 37 * MHz, /* estimate */
  366. .frequency_max_hz = 862 * MHz, /* estimate */
  367. },
  368. .release = fc0012_release,
  369. .init = fc0012_init,
  370. .set_params = fc0012_set_params,
  371. .get_frequency = fc0012_get_frequency,
  372. .get_if_frequency = fc0012_get_if_frequency,
  373. .get_bandwidth = fc0012_get_bandwidth,
  374. .get_rf_strength = fc0012_get_rf_strength,
  375. };
  376. struct dvb_frontend *fc0012_attach(struct dvb_frontend *fe,
  377. struct i2c_adapter *i2c, const struct fc0012_config *cfg)
  378. {
  379. struct fc0012_priv *priv;
  380. int ret;
  381. u8 chip_id;
  382. if (fe->ops.i2c_gate_ctrl)
  383. fe->ops.i2c_gate_ctrl(fe, 1);
  384. priv = kzalloc(sizeof(struct fc0012_priv), GFP_KERNEL);
  385. if (!priv) {
  386. ret = -ENOMEM;
  387. dev_err(&i2c->dev, "%s: kzalloc() failed\n", KBUILD_MODNAME);
  388. goto err;
  389. }
  390. priv->cfg = cfg;
  391. priv->i2c = i2c;
  392. /* check if the tuner is there */
  393. ret = fc0012_readreg(priv, 0x00, &chip_id);
  394. if (ret < 0)
  395. goto err;
  396. dev_dbg(&i2c->dev, "%s: chip_id=%02x\n", __func__, chip_id);
  397. switch (chip_id) {
  398. case 0xa1:
  399. break;
  400. default:
  401. ret = -ENODEV;
  402. goto err;
  403. }
  404. dev_info(&i2c->dev, "%s: Fitipower FC0012 successfully identified\n",
  405. KBUILD_MODNAME);
  406. if (priv->cfg->loop_through) {
  407. ret = fc0012_writereg(priv, 0x09, 0x6f);
  408. if (ret < 0)
  409. goto err;
  410. }
  411. /*
  412. * TODO: Clock out en or div?
  413. * For dual tuner configuration clearing bit [0] is required.
  414. */
  415. if (priv->cfg->clock_out) {
  416. ret = fc0012_writereg(priv, 0x0b, 0x82);
  417. if (ret < 0)
  418. goto err;
  419. }
  420. fe->tuner_priv = priv;
  421. memcpy(&fe->ops.tuner_ops, &fc0012_tuner_ops,
  422. sizeof(struct dvb_tuner_ops));
  423. err:
  424. if (fe->ops.i2c_gate_ctrl)
  425. fe->ops.i2c_gate_ctrl(fe, 0);
  426. if (ret) {
  427. dev_dbg(&i2c->dev, "%s: failed: %d\n", __func__, ret);
  428. kfree(priv);
  429. return NULL;
  430. }
  431. return fe;
  432. }
  433. EXPORT_SYMBOL(fc0012_attach);
  434. MODULE_DESCRIPTION("Fitipower FC0012 silicon tuner driver");
  435. MODULE_AUTHOR("Hans-Frieder Vogt <hfvogt@gmx.net>");
  436. MODULE_LICENSE("GPL");
  437. MODULE_VERSION("0.6");