blob: 41bc91817a167d6a571a7b212995fc2ccbc44175 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 *
3 * i2c tv tuner chip device driver
4 * controls microtune tuners, mt2032 + mt2050 at the moment.
5 */
6#include <linux/delay.h>
7#include <linux/i2c.h>
Michael Krufkyffbb8072007-08-21 01:14:12 -03008#include <linux/videodev.h>
Michael Krufky8218b0b2007-06-26 13:12:08 -03009#include "tuner-driver.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070010
11/* ---------------------------------------------------------------------- */
12
13static unsigned int optimize_vco = 1;
14module_param(optimize_vco, int, 0644);
15
16static unsigned int tv_antenna = 1;
17module_param(tv_antenna, int, 0644);
18
19static unsigned int radio_antenna = 0;
20module_param(radio_antenna, int, 0644);
21
Hans Verkuilfac9e892006-01-09 15:32:40 -020022/* from tuner-core.c */
Hans Verkuilf9195de2006-01-11 19:01:01 -020023extern int tuner_debug;
Hans Verkuilfac9e892006-01-09 15:32:40 -020024
Linus Torvalds1da177e2005-04-16 15:20:36 -070025/* ---------------------------------------------------------------------- */
26
27#define MT2032 0x04
28#define MT2030 0x06
29#define MT2040 0x07
30#define MT2050 0x42
31
32static char *microtune_part[] = {
33 [ MT2030 ] = "MT2030",
34 [ MT2032 ] = "MT2032",
35 [ MT2040 ] = "MT2040",
36 [ MT2050 ] = "MT2050",
37};
38
Michael Krufkyb2083192007-05-29 22:54:06 -030039struct microtune_priv {
Michael Krufkydb8a6952007-08-21 01:24:42 -030040 struct tuner_i2c_props i2c_props;
41
Michael Krufkyb2083192007-05-29 22:54:06 -030042 unsigned int xogc;
43 unsigned int radio_if2;
44};
45
Michael Krufkydb8a6952007-08-21 01:24:42 -030046static void microtune_release(struct tuner *t)
Michael Krufkyc22bcb02007-06-06 16:14:18 -030047{
Michael Krufkyc22bcb02007-06-06 16:14:18 -030048 kfree(t->priv);
49 t->priv = NULL;
50}
51
Linus Torvalds1da177e2005-04-16 15:20:36 -070052// IsSpurInBand()?
Michael Krufkydb8a6952007-08-21 01:24:42 -030053static int mt2032_spurcheck(struct tuner *t,
Linus Torvalds1da177e2005-04-16 15:20:36 -070054 int f1, int f2, int spectrum_from,int spectrum_to)
55{
Linus Torvalds1da177e2005-04-16 15:20:36 -070056 int n1=1,n2,f;
57
58 f1=f1/1000; //scale to kHz to avoid 32bit overflows
59 f2=f2/1000;
60 spectrum_from/=1000;
61 spectrum_to/=1000;
62
63 tuner_dbg("spurcheck f1=%d f2=%d from=%d to=%d\n",
64 f1,f2,spectrum_from,spectrum_to);
65
66 do {
67 n2=-n1;
68 f=n1*(f1-f2);
69 do {
70 n2--;
71 f=f-f2;
72 tuner_dbg("spurtest n1=%d n2=%d ftest=%d\n",n1,n2,f);
73
74 if( (f>spectrum_from) && (f<spectrum_to))
75 tuner_dbg("mt2032 spurcheck triggered: %d\n",n1);
76 } while ( (f>(f2-spectrum_to)) || (n2>-5));
77 n1++;
78 } while (n1<5);
79
80 return 1;
81}
82
Michael Krufkydb8a6952007-08-21 01:24:42 -030083static int mt2032_compute_freq(struct tuner *t,
Linus Torvalds1da177e2005-04-16 15:20:36 -070084 unsigned int rfin,
85 unsigned int if1, unsigned int if2,
86 unsigned int spectrum_from,
87 unsigned int spectrum_to,
88 unsigned char *buf,
89 int *ret_sel,
90 unsigned int xogc) //all in Hz
91{
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -080092 unsigned int fref,lo1,lo1n,lo1a,s,sel,lo1freq, desired_lo1,
Linus Torvalds1da177e2005-04-16 15:20:36 -070093 desired_lo2,lo2,lo2n,lo2a,lo2num,lo2freq;
94
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -080095 fref= 5250 *1000; //5.25MHz
Linus Torvalds1da177e2005-04-16 15:20:36 -070096 desired_lo1=rfin+if1;
97
98 lo1=(2*(desired_lo1/1000)+(fref/1000)) / (2*fref/1000);
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -080099 lo1n=lo1/8;
100 lo1a=lo1-(lo1n*8);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800102 s=rfin/1000/1000+1090;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103
104 if(optimize_vco) {
105 if(s>1890) sel=0;
106 else if(s>1720) sel=1;
107 else if(s>1530) sel=2;
108 else if(s>1370) sel=3;
109 else sel=4; // >1090
110 }
111 else {
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800112 if(s>1790) sel=0; // <1958
113 else if(s>1617) sel=1;
114 else if(s>1449) sel=2;
115 else if(s>1291) sel=3;
116 else sel=4; // >1090
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 }
118 *ret_sel=sel;
119
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800120 lo1freq=(lo1a+8*lo1n)*fref;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121
122 tuner_dbg("mt2032: rfin=%d lo1=%d lo1n=%d lo1a=%d sel=%d, lo1freq=%d\n",
123 rfin,lo1,lo1n,lo1a,sel,lo1freq);
124
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800125 desired_lo2=lo1freq-rfin-if2;
126 lo2=(desired_lo2)/fref;
127 lo2n=lo2/8;
128 lo2a=lo2-(lo2n*8);
129 lo2num=((desired_lo2/1000)%(fref/1000))* 3780/(fref/1000); //scale to fit in 32bit arith
130 lo2freq=(lo2a+8*lo2n)*fref + lo2num*(fref/1000)/3780*1000;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131
132 tuner_dbg("mt2032: rfin=%d lo2=%d lo2n=%d lo2a=%d num=%d lo2freq=%d\n",
133 rfin,lo2,lo2n,lo2a,lo2num,lo2freq);
134
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800135 if(lo1a<0 || lo1a>7 || lo1n<17 ||lo1n>48 || lo2a<0 ||lo2a >7 ||lo2n<17 || lo2n>30) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136 tuner_info("mt2032: frequency parameters out of range: %d %d %d %d\n",
137 lo1a, lo1n, lo2a,lo2n);
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800138 return(-1);
139 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140
Michael Krufkydb8a6952007-08-21 01:24:42 -0300141 mt2032_spurcheck(t, lo1freq, desired_lo2, spectrum_from, spectrum_to);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142 // should recalculate lo1 (one step up/down)
143
144 // set up MT2032 register map for transfer over i2c
145 buf[0]=lo1n-1;
146 buf[1]=lo1a | (sel<<4);
147 buf[2]=0x86; // LOGC
148 buf[3]=0x0f; //reserved
149 buf[4]=0x1f;
150 buf[5]=(lo2n-1) | (lo2a<<5);
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800151 if(rfin >400*1000*1000)
152 buf[6]=0xe4;
153 else
154 buf[6]=0xf4; // set PKEN per rev 1.2
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155 buf[7]=8+xogc;
156 buf[8]=0xc3; //reserved
157 buf[9]=0x4e; //reserved
158 buf[10]=0xec; //reserved
159 buf[11]=(lo2num&0xff);
160 buf[12]=(lo2num>>8) |0x80; // Lo2RST
161
162 return 0;
163}
164
Michael Krufkydb8a6952007-08-21 01:24:42 -0300165static int mt2032_check_lo_lock(struct tuner *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166{
Michael Krufkydb8a6952007-08-21 01:24:42 -0300167 struct microtune_priv *priv = t->priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168 int try,lock=0;
169 unsigned char buf[2];
170
171 for(try=0;try<10;try++) {
172 buf[0]=0x0e;
Michael Krufkydb8a6952007-08-21 01:24:42 -0300173 tuner_i2c_xfer_send(&priv->i2c_props,buf,1);
174 tuner_i2c_xfer_recv(&priv->i2c_props,buf,1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175 tuner_dbg("mt2032 Reg.E=0x%02x\n",buf[0]);
176 lock=buf[0] &0x06;
177
178 if (lock==6)
179 break;
180
181 tuner_dbg("mt2032: pll wait 1ms for lock (0x%2x)\n",buf[0]);
182 udelay(1000);
183 }
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800184 return lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185}
186
Michael Krufkydb8a6952007-08-21 01:24:42 -0300187static int mt2032_optimize_vco(struct tuner *t,int sel,int lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188{
Michael Krufkydb8a6952007-08-21 01:24:42 -0300189 struct microtune_priv *priv = t->priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 unsigned char buf[2];
191 int tad1;
192
193 buf[0]=0x0f;
Michael Krufkydb8a6952007-08-21 01:24:42 -0300194 tuner_i2c_xfer_send(&priv->i2c_props,buf,1);
195 tuner_i2c_xfer_recv(&priv->i2c_props,buf,1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 tuner_dbg("mt2032 Reg.F=0x%02x\n",buf[0]);
197 tad1=buf[0]&0x07;
198
199 if(tad1 ==0) return lock;
200 if(tad1 ==1) return lock;
201
202 if(tad1==2) {
203 if(sel==0)
204 return lock;
205 else sel--;
206 }
207 else {
208 if(sel<4)
209 sel++;
210 else
211 return lock;
212 }
213
214 tuner_dbg("mt2032 optimize_vco: sel=%d\n",sel);
215
216 buf[0]=0x0f;
217 buf[1]=sel;
Michael Krufkydb8a6952007-08-21 01:24:42 -0300218 tuner_i2c_xfer_send(&priv->i2c_props,buf,2);
219 lock=mt2032_check_lo_lock(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220 return lock;
221}
222
223
Michael Krufkydb8a6952007-08-21 01:24:42 -0300224static void mt2032_set_if_freq(struct tuner *t, unsigned int rfin,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225 unsigned int if1, unsigned int if2,
226 unsigned int from, unsigned int to)
227{
228 unsigned char buf[21];
229 int lint_try,ret,sel,lock=0;
Michael Krufkyb2083192007-05-29 22:54:06 -0300230 struct microtune_priv *priv = t->priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231
232 tuner_dbg("mt2032_set_if_freq rfin=%d if1=%d if2=%d from=%d to=%d\n",
233 rfin,if1,if2,from,to);
234
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800235 buf[0]=0;
Michael Krufkydb8a6952007-08-21 01:24:42 -0300236 ret=tuner_i2c_xfer_send(&priv->i2c_props,buf,1);
237 tuner_i2c_xfer_recv(&priv->i2c_props,buf,21);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238
239 buf[0]=0;
Michael Krufkydb8a6952007-08-21 01:24:42 -0300240 ret=mt2032_compute_freq(t,rfin,if1,if2,from,to,&buf[1],&sel,priv->xogc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241 if (ret<0)
242 return;
243
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800244 // send only the relevant registers per Rev. 1.2
245 buf[0]=0;
Michael Krufkydb8a6952007-08-21 01:24:42 -0300246 ret=tuner_i2c_xfer_send(&priv->i2c_props,buf,4);
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800247 buf[5]=5;
Michael Krufkydb8a6952007-08-21 01:24:42 -0300248 ret=tuner_i2c_xfer_send(&priv->i2c_props,buf+5,4);
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800249 buf[11]=11;
Michael Krufkydb8a6952007-08-21 01:24:42 -0300250 ret=tuner_i2c_xfer_send(&priv->i2c_props,buf+11,3);
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800251 if(ret!=3)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252 tuner_warn("i2c i/o error: rc == %d (should be 3)\n",ret);
253
254 // wait for PLLs to lock (per manual), retry LINT if not.
255 for(lint_try=0; lint_try<2; lint_try++) {
Michael Krufkydb8a6952007-08-21 01:24:42 -0300256 lock=mt2032_check_lo_lock(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257
258 if(optimize_vco)
Michael Krufkydb8a6952007-08-21 01:24:42 -0300259 lock=mt2032_optimize_vco(t,sel,lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 if(lock==6) break;
261
262 tuner_dbg("mt2032: re-init PLLs by LINT\n");
263 buf[0]=7;
Michael Krufkyb2083192007-05-29 22:54:06 -0300264 buf[1]=0x80 +8+priv->xogc; // set LINT to re-init PLLs
Michael Krufkydb8a6952007-08-21 01:24:42 -0300265 tuner_i2c_xfer_send(&priv->i2c_props,buf,2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266 mdelay(10);
Michael Krufkyb2083192007-05-29 22:54:06 -0300267 buf[1]=8+priv->xogc;
Michael Krufkydb8a6952007-08-21 01:24:42 -0300268 tuner_i2c_xfer_send(&priv->i2c_props,buf,2);
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800269 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270
271 if (lock!=6)
272 tuner_warn("MT2032 Fatal Error: PLLs didn't lock.\n");
273
274 buf[0]=2;
275 buf[1]=0x20; // LOGC for optimal phase noise
Michael Krufkydb8a6952007-08-21 01:24:42 -0300276 ret=tuner_i2c_xfer_send(&priv->i2c_props,buf,2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277 if (ret!=2)
278 tuner_warn("i2c i/o error: rc == %d (should be 2)\n",ret);
279}
280
281
Michael Krufkydb8a6952007-08-21 01:24:42 -0300282static void mt2032_set_tv_freq(struct tuner *t, unsigned int freq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 int if2,from,to;
285
286 // signal bandwidth and picture carrier
287 if (t->std & V4L2_STD_525_60) {
288 // NTSC
289 from = 40750*1000;
290 to = 46750*1000;
291 if2 = 45750*1000;
292 } else {
293 // PAL
294 from = 32900*1000;
295 to = 39900*1000;
296 if2 = 38900*1000;
297 }
298
Michael Krufkydb8a6952007-08-21 01:24:42 -0300299 mt2032_set_if_freq(t, freq*62500 /* freq*1000*1000/16 */,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300 1090*1000*1000, if2, from, to);
301}
302
Michael Krufkydb8a6952007-08-21 01:24:42 -0300303static void mt2032_set_radio_freq(struct tuner *t, unsigned int freq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304{
Michael Krufkyb2083192007-05-29 22:54:06 -0300305 struct microtune_priv *priv = t->priv;
306 int if2 = priv->radio_if2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307
308 // per Manual for FM tuning: first if center freq. 1085 MHz
Michael Krufkydb8a6952007-08-21 01:24:42 -0300309 mt2032_set_if_freq(t, freq * 1000 / 16,
Mauro Carvalho Chehab586b0ca2005-06-28 20:45:21 -0700310 1085*1000*1000,if2,if2,if2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311}
312
Michael Krufkyc22bcb02007-06-06 16:14:18 -0300313static struct tuner_operations mt2032_tuner_ops = {
314 .set_tv_freq = mt2032_set_tv_freq,
315 .set_radio_freq = mt2032_set_radio_freq,
316 .release = microtune_release,
317};
318
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319// Initalization as described in "MT203x Programming Procedures", Rev 1.2, Feb.2001
Michael Krufkydb8a6952007-08-21 01:24:42 -0300320static int mt2032_init(struct tuner *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321{
Michael Krufkyb2083192007-05-29 22:54:06 -0300322 struct microtune_priv *priv = t->priv;
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800323 unsigned char buf[21];
324 int ret,xogc,xok=0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325
326 // Initialize Registers per spec.
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800327 buf[1]=2; // Index to register 2
328 buf[2]=0xff;
329 buf[3]=0x0f;
330 buf[4]=0x1f;
Michael Krufkydb8a6952007-08-21 01:24:42 -0300331 ret=tuner_i2c_xfer_send(&priv->i2c_props,buf+1,4);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800333 buf[5]=6; // Index register 6
334 buf[6]=0xe4;
335 buf[7]=0x8f;
336 buf[8]=0xc3;
337 buf[9]=0x4e;
338 buf[10]=0xec;
Michael Krufkydb8a6952007-08-21 01:24:42 -0300339 ret=tuner_i2c_xfer_send(&priv->i2c_props,buf+5,6);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800341 buf[12]=13; // Index register 13
342 buf[13]=0x32;
Michael Krufkydb8a6952007-08-21 01:24:42 -0300343 ret=tuner_i2c_xfer_send(&priv->i2c_props,buf+12,2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800345 // Adjust XOGC (register 7), wait for XOK
346 xogc=7;
347 do {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 tuner_dbg("mt2032: xogc = 0x%02x\n",xogc&0x07);
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800349 mdelay(10);
350 buf[0]=0x0e;
Michael Krufkydb8a6952007-08-21 01:24:42 -0300351 tuner_i2c_xfer_send(&priv->i2c_props,buf,1);
352 tuner_i2c_xfer_recv(&priv->i2c_props,buf,1);
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800353 xok=buf[0]&0x01;
354 tuner_dbg("mt2032: xok = 0x%02x\n",xok);
355 if (xok == 1) break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800357 xogc--;
358 tuner_dbg("mt2032: xogc = 0x%02x\n",xogc&0x07);
359 if (xogc == 3) {
360 xogc=4; // min. 4 per spec
361 break;
362 }
363 buf[0]=0x07;
364 buf[1]=0x88 + xogc;
Michael Krufkydb8a6952007-08-21 01:24:42 -0300365 ret=tuner_i2c_xfer_send(&priv->i2c_props,buf,2);
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800366 if (ret!=2)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367 tuner_warn("i2c i/o error: rc == %d (should be 2)\n",ret);
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800368 } while (xok != 1 );
Michael Krufkyb2083192007-05-29 22:54:06 -0300369 priv->xogc=xogc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370
Michael Krufkyc22bcb02007-06-06 16:14:18 -0300371 memcpy(&t->ops, &mt2032_tuner_ops, sizeof(struct tuner_operations));
372
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800373 return(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374}
375
Michael Krufkydb8a6952007-08-21 01:24:42 -0300376static void mt2050_set_antenna(struct tuner *t, unsigned char antenna)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377{
Michael Krufkydb8a6952007-08-21 01:24:42 -0300378 struct microtune_priv *priv = t->priv;
Michael Krufky56584c92007-05-29 15:36:37 -0300379 unsigned char buf[2];
380 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381
Michael Krufky56584c92007-05-29 15:36:37 -0300382 buf[0] = 6;
383 buf[1] = antenna ? 0x11 : 0x10;
Michael Krufkydb8a6952007-08-21 01:24:42 -0300384 ret=tuner_i2c_xfer_send(&priv->i2c_props,buf,2);
Michael Krufky56584c92007-05-29 15:36:37 -0300385 tuner_dbg("mt2050: enabled antenna connector %d\n", antenna);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386}
387
Michael Krufkydb8a6952007-08-21 01:24:42 -0300388static void mt2050_set_if_freq(struct tuner *t,unsigned int freq, unsigned int if2)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389{
Michael Krufkydb8a6952007-08-21 01:24:42 -0300390 struct microtune_priv *priv = t->priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391 unsigned int if1=1218*1000*1000;
392 unsigned int f_lo1,f_lo2,lo1,lo2,f_lo1_modulo,f_lo2_modulo,num1,num2,div1a,div1b,div2a,div2b;
393 int ret;
394 unsigned char buf[6];
395
396 tuner_dbg("mt2050_set_if_freq freq=%d if1=%d if2=%d\n",
397 freq,if1,if2);
398
399 f_lo1=freq+if1;
400 f_lo1=(f_lo1/1000000)*1000000;
401
402 f_lo2=f_lo1-freq-if2;
403 f_lo2=(f_lo2/50000)*50000;
404
405 lo1=f_lo1/4000000;
406 lo2=f_lo2/4000000;
407
408 f_lo1_modulo= f_lo1-(lo1*4000000);
409 f_lo2_modulo= f_lo2-(lo2*4000000);
410
411 num1=4*f_lo1_modulo/4000000;
412 num2=4096*(f_lo2_modulo/1000)/4000;
413
414 // todo spurchecks
415
416 div1a=(lo1/12)-1;
417 div1b=lo1-(div1a+1)*12;
418
419 div2a=(lo2/8)-1;
420 div2b=lo2-(div2a+1)*8;
421
Hans Verkuilf9195de2006-01-11 19:01:01 -0200422 if (tuner_debug > 1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423 tuner_dbg("lo1 lo2 = %d %d\n", lo1, lo2);
424 tuner_dbg("num1 num2 div1a div1b div2a div2b= %x %x %x %x %x %x\n",
425 num1,num2,div1a,div1b,div2a,div2b);
426 }
427
428 buf[0]=1;
429 buf[1]= 4*div1b + num1;
430 if(freq<275*1000*1000) buf[1] = buf[1]|0x80;
431
432 buf[2]=div1a;
433 buf[3]=32*div2b + num2/256;
434 buf[4]=num2-(num2/256)*256;
435 buf[5]=div2a;
436 if(num2!=0) buf[5]=buf[5]|0x40;
437
Hans Verkuilf9195de2006-01-11 19:01:01 -0200438 if (tuner_debug > 1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439 int i;
440 tuner_dbg("bufs is: ");
441 for(i=0;i<6;i++)
442 printk("%x ",buf[i]);
443 printk("\n");
444 }
445
Michael Krufkydb8a6952007-08-21 01:24:42 -0300446 ret=tuner_i2c_xfer_send(&priv->i2c_props,buf,6);
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800447 if (ret!=6)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448 tuner_warn("i2c i/o error: rc == %d (should be 6)\n",ret);
449}
450
Michael Krufkydb8a6952007-08-21 01:24:42 -0300451static void mt2050_set_tv_freq(struct tuner *t, unsigned int freq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453 unsigned int if2;
454
455 if (t->std & V4L2_STD_525_60) {
456 // NTSC
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800457 if2 = 45750*1000;
458 } else {
459 // PAL
460 if2 = 38900*1000;
461 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462 if (V4L2_TUNER_DIGITAL_TV == t->mode) {
463 // DVB (pinnacle 300i)
464 if2 = 36150*1000;
465 }
Michael Krufkydb8a6952007-08-21 01:24:42 -0300466 mt2050_set_if_freq(t, freq*62500, if2);
467 mt2050_set_antenna(t, tv_antenna);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468}
469
Michael Krufkydb8a6952007-08-21 01:24:42 -0300470static void mt2050_set_radio_freq(struct tuner *t, unsigned int freq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471{
Michael Krufkyb2083192007-05-29 22:54:06 -0300472 struct microtune_priv *priv = t->priv;
473 int if2 = priv->radio_if2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474
Michael Krufkydb8a6952007-08-21 01:24:42 -0300475 mt2050_set_if_freq(t, freq * 1000 / 16, if2);
476 mt2050_set_antenna(t, radio_antenna);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477}
478
Michael Krufkyc22bcb02007-06-06 16:14:18 -0300479static struct tuner_operations mt2050_tuner_ops = {
480 .set_tv_freq = mt2050_set_tv_freq,
481 .set_radio_freq = mt2050_set_radio_freq,
482 .release = microtune_release,
483};
484
Michael Krufkydb8a6952007-08-21 01:24:42 -0300485static int mt2050_init(struct tuner *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486{
Michael Krufkydb8a6952007-08-21 01:24:42 -0300487 struct microtune_priv *priv = t->priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488 unsigned char buf[2];
489 int ret;
490
491 buf[0]=6;
492 buf[1]=0x10;
Michael Krufkydb8a6952007-08-21 01:24:42 -0300493 ret=tuner_i2c_xfer_send(&priv->i2c_props,buf,2); // power
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494
495 buf[0]=0x0f;
496 buf[1]=0x0f;
Michael Krufkydb8a6952007-08-21 01:24:42 -0300497 ret=tuner_i2c_xfer_send(&priv->i2c_props,buf,2); // m1lo
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498
499 buf[0]=0x0d;
Michael Krufkydb8a6952007-08-21 01:24:42 -0300500 ret=tuner_i2c_xfer_send(&priv->i2c_props,buf,1);
501 tuner_i2c_xfer_recv(&priv->i2c_props,buf,1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502
503 tuner_dbg("mt2050: sro is %x\n",buf[0]);
Michael Krufkyc22bcb02007-06-06 16:14:18 -0300504
505 memcpy(&t->ops, &mt2050_tuner_ops, sizeof(struct tuner_operations));
506
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507 return 0;
508}
509
Michael Krufkydb8a6952007-08-21 01:24:42 -0300510int microtune_init(struct tuner *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511{
Michael Krufkyb2083192007-05-29 22:54:06 -0300512 struct microtune_priv *priv = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 char *name;
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800514 unsigned char buf[21];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515 int company_code;
516
Michael Krufkyb2083192007-05-29 22:54:06 -0300517 priv = kzalloc(sizeof(struct microtune_priv), GFP_KERNEL);
518 if (priv == NULL)
519 return -ENOMEM;
520 t->priv = priv;
521
Michael Krufkydb8a6952007-08-21 01:24:42 -0300522 priv->i2c_props.addr = t->i2c.addr;
523 priv->i2c_props.adap = t->i2c.adapter;
524
Michael Krufkyb2083192007-05-29 22:54:06 -0300525 priv->radio_if2 = 10700 * 1000; /* 10.7MHz - FM radio */
526
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527 memset(buf,0,sizeof(buf));
Michael Krufkyc22bcb02007-06-06 16:14:18 -0300528
Hans Verkuil39e8f402006-01-09 15:25:16 -0200529 if (t->std & V4L2_STD_525_60) {
530 tuner_dbg("pinnacle ntsc\n");
Michael Krufkyb2083192007-05-29 22:54:06 -0300531 priv->radio_if2 = 41300 * 1000;
Hans Verkuil39e8f402006-01-09 15:25:16 -0200532 } else {
533 tuner_dbg("pinnacle pal\n");
Michael Krufkyb2083192007-05-29 22:54:06 -0300534 priv->radio_if2 = 33300 * 1000;
Hans Verkuil39e8f402006-01-09 15:25:16 -0200535 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536 name = "unknown";
537
Michael Krufkydb8a6952007-08-21 01:24:42 -0300538 tuner_i2c_xfer_send(&priv->i2c_props,buf,1);
539 tuner_i2c_xfer_recv(&priv->i2c_props,buf,21);
Hans Verkuilf9195de2006-01-11 19:01:01 -0200540 if (tuner_debug) {
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800541 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542 tuner_dbg("MT20xx hexdump:");
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800543 for(i=0;i<21;i++) {
544 printk(" %02x",buf[i]);
545 if(((i+1)%8)==0) printk(" ");
546 }
547 printk("\n");
548 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549 company_code = buf[0x11] << 8 | buf[0x12];
550 tuner_info("microtune: companycode=%04x part=%02x rev=%02x\n",
551 company_code,buf[0x13],buf[0x14]);
552
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553
554 if (buf[0x13] < ARRAY_SIZE(microtune_part) &&
555 NULL != microtune_part[buf[0x13]])
556 name = microtune_part[buf[0x13]];
557 switch (buf[0x13]) {
558 case MT2032:
Michael Krufkydb8a6952007-08-21 01:24:42 -0300559 mt2032_init(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560 break;
561 case MT2050:
Michael Krufkydb8a6952007-08-21 01:24:42 -0300562 mt2050_init(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563 break;
564 default:
565 tuner_info("microtune %s found, not (yet?) supported, sorry :-/\n",
566 name);
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800567 return 0;
568 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569
Michael Krufkydb8a6952007-08-21 01:24:42 -0300570 strlcpy(t->i2c.name, name, sizeof(t->i2c.name));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571 tuner_info("microtune %s found, OK\n",name);
572 return 0;
573}
574
575/*
576 * Overrides for Emacs so that we follow Linus's tabbing style.
577 * ---------------------------------------------------------------------------
578 * Local variables:
579 * c-basic-offset: 8
580 * End:
581 */