blob: dd883d7819680679ce6b30e66af905b085dbb089 [file] [log] [blame]
Olivier DANET54d75eb2007-07-25 14:42:54 -03001/*
2 * Driver for Microtune MT2266 "Direct conversion low power broadband tuner"
3 *
4 * Copyright (c) 2007 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 * GNU General Public License for more details.
15 */
16
17#include <linux/module.h>
Olivier DANET54d75eb2007-07-25 14:42:54 -030018#include <linux/delay.h>
19#include <linux/dvb/frontend.h>
20#include <linux/i2c.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090021#include <linux/slab.h>
Olivier DANET54d75eb2007-07-25 14:42:54 -030022
23#include "dvb_frontend.h"
24#include "mt2266.h"
25
26#define I2C_ADDRESS 0x60
27
28#define REG_PART_REV 0
29#define REG_TUNE 1
30#define REG_BAND 6
31#define REG_BANDWIDTH 8
32#define REG_LOCK 0x12
33
34#define PART_REV 0x85
35
36struct mt2266_priv {
37 struct mt2266_config *cfg;
38 struct i2c_adapter *i2c;
39
40 u32 frequency;
41 u32 bandwidth;
Olivier DANET542794b2007-11-12 10:48:51 -030042 u8 band;
Olivier DANET54d75eb2007-07-25 14:42:54 -030043};
44
Olivier DANET542794b2007-11-12 10:48:51 -030045#define MT2266_VHF 1
46#define MT2266_UHF 0
47
Olivier DANET54d75eb2007-07-25 14:42:54 -030048/* Here, frequencies are expressed in kiloHertz to avoid 32 bits overflows */
49
50static int debug;
51module_param(debug, int, 0644);
52MODULE_PARM_DESC(debug, "Turn on/off debugging (default:off).");
53
54#define dprintk(args...) do { if (debug) {printk(KERN_DEBUG "MT2266: " args); printk("\n"); }} while (0)
55
56// Reads a single register
57static int mt2266_readreg(struct mt2266_priv *priv, u8 reg, u8 *val)
58{
59 struct i2c_msg msg[2] = {
60 { .addr = priv->cfg->i2c_address, .flags = 0, .buf = &reg, .len = 1 },
61 { .addr = priv->cfg->i2c_address, .flags = I2C_M_RD, .buf = val, .len = 1 },
62 };
63 if (i2c_transfer(priv->i2c, msg, 2) != 2) {
64 printk(KERN_WARNING "MT2266 I2C read failed\n");
65 return -EREMOTEIO;
66 }
67 return 0;
68}
69
70// Writes a single register
71static int mt2266_writereg(struct mt2266_priv *priv, u8 reg, u8 val)
72{
73 u8 buf[2] = { reg, val };
74 struct i2c_msg msg = {
75 .addr = priv->cfg->i2c_address, .flags = 0, .buf = buf, .len = 2
76 };
77 if (i2c_transfer(priv->i2c, &msg, 1) != 1) {
78 printk(KERN_WARNING "MT2266 I2C write failed\n");
79 return -EREMOTEIO;
80 }
81 return 0;
82}
83
84// Writes a set of consecutive registers
85static int mt2266_writeregs(struct mt2266_priv *priv,u8 *buf, u8 len)
86{
87 struct i2c_msg msg = {
88 .addr = priv->cfg->i2c_address, .flags = 0, .buf = buf, .len = len
89 };
90 if (i2c_transfer(priv->i2c, &msg, 1) != 1) {
91 printk(KERN_WARNING "MT2266 I2C write failed (len=%i)\n",(int)len);
92 return -EREMOTEIO;
93 }
94 return 0;
95}
96
97// Initialisation sequences
Olivier DANET542794b2007-11-12 10:48:51 -030098static u8 mt2266_init1[] = { REG_TUNE, 0x00, 0x00, 0x28,
99 0x00, 0x52, 0x99, 0x3f };
Olivier DANET54d75eb2007-07-25 14:42:54 -0300100
101static u8 mt2266_init2[] = {
Olivier DANET542794b2007-11-12 10:48:51 -0300102 0x17, 0x6d, 0x71, 0x61, 0xc0, 0xbf, 0xff, 0xdc, 0x00, 0x0a, 0xd4,
103 0x03, 0x64, 0x64, 0x64, 0x64, 0x22, 0xaa, 0xf2, 0x1e, 0x80, 0x14,
104 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x7f, 0x5e, 0x3f, 0xff, 0xff,
105 0xff, 0x00, 0x77, 0x0f, 0x2d
106};
Olivier DANET54d75eb2007-07-25 14:42:54 -0300107
Olivier DANET542794b2007-11-12 10:48:51 -0300108static u8 mt2266_init_8mhz[] = { REG_BANDWIDTH, 0x22, 0x22, 0x22, 0x22,
109 0x22, 0x22, 0x22, 0x22 };
Olivier DANET54d75eb2007-07-25 14:42:54 -0300110
Olivier DANET542794b2007-11-12 10:48:51 -0300111static u8 mt2266_init_7mhz[] = { REG_BANDWIDTH, 0x32, 0x32, 0x32, 0x32,
112 0x32, 0x32, 0x32, 0x32 };
Olivier DANET54d75eb2007-07-25 14:42:54 -0300113
Olivier DANET542794b2007-11-12 10:48:51 -0300114static u8 mt2266_init_6mhz[] = { REG_BANDWIDTH, 0xa7, 0xa7, 0xa7, 0xa7,
115 0xa7, 0xa7, 0xa7, 0xa7 };
116
117static u8 mt2266_uhf[] = { 0x1d, 0xdc, 0x00, 0x0a, 0xd4, 0x03, 0x64, 0x64,
118 0x64, 0x64, 0x22, 0xaa, 0xf2, 0x1e, 0x80, 0x14 };
119
120static u8 mt2266_vhf[] = { 0x1d, 0xfe, 0x00, 0x00, 0xb4, 0x03, 0xa5, 0xa5,
121 0xa5, 0xa5, 0x82, 0xaa, 0xf1, 0x17, 0x80, 0x1f };
Olivier DANET54d75eb2007-07-25 14:42:54 -0300122
123#define FREF 30000 // Quartz oscillator 30 MHz
124
125static int mt2266_set_params(struct dvb_frontend *fe, struct dvb_frontend_parameters *params)
126{
Mauro Carvalho Chehab82c01262011-12-20 21:49:12 -0300127 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
Olivier DANET54d75eb2007-07-25 14:42:54 -0300128 struct mt2266_priv *priv;
129 int ret=0;
130 u32 freq;
131 u32 tune;
132 u8 lnaband;
133 u8 b[10];
134 int i;
Olivier DANET542794b2007-11-12 10:48:51 -0300135 u8 band;
Olivier DANET54d75eb2007-07-25 14:42:54 -0300136
137 priv = fe->tuner_priv;
138
Mauro Carvalho Chehab82c01262011-12-20 21:49:12 -0300139 freq = priv->frequency / 1000; /* Hz -> kHz */
Olivier DANET542794b2007-11-12 10:48:51 -0300140 if (freq < 470000 && freq > 230000)
141 return -EINVAL; /* Gap between VHF and UHF bands */
Olivier DANET54d75eb2007-07-25 14:42:54 -0300142
Mauro Carvalho Chehab82c01262011-12-20 21:49:12 -0300143 priv->frequency = c->frequency;
Olivier DANET542794b2007-11-12 10:48:51 -0300144 tune = 2 * freq * (8192/16) / (FREF/16);
145 band = (freq < 300000) ? MT2266_VHF : MT2266_UHF;
146 if (band == MT2266_VHF)
147 tune *= 2;
Olivier DANET54d75eb2007-07-25 14:42:54 -0300148
Mauro Carvalho Chehab82c01262011-12-20 21:49:12 -0300149 switch (c->bandwidth_hz) {
150 case 6000000:
Olivier DANET542794b2007-11-12 10:48:51 -0300151 mt2266_writeregs(priv, mt2266_init_6mhz,
152 sizeof(mt2266_init_6mhz));
Mauro Carvalho Chehab82c01262011-12-20 21:49:12 -0300153 priv->bandwidth = BANDWIDTH_6_MHZ;
Olivier DANET542794b2007-11-12 10:48:51 -0300154 break;
Mauro Carvalho Chehab82c01262011-12-20 21:49:12 -0300155 case 8000000:
Olivier DANET542794b2007-11-12 10:48:51 -0300156 mt2266_writeregs(priv, mt2266_init_8mhz,
157 sizeof(mt2266_init_8mhz));
Mauro Carvalho Chehab82c01262011-12-20 21:49:12 -0300158 priv->bandwidth = BANDWIDTH_8_MHZ;
159 break;
160 case 7000000:
161 default:
162 mt2266_writeregs(priv, mt2266_init_7mhz,
163 sizeof(mt2266_init_7mhz));
164 priv->bandwidth = BANDWIDTH_7_MHZ;
Olivier DANET542794b2007-11-12 10:48:51 -0300165 break;
166 }
167
168 if (band == MT2266_VHF && priv->band == MT2266_UHF) {
169 dprintk("Switch from UHF to VHF");
170 mt2266_writereg(priv, 0x05, 0x04);
171 mt2266_writereg(priv, 0x19, 0x61);
172 mt2266_writeregs(priv, mt2266_vhf, sizeof(mt2266_vhf));
173 } else if (band == MT2266_UHF && priv->band == MT2266_VHF) {
174 dprintk("Switch from VHF to UHF");
175 mt2266_writereg(priv, 0x05, 0x52);
176 mt2266_writereg(priv, 0x19, 0x61);
177 mt2266_writeregs(priv, mt2266_uhf, sizeof(mt2266_uhf));
178 }
179 msleep(10);
180
181 if (freq <= 495000)
182 lnaband = 0xEE;
183 else if (freq <= 525000)
184 lnaband = 0xDD;
185 else if (freq <= 550000)
186 lnaband = 0xCC;
187 else if (freq <= 580000)
188 lnaband = 0xBB;
189 else if (freq <= 605000)
190 lnaband = 0xAA;
191 else if (freq <= 630000)
192 lnaband = 0x99;
193 else if (freq <= 655000)
194 lnaband = 0x88;
195 else if (freq <= 685000)
196 lnaband = 0x77;
197 else if (freq <= 710000)
198 lnaband = 0x66;
199 else if (freq <= 735000)
200 lnaband = 0x55;
201 else if (freq <= 765000)
202 lnaband = 0x44;
203 else if (freq <= 802000)
204 lnaband = 0x33;
205 else if (freq <= 840000)
206 lnaband = 0x22;
207 else
208 lnaband = 0x11;
Olivier DANET54d75eb2007-07-25 14:42:54 -0300209
210 b[0] = REG_TUNE;
211 b[1] = (tune >> 8) & 0x1F;
212 b[2] = tune & 0xFF;
213 b[3] = tune >> 13;
214 mt2266_writeregs(priv,b,4);
215
Olivier DANET542794b2007-11-12 10:48:51 -0300216 dprintk("set_parms: tune=%d band=%d %s",
217 (int) tune, (int) lnaband,
218 (band == MT2266_UHF) ? "UHF" : "VHF");
219 dprintk("set_parms: [1..3]: %2x %2x %2x",
220 (int) b[1], (int) b[2], (int)b[3]);
Olivier DANET54d75eb2007-07-25 14:42:54 -0300221
Olivier DANET542794b2007-11-12 10:48:51 -0300222 if (band == MT2266_UHF) {
223 b[0] = 0x05;
224 b[1] = (priv->band == MT2266_VHF) ? 0x52 : 0x62;
225 b[2] = lnaband;
226 mt2266_writeregs(priv, b, 3);
227 }
Olivier DANET54d75eb2007-07-25 14:42:54 -0300228
Olivier DANET542794b2007-11-12 10:48:51 -0300229 /* Wait for pll lock or timeout */
Olivier DANET54d75eb2007-07-25 14:42:54 -0300230 i = 0;
231 do {
232 mt2266_readreg(priv,REG_LOCK,b);
Olivier DANET542794b2007-11-12 10:48:51 -0300233 if (b[0] & 0x40)
Olivier DANET54d75eb2007-07-25 14:42:54 -0300234 break;
235 msleep(10);
236 i++;
237 } while (i<10);
Patrick Boettcherb6884a12007-07-27 10:08:51 -0300238 dprintk("Lock when i=%i",(int)i);
Olivier DANET542794b2007-11-12 10:48:51 -0300239
240 if (band == MT2266_UHF && priv->band == MT2266_VHF)
241 mt2266_writereg(priv, 0x05, 0x62);
242
243 priv->band = band;
244
Olivier DANET54d75eb2007-07-25 14:42:54 -0300245 return ret;
246}
247
248static void mt2266_calibrate(struct mt2266_priv *priv)
249{
Olivier DANET542794b2007-11-12 10:48:51 -0300250 mt2266_writereg(priv, 0x11, 0x03);
251 mt2266_writereg(priv, 0x11, 0x01);
252 mt2266_writeregs(priv, mt2266_init1, sizeof(mt2266_init1));
253 mt2266_writeregs(priv, mt2266_init2, sizeof(mt2266_init2));
254 mt2266_writereg(priv, 0x33, 0x5e);
255 mt2266_writereg(priv, 0x10, 0x10);
256 mt2266_writereg(priv, 0x10, 0x00);
257 mt2266_writeregs(priv, mt2266_init_8mhz, sizeof(mt2266_init_8mhz));
Olivier DANET54d75eb2007-07-25 14:42:54 -0300258 msleep(25);
Olivier DANET542794b2007-11-12 10:48:51 -0300259 mt2266_writereg(priv, 0x17, 0x6d);
260 mt2266_writereg(priv, 0x1c, 0x00);
Olivier DANET54d75eb2007-07-25 14:42:54 -0300261 msleep(75);
Olivier DANET542794b2007-11-12 10:48:51 -0300262 mt2266_writereg(priv, 0x17, 0x6d);
263 mt2266_writereg(priv, 0x1c, 0xff);
Olivier DANET54d75eb2007-07-25 14:42:54 -0300264}
265
266static int mt2266_get_frequency(struct dvb_frontend *fe, u32 *frequency)
267{
268 struct mt2266_priv *priv = fe->tuner_priv;
269 *frequency = priv->frequency;
270 return 0;
271}
272
273static int mt2266_get_bandwidth(struct dvb_frontend *fe, u32 *bandwidth)
274{
275 struct mt2266_priv *priv = fe->tuner_priv;
276 *bandwidth = priv->bandwidth;
277 return 0;
278}
279
280static int mt2266_init(struct dvb_frontend *fe)
281{
Olivier DANET542794b2007-11-12 10:48:51 -0300282 int ret;
Olivier DANET54d75eb2007-07-25 14:42:54 -0300283 struct mt2266_priv *priv = fe->tuner_priv;
Olivier DANET542794b2007-11-12 10:48:51 -0300284 ret = mt2266_writereg(priv, 0x17, 0x6d);
285 if (ret < 0)
286 return ret;
287 ret = mt2266_writereg(priv, 0x1c, 0xff);
288 if (ret < 0)
289 return ret;
Olivier DANET54d75eb2007-07-25 14:42:54 -0300290 return 0;
291}
292
293static int mt2266_sleep(struct dvb_frontend *fe)
294{
295 struct mt2266_priv *priv = fe->tuner_priv;
Olivier DANET542794b2007-11-12 10:48:51 -0300296 mt2266_writereg(priv, 0x17, 0x6d);
297 mt2266_writereg(priv, 0x1c, 0x00);
Olivier DANET54d75eb2007-07-25 14:42:54 -0300298 return 0;
299}
300
301static int mt2266_release(struct dvb_frontend *fe)
302{
303 kfree(fe->tuner_priv);
304 fe->tuner_priv = NULL;
305 return 0;
306}
307
308static const struct dvb_tuner_ops mt2266_tuner_ops = {
309 .info = {
310 .name = "Microtune MT2266",
Olivier DANET542794b2007-11-12 10:48:51 -0300311 .frequency_min = 174000000,
312 .frequency_max = 862000000,
Olivier DANET54d75eb2007-07-25 14:42:54 -0300313 .frequency_step = 50000,
314 },
315 .release = mt2266_release,
316 .init = mt2266_init,
317 .sleep = mt2266_sleep,
318 .set_params = mt2266_set_params,
319 .get_frequency = mt2266_get_frequency,
320 .get_bandwidth = mt2266_get_bandwidth
321};
322
323struct dvb_frontend * mt2266_attach(struct dvb_frontend *fe, struct i2c_adapter *i2c, struct mt2266_config *cfg)
324{
325 struct mt2266_priv *priv = NULL;
326 u8 id = 0;
327
328 priv = kzalloc(sizeof(struct mt2266_priv), GFP_KERNEL);
329 if (priv == NULL)
330 return NULL;
331
332 priv->cfg = cfg;
333 priv->i2c = i2c;
Olivier DANET542794b2007-11-12 10:48:51 -0300334 priv->band = MT2266_UHF;
Olivier DANET54d75eb2007-07-25 14:42:54 -0300335
Olivier DANET542794b2007-11-12 10:48:51 -0300336 if (mt2266_readreg(priv, 0, &id)) {
Olivier DANET54d75eb2007-07-25 14:42:54 -0300337 kfree(priv);
338 return NULL;
339 }
340 if (id != PART_REV) {
341 kfree(priv);
342 return NULL;
343 }
344 printk(KERN_INFO "MT2266: successfully identified\n");
345 memcpy(&fe->ops.tuner_ops, &mt2266_tuner_ops, sizeof(struct dvb_tuner_ops));
346
347 fe->tuner_priv = priv;
348 mt2266_calibrate(priv);
349 return fe;
350}
351EXPORT_SYMBOL(mt2266_attach);
352
353MODULE_AUTHOR("Olivier DANET");
354MODULE_DESCRIPTION("Microtune MT2266 silicon tuner driver");
355MODULE_LICENSE("GPL");