mt2060.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  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. /* See mt2060_priv.h for details */
  22. /* In that file, frequencies are expressed in kiloHertz to avoid 32 bits overflows */
  23. #include <linux/module.h>
  24. #include <linux/moduleparam.h>
  25. #include <linux/delay.h>
  26. #include <linux/dvb/frontend.h>
  27. #include "mt2060.h"
  28. #include "mt2060_priv.h"
  29. static int debug=0;
  30. module_param(debug, int, 0644);
  31. MODULE_PARM_DESC(debug, "Turn on/off debugging (default:off).");
  32. #define dprintk(args...) do { if (debug) { printk(KERN_DEBUG "MT2060: " args); printk("\n"); } } while (0)
  33. // Reads a single register
  34. static int mt2060_readreg(struct mt2060_state *state, u8 reg, u8 *val)
  35. {
  36. struct i2c_msg msg[2] = {
  37. { .addr = state->config->i2c_address, .flags = 0, .buf = &reg, .len = 1 },
  38. { .addr = state->config->i2c_address, .flags = I2C_M_RD, .buf = val, .len = 1 },
  39. };
  40. if (i2c_transfer(state->i2c, msg, 2) != 2) {
  41. printk(KERN_WARNING "mt2060 I2C read failed\n");
  42. return -EREMOTEIO;
  43. }
  44. return 0;
  45. }
  46. // Writes a single register
  47. static int mt2060_writereg(struct mt2060_state *state, u8 reg, u8 val)
  48. {
  49. u8 buf[2];
  50. struct i2c_msg msg = {
  51. .addr = state->config->i2c_address, .flags = 0, .buf = buf, .len = 2
  52. };
  53. buf[0]=reg;
  54. buf[1]=val;
  55. if (i2c_transfer(state->i2c, &msg, 1) != 1) {
  56. printk(KERN_WARNING "mt2060 I2C write failed\n");
  57. return -EREMOTEIO;
  58. }
  59. return 0;
  60. }
  61. // Writes a set of consecutive registers
  62. static int mt2060_writeregs(struct mt2060_state *state,u8 *buf, u8 len)
  63. {
  64. struct i2c_msg msg = {
  65. .addr = state->config->i2c_address, .flags = 0, .buf = buf, .len = len
  66. };
  67. if (i2c_transfer(state->i2c, &msg, 1) != 1) {
  68. printk(KERN_WARNING "mt2060 I2C write failed (len=%i)\n",(int)len);
  69. return -EREMOTEIO;
  70. }
  71. return 0;
  72. }
  73. // Initialisation sequences
  74. // LNABAND=3, NUM1=0x3C, DIV1=0x74, NUM2=0x1080, DIV2=0x49
  75. static u8 mt2060_config1[] = {
  76. REG_LO1C1,
  77. 0x3F, 0x74, 0x00, 0x08, 0x93
  78. };
  79. // FMCG=2, GP2=0, GP1=0
  80. static u8 mt2060_config2[] = {
  81. REG_MISC_CTRL,
  82. 0x20, 0x1E, 0x30, 0xff, 0x80, 0xff, 0x00, 0x2c, 0x42
  83. };
  84. // VGAG=3, V1CSE=1
  85. static u8 mt2060_config3[] = {
  86. REG_VGAG,
  87. 0x33
  88. };
  89. int mt2060_init(struct mt2060_state *state)
  90. {
  91. if (mt2060_writeregs(state,mt2060_config1,sizeof(mt2060_config1)))
  92. return -EREMOTEIO;
  93. if (mt2060_writeregs(state,mt2060_config3,sizeof(mt2060_config3)))
  94. return -EREMOTEIO;
  95. return 0;
  96. }
  97. EXPORT_SYMBOL(mt2060_init);
  98. #ifdef MT2060_SPURCHECK
  99. /* The function below calculates the frequency offset between the output frequency if2
  100. and the closer cross modulation subcarrier between lo1 and lo2 up to the tenth harmonic */
  101. static int mt2060_spurcalc(u32 lo1,u32 lo2,u32 if2)
  102. {
  103. int I,J;
  104. int dia,diamin,diff;
  105. diamin=1000000;
  106. for (I = 1; I < 10; I++) {
  107. J = ((2*I*lo1)/lo2+1)/2;
  108. diff = I*(int)lo1-J*(int)lo2;
  109. if (diff < 0) diff=-diff;
  110. dia = (diff-(int)if2);
  111. if (dia < 0) dia=-dia;
  112. if (diamin > dia) diamin=dia;
  113. }
  114. return diamin;
  115. }
  116. #define BANDWIDTH 4000 // kHz
  117. /* Calculates the frequency offset to add to avoid spurs. Returns 0 if no offset is needed */
  118. static int mt2060_spurcheck(u32 lo1,u32 lo2,u32 if2)
  119. {
  120. u32 Spur,Sp1,Sp2;
  121. int I,J;
  122. I=0;
  123. J=1000;
  124. Spur=mt2060_spurcalc(lo1,lo2,if2);
  125. if (Spur < BANDWIDTH) {
  126. /* Potential spurs detected */
  127. dprintk("Spurs before : f_lo1: %d f_lo2: %d (kHz)",
  128. (int)lo1,(int)lo2);
  129. I=1000;
  130. Sp1 = mt2060_spurcalc(lo1+I,lo2+I,if2);
  131. Sp2 = mt2060_spurcalc(lo1-I,lo2-I,if2);
  132. if (Sp1 < Sp2) {
  133. J=-J; I=-I; Spur=Sp2;
  134. } else
  135. Spur=Sp1;
  136. while (Spur < BANDWIDTH) {
  137. I += J;
  138. Spur = mt2060_spurcalc(lo1+I,lo2+I,if2);
  139. }
  140. dprintk("Spurs after : f_lo1: %d f_lo2: %d (kHz)",
  141. (int)(lo1+I),(int)(lo2+I));
  142. }
  143. return I;
  144. }
  145. #endif
  146. #define IF2 36150 // IF2 frequency = 36.150 MHz
  147. #define FREF 16000 // Quartz oscillator 16 MHz
  148. int mt2060_set(struct mt2060_state *state, struct dvb_frontend_parameters *fep)
  149. {
  150. int ret=0;
  151. int i=0;
  152. u32 freq;
  153. u8 lnaband;
  154. u32 f_lo1,f_lo2;
  155. u32 div1,num1,div2,num2;
  156. u8 b[8];
  157. u32 if1;
  158. if1 = state->if1_freq;
  159. b[0] = REG_LO1B1;
  160. b[1] = 0xFF;
  161. mt2060_writeregs(state,b,2);
  162. freq = fep->frequency / 1000; // Hz -> kHz
  163. f_lo1 = freq + if1 * 1000;
  164. f_lo1 = (f_lo1/250)*250;
  165. f_lo2 = f_lo1 - freq - IF2;
  166. f_lo2 = (f_lo2/50)*50;
  167. #ifdef MT2060_SPURCHECK
  168. // LO-related spurs detection and correction
  169. num1 = mt2060_spurcheck(f_lo1,f_lo2,IF2);
  170. f_lo1 += num1;
  171. f_lo2 += num1;
  172. #endif
  173. //Frequency LO1 = 16MHz * (DIV1 + NUM1/64 )
  174. div1 = f_lo1 / FREF;
  175. num1 = (64 * (f_lo1 % FREF) )/FREF;
  176. // Frequency LO2 = 16MHz * (DIV2 + NUM2/8192 )
  177. div2 = f_lo2 / FREF;
  178. num2 = (16384 * (f_lo2 % FREF) /FREF +1)/2;
  179. if (freq <= 95000) lnaband = 0xB0; else
  180. if (freq <= 180000) lnaband = 0xA0; else
  181. if (freq <= 260000) lnaband = 0x90; else
  182. if (freq <= 335000) lnaband = 0x80; else
  183. if (freq <= 425000) lnaband = 0x70; else
  184. if (freq <= 480000) lnaband = 0x60; else
  185. if (freq <= 570000) lnaband = 0x50; else
  186. if (freq <= 645000) lnaband = 0x40; else
  187. if (freq <= 730000) lnaband = 0x30; else
  188. if (freq <= 810000) lnaband = 0x20; else lnaband = 0x10;
  189. b[0] = REG_LO1C1;
  190. b[1] = lnaband | ((num1 >>2) & 0x0F);
  191. b[2] = div1;
  192. b[3] = (num2 & 0x0F) | ((num1 & 3) << 4);
  193. b[4] = num2 >> 4;
  194. b[5] = ((num2 >>12) & 1) | (div2 << 1);
  195. dprintk("IF1: %dMHz",(int)if1);
  196. dprintk("PLL freq: %d f_lo1: %d f_lo2: %d (kHz)",(int)freq,(int)f_lo1,(int)f_lo2);
  197. dprintk("PLL div1: %d num1: %d div2: %d num2: %d",(int)div1,(int)num1,(int)div2,(int)num2);
  198. dprintk("PLL [1..5]: %2x %2x %2x %2x %2x",(int)b[1],(int)b[2],(int)b[3],(int)b[4],(int)b[5]);
  199. mt2060_writeregs(state,b,6);
  200. //Waits for pll lock or timeout
  201. i=0;
  202. do {
  203. mt2060_readreg(state,REG_LO_STATUS,b);
  204. if ((b[0] & 0x88)==0x88) break;
  205. msleep(4);
  206. i++;
  207. } while (i<10);
  208. return ret;
  209. }
  210. EXPORT_SYMBOL(mt2060_set);
  211. /* from usbsnoop.log */
  212. static void mt2060_calibrate(struct mt2060_state *state)
  213. {
  214. u8 b = 0;
  215. int i = 0;
  216. if (mt2060_writeregs(state,mt2060_config1,sizeof(mt2060_config1)))
  217. return;
  218. if (mt2060_writeregs(state,mt2060_config2,sizeof(mt2060_config2)))
  219. return;
  220. do {
  221. b |= (1 << 6); // FM1SS;
  222. mt2060_writereg(state, REG_LO2C1,b);
  223. msleep(20);
  224. if (i == 0) {
  225. b |= (1 << 7); // FM1CA;
  226. mt2060_writereg(state, REG_LO2C1,b);
  227. b &= ~(1 << 7); // FM1CA;
  228. msleep(20);
  229. }
  230. b &= ~(1 << 6); // FM1SS
  231. mt2060_writereg(state, REG_LO2C1,b);
  232. msleep(20);
  233. i++;
  234. } while (i < 9);
  235. i = 0;
  236. while (i++ < 10 && mt2060_readreg(state, REG_MISC_STAT, &b) == 0 && (b & (1 << 6)) == 0)
  237. msleep(20);
  238. if (i < 10) {
  239. mt2060_readreg(state, REG_FM_FREQ, &state->fmfreq); // now find out, what is fmreq used for :)
  240. dprintk("calibration was successful: %d", state->fmfreq);
  241. } else
  242. dprintk("FMCAL timed out");
  243. }
  244. /* This functions tries to identify a MT2060 tuner by reading the PART/REV register. This is hasty. */
  245. int mt2060_attach(struct mt2060_state *state, struct mt2060_config *config, struct i2c_adapter *i2c,u16 if1)
  246. {
  247. u8 id = 0;
  248. memset(state,0,sizeof(struct mt2060_state));
  249. state->config = config;
  250. state->i2c = i2c;
  251. state->if1_freq = if1;
  252. if (mt2060_readreg(state,REG_PART_REV,&id) != 0)
  253. return -ENODEV;
  254. if (id != PART_REV)
  255. return -ENODEV;
  256. printk(KERN_INFO "MT2060: successfully identified\n");
  257. mt2060_calibrate(state);
  258. return 0;
  259. }
  260. EXPORT_SYMBOL(mt2060_attach);
  261. MODULE_AUTHOR("Olivier DANET");
  262. MODULE_DESCRIPTION("Microtune MT2060 silicon tuner driver");
  263. MODULE_LICENSE("GPL");