mt2060.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  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/moduleparam.h>
  24. #include <linux/delay.h>
  25. #include <linux/dvb/frontend.h>
  26. #include <linux/i2c.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, struct dvb_frontend_parameters *params)
  135. {
  136. struct mt2060_priv *priv;
  137. int ret=0;
  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. mt2060_writeregs(priv,b,2);
  150. freq = params->frequency / 1000; // Hz -> kHz
  151. priv->bandwidth = (fe->ops.info.type == FE_OFDM) ? params->u.ofdm.bandwidth : 0;
  152. f_lo1 = freq + if1 * 1000;
  153. f_lo1 = (f_lo1 / 250) * 250;
  154. f_lo2 = f_lo1 - freq - IF2;
  155. // From the Comtech datasheet, the step used is 50kHz. The tuner chip could be more precise
  156. f_lo2 = ((f_lo2 + 25) / 50) * 50;
  157. priv->frequency = (f_lo1 - f_lo2 - IF2) * 1000,
  158. #ifdef MT2060_SPURCHECK
  159. // LO-related spurs detection and correction
  160. num1 = mt2060_spurcheck(f_lo1,f_lo2,IF2);
  161. f_lo1 += num1;
  162. f_lo2 += num1;
  163. #endif
  164. //Frequency LO1 = 16MHz * (DIV1 + NUM1/64 )
  165. num1 = f_lo1 / (FREF / 64);
  166. div1 = num1 / 64;
  167. num1 &= 0x3f;
  168. // Frequency LO2 = 16MHz * (DIV2 + NUM2/8192 )
  169. num2 = f_lo2 * 64 / (FREF / 128);
  170. div2 = num2 / 8192;
  171. num2 &= 0x1fff;
  172. if (freq <= 95000) lnaband = 0xB0; else
  173. if (freq <= 180000) lnaband = 0xA0; else
  174. if (freq <= 260000) lnaband = 0x90; else
  175. if (freq <= 335000) lnaband = 0x80; else
  176. if (freq <= 425000) lnaband = 0x70; else
  177. if (freq <= 480000) lnaband = 0x60; else
  178. if (freq <= 570000) lnaband = 0x50; else
  179. if (freq <= 645000) lnaband = 0x40; else
  180. if (freq <= 730000) lnaband = 0x30; else
  181. if (freq <= 810000) lnaband = 0x20; else lnaband = 0x10;
  182. b[0] = REG_LO1C1;
  183. b[1] = lnaband | ((num1 >>2) & 0x0F);
  184. b[2] = div1;
  185. b[3] = (num2 & 0x0F) | ((num1 & 3) << 4);
  186. b[4] = num2 >> 4;
  187. b[5] = ((num2 >>12) & 1) | (div2 << 1);
  188. dprintk("IF1: %dMHz",(int)if1);
  189. dprintk("PLL freq=%dkHz f_lo1=%dkHz f_lo2=%dkHz",(int)freq,(int)f_lo1,(int)f_lo2);
  190. dprintk("PLL div1=%d num1=%d div2=%d num2=%d",(int)div1,(int)num1,(int)div2,(int)num2);
  191. dprintk("PLL [1..5]: %2x %2x %2x %2x %2x",(int)b[1],(int)b[2],(int)b[3],(int)b[4],(int)b[5]);
  192. mt2060_writeregs(priv,b,6);
  193. //Waits for pll lock or timeout
  194. i = 0;
  195. do {
  196. mt2060_readreg(priv,REG_LO_STATUS,b);
  197. if ((b[0] & 0x88)==0x88)
  198. break;
  199. msleep(4);
  200. i++;
  201. } while (i<10);
  202. return ret;
  203. }
  204. static void mt2060_calibrate(struct mt2060_priv *priv)
  205. {
  206. u8 b = 0;
  207. int i = 0;
  208. if (mt2060_writeregs(priv,mt2060_config1,sizeof(mt2060_config1)))
  209. return;
  210. if (mt2060_writeregs(priv,mt2060_config2,sizeof(mt2060_config2)))
  211. return;
  212. /* initialize the clock output */
  213. mt2060_writereg(priv, REG_VGAG, (priv->cfg->clock_out << 6) | 0x30);
  214. do {
  215. b |= (1 << 6); // FM1SS;
  216. mt2060_writereg(priv, REG_LO2C1,b);
  217. msleep(20);
  218. if (i == 0) {
  219. b |= (1 << 7); // FM1CA;
  220. mt2060_writereg(priv, REG_LO2C1,b);
  221. b &= ~(1 << 7); // FM1CA;
  222. msleep(20);
  223. }
  224. b &= ~(1 << 6); // FM1SS
  225. mt2060_writereg(priv, REG_LO2C1,b);
  226. msleep(20);
  227. i++;
  228. } while (i < 9);
  229. i = 0;
  230. while (i++ < 10 && mt2060_readreg(priv, REG_MISC_STAT, &b) == 0 && (b & (1 << 6)) == 0)
  231. msleep(20);
  232. if (i < 10) {
  233. mt2060_readreg(priv, REG_FM_FREQ, &priv->fmfreq); // now find out, what is fmreq used for :)
  234. dprintk("calibration was successful: %d", (int)priv->fmfreq);
  235. } else
  236. dprintk("FMCAL timed out");
  237. }
  238. static int mt2060_get_frequency(struct dvb_frontend *fe, u32 *frequency)
  239. {
  240. struct mt2060_priv *priv = fe->tuner_priv;
  241. *frequency = priv->frequency;
  242. return 0;
  243. }
  244. static int mt2060_get_bandwidth(struct dvb_frontend *fe, u32 *bandwidth)
  245. {
  246. struct mt2060_priv *priv = fe->tuner_priv;
  247. *bandwidth = priv->bandwidth;
  248. return 0;
  249. }
  250. static int mt2060_init(struct dvb_frontend *fe)
  251. {
  252. struct mt2060_priv *priv = fe->tuner_priv;
  253. return mt2060_writereg(priv, REG_VGAG, (priv->cfg->clock_out << 6) | 0x33);
  254. }
  255. static int mt2060_sleep(struct dvb_frontend *fe)
  256. {
  257. struct mt2060_priv *priv = fe->tuner_priv;
  258. return mt2060_writereg(priv, REG_VGAG, (priv->cfg->clock_out << 6) | 0x30);
  259. }
  260. static int mt2060_release(struct dvb_frontend *fe)
  261. {
  262. kfree(fe->tuner_priv);
  263. fe->tuner_priv = NULL;
  264. return 0;
  265. }
  266. static const struct dvb_tuner_ops mt2060_tuner_ops = {
  267. .info = {
  268. .name = "Microtune MT2060",
  269. .frequency_min = 48000000,
  270. .frequency_max = 860000000,
  271. .frequency_step = 50000,
  272. },
  273. .release = mt2060_release,
  274. .init = mt2060_init,
  275. .sleep = mt2060_sleep,
  276. .set_params = mt2060_set_params,
  277. .get_frequency = mt2060_get_frequency,
  278. .get_bandwidth = mt2060_get_bandwidth
  279. };
  280. /* This functions tries to identify a MT2060 tuner by reading the PART/REV register. This is hasty. */
  281. struct dvb_frontend * mt2060_attach(struct dvb_frontend *fe, struct i2c_adapter *i2c, struct mt2060_config *cfg, u16 if1)
  282. {
  283. struct mt2060_priv *priv = NULL;
  284. u8 id = 0;
  285. priv = kzalloc(sizeof(struct mt2060_priv), GFP_KERNEL);
  286. if (priv == NULL)
  287. return NULL;
  288. priv->cfg = cfg;
  289. priv->i2c = i2c;
  290. priv->if1_freq = if1;
  291. if (mt2060_readreg(priv,REG_PART_REV,&id) != 0) {
  292. kfree(priv);
  293. return NULL;
  294. }
  295. if (id != PART_REV) {
  296. kfree(priv);
  297. return NULL;
  298. }
  299. printk(KERN_INFO "MT2060: successfully identified (IF1 = %d)\n", if1);
  300. memcpy(&fe->ops.tuner_ops, &mt2060_tuner_ops, sizeof(struct dvb_tuner_ops));
  301. fe->tuner_priv = priv;
  302. mt2060_calibrate(priv);
  303. return fe;
  304. }
  305. EXPORT_SYMBOL(mt2060_attach);
  306. MODULE_AUTHOR("Olivier DANET");
  307. MODULE_DESCRIPTION("Microtune MT2060 silicon tuner driver");
  308. MODULE_LICENSE("GPL");