mt2060.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402
  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. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.=
  20. */
  21. /* In that file, frequencies are expressed in kiloHertz to avoid 32 bits overflows */
  22. #include <linux/module.h>
  23. #include <linux/delay.h>
  24. #include <linux/dvb/frontend.h>
  25. #include <linux/i2c.h>
  26. #include <linux/slab.h>
  27. #include "dvb_frontend.h"
  28. #include "mt2060.h"
  29. #include "mt2060_priv.h"
  30. static int debug;
  31. module_param(debug, int, 0644);
  32. MODULE_PARM_DESC(debug, "Turn on/off debugging (default:off).");
  33. #define dprintk(args...) do { if (debug) {printk(KERN_DEBUG "MT2060: " args); printk("\n"); }} while (0)
  34. // Reads a single register
  35. static int mt2060_readreg(struct mt2060_priv *priv, u8 reg, u8 *val)
  36. {
  37. struct i2c_msg msg[2] = {
  38. { .addr = priv->cfg->i2c_address, .flags = 0, .buf = &reg, .len = 1 },
  39. { .addr = priv->cfg->i2c_address, .flags = I2C_M_RD, .buf = val, .len = 1 },
  40. };
  41. if (i2c_transfer(priv->i2c, msg, 2) != 2) {
  42. printk(KERN_WARNING "mt2060 I2C read failed\n");
  43. return -EREMOTEIO;
  44. }
  45. return 0;
  46. }
  47. // Writes a single register
  48. static int mt2060_writereg(struct mt2060_priv *priv, u8 reg, u8 val)
  49. {
  50. u8 buf[2] = { reg, val };
  51. struct i2c_msg msg = {
  52. .addr = priv->cfg->i2c_address, .flags = 0, .buf = buf, .len = 2
  53. };
  54. if (i2c_transfer(priv->i2c, &msg, 1) != 1) {
  55. printk(KERN_WARNING "mt2060 I2C write failed\n");
  56. return -EREMOTEIO;
  57. }
  58. return 0;
  59. }
  60. // Writes a set of consecutive registers
  61. static int mt2060_writeregs(struct mt2060_priv *priv,u8 *buf, u8 len)
  62. {
  63. struct i2c_msg msg = {
  64. .addr = priv->cfg->i2c_address, .flags = 0, .buf = buf, .len = len
  65. };
  66. if (i2c_transfer(priv->i2c, &msg, 1) != 1) {
  67. printk(KERN_WARNING "mt2060 I2C write failed (len=%i)\n",(int)len);
  68. return -EREMOTEIO;
  69. }
  70. return 0;
  71. }
  72. // Initialisation sequences
  73. // LNABAND=3, NUM1=0x3C, DIV1=0x74, NUM2=0x1080, DIV2=0x49
  74. static u8 mt2060_config1[] = {
  75. REG_LO1C1,
  76. 0x3F, 0x74, 0x00, 0x08, 0x93
  77. };
  78. // FMCG=2, GP2=0, GP1=0
  79. static u8 mt2060_config2[] = {
  80. REG_MISC_CTRL,
  81. 0x20, 0x1E, 0x30, 0xff, 0x80, 0xff, 0x00, 0x2c, 0x42
  82. };
  83. // VGAG=3, V1CSE=1
  84. #ifdef MT2060_SPURCHECK
  85. /* The function below calculates the frequency offset between the output frequency if2
  86. and the closer cross modulation subcarrier between lo1 and lo2 up to the tenth harmonic */
  87. static int mt2060_spurcalc(u32 lo1,u32 lo2,u32 if2)
  88. {
  89. int I,J;
  90. int dia,diamin,diff;
  91. diamin=1000000;
  92. for (I = 1; I < 10; I++) {
  93. J = ((2*I*lo1)/lo2+1)/2;
  94. diff = I*(int)lo1-J*(int)lo2;
  95. if (diff < 0) diff=-diff;
  96. dia = (diff-(int)if2);
  97. if (dia < 0) dia=-dia;
  98. if (diamin > dia) diamin=dia;
  99. }
  100. return diamin;
  101. }
  102. #define BANDWIDTH 4000 // kHz
  103. /* Calculates the frequency offset to add to avoid spurs. Returns 0 if no offset is needed */
  104. static int mt2060_spurcheck(u32 lo1,u32 lo2,u32 if2)
  105. {
  106. u32 Spur,Sp1,Sp2;
  107. int I,J;
  108. I=0;
  109. J=1000;
  110. Spur=mt2060_spurcalc(lo1,lo2,if2);
  111. if (Spur < BANDWIDTH) {
  112. /* Potential spurs detected */
  113. dprintk("Spurs before : f_lo1: %d f_lo2: %d (kHz)",
  114. (int)lo1,(int)lo2);
  115. I=1000;
  116. Sp1 = mt2060_spurcalc(lo1+I,lo2+I,if2);
  117. Sp2 = mt2060_spurcalc(lo1-I,lo2-I,if2);
  118. if (Sp1 < Sp2) {
  119. J=-J; I=-I; Spur=Sp2;
  120. } else
  121. Spur=Sp1;
  122. while (Spur < BANDWIDTH) {
  123. I += J;
  124. Spur = mt2060_spurcalc(lo1+I,lo2+I,if2);
  125. }
  126. dprintk("Spurs after : f_lo1: %d f_lo2: %d (kHz)",
  127. (int)(lo1+I),(int)(lo2+I));
  128. }
  129. return I;
  130. }
  131. #endif
  132. #define IF2 36150 // IF2 frequency = 36.150 MHz
  133. #define FREF 16000 // Quartz oscillator 16 MHz
  134. static int mt2060_set_params(struct dvb_frontend *fe)
  135. {
  136. struct dtv_frontend_properties *c = &fe->dtv_property_cache;
  137. struct mt2060_priv *priv;
  138. int i=0;
  139. u32 freq;
  140. u8 lnaband;
  141. u32 f_lo1,f_lo2;
  142. u32 div1,num1,div2,num2;
  143. u8 b[8];
  144. u32 if1;
  145. priv = fe->tuner_priv;
  146. if1 = priv->if1_freq;
  147. b[0] = REG_LO1B1;
  148. b[1] = 0xFF;
  149. if (fe->ops.i2c_gate_ctrl)
  150. fe->ops.i2c_gate_ctrl(fe, 1); /* open i2c_gate */
  151. mt2060_writeregs(priv,b,2);
  152. freq = c->frequency / 1000; /* Hz -> kHz */
  153. f_lo1 = freq + if1 * 1000;
  154. f_lo1 = (f_lo1 / 250) * 250;
  155. f_lo2 = f_lo1 - freq - IF2;
  156. // From the Comtech datasheet, the step used is 50kHz. The tuner chip could be more precise
  157. f_lo2 = ((f_lo2 + 25) / 50) * 50;
  158. priv->frequency = (f_lo1 - f_lo2 - IF2) * 1000,
  159. #ifdef MT2060_SPURCHECK
  160. // LO-related spurs detection and correction
  161. num1 = mt2060_spurcheck(f_lo1,f_lo2,IF2);
  162. f_lo1 += num1;
  163. f_lo2 += num1;
  164. #endif
  165. //Frequency LO1 = 16MHz * (DIV1 + NUM1/64 )
  166. num1 = f_lo1 / (FREF / 64);
  167. div1 = num1 / 64;
  168. num1 &= 0x3f;
  169. // Frequency LO2 = 16MHz * (DIV2 + NUM2/8192 )
  170. num2 = f_lo2 * 64 / (FREF / 128);
  171. div2 = num2 / 8192;
  172. num2 &= 0x1fff;
  173. if (freq <= 95000) lnaband = 0xB0; else
  174. if (freq <= 180000) lnaband = 0xA0; else
  175. if (freq <= 260000) lnaband = 0x90; else
  176. if (freq <= 335000) lnaband = 0x80; else
  177. if (freq <= 425000) lnaband = 0x70; else
  178. if (freq <= 480000) lnaband = 0x60; else
  179. if (freq <= 570000) lnaband = 0x50; else
  180. if (freq <= 645000) lnaband = 0x40; else
  181. if (freq <= 730000) lnaband = 0x30; else
  182. if (freq <= 810000) lnaband = 0x20; else lnaband = 0x10;
  183. b[0] = REG_LO1C1;
  184. b[1] = lnaband | ((num1 >>2) & 0x0F);
  185. b[2] = div1;
  186. b[3] = (num2 & 0x0F) | ((num1 & 3) << 4);
  187. b[4] = num2 >> 4;
  188. b[5] = ((num2 >>12) & 1) | (div2 << 1);
  189. dprintk("IF1: %dMHz",(int)if1);
  190. dprintk("PLL freq=%dkHz f_lo1=%dkHz f_lo2=%dkHz",(int)freq,(int)f_lo1,(int)f_lo2);
  191. dprintk("PLL div1=%d num1=%d div2=%d num2=%d",(int)div1,(int)num1,(int)div2,(int)num2);
  192. dprintk("PLL [1..5]: %2x %2x %2x %2x %2x",(int)b[1],(int)b[2],(int)b[3],(int)b[4],(int)b[5]);
  193. mt2060_writeregs(priv,b,6);
  194. //Waits for pll lock or timeout
  195. i = 0;
  196. do {
  197. mt2060_readreg(priv,REG_LO_STATUS,b);
  198. if ((b[0] & 0x88)==0x88)
  199. break;
  200. msleep(4);
  201. i++;
  202. } while (i<10);
  203. if (fe->ops.i2c_gate_ctrl)
  204. fe->ops.i2c_gate_ctrl(fe, 0); /* close i2c_gate */
  205. return 0;
  206. }
  207. static void mt2060_calibrate(struct mt2060_priv *priv)
  208. {
  209. u8 b = 0;
  210. int i = 0;
  211. if (mt2060_writeregs(priv,mt2060_config1,sizeof(mt2060_config1)))
  212. return;
  213. if (mt2060_writeregs(priv,mt2060_config2,sizeof(mt2060_config2)))
  214. return;
  215. /* initialize the clock output */
  216. mt2060_writereg(priv, REG_VGAG, (priv->cfg->clock_out << 6) | 0x30);
  217. do {
  218. b |= (1 << 6); // FM1SS;
  219. mt2060_writereg(priv, REG_LO2C1,b);
  220. msleep(20);
  221. if (i == 0) {
  222. b |= (1 << 7); // FM1CA;
  223. mt2060_writereg(priv, REG_LO2C1,b);
  224. b &= ~(1 << 7); // FM1CA;
  225. msleep(20);
  226. }
  227. b &= ~(1 << 6); // FM1SS
  228. mt2060_writereg(priv, REG_LO2C1,b);
  229. msleep(20);
  230. i++;
  231. } while (i < 9);
  232. i = 0;
  233. while (i++ < 10 && mt2060_readreg(priv, REG_MISC_STAT, &b) == 0 && (b & (1 << 6)) == 0)
  234. msleep(20);
  235. if (i <= 10) {
  236. mt2060_readreg(priv, REG_FM_FREQ, &priv->fmfreq); // now find out, what is fmreq used for :)
  237. dprintk("calibration was successful: %d", (int)priv->fmfreq);
  238. } else
  239. dprintk("FMCAL timed out");
  240. }
  241. static int mt2060_get_frequency(struct dvb_frontend *fe, u32 *frequency)
  242. {
  243. struct mt2060_priv *priv = fe->tuner_priv;
  244. *frequency = priv->frequency;
  245. return 0;
  246. }
  247. static int mt2060_get_if_frequency(struct dvb_frontend *fe, u32 *frequency)
  248. {
  249. *frequency = IF2 * 1000;
  250. return 0;
  251. }
  252. static int mt2060_init(struct dvb_frontend *fe)
  253. {
  254. struct mt2060_priv *priv = fe->tuner_priv;
  255. int ret;
  256. if (fe->ops.i2c_gate_ctrl)
  257. fe->ops.i2c_gate_ctrl(fe, 1); /* open i2c_gate */
  258. ret = mt2060_writereg(priv, REG_VGAG,
  259. (priv->cfg->clock_out << 6) | 0x33);
  260. if (fe->ops.i2c_gate_ctrl)
  261. fe->ops.i2c_gate_ctrl(fe, 0); /* close i2c_gate */
  262. return ret;
  263. }
  264. static int mt2060_sleep(struct dvb_frontend *fe)
  265. {
  266. struct mt2060_priv *priv = fe->tuner_priv;
  267. int ret;
  268. if (fe->ops.i2c_gate_ctrl)
  269. fe->ops.i2c_gate_ctrl(fe, 1); /* open i2c_gate */
  270. ret = mt2060_writereg(priv, REG_VGAG,
  271. (priv->cfg->clock_out << 6) | 0x30);
  272. if (fe->ops.i2c_gate_ctrl)
  273. fe->ops.i2c_gate_ctrl(fe, 0); /* close i2c_gate */
  274. return ret;
  275. }
  276. static int mt2060_release(struct dvb_frontend *fe)
  277. {
  278. kfree(fe->tuner_priv);
  279. fe->tuner_priv = NULL;
  280. return 0;
  281. }
  282. static const struct dvb_tuner_ops mt2060_tuner_ops = {
  283. .info = {
  284. .name = "Microtune MT2060",
  285. .frequency_min = 48000000,
  286. .frequency_max = 860000000,
  287. .frequency_step = 50000,
  288. },
  289. .release = mt2060_release,
  290. .init = mt2060_init,
  291. .sleep = mt2060_sleep,
  292. .set_params = mt2060_set_params,
  293. .get_frequency = mt2060_get_frequency,
  294. .get_if_frequency = mt2060_get_if_frequency,
  295. };
  296. /* This functions tries to identify a MT2060 tuner by reading the PART/REV register. This is hasty. */
  297. struct dvb_frontend * mt2060_attach(struct dvb_frontend *fe, struct i2c_adapter *i2c, struct mt2060_config *cfg, u16 if1)
  298. {
  299. struct mt2060_priv *priv = NULL;
  300. u8 id = 0;
  301. priv = kzalloc(sizeof(struct mt2060_priv), GFP_KERNEL);
  302. if (priv == NULL)
  303. return NULL;
  304. priv->cfg = cfg;
  305. priv->i2c = i2c;
  306. priv->if1_freq = if1;
  307. if (fe->ops.i2c_gate_ctrl)
  308. fe->ops.i2c_gate_ctrl(fe, 1); /* open i2c_gate */
  309. if (mt2060_readreg(priv,REG_PART_REV,&id) != 0) {
  310. kfree(priv);
  311. return NULL;
  312. }
  313. if (id != PART_REV) {
  314. kfree(priv);
  315. return NULL;
  316. }
  317. printk(KERN_INFO "MT2060: successfully identified (IF1 = %d)\n", if1);
  318. memcpy(&fe->ops.tuner_ops, &mt2060_tuner_ops, sizeof(struct dvb_tuner_ops));
  319. fe->tuner_priv = priv;
  320. mt2060_calibrate(priv);
  321. if (fe->ops.i2c_gate_ctrl)
  322. fe->ops.i2c_gate_ctrl(fe, 0); /* close i2c_gate */
  323. return fe;
  324. }
  325. EXPORT_SYMBOL(mt2060_attach);
  326. MODULE_AUTHOR("Olivier DANET");
  327. MODULE_DESCRIPTION("Microtune MT2060 silicon tuner driver");
  328. MODULE_LICENSE("GPL");