2 * Driver for Microtune MT2266 "Direct conversion low power broadband tuner"
4 * Copyright (c) 2007 Olivier DANET <odanet@caramail.com>
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.
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 * GNU General Public License for more details.
17 #include <linux/module.h>
18 #include <linux/moduleparam.h>
19 #include <linux/delay.h>
20 #include <linux/dvb/frontend.h>
21 #include <linux/i2c.h>
23 #include "dvb_frontend.h"
26 #define I2C_ADDRESS 0x60
28 #define REG_PART_REV 0
31 #define REG_BANDWIDTH 8
37 struct mt2266_config
*cfg
;
38 struct i2c_adapter
*i2c
;
44 /* Here, frequencies are expressed in kiloHertz to avoid 32 bits overflows */
47 module_param(debug
, int, 0644);
48 MODULE_PARM_DESC(debug
, "Turn on/off debugging (default:off).");
50 #define dprintk(args...) do { if (debug) {printk(KERN_DEBUG "MT2266: " args); printk("\n"); }} while (0)
52 // Reads a single register
53 static int mt2266_readreg(struct mt2266_priv
*priv
, u8 reg
, u8
*val
)
55 struct i2c_msg msg
[2] = {
56 { .addr
= priv
->cfg
->i2c_address
, .flags
= 0, .buf
= ®
, .len
= 1 },
57 { .addr
= priv
->cfg
->i2c_address
, .flags
= I2C_M_RD
, .buf
= val
, .len
= 1 },
59 if (i2c_transfer(priv
->i2c
, msg
, 2) != 2) {
60 printk(KERN_WARNING
"MT2266 I2C read failed\n");
66 // Writes a single register
67 static int mt2266_writereg(struct mt2266_priv
*priv
, u8 reg
, u8 val
)
69 u8 buf
[2] = { reg
, val
};
70 struct i2c_msg msg
= {
71 .addr
= priv
->cfg
->i2c_address
, .flags
= 0, .buf
= buf
, .len
= 2
73 if (i2c_transfer(priv
->i2c
, &msg
, 1) != 1) {
74 printk(KERN_WARNING
"MT2266 I2C write failed\n");
80 // Writes a set of consecutive registers
81 static int mt2266_writeregs(struct mt2266_priv
*priv
,u8
*buf
, u8 len
)
83 struct i2c_msg msg
= {
84 .addr
= priv
->cfg
->i2c_address
, .flags
= 0, .buf
= buf
, .len
= len
86 if (i2c_transfer(priv
->i2c
, &msg
, 1) != 1) {
87 printk(KERN_WARNING
"MT2266 I2C write failed (len=%i)\n",(int)len
);
93 // Initialisation sequences
94 static u8 mt2266_init1
[] = {
96 0x00, 0x00, 0x28, 0x00, 0x52, 0x99, 0x3f };
98 static u8 mt2266_init2
[] = {
99 0x17, 0x6d, 0x71, 0x61, 0xc0, 0xbf, 0xff, 0xdc, 0x00, 0x0a,
100 0xd4, 0x03, 0x64, 0x64, 0x64, 0x64, 0x22, 0xaa, 0xf2, 0x1e, 0x80, 0x14, 0x01, 0x01, 0x01, 0x01,
101 0x01, 0x01, 0x7f, 0x5e, 0x3f, 0xff, 0xff, 0xff, 0x00, 0x77, 0x0f, 0x2d };
103 static u8 mt2266_init_8mhz
[] = {
105 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22 };
107 static u8 mt2266_init_7mhz
[] = {
109 0x32, 0x32, 0x32, 0x32, 0x32, 0x32, 0x32, 0x32 };
111 static u8 mt2266_init_6mhz
[] = {
113 0xa7, 0xa7, 0xa7, 0xa7, 0xa7, 0xa7, 0xa7, 0xa7 };
115 #define FREF 30000 // Quartz oscillator 30 MHz
117 static int mt2266_set_params(struct dvb_frontend
*fe
, struct dvb_frontend_parameters
*params
)
119 struct mt2266_priv
*priv
;
127 priv
= fe
->tuner_priv
;
129 mt2266_writereg(priv
,0x17,0x6d);
130 mt2266_writereg(priv
,0x1c,0xff);
132 freq
= params
->frequency
/ 1000; // Hz -> kHz
133 priv
->bandwidth
= (fe
->ops
.info
.type
== FE_OFDM
) ? params
->u
.ofdm
.bandwidth
: 0;
134 priv
->frequency
= freq
* 1000;
135 tune
=2 * freq
* (8192/16) / (FREF
/16);
137 if (freq
<= 495000) lnaband
= 0xEE; else
138 if (freq
<= 525000) lnaband
= 0xDD; else
139 if (freq
<= 550000) lnaband
= 0xCC; else
140 if (freq
<= 580000) lnaband
= 0xBB; else
141 if (freq
<= 605000) lnaband
= 0xAA; else
142 if (freq
<= 630000) lnaband
= 0x99; else
143 if (freq
<= 655000) lnaband
= 0x88; else
144 if (freq
<= 685000) lnaband
= 0x77; else
145 if (freq
<= 710000) lnaband
= 0x66; else
146 if (freq
<= 735000) lnaband
= 0x55; else
147 if (freq
<= 765000) lnaband
= 0x44; else
148 if (freq
<= 802000) lnaband
= 0x33; else
149 if (freq
<= 840000) lnaband
= 0x22; else lnaband
= 0x11;
152 mt2266_writeregs(priv
,(params
->u
.ofdm
.bandwidth
==BANDWIDTH_6_MHZ
)?mt2266_init_6mhz
:
153 (params
->u
.ofdm
.bandwidth
==BANDWIDTH_7_MHZ
)?mt2266_init_7mhz
:
154 mt2266_init_8mhz
,sizeof(mt2266_init_8mhz
));
157 b
[1] = (tune
>> 8) & 0x1F;
160 mt2266_writeregs(priv
,b
,4);
162 dprintk("set_parms: tune=%d band=%d\n",(int)tune
,(int)lnaband
);
163 dprintk("set_parms: [1..3]: %2x %2x %2x",(int)b
[1],(int)b
[2],(int)b
[3]);
168 mt2266_writeregs(priv
,b
,3);
170 //Waits for pll lock or timeout
173 mt2266_readreg(priv
,REG_LOCK
,b
);
174 if ((b
[0] & 0x40)==0x40)
179 dprintk("Lock when i=%i\n",(int)i
);
183 static void mt2266_calibrate(struct mt2266_priv
*priv
)
185 mt2266_writereg(priv
,0x11,0x03);
186 mt2266_writereg(priv
,0x11,0x01);
188 mt2266_writeregs(priv
,mt2266_init1
,sizeof(mt2266_init1
));
189 mt2266_writeregs(priv
,mt2266_init2
,sizeof(mt2266_init2
));
191 mt2266_writereg(priv
,0x33,0x5e);
192 mt2266_writereg(priv
,0x10,0x10);
193 mt2266_writereg(priv
,0x10,0x00);
195 mt2266_writeregs(priv
,mt2266_init_8mhz
,sizeof(mt2266_init_8mhz
));
198 mt2266_writereg(priv
,0x17,0x6d);
199 mt2266_writereg(priv
,0x1c,0x00);
201 mt2266_writereg(priv
,0x17,0x6d);
202 mt2266_writereg(priv
,0x1c,0xff);
205 static int mt2266_get_frequency(struct dvb_frontend
*fe
, u32
*frequency
)
207 struct mt2266_priv
*priv
= fe
->tuner_priv
;
208 *frequency
= priv
->frequency
;
212 static int mt2266_get_bandwidth(struct dvb_frontend
*fe
, u32
*bandwidth
)
214 struct mt2266_priv
*priv
= fe
->tuner_priv
;
215 *bandwidth
= priv
->bandwidth
;
219 static int mt2266_init(struct dvb_frontend
*fe
)
221 struct mt2266_priv
*priv
= fe
->tuner_priv
;
222 mt2266_writereg(priv
,0x17,0x6d);
223 mt2266_writereg(priv
,0x1c,0xff);
227 static int mt2266_sleep(struct dvb_frontend
*fe
)
229 struct mt2266_priv
*priv
= fe
->tuner_priv
;
230 mt2266_writereg(priv
,0x17,0x6d);
231 mt2266_writereg(priv
,0x1c,0x00);
235 static int mt2266_release(struct dvb_frontend
*fe
)
237 kfree(fe
->tuner_priv
);
238 fe
->tuner_priv
= NULL
;
242 static const struct dvb_tuner_ops mt2266_tuner_ops
= {
244 .name
= "Microtune MT2266",
245 .frequency_min
= 470000000,
246 .frequency_max
= 860000000,
247 .frequency_step
= 50000,
249 .release
= mt2266_release
,
251 .sleep
= mt2266_sleep
,
252 .set_params
= mt2266_set_params
,
253 .get_frequency
= mt2266_get_frequency
,
254 .get_bandwidth
= mt2266_get_bandwidth
257 struct dvb_frontend
* mt2266_attach(struct dvb_frontend
*fe
, struct i2c_adapter
*i2c
, struct mt2266_config
*cfg
)
259 struct mt2266_priv
*priv
= NULL
;
262 priv
= kzalloc(sizeof(struct mt2266_priv
), GFP_KERNEL
);
269 if (mt2266_readreg(priv
,0,&id
) != 0) {
273 if (id
!= PART_REV
) {
277 printk(KERN_INFO
"MT2266: successfully identified\n");
278 memcpy(&fe
->ops
.tuner_ops
, &mt2266_tuner_ops
, sizeof(struct dvb_tuner_ops
));
280 fe
->tuner_priv
= priv
;
281 mt2266_calibrate(priv
);
284 EXPORT_SYMBOL(mt2266_attach
);
286 MODULE_AUTHOR("Olivier DANET");
287 MODULE_DESCRIPTION("Microtune MT2266 silicon tuner driver");
288 MODULE_LICENSE("GPL");