PageRenderTime 53ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/drivers/media/tuners/fc0012.c

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