blob: 5a8758cad5e9f12d9f3aa51cd6dc10170fb586bb [file] [log] [blame]
Jochen Friedrichb72dbae2009-02-02 14:50:09 -03001/*
2 * Driver for Freescale MC44S803 Low Power CMOS Broadband Tuner
3 *
4 * Copyright (c) 2009 Jochen Friedrich <jochen@scram.de>
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 *
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.=
20 */
21
22#include <linux/module.h>
23#include <linux/delay.h>
24#include <linux/dvb/frontend.h>
25#include <linux/i2c.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090026#include <linux/slab.h>
Jochen Friedrichb72dbae2009-02-02 14:50:09 -030027
28#include "dvb_frontend.h"
29
30#include "mc44s803.h"
31#include "mc44s803_priv.h"
32
33#define mc_printk(level, format, arg...) \
34 printk(level "mc44s803: " format , ## arg)
35
36/* Writes a single register */
37static int mc44s803_writereg(struct mc44s803_priv *priv, u32 val)
38{
39 u8 buf[3];
40 struct i2c_msg msg = {
41 .addr = priv->cfg->i2c_address, .flags = 0, .buf = buf, .len = 3
42 };
43
44 buf[0] = (val & 0xff0000) >> 16;
45 buf[1] = (val & 0xff00) >> 8;
46 buf[2] = (val & 0xff);
47
48 if (i2c_transfer(priv->i2c, &msg, 1) != 1) {
49 mc_printk(KERN_WARNING, "I2C write failed\n");
50 return -EREMOTEIO;
51 }
52 return 0;
53}
54
55/* Reads a single register */
56static int mc44s803_readreg(struct mc44s803_priv *priv, u8 reg, u32 *val)
57{
58 u32 wval;
59 u8 buf[3];
60 int ret;
61 struct i2c_msg msg[] = {
62 { .addr = priv->cfg->i2c_address, .flags = I2C_M_RD,
63 .buf = buf, .len = 3 },
64 };
65
66 wval = MC44S803_REG_SM(MC44S803_REG_DATAREG, MC44S803_ADDR) |
67 MC44S803_REG_SM(reg, MC44S803_D);
68
69 ret = mc44s803_writereg(priv, wval);
70 if (ret)
71 return ret;
72
73 if (i2c_transfer(priv->i2c, msg, 1) != 1) {
74 mc_printk(KERN_WARNING, "I2C read failed\n");
75 return -EREMOTEIO;
76 }
77
78 *val = (buf[0] << 16) | (buf[1] << 8) | buf[2];
79
80 return 0;
81}
82
83static int mc44s803_release(struct dvb_frontend *fe)
84{
85 struct mc44s803_priv *priv = fe->tuner_priv;
86
87 fe->tuner_priv = NULL;
88 kfree(priv);
89
90 return 0;
91}
92
93static int mc44s803_init(struct dvb_frontend *fe)
94{
95 struct mc44s803_priv *priv = fe->tuner_priv;
96 u32 val;
97 int err;
98
99 if (fe->ops.i2c_gate_ctrl)
100 fe->ops.i2c_gate_ctrl(fe, 1);
101
102/* Reset chip */
103 val = MC44S803_REG_SM(MC44S803_REG_RESET, MC44S803_ADDR) |
104 MC44S803_REG_SM(1, MC44S803_RS);
105
106 err = mc44s803_writereg(priv, val);
107 if (err)
108 goto exit;
109
110 val = MC44S803_REG_SM(MC44S803_REG_RESET, MC44S803_ADDR);
111
112 err = mc44s803_writereg(priv, val);
113 if (err)
114 goto exit;
115
116/* Power Up and Start Osc */
117
118 val = MC44S803_REG_SM(MC44S803_REG_REFOSC, MC44S803_ADDR) |
119 MC44S803_REG_SM(0xC0, MC44S803_REFOSC) |
120 MC44S803_REG_SM(1, MC44S803_OSCSEL);
121
122 err = mc44s803_writereg(priv, val);
123 if (err)
124 goto exit;
125
126 val = MC44S803_REG_SM(MC44S803_REG_POWER, MC44S803_ADDR) |
127 MC44S803_REG_SM(0x200, MC44S803_POWER);
128
129 err = mc44s803_writereg(priv, val);
130 if (err)
131 goto exit;
132
133 msleep(10);
134
135 val = MC44S803_REG_SM(MC44S803_REG_REFOSC, MC44S803_ADDR) |
136 MC44S803_REG_SM(0x40, MC44S803_REFOSC) |
137 MC44S803_REG_SM(1, MC44S803_OSCSEL);
138
139 err = mc44s803_writereg(priv, val);
140 if (err)
141 goto exit;
142
143 msleep(20);
144
145/* Setup Mixer */
146
147 val = MC44S803_REG_SM(MC44S803_REG_MIXER, MC44S803_ADDR) |
148 MC44S803_REG_SM(1, MC44S803_TRI_STATE) |
149 MC44S803_REG_SM(0x7F, MC44S803_MIXER_RES);
150
151 err = mc44s803_writereg(priv, val);
152 if (err)
153 goto exit;
154
155/* Setup Cirquit Adjust */
156
157 val = MC44S803_REG_SM(MC44S803_REG_CIRCADJ, MC44S803_ADDR) |
158 MC44S803_REG_SM(1, MC44S803_G1) |
159 MC44S803_REG_SM(1, MC44S803_G3) |
160 MC44S803_REG_SM(0x3, MC44S803_CIRCADJ_RES) |
161 MC44S803_REG_SM(1, MC44S803_G6) |
162 MC44S803_REG_SM(priv->cfg->dig_out, MC44S803_S1) |
163 MC44S803_REG_SM(0x3, MC44S803_LP) |
164 MC44S803_REG_SM(1, MC44S803_CLRF) |
165 MC44S803_REG_SM(1, MC44S803_CLIF);
166
167 err = mc44s803_writereg(priv, val);
168 if (err)
169 goto exit;
170
171 val = MC44S803_REG_SM(MC44S803_REG_CIRCADJ, MC44S803_ADDR) |
172 MC44S803_REG_SM(1, MC44S803_G1) |
173 MC44S803_REG_SM(1, MC44S803_G3) |
174 MC44S803_REG_SM(0x3, MC44S803_CIRCADJ_RES) |
175 MC44S803_REG_SM(1, MC44S803_G6) |
176 MC44S803_REG_SM(priv->cfg->dig_out, MC44S803_S1) |
177 MC44S803_REG_SM(0x3, MC44S803_LP);
178
179 err = mc44s803_writereg(priv, val);
180 if (err)
181 goto exit;
182
183/* Setup Digtune */
184
185 val = MC44S803_REG_SM(MC44S803_REG_DIGTUNE, MC44S803_ADDR) |
186 MC44S803_REG_SM(3, MC44S803_XOD);
187
188 err = mc44s803_writereg(priv, val);
189 if (err)
190 goto exit;
191
192/* Setup AGC */
193
194 val = MC44S803_REG_SM(MC44S803_REG_LNAAGC, MC44S803_ADDR) |
195 MC44S803_REG_SM(1, MC44S803_AT1) |
196 MC44S803_REG_SM(1, MC44S803_AT2) |
197 MC44S803_REG_SM(1, MC44S803_AGC_AN_DIG) |
198 MC44S803_REG_SM(1, MC44S803_AGC_READ_EN) |
199 MC44S803_REG_SM(1, MC44S803_LNA0);
200
201 err = mc44s803_writereg(priv, val);
202 if (err)
203 goto exit;
204
205 if (fe->ops.i2c_gate_ctrl)
206 fe->ops.i2c_gate_ctrl(fe, 0);
207 return 0;
208
209exit:
210 if (fe->ops.i2c_gate_ctrl)
211 fe->ops.i2c_gate_ctrl(fe, 0);
212
213 mc_printk(KERN_WARNING, "I/O Error\n");
214 return err;
215}
216
217static int mc44s803_set_params(struct dvb_frontend *fe,
218 struct dvb_frontend_parameters *params)
219{
220 struct mc44s803_priv *priv = fe->tuner_priv;
Mauro Carvalho Chehab35fea062011-12-20 16:53:50 -0300221 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
Jochen Friedrichb72dbae2009-02-02 14:50:09 -0300222 u32 r1, r2, n1, n2, lo1, lo2, freq, val;
223 int err;
224
Mauro Carvalho Chehab35fea062011-12-20 16:53:50 -0300225 priv->frequency = c->frequency;
Jochen Friedrichb72dbae2009-02-02 14:50:09 -0300226
227 r1 = MC44S803_OSC / 1000000;
228 r2 = MC44S803_OSC / 100000;
229
Mauro Carvalho Chehab35fea062011-12-20 16:53:50 -0300230 n1 = (c->frequency + MC44S803_IF1 + 500000) / 1000000;
Jochen Friedrichb72dbae2009-02-02 14:50:09 -0300231 freq = MC44S803_OSC / r1 * n1;
232 lo1 = ((60 * n1) + (r1 / 2)) / r1;
Mauro Carvalho Chehab35fea062011-12-20 16:53:50 -0300233 freq = freq - c->frequency;
Jochen Friedrichb72dbae2009-02-02 14:50:09 -0300234
235 n2 = (freq - MC44S803_IF2 + 50000) / 100000;
236 lo2 = ((60 * n2) + (r2 / 2)) / r2;
237
238 if (fe->ops.i2c_gate_ctrl)
239 fe->ops.i2c_gate_ctrl(fe, 1);
240
241 val = MC44S803_REG_SM(MC44S803_REG_REFDIV, MC44S803_ADDR) |
242 MC44S803_REG_SM(r1-1, MC44S803_R1) |
243 MC44S803_REG_SM(r2-1, MC44S803_R2) |
244 MC44S803_REG_SM(1, MC44S803_REFBUF_EN);
245
246 err = mc44s803_writereg(priv, val);
247 if (err)
248 goto exit;
249
250 val = MC44S803_REG_SM(MC44S803_REG_LO1, MC44S803_ADDR) |
251 MC44S803_REG_SM(n1-2, MC44S803_LO1);
252
253 err = mc44s803_writereg(priv, val);
254 if (err)
255 goto exit;
256
257 val = MC44S803_REG_SM(MC44S803_REG_LO2, MC44S803_ADDR) |
258 MC44S803_REG_SM(n2-2, MC44S803_LO2);
259
260 err = mc44s803_writereg(priv, val);
261 if (err)
262 goto exit;
263
264 val = MC44S803_REG_SM(MC44S803_REG_DIGTUNE, MC44S803_ADDR) |
265 MC44S803_REG_SM(1, MC44S803_DA) |
266 MC44S803_REG_SM(lo1, MC44S803_LO_REF) |
267 MC44S803_REG_SM(1, MC44S803_AT);
268
269 err = mc44s803_writereg(priv, val);
270 if (err)
271 goto exit;
272
273 val = MC44S803_REG_SM(MC44S803_REG_DIGTUNE, MC44S803_ADDR) |
274 MC44S803_REG_SM(2, MC44S803_DA) |
275 MC44S803_REG_SM(lo2, MC44S803_LO_REF) |
276 MC44S803_REG_SM(1, MC44S803_AT);
277
278 err = mc44s803_writereg(priv, val);
279 if (err)
280 goto exit;
281
282 if (fe->ops.i2c_gate_ctrl)
283 fe->ops.i2c_gate_ctrl(fe, 0);
284
285 return 0;
286
287exit:
288 if (fe->ops.i2c_gate_ctrl)
289 fe->ops.i2c_gate_ctrl(fe, 0);
290
291 mc_printk(KERN_WARNING, "I/O Error\n");
292 return err;
293}
294
295static int mc44s803_get_frequency(struct dvb_frontend *fe, u32 *frequency)
296{
297 struct mc44s803_priv *priv = fe->tuner_priv;
298 *frequency = priv->frequency;
299 return 0;
300}
301
302static const struct dvb_tuner_ops mc44s803_tuner_ops = {
303 .info = {
304 .name = "Freescale MC44S803",
305 .frequency_min = 48000000,
306 .frequency_max = 1000000000,
307 .frequency_step = 100000,
308 },
309
310 .release = mc44s803_release,
311 .init = mc44s803_init,
312 .set_params = mc44s803_set_params,
313 .get_frequency = mc44s803_get_frequency
314};
315
316/* This functions tries to identify a MC44S803 tuner by reading the ID
317 register. This is hasty. */
318struct dvb_frontend *mc44s803_attach(struct dvb_frontend *fe,
319 struct i2c_adapter *i2c, struct mc44s803_config *cfg)
320{
321 struct mc44s803_priv *priv;
322 u32 reg;
323 u8 id;
324 int ret;
325
326 reg = 0;
327
328 priv = kzalloc(sizeof(struct mc44s803_priv), GFP_KERNEL);
329 if (priv == NULL)
330 return NULL;
331
332 priv->cfg = cfg;
333 priv->i2c = i2c;
334 priv->fe = fe;
335
336 if (fe->ops.i2c_gate_ctrl)
337 fe->ops.i2c_gate_ctrl(fe, 1); /* open i2c_gate */
338
339 ret = mc44s803_readreg(priv, MC44S803_REG_ID, &reg);
340 if (ret)
341 goto error;
342
343 id = MC44S803_REG_MS(reg, MC44S803_ID);
344
345 if (id != 0x14) {
346 mc_printk(KERN_ERR, "unsupported ID "
347 "(%x should be 0x14)\n", id);
348 goto error;
349 }
350
351 mc_printk(KERN_INFO, "successfully identified (ID = %x)\n", id);
352 memcpy(&fe->ops.tuner_ops, &mc44s803_tuner_ops,
353 sizeof(struct dvb_tuner_ops));
354
355 fe->tuner_priv = priv;
356
357 if (fe->ops.i2c_gate_ctrl)
358 fe->ops.i2c_gate_ctrl(fe, 0); /* close i2c_gate */
359
360 return fe;
361
362error:
363 if (fe->ops.i2c_gate_ctrl)
364 fe->ops.i2c_gate_ctrl(fe, 0); /* close i2c_gate */
365
366 kfree(priv);
367 return NULL;
368}
369EXPORT_SYMBOL(mc44s803_attach);
370
371MODULE_AUTHOR("Jochen Friedrich");
372MODULE_DESCRIPTION("Freescale MC44S803 silicon tuner driver");
373MODULE_LICENSE("GPL");