mt2060.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514
  1. /*
  2. * Driver for Microtune MT2060 "Single chip dual conversion broadband tuner"
  3. *
  4. * Copyright (c) 2006 Olivier DANET <odanet@caramail.com>
  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. *
  15. * GNU General Public License for more details.
  16. */
  17. /* In that file, frequencies are expressed in kiloHertz to avoid 32 bits overflows */
  18. #include <linux/module.h>
  19. #include <linux/delay.h>
  20. #include <linux/dvb/frontend.h>
  21. #include <linux/i2c.h>
  22. #include <linux/slab.h>
  23. #include "dvb_frontend.h"
  24. #include "mt2060.h"
  25. #include "mt2060_priv.h"
  26. static int debug;
  27. module_param(debug, int, 0644);
  28. MODULE_PARM_DESC(debug, "Turn on/off debugging (default:off).");
  29. #define dprintk(args...) do { if (debug) {printk(KERN_DEBUG "MT2060: " args); printk("\n"); }} while (0)
  30. // Reads a single register
  31. static int mt2060_readreg(struct mt2060_priv *priv, u8 reg, u8 *val)
  32. {
  33. struct i2c_msg msg[2] = {
  34. { .addr = priv->cfg->i2c_address, .flags = 0, .buf = &reg, .len = 1 },
  35. { .addr = priv->cfg->i2c_address, .flags = I2C_M_RD, .buf = val, .len = 1 },
  36. };
  37. if (i2c_transfer(priv->i2c, msg, 2) != 2) {
  38. printk(KERN_WARNING "mt2060 I2C read failed\n");
  39. return -EREMOTEIO;
  40. }
  41. return 0;
  42. }
  43. // Writes a single register
  44. static int mt2060_writereg(struct mt2060_priv *priv, u8 reg, u8 val)
  45. {
  46. u8 buf[2] = { reg, val };
  47. struct i2c_msg msg = {
  48. .addr = priv->cfg->i2c_address, .flags = 0, .buf = buf, .len = 2
  49. };
  50. if (i2c_transfer(priv->i2c, &msg, 1) != 1) {
  51. printk(KERN_WARNING "mt2060 I2C write failed\n");
  52. return -EREMOTEIO;
  53. }
  54. return 0;
  55. }
  56. // Writes a set of consecutive registers
  57. static int mt2060_writeregs(struct mt2060_priv *priv,u8 *buf, u8 len)
  58. {
  59. int rem, val_len;
  60. u8 xfer_buf[16];
  61. struct i2c_msg msg = {
  62. .addr = priv->cfg->i2c_address, .flags = 0, .buf = xfer_buf
  63. };
  64. for (rem = len - 1; rem > 0; rem -= priv->i2c_max_regs) {
  65. val_len = min_t(int, rem, priv->i2c_max_regs);
  66. msg.len = 1 + val_len;
  67. xfer_buf[0] = buf[0] + len - 1 - rem;
  68. memcpy(&xfer_buf[1], &buf[1 + len - 1 - rem], val_len);
  69. if (i2c_transfer(priv->i2c, &msg, 1) != 1) {
  70. printk(KERN_WARNING "mt2060 I2C write failed (len=%i)\n", val_len);
  71. return -EREMOTEIO;
  72. }
  73. }
  74. return 0;
  75. }
  76. // Initialisation sequences
  77. // LNABAND=3, NUM1=0x3C, DIV1=0x74, NUM2=0x1080, DIV2=0x49
  78. static u8 mt2060_config1[] = {
  79. REG_LO1C1,
  80. 0x3F, 0x74, 0x00, 0x08, 0x93
  81. };
  82. // FMCG=2, GP2=0, GP1=0
  83. static u8 mt2060_config2[] = {
  84. REG_MISC_CTRL,
  85. 0x20, 0x1E, 0x30, 0xff, 0x80, 0xff, 0x00, 0x2c, 0x42
  86. };
  87. // VGAG=3, V1CSE=1
  88. #ifdef MT2060_SPURCHECK
  89. /* The function below calculates the frequency offset between the output frequency if2
  90. and the closer cross modulation subcarrier between lo1 and lo2 up to the tenth harmonic */
  91. static int mt2060_spurcalc(u32 lo1,u32 lo2,u32 if2)
  92. {
  93. int I,J;
  94. int dia,diamin,diff;
  95. diamin=1000000;
  96. for (I = 1; I < 10; I++) {
  97. J = ((2*I*lo1)/lo2+1)/2;
  98. diff = I*(int)lo1-J*(int)lo2;
  99. if (diff < 0) diff=-diff;
  100. dia = (diff-(int)if2);
  101. if (dia < 0) dia=-dia;
  102. if (diamin > dia) diamin=dia;
  103. }
  104. return diamin;
  105. }
  106. #define BANDWIDTH 4000 // kHz
  107. /* Calculates the frequency offset to add to avoid spurs. Returns 0 if no offset is needed */
  108. static int mt2060_spurcheck(u32 lo1,u32 lo2,u32 if2)
  109. {
  110. u32 Spur,Sp1,Sp2;
  111. int I,J;
  112. I=0;
  113. J=1000;
  114. Spur=mt2060_spurcalc(lo1,lo2,if2);
  115. if (Spur < BANDWIDTH) {
  116. /* Potential spurs detected */
  117. dprintk("Spurs before : f_lo1: %d f_lo2: %d (kHz)",
  118. (int)lo1,(int)lo2);
  119. I=1000;
  120. Sp1 = mt2060_spurcalc(lo1+I,lo2+I,if2);
  121. Sp2 = mt2060_spurcalc(lo1-I,lo2-I,if2);
  122. if (Sp1 < Sp2) {
  123. J=-J; I=-I; Spur=Sp2;
  124. } else
  125. Spur=Sp1;
  126. while (Spur < BANDWIDTH) {
  127. I += J;
  128. Spur = mt2060_spurcalc(lo1+I,lo2+I,if2);
  129. }
  130. dprintk("Spurs after : f_lo1: %d f_lo2: %d (kHz)",
  131. (int)(lo1+I),(int)(lo2+I));
  132. }
  133. return I;
  134. }
  135. #endif
  136. #define IF2 36150 // IF2 frequency = 36.150 MHz
  137. #define FREF 16000 // Quartz oscillator 16 MHz
  138. static int mt2060_set_params(struct dvb_frontend *fe)
  139. {
  140. struct dtv_frontend_properties *c = &fe->dtv_property_cache;
  141. struct mt2060_priv *priv;
  142. int i=0;
  143. u32 freq;
  144. u8 lnaband;
  145. u32 f_lo1,f_lo2;
  146. u32 div1,num1,div2,num2;
  147. u8 b[8];
  148. u32 if1;
  149. priv = fe->tuner_priv;
  150. if1 = priv->if1_freq;
  151. b[0] = REG_LO1B1;
  152. b[1] = 0xFF;
  153. if (fe->ops.i2c_gate_ctrl)
  154. fe->ops.i2c_gate_ctrl(fe, 1); /* open i2c_gate */
  155. mt2060_writeregs(priv,b,2);
  156. freq = c->frequency / 1000; /* Hz -> kHz */
  157. f_lo1 = freq + if1 * 1000;
  158. f_lo1 = (f_lo1 / 250) * 250;
  159. f_lo2 = f_lo1 - freq - IF2;
  160. // From the Comtech datasheet, the step used is 50kHz. The tuner chip could be more precise
  161. f_lo2 = ((f_lo2 + 25) / 50) * 50;
  162. priv->frequency = (f_lo1 - f_lo2 - IF2) * 1000,
  163. #ifdef MT2060_SPURCHECK
  164. // LO-related spurs detection and correction
  165. num1 = mt2060_spurcheck(f_lo1,f_lo2,IF2);
  166. f_lo1 += num1;
  167. f_lo2 += num1;
  168. #endif
  169. //Frequency LO1 = 16MHz * (DIV1 + NUM1/64 )
  170. num1 = f_lo1 / (FREF / 64);
  171. div1 = num1 / 64;
  172. num1 &= 0x3f;
  173. // Frequency LO2 = 16MHz * (DIV2 + NUM2/8192 )
  174. num2 = f_lo2 * 64 / (FREF / 128);
  175. div2 = num2 / 8192;
  176. num2 &= 0x1fff;
  177. if (freq <= 95000) lnaband = 0xB0; else
  178. if (freq <= 180000) lnaband = 0xA0; else
  179. if (freq <= 260000) lnaband = 0x90; else
  180. if (freq <= 335000) lnaband = 0x80; else
  181. if (freq <= 425000) lnaband = 0x70; else
  182. if (freq <= 480000) lnaband = 0x60; else
  183. if (freq <= 570000) lnaband = 0x50; else
  184. if (freq <= 645000) lnaband = 0x40; else
  185. if (freq <= 730000) lnaband = 0x30; else
  186. if (freq <= 810000) lnaband = 0x20; else lnaband = 0x10;
  187. b[0] = REG_LO1C1;
  188. b[1] = lnaband | ((num1 >>2) & 0x0F);
  189. b[2] = div1;
  190. b[3] = (num2 & 0x0F) | ((num1 & 3) << 4);
  191. b[4] = num2 >> 4;
  192. b[5] = ((num2 >>12) & 1) | (div2 << 1);
  193. dprintk("IF1: %dMHz",(int)if1);
  194. dprintk("PLL freq=%dkHz f_lo1=%dkHz f_lo2=%dkHz",(int)freq,(int)f_lo1,(int)f_lo2);
  195. dprintk("PLL div1=%d num1=%d div2=%d num2=%d",(int)div1,(int)num1,(int)div2,(int)num2);
  196. dprintk("PLL [1..5]: %2x %2x %2x %2x %2x",(int)b[1],(int)b[2],(int)b[3],(int)b[4],(int)b[5]);
  197. mt2060_writeregs(priv,b,6);
  198. //Waits for pll lock or timeout
  199. i = 0;
  200. do {
  201. mt2060_readreg(priv,REG_LO_STATUS,b);
  202. if ((b[0] & 0x88)==0x88)
  203. break;
  204. msleep(4);
  205. i++;
  206. } while (i<10);
  207. if (fe->ops.i2c_gate_ctrl)
  208. fe->ops.i2c_gate_ctrl(fe, 0); /* close i2c_gate */
  209. return 0;
  210. }
  211. static void mt2060_calibrate(struct mt2060_priv *priv)
  212. {
  213. u8 b = 0;
  214. int i = 0;
  215. if (mt2060_writeregs(priv,mt2060_config1,sizeof(mt2060_config1)))
  216. return;
  217. if (mt2060_writeregs(priv,mt2060_config2,sizeof(mt2060_config2)))
  218. return;
  219. /* initialize the clock output */
  220. mt2060_writereg(priv, REG_VGAG, (priv->cfg->clock_out << 6) | 0x30);
  221. do {
  222. b |= (1 << 6); // FM1SS;
  223. mt2060_writereg(priv, REG_LO2C1,b);
  224. msleep(20);
  225. if (i == 0) {
  226. b |= (1 << 7); // FM1CA;
  227. mt2060_writereg(priv, REG_LO2C1,b);
  228. b &= ~(1 << 7); // FM1CA;
  229. msleep(20);
  230. }
  231. b &= ~(1 << 6); // FM1SS
  232. mt2060_writereg(priv, REG_LO2C1,b);
  233. msleep(20);
  234. i++;
  235. } while (i < 9);
  236. i = 0;
  237. while (i++ < 10 && mt2060_readreg(priv, REG_MISC_STAT, &b) == 0 && (b & (1 << 6)) == 0)
  238. msleep(20);
  239. if (i <= 10) {
  240. mt2060_readreg(priv, REG_FM_FREQ, &priv->fmfreq); // now find out, what is fmreq used for :)
  241. dprintk("calibration was successful: %d", (int)priv->fmfreq);
  242. } else
  243. dprintk("FMCAL timed out");
  244. }
  245. static int mt2060_get_frequency(struct dvb_frontend *fe, u32 *frequency)
  246. {
  247. struct mt2060_priv *priv = fe->tuner_priv;
  248. *frequency = priv->frequency;
  249. return 0;
  250. }
  251. static int mt2060_get_if_frequency(struct dvb_frontend *fe, u32 *frequency)
  252. {
  253. *frequency = IF2 * 1000;
  254. return 0;
  255. }
  256. static int mt2060_init(struct dvb_frontend *fe)
  257. {
  258. struct mt2060_priv *priv = fe->tuner_priv;
  259. int ret;
  260. if (fe->ops.i2c_gate_ctrl)
  261. fe->ops.i2c_gate_ctrl(fe, 1); /* open i2c_gate */
  262. if (priv->sleep) {
  263. ret = mt2060_writereg(priv, REG_MISC_CTRL, 0x20);
  264. if (ret)
  265. goto err_i2c_gate_ctrl;
  266. }
  267. ret = mt2060_writereg(priv, REG_VGAG,
  268. (priv->cfg->clock_out << 6) | 0x33);
  269. err_i2c_gate_ctrl:
  270. if (fe->ops.i2c_gate_ctrl)
  271. fe->ops.i2c_gate_ctrl(fe, 0); /* close i2c_gate */
  272. return ret;
  273. }
  274. static int mt2060_sleep(struct dvb_frontend *fe)
  275. {
  276. struct mt2060_priv *priv = fe->tuner_priv;
  277. int ret;
  278. if (fe->ops.i2c_gate_ctrl)
  279. fe->ops.i2c_gate_ctrl(fe, 1); /* open i2c_gate */
  280. ret = mt2060_writereg(priv, REG_VGAG,
  281. (priv->cfg->clock_out << 6) | 0x30);
  282. if (ret)
  283. goto err_i2c_gate_ctrl;
  284. if (priv->sleep)
  285. ret = mt2060_writereg(priv, REG_MISC_CTRL, 0xe8);
  286. err_i2c_gate_ctrl:
  287. if (fe->ops.i2c_gate_ctrl)
  288. fe->ops.i2c_gate_ctrl(fe, 0); /* close i2c_gate */
  289. return ret;
  290. }
  291. static void mt2060_release(struct dvb_frontend *fe)
  292. {
  293. kfree(fe->tuner_priv);
  294. fe->tuner_priv = NULL;
  295. }
  296. static const struct dvb_tuner_ops mt2060_tuner_ops = {
  297. .info = {
  298. .name = "Microtune MT2060",
  299. .frequency_min = 48000000,
  300. .frequency_max = 860000000,
  301. .frequency_step = 50000,
  302. },
  303. .release = mt2060_release,
  304. .init = mt2060_init,
  305. .sleep = mt2060_sleep,
  306. .set_params = mt2060_set_params,
  307. .get_frequency = mt2060_get_frequency,
  308. .get_if_frequency = mt2060_get_if_frequency,
  309. };
  310. /* This functions tries to identify a MT2060 tuner by reading the PART/REV register. This is hasty. */
  311. struct dvb_frontend * mt2060_attach(struct dvb_frontend *fe, struct i2c_adapter *i2c, struct mt2060_config *cfg, u16 if1)
  312. {
  313. struct mt2060_priv *priv = NULL;
  314. u8 id = 0;
  315. priv = kzalloc(sizeof(struct mt2060_priv), GFP_KERNEL);
  316. if (priv == NULL)
  317. return NULL;
  318. priv->cfg = cfg;
  319. priv->i2c = i2c;
  320. priv->if1_freq = if1;
  321. priv->i2c_max_regs = ~0;
  322. if (fe->ops.i2c_gate_ctrl)
  323. fe->ops.i2c_gate_ctrl(fe, 1); /* open i2c_gate */
  324. if (mt2060_readreg(priv,REG_PART_REV,&id) != 0) {
  325. kfree(priv);
  326. return NULL;
  327. }
  328. if (id != PART_REV) {
  329. kfree(priv);
  330. return NULL;
  331. }
  332. printk(KERN_INFO "MT2060: successfully identified (IF1 = %d)\n", if1);
  333. memcpy(&fe->ops.tuner_ops, &mt2060_tuner_ops, sizeof(struct dvb_tuner_ops));
  334. fe->tuner_priv = priv;
  335. mt2060_calibrate(priv);
  336. if (fe->ops.i2c_gate_ctrl)
  337. fe->ops.i2c_gate_ctrl(fe, 0); /* close i2c_gate */
  338. return fe;
  339. }
  340. EXPORT_SYMBOL(mt2060_attach);
  341. static int mt2060_probe(struct i2c_client *client,
  342. const struct i2c_device_id *id)
  343. {
  344. struct mt2060_platform_data *pdata = client->dev.platform_data;
  345. struct dvb_frontend *fe;
  346. struct mt2060_priv *dev;
  347. int ret;
  348. u8 chip_id;
  349. dev_dbg(&client->dev, "\n");
  350. if (!pdata) {
  351. dev_err(&client->dev, "Cannot proceed without platform data\n");
  352. ret = -EINVAL;
  353. goto err;
  354. }
  355. dev = devm_kzalloc(&client->dev, sizeof(*dev), GFP_KERNEL);
  356. if (!dev) {
  357. ret = -ENOMEM;
  358. goto err;
  359. }
  360. fe = pdata->dvb_frontend;
  361. dev->config.i2c_address = client->addr;
  362. dev->config.clock_out = pdata->clock_out;
  363. dev->cfg = &dev->config;
  364. dev->i2c = client->adapter;
  365. dev->if1_freq = pdata->if1 ? pdata->if1 : 1220;
  366. dev->client = client;
  367. dev->i2c_max_regs = pdata->i2c_write_max ? pdata->i2c_write_max - 1 : ~0;
  368. dev->sleep = true;
  369. ret = mt2060_readreg(dev, REG_PART_REV, &chip_id);
  370. if (ret) {
  371. ret = -ENODEV;
  372. goto err;
  373. }
  374. dev_dbg(&client->dev, "chip id=%02x\n", chip_id);
  375. if (chip_id != PART_REV) {
  376. ret = -ENODEV;
  377. goto err;
  378. }
  379. /* Power on, calibrate, sleep */
  380. ret = mt2060_writereg(dev, REG_MISC_CTRL, 0x20);
  381. if (ret)
  382. goto err;
  383. mt2060_calibrate(dev);
  384. ret = mt2060_writereg(dev, REG_MISC_CTRL, 0xe8);
  385. if (ret)
  386. goto err;
  387. dev_info(&client->dev, "Microtune MT2060 successfully identified\n");
  388. memcpy(&fe->ops.tuner_ops, &mt2060_tuner_ops, sizeof(fe->ops.tuner_ops));
  389. fe->ops.tuner_ops.release = NULL;
  390. fe->tuner_priv = dev;
  391. i2c_set_clientdata(client, dev);
  392. return 0;
  393. err:
  394. dev_dbg(&client->dev, "failed=%d\n", ret);
  395. return ret;
  396. }
  397. static int mt2060_remove(struct i2c_client *client)
  398. {
  399. dev_dbg(&client->dev, "\n");
  400. return 0;
  401. }
  402. static const struct i2c_device_id mt2060_id_table[] = {
  403. {"mt2060", 0},
  404. {}
  405. };
  406. MODULE_DEVICE_TABLE(i2c, mt2060_id_table);
  407. static struct i2c_driver mt2060_driver = {
  408. .driver = {
  409. .name = "mt2060",
  410. .suppress_bind_attrs = true,
  411. },
  412. .probe = mt2060_probe,
  413. .remove = mt2060_remove,
  414. .id_table = mt2060_id_table,
  415. };
  416. module_i2c_driver(mt2060_driver);
  417. MODULE_AUTHOR("Olivier DANET");
  418. MODULE_DESCRIPTION("Microtune MT2060 silicon tuner driver");
  419. MODULE_LICENSE("GPL");