fc0012.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467
  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->addr, .flags = 0, .buf = buf, .len = 2
  27. };
  28. if (i2c_transfer(priv->i2c, &msg, 1) != 1) {
  29. err("I2C write reg failed, reg: %02x, val: %02x", reg, val);
  30. return -EREMOTEIO;
  31. }
  32. return 0;
  33. }
  34. static int fc0012_readreg(struct fc0012_priv *priv, u8 reg, u8 *val)
  35. {
  36. struct i2c_msg msg[2] = {
  37. { .addr = priv->addr, .flags = 0, .buf = &reg, .len = 1 },
  38. { .addr = priv->addr, .flags = I2C_M_RD, .buf = val, .len = 1 },
  39. };
  40. if (i2c_transfer(priv->i2c, msg, 2) != 2) {
  41. err("I2C read reg failed, reg: %02x", reg);
  42. return -EREMOTEIO;
  43. }
  44. return 0;
  45. }
  46. static int fc0012_release(struct dvb_frontend *fe)
  47. {
  48. kfree(fe->tuner_priv);
  49. fe->tuner_priv = NULL;
  50. return 0;
  51. }
  52. static int fc0012_init(struct dvb_frontend *fe)
  53. {
  54. struct fc0012_priv *priv = fe->tuner_priv;
  55. int i, ret = 0;
  56. unsigned char reg[] = {
  57. 0x00, /* dummy reg. 0 */
  58. 0x05, /* reg. 0x01 */
  59. 0x10, /* reg. 0x02 */
  60. 0x00, /* reg. 0x03 */
  61. 0x00, /* reg. 0x04 */
  62. 0x0f, /* reg. 0x05: may also be 0x0a */
  63. 0x00, /* reg. 0x06: divider 2, VCO slow */
  64. 0x00, /* reg. 0x07: may also be 0x0f */
  65. 0xff, /* reg. 0x08: AGC Clock divide by 256, AGC gain 1/256,
  66. Loop Bw 1/8 */
  67. 0x6e, /* reg. 0x09: Disable LoopThrough, Enable LoopThrough: 0x6f */
  68. 0xb8, /* reg. 0x0a: Disable LO Test Buffer */
  69. 0x82, /* reg. 0x0b: Output Clock is same as clock frequency,
  70. may also be 0x83 */
  71. 0xfc, /* reg. 0x0c: depending on AGC Up-Down mode, may need 0xf8 */
  72. 0x02, /* reg. 0x0d: AGC Not Forcing & LNA Forcing, 0x02 for DVB-T */
  73. 0x00, /* reg. 0x0e */
  74. 0x00, /* reg. 0x0f */
  75. 0x00, /* reg. 0x10: may also be 0x0d */
  76. 0x00, /* reg. 0x11 */
  77. 0x1f, /* reg. 0x12: Set to maximum gain */
  78. 0x08, /* reg. 0x13: Set to Middle Gain: 0x08,
  79. Low Gain: 0x00, High Gain: 0x10, enable IX2: 0x80 */
  80. 0x00, /* reg. 0x14 */
  81. 0x04, /* reg. 0x15: Enable LNA COMPS */
  82. };
  83. switch (priv->xtal_freq) {
  84. case FC_XTAL_27_MHZ:
  85. case FC_XTAL_28_8_MHZ:
  86. reg[0x07] |= 0x20;
  87. break;
  88. case FC_XTAL_36_MHZ:
  89. default:
  90. break;
  91. }
  92. if (priv->dual_master)
  93. reg[0x0c] |= 0x02;
  94. if (fe->ops.i2c_gate_ctrl)
  95. fe->ops.i2c_gate_ctrl(fe, 1); /* open I2C-gate */
  96. for (i = 1; i < sizeof(reg); i++) {
  97. ret = fc0012_writereg(priv, i, reg[i]);
  98. if (ret)
  99. break;
  100. }
  101. if (fe->ops.i2c_gate_ctrl)
  102. fe->ops.i2c_gate_ctrl(fe, 0); /* close I2C-gate */
  103. if (ret)
  104. err("fc0012_writereg failed: %d", ret);
  105. return ret;
  106. }
  107. static int fc0012_sleep(struct dvb_frontend *fe)
  108. {
  109. /* nothing to do here */
  110. return 0;
  111. }
  112. static int fc0012_set_params(struct dvb_frontend *fe)
  113. {
  114. struct fc0012_priv *priv = fe->tuner_priv;
  115. int i, ret = 0;
  116. struct dtv_frontend_properties *p = &fe->dtv_property_cache;
  117. u32 freq = p->frequency / 1000;
  118. u32 delsys = p->delivery_system;
  119. unsigned char reg[7], am, pm, multi, tmp;
  120. unsigned long f_vco;
  121. unsigned short xtal_freq_khz_2, xin, xdiv;
  122. int vco_select = false;
  123. if (fe->callback) {
  124. ret = fe->callback(priv->i2c, DVB_FRONTEND_COMPONENT_TUNER,
  125. FC_FE_CALLBACK_VHF_ENABLE, (freq > 300000 ? 0 : 1));
  126. if (ret)
  127. goto exit;
  128. }
  129. switch (priv->xtal_freq) {
  130. case FC_XTAL_27_MHZ:
  131. xtal_freq_khz_2 = 27000 / 2;
  132. break;
  133. case FC_XTAL_36_MHZ:
  134. xtal_freq_khz_2 = 36000 / 2;
  135. break;
  136. case FC_XTAL_28_8_MHZ:
  137. default:
  138. xtal_freq_khz_2 = 28800 / 2;
  139. break;
  140. }
  141. /* select frequency divider and the frequency of VCO */
  142. if (freq < 37084) { /* freq * 96 < 3560000 */
  143. multi = 96;
  144. reg[5] = 0x82;
  145. reg[6] = 0x00;
  146. } else if (freq < 55625) { /* freq * 64 < 3560000 */
  147. multi = 64;
  148. reg[5] = 0x82;
  149. reg[6] = 0x02;
  150. } else if (freq < 74167) { /* freq * 48 < 3560000 */
  151. multi = 48;
  152. reg[5] = 0x42;
  153. reg[6] = 0x00;
  154. } else if (freq < 111250) { /* freq * 32 < 3560000 */
  155. multi = 32;
  156. reg[5] = 0x42;
  157. reg[6] = 0x02;
  158. } else if (freq < 148334) { /* freq * 24 < 3560000 */
  159. multi = 24;
  160. reg[5] = 0x22;
  161. reg[6] = 0x00;
  162. } else if (freq < 222500) { /* freq * 16 < 3560000 */
  163. multi = 16;
  164. reg[5] = 0x22;
  165. reg[6] = 0x02;
  166. } else if (freq < 296667) { /* freq * 12 < 3560000 */
  167. multi = 12;
  168. reg[5] = 0x12;
  169. reg[6] = 0x00;
  170. } else if (freq < 445000) { /* freq * 8 < 3560000 */
  171. multi = 8;
  172. reg[5] = 0x12;
  173. reg[6] = 0x02;
  174. } else if (freq < 593334) { /* freq * 6 < 3560000 */
  175. multi = 6;
  176. reg[5] = 0x0a;
  177. reg[6] = 0x00;
  178. } else {
  179. multi = 4;
  180. reg[5] = 0x0a;
  181. reg[6] = 0x02;
  182. }
  183. f_vco = freq * multi;
  184. if (f_vco >= 3060000) {
  185. reg[6] |= 0x08;
  186. vco_select = true;
  187. }
  188. if (freq >= 45000) {
  189. /* From divided value (XDIV) determined the FA and FP value */
  190. xdiv = (unsigned short)(f_vco / xtal_freq_khz_2);
  191. if ((f_vco - xdiv * xtal_freq_khz_2) >= (xtal_freq_khz_2 / 2))
  192. xdiv++;
  193. pm = (unsigned char)(xdiv / 8);
  194. am = (unsigned char)(xdiv - (8 * pm));
  195. if (am < 2) {
  196. reg[1] = am + 8;
  197. reg[2] = pm - 1;
  198. } else {
  199. reg[1] = am;
  200. reg[2] = pm;
  201. }
  202. } else {
  203. /* fix for frequency less than 45 MHz */
  204. reg[1] = 0x06;
  205. reg[2] = 0x11;
  206. }
  207. /* fix clock out */
  208. reg[6] |= 0x20;
  209. /* From VCO frequency determines the XIN ( fractional part of Delta
  210. Sigma PLL) and divided value (XDIV) */
  211. xin = (unsigned short)(f_vco - (f_vco / xtal_freq_khz_2) * xtal_freq_khz_2);
  212. xin = (xin << 15) / xtal_freq_khz_2;
  213. if (xin >= 16384)
  214. xin += 32768;
  215. reg[3] = xin >> 8; /* xin with 9 bit resolution */
  216. reg[4] = xin & 0xff;
  217. if (delsys == SYS_DVBT) {
  218. reg[6] &= 0x3f; /* bits 6 and 7 describe the bandwidth */
  219. switch (p->bandwidth_hz) {
  220. case 6000000:
  221. reg[6] |= 0x80;
  222. break;
  223. case 7000000:
  224. reg[6] |= 0x40;
  225. break;
  226. case 8000000:
  227. default:
  228. break;
  229. }
  230. } else {
  231. err("%s: modulation type not supported!", __func__);
  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. warn("%s: failed: %d", __func__, ret);
  284. return ret;
  285. }
  286. static int fc0012_get_frequency(struct dvb_frontend *fe, u32 *frequency)
  287. {
  288. struct fc0012_priv *priv = fe->tuner_priv;
  289. *frequency = priv->frequency;
  290. return 0;
  291. }
  292. static int fc0012_get_if_frequency(struct dvb_frontend *fe, u32 *frequency)
  293. {
  294. /* CHECK: always ? */
  295. *frequency = 0;
  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. 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. warn("%s: failed: %d", __func__, ret);
  359. return ret;
  360. }
  361. static const struct dvb_tuner_ops fc0012_tuner_ops = {
  362. .info = {
  363. .name = "Fitipower FC0012",
  364. .frequency_min = 37000000, /* estimate */
  365. .frequency_max = 862000000, /* estimate */
  366. .frequency_step = 0,
  367. },
  368. .release = fc0012_release,
  369. .init = fc0012_init,
  370. .sleep = fc0012_sleep,
  371. .set_params = fc0012_set_params,
  372. .get_frequency = fc0012_get_frequency,
  373. .get_if_frequency = fc0012_get_if_frequency,
  374. .get_bandwidth = fc0012_get_bandwidth,
  375. .get_rf_strength = fc0012_get_rf_strength,
  376. };
  377. struct dvb_frontend *fc0012_attach(struct dvb_frontend *fe,
  378. struct i2c_adapter *i2c, u8 i2c_address, int dual_master,
  379. enum fc001x_xtal_freq xtal_freq)
  380. {
  381. struct fc0012_priv *priv = NULL;
  382. priv = kzalloc(sizeof(struct fc0012_priv), GFP_KERNEL);
  383. if (priv == NULL)
  384. return NULL;
  385. priv->i2c = i2c;
  386. priv->dual_master = dual_master;
  387. priv->addr = i2c_address;
  388. priv->xtal_freq = xtal_freq;
  389. info("Fitipower FC0012 successfully attached.");
  390. fe->tuner_priv = priv;
  391. memcpy(&fe->ops.tuner_ops, &fc0012_tuner_ops,
  392. sizeof(struct dvb_tuner_ops));
  393. return fe;
  394. }
  395. EXPORT_SYMBOL(fc0012_attach);
  396. MODULE_DESCRIPTION("Fitipower FC0012 silicon tuner driver");
  397. MODULE_AUTHOR("Hans-Frieder Vogt <hfvogt@gmx.net>");
  398. MODULE_LICENSE("GPL");
  399. MODULE_VERSION("0.6");