1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Driver for Microtune MT2266 "Direct conversion low power broadband tuner"
5 * Copyright (c) 2007 Olivier DANET <odanet@caramail.com>
8 #include <linux/module.h>
9 #include <linux/delay.h>
10 #include <linux/dvb/frontend.h>
11 #include <linux/i2c.h>
12 #include <linux/slab.h>
14 #include <media/dvb_frontend.h>
17 #define I2C_ADDRESS 0x60
19 #define REG_PART_REV 0
22 #define REG_BANDWIDTH 8
28 struct mt2266_config
*cfg
;
29 struct i2c_adapter
*i2c
;
39 /* Here, frequencies are expressed in kiloHertz to avoid 32 bits overflows */
42 module_param(debug
, int, 0644);
43 MODULE_PARM_DESC(debug
, "Turn on/off debugging (default:off).");
45 #define dprintk(args...) do { if (debug) {printk(KERN_DEBUG "MT2266: " args); printk("\n"); }} while (0)
47 // Reads a single register
48 static int mt2266_readreg(struct mt2266_priv
*priv
, u8 reg
, u8
*val
)
50 struct i2c_msg msg
[2] = {
51 { .addr
= priv
->cfg
->i2c_address
, .flags
= 0, .buf
= ®
, .len
= 1 },
52 { .addr
= priv
->cfg
->i2c_address
, .flags
= I2C_M_RD
, .buf
= val
, .len
= 1 },
54 if (i2c_transfer(priv
->i2c
, msg
, 2) != 2) {
55 printk(KERN_WARNING
"MT2266 I2C read failed\n");
61 // Writes a single register
62 static int mt2266_writereg(struct mt2266_priv
*priv
, u8 reg
, u8 val
)
64 u8 buf
[2] = { reg
, val
};
65 struct i2c_msg msg
= {
66 .addr
= priv
->cfg
->i2c_address
, .flags
= 0, .buf
= buf
, .len
= 2
68 if (i2c_transfer(priv
->i2c
, &msg
, 1) != 1) {
69 printk(KERN_WARNING
"MT2266 I2C write failed\n");
75 // Writes a set of consecutive registers
76 static int mt2266_writeregs(struct mt2266_priv
*priv
,u8
*buf
, u8 len
)
78 struct i2c_msg msg
= {
79 .addr
= priv
->cfg
->i2c_address
, .flags
= 0, .buf
= buf
, .len
= len
81 if (i2c_transfer(priv
->i2c
, &msg
, 1) != 1) {
82 printk(KERN_WARNING
"MT2266 I2C write failed (len=%i)\n",(int)len
);
88 // Initialisation sequences
89 static u8 mt2266_init1
[] = { REG_TUNE
, 0x00, 0x00, 0x28,
90 0x00, 0x52, 0x99, 0x3f };
92 static u8 mt2266_init2
[] = {
93 0x17, 0x6d, 0x71, 0x61, 0xc0, 0xbf, 0xff, 0xdc, 0x00, 0x0a, 0xd4,
94 0x03, 0x64, 0x64, 0x64, 0x64, 0x22, 0xaa, 0xf2, 0x1e, 0x80, 0x14,
95 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x7f, 0x5e, 0x3f, 0xff, 0xff,
96 0xff, 0x00, 0x77, 0x0f, 0x2d
99 static u8 mt2266_init_8mhz
[] = { REG_BANDWIDTH
, 0x22, 0x22, 0x22, 0x22,
100 0x22, 0x22, 0x22, 0x22 };
102 static u8 mt2266_init_7mhz
[] = { REG_BANDWIDTH
, 0x32, 0x32, 0x32, 0x32,
103 0x32, 0x32, 0x32, 0x32 };
105 static u8 mt2266_init_6mhz
[] = { REG_BANDWIDTH
, 0xa7, 0xa7, 0xa7, 0xa7,
106 0xa7, 0xa7, 0xa7, 0xa7 };
108 static u8 mt2266_uhf
[] = { 0x1d, 0xdc, 0x00, 0x0a, 0xd4, 0x03, 0x64, 0x64,
109 0x64, 0x64, 0x22, 0xaa, 0xf2, 0x1e, 0x80, 0x14 };
111 static u8 mt2266_vhf
[] = { 0x1d, 0xfe, 0x00, 0x00, 0xb4, 0x03, 0xa5, 0xa5,
112 0xa5, 0xa5, 0x82, 0xaa, 0xf1, 0x17, 0x80, 0x1f };
114 #define FREF 30000 // Quartz oscillator 30 MHz
116 static int mt2266_set_params(struct dvb_frontend
*fe
)
118 struct dtv_frontend_properties
*c
= &fe
->dtv_property_cache
;
119 struct mt2266_priv
*priv
;
128 priv
= fe
->tuner_priv
;
130 freq
= priv
->frequency
/ 1000; /* Hz -> kHz */
131 if (freq
< 470000 && freq
> 230000)
132 return -EINVAL
; /* Gap between VHF and UHF bands */
134 priv
->frequency
= c
->frequency
;
135 tune
= 2 * freq
* (8192/16) / (FREF
/16);
136 band
= (freq
< 300000) ? MT2266_VHF
: MT2266_UHF
;
137 if (band
== MT2266_VHF
)
140 switch (c
->bandwidth_hz
) {
142 mt2266_writeregs(priv
, mt2266_init_6mhz
,
143 sizeof(mt2266_init_6mhz
));
146 mt2266_writeregs(priv
, mt2266_init_8mhz
,
147 sizeof(mt2266_init_8mhz
));
151 mt2266_writeregs(priv
, mt2266_init_7mhz
,
152 sizeof(mt2266_init_7mhz
));
155 priv
->bandwidth
= c
->bandwidth_hz
;
157 if (band
== MT2266_VHF
&& priv
->band
== MT2266_UHF
) {
158 dprintk("Switch from UHF to VHF");
159 mt2266_writereg(priv
, 0x05, 0x04);
160 mt2266_writereg(priv
, 0x19, 0x61);
161 mt2266_writeregs(priv
, mt2266_vhf
, sizeof(mt2266_vhf
));
162 } else if (band
== MT2266_UHF
&& priv
->band
== MT2266_VHF
) {
163 dprintk("Switch from VHF to UHF");
164 mt2266_writereg(priv
, 0x05, 0x52);
165 mt2266_writereg(priv
, 0x19, 0x61);
166 mt2266_writeregs(priv
, mt2266_uhf
, sizeof(mt2266_uhf
));
172 else if (freq
<= 525000)
174 else if (freq
<= 550000)
176 else if (freq
<= 580000)
178 else if (freq
<= 605000)
180 else if (freq
<= 630000)
182 else if (freq
<= 655000)
184 else if (freq
<= 685000)
186 else if (freq
<= 710000)
188 else if (freq
<= 735000)
190 else if (freq
<= 765000)
192 else if (freq
<= 802000)
194 else if (freq
<= 840000)
200 b
[1] = (tune
>> 8) & 0x1F;
203 mt2266_writeregs(priv
,b
,4);
205 dprintk("set_parms: tune=%d band=%d %s",
206 (int) tune
, (int) lnaband
,
207 (band
== MT2266_UHF
) ? "UHF" : "VHF");
208 dprintk("set_parms: [1..3]: %2x %2x %2x",
209 (int) b
[1], (int) b
[2], (int)b
[3]);
211 if (band
== MT2266_UHF
) {
213 b
[1] = (priv
->band
== MT2266_VHF
) ? 0x52 : 0x62;
215 mt2266_writeregs(priv
, b
, 3);
218 /* Wait for pll lock or timeout */
221 mt2266_readreg(priv
,REG_LOCK
,b
);
227 dprintk("Lock when i=%i",(int)i
);
229 if (band
== MT2266_UHF
&& priv
->band
== MT2266_VHF
)
230 mt2266_writereg(priv
, 0x05, 0x62);
237 static void mt2266_calibrate(struct mt2266_priv
*priv
)
239 mt2266_writereg(priv
, 0x11, 0x03);
240 mt2266_writereg(priv
, 0x11, 0x01);
241 mt2266_writeregs(priv
, mt2266_init1
, sizeof(mt2266_init1
));
242 mt2266_writeregs(priv
, mt2266_init2
, sizeof(mt2266_init2
));
243 mt2266_writereg(priv
, 0x33, 0x5e);
244 mt2266_writereg(priv
, 0x10, 0x10);
245 mt2266_writereg(priv
, 0x10, 0x00);
246 mt2266_writeregs(priv
, mt2266_init_8mhz
, sizeof(mt2266_init_8mhz
));
248 mt2266_writereg(priv
, 0x17, 0x6d);
249 mt2266_writereg(priv
, 0x1c, 0x00);
251 mt2266_writereg(priv
, 0x17, 0x6d);
252 mt2266_writereg(priv
, 0x1c, 0xff);
255 static int mt2266_get_frequency(struct dvb_frontend
*fe
, u32
*frequency
)
257 struct mt2266_priv
*priv
= fe
->tuner_priv
;
258 *frequency
= priv
->frequency
;
262 static int mt2266_get_bandwidth(struct dvb_frontend
*fe
, u32
*bandwidth
)
264 struct mt2266_priv
*priv
= fe
->tuner_priv
;
265 *bandwidth
= priv
->bandwidth
;
269 static int mt2266_init(struct dvb_frontend
*fe
)
272 struct mt2266_priv
*priv
= fe
->tuner_priv
;
273 ret
= mt2266_writereg(priv
, 0x17, 0x6d);
276 ret
= mt2266_writereg(priv
, 0x1c, 0xff);
282 static int mt2266_sleep(struct dvb_frontend
*fe
)
284 struct mt2266_priv
*priv
= fe
->tuner_priv
;
285 mt2266_writereg(priv
, 0x17, 0x6d);
286 mt2266_writereg(priv
, 0x1c, 0x00);
290 static void mt2266_release(struct dvb_frontend
*fe
)
292 kfree(fe
->tuner_priv
);
293 fe
->tuner_priv
= NULL
;
296 static const struct dvb_tuner_ops mt2266_tuner_ops
= {
298 .name
= "Microtune MT2266",
299 .frequency_min_hz
= 174 * MHz
,
300 .frequency_max_hz
= 862 * MHz
,
301 .frequency_step_hz
= 50 * kHz
,
303 .release
= mt2266_release
,
305 .sleep
= mt2266_sleep
,
306 .set_params
= mt2266_set_params
,
307 .get_frequency
= mt2266_get_frequency
,
308 .get_bandwidth
= mt2266_get_bandwidth
311 struct dvb_frontend
* mt2266_attach(struct dvb_frontend
*fe
, struct i2c_adapter
*i2c
, struct mt2266_config
*cfg
)
313 struct mt2266_priv
*priv
= NULL
;
316 priv
= kzalloc(sizeof(struct mt2266_priv
), GFP_KERNEL
);
322 priv
->band
= MT2266_UHF
;
324 if (mt2266_readreg(priv
, 0, &id
)) {
328 if (id
!= PART_REV
) {
332 printk(KERN_INFO
"MT2266: successfully identified\n");
333 memcpy(&fe
->ops
.tuner_ops
, &mt2266_tuner_ops
, sizeof(struct dvb_tuner_ops
));
335 fe
->tuner_priv
= priv
;
336 mt2266_calibrate(priv
);
339 EXPORT_SYMBOL(mt2266_attach
);
341 MODULE_AUTHOR("Olivier DANET");
342 MODULE_DESCRIPTION("Microtune MT2266 silicon tuner driver");
343 MODULE_LICENSE("GPL");