V4L/DVB (5914): Add initial support for Dual-DVB-T stick
[deliverable/linux.git] / drivers / media / dvb / frontends / mt2266.c
1 /*
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>
18 #include <linux/moduleparam.h>
19 #include <linux/delay.h>
20 #include <linux/dvb/frontend.h>
21 #include <linux/i2c.h>
22
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
36 struct mt2266_priv {
37 struct mt2266_config *cfg;
38 struct i2c_adapter *i2c;
39
40 u32 frequency;
41 u32 bandwidth;
42 };
43
44 /* Here, frequencies are expressed in kiloHertz to avoid 32 bits overflows */
45
46 static int debug;
47 module_param(debug, int, 0644);
48 MODULE_PARM_DESC(debug, "Turn on/off debugging (default:off).");
49
50 #define dprintk(args...) do { if (debug) {printk(KERN_DEBUG "MT2266: " args); printk("\n"); }} while (0)
51
52 // Reads a single register
53 static int mt2266_readreg(struct mt2266_priv *priv, u8 reg, u8 *val)
54 {
55 struct i2c_msg msg[2] = {
56 { .addr = priv->cfg->i2c_address, .flags = 0, .buf = &reg, .len = 1 },
57 { .addr = priv->cfg->i2c_address, .flags = I2C_M_RD, .buf = val, .len = 1 },
58 };
59 if (i2c_transfer(priv->i2c, msg, 2) != 2) {
60 printk(KERN_WARNING "MT2266 I2C read failed\n");
61 return -EREMOTEIO;
62 }
63 return 0;
64 }
65
66 // Writes a single register
67 static int mt2266_writereg(struct mt2266_priv *priv, u8 reg, u8 val)
68 {
69 u8 buf[2] = { reg, val };
70 struct i2c_msg msg = {
71 .addr = priv->cfg->i2c_address, .flags = 0, .buf = buf, .len = 2
72 };
73 if (i2c_transfer(priv->i2c, &msg, 1) != 1) {
74 printk(KERN_WARNING "MT2266 I2C write failed\n");
75 return -EREMOTEIO;
76 }
77 return 0;
78 }
79
80 // Writes a set of consecutive registers
81 static int mt2266_writeregs(struct mt2266_priv *priv,u8 *buf, u8 len)
82 {
83 struct i2c_msg msg = {
84 .addr = priv->cfg->i2c_address, .flags = 0, .buf = buf, .len = len
85 };
86 if (i2c_transfer(priv->i2c, &msg, 1) != 1) {
87 printk(KERN_WARNING "MT2266 I2C write failed (len=%i)\n",(int)len);
88 return -EREMOTEIO;
89 }
90 return 0;
91 }
92
93 // Initialisation sequences
94 static u8 mt2266_init1[] = {
95 REG_TUNE,
96 0x00, 0x00, 0x28, 0x00, 0x52, 0x99, 0x3f };
97
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 };
102
103 static u8 mt2266_init_8mhz[] = {
104 REG_BANDWIDTH,
105 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22 };
106
107 static u8 mt2266_init_7mhz[] = {
108 REG_BANDWIDTH,
109 0x32, 0x32, 0x32, 0x32, 0x32, 0x32, 0x32, 0x32 };
110
111 static u8 mt2266_init_6mhz[] = {
112 REG_BANDWIDTH,
113 0xa7, 0xa7, 0xa7, 0xa7, 0xa7, 0xa7, 0xa7, 0xa7 };
114
115 #define FREF 30000 // Quartz oscillator 30 MHz
116
117 static int mt2266_set_params(struct dvb_frontend *fe, struct dvb_frontend_parameters *params)
118 {
119 struct mt2266_priv *priv;
120 int ret=0;
121 u32 freq;
122 u32 tune;
123 u8 lnaband;
124 u8 b[10];
125 int i;
126
127 priv = fe->tuner_priv;
128
129 mt2266_writereg(priv,0x17,0x6d);
130 mt2266_writereg(priv,0x1c,0xff);
131
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);
136
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;
150
151 msleep(100);
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));
155
156 b[0] = REG_TUNE;
157 b[1] = (tune >> 8) & 0x1F;
158 b[2] = tune & 0xFF;
159 b[3] = tune >> 13;
160 mt2266_writeregs(priv,b,4);
161
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]);
164
165 b[0] = 0x05;
166 b[1] = 0x62;
167 b[2] = lnaband;
168 mt2266_writeregs(priv,b,3);
169
170 //Waits for pll lock or timeout
171 i = 0;
172 do {
173 mt2266_readreg(priv,REG_LOCK,b);
174 if ((b[0] & 0x40)==0x40)
175 break;
176 msleep(10);
177 i++;
178 } while (i<10);
179 dprintk("Lock when i=%i\n",(int)i);
180 return ret;
181 }
182
183 static void mt2266_calibrate(struct mt2266_priv *priv)
184 {
185 mt2266_writereg(priv,0x11,0x03);
186 mt2266_writereg(priv,0x11,0x01);
187
188 mt2266_writeregs(priv,mt2266_init1,sizeof(mt2266_init1));
189 mt2266_writeregs(priv,mt2266_init2,sizeof(mt2266_init2));
190
191 mt2266_writereg(priv,0x33,0x5e);
192 mt2266_writereg(priv,0x10,0x10);
193 mt2266_writereg(priv,0x10,0x00);
194
195 mt2266_writeregs(priv,mt2266_init_8mhz,sizeof(mt2266_init_8mhz));
196
197 msleep(25);
198 mt2266_writereg(priv,0x17,0x6d);
199 mt2266_writereg(priv,0x1c,0x00);
200 msleep(75);
201 mt2266_writereg(priv,0x17,0x6d);
202 mt2266_writereg(priv,0x1c,0xff);
203 }
204
205 static int mt2266_get_frequency(struct dvb_frontend *fe, u32 *frequency)
206 {
207 struct mt2266_priv *priv = fe->tuner_priv;
208 *frequency = priv->frequency;
209 return 0;
210 }
211
212 static int mt2266_get_bandwidth(struct dvb_frontend *fe, u32 *bandwidth)
213 {
214 struct mt2266_priv *priv = fe->tuner_priv;
215 *bandwidth = priv->bandwidth;
216 return 0;
217 }
218
219 static int mt2266_init(struct dvb_frontend *fe)
220 {
221 struct mt2266_priv *priv = fe->tuner_priv;
222 mt2266_writereg(priv,0x17,0x6d);
223 mt2266_writereg(priv,0x1c,0xff);
224 return 0;
225 }
226
227 static int mt2266_sleep(struct dvb_frontend *fe)
228 {
229 struct mt2266_priv *priv = fe->tuner_priv;
230 mt2266_writereg(priv,0x17,0x6d);
231 mt2266_writereg(priv,0x1c,0x00);
232 return 0;
233 }
234
235 static int mt2266_release(struct dvb_frontend *fe)
236 {
237 kfree(fe->tuner_priv);
238 fe->tuner_priv = NULL;
239 return 0;
240 }
241
242 static const struct dvb_tuner_ops mt2266_tuner_ops = {
243 .info = {
244 .name = "Microtune MT2266",
245 .frequency_min = 470000000,
246 .frequency_max = 860000000,
247 .frequency_step = 50000,
248 },
249 .release = mt2266_release,
250 .init = mt2266_init,
251 .sleep = mt2266_sleep,
252 .set_params = mt2266_set_params,
253 .get_frequency = mt2266_get_frequency,
254 .get_bandwidth = mt2266_get_bandwidth
255 };
256
257 struct dvb_frontend * mt2266_attach(struct dvb_frontend *fe, struct i2c_adapter *i2c, struct mt2266_config *cfg)
258 {
259 struct mt2266_priv *priv = NULL;
260 u8 id = 0;
261
262 priv = kzalloc(sizeof(struct mt2266_priv), GFP_KERNEL);
263 if (priv == NULL)
264 return NULL;
265
266 priv->cfg = cfg;
267 priv->i2c = i2c;
268
269 if (mt2266_readreg(priv,0,&id) != 0) {
270 kfree(priv);
271 return NULL;
272 }
273 if (id != PART_REV) {
274 kfree(priv);
275 return NULL;
276 }
277 printk(KERN_INFO "MT2266: successfully identified\n");
278 memcpy(&fe->ops.tuner_ops, &mt2266_tuner_ops, sizeof(struct dvb_tuner_ops));
279
280 fe->tuner_priv = priv;
281 mt2266_calibrate(priv);
282 return fe;
283 }
284 EXPORT_SYMBOL(mt2266_attach);
285
286 MODULE_AUTHOR("Olivier DANET");
287 MODULE_DESCRIPTION("Microtune MT2266 silicon tuner driver");
288 MODULE_LICENSE("GPL");
This page took 0.070075 seconds and 5 git commands to generate.