fc0012.c 12 KB

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