2 * Driver for Freescale MC44S803 Low Power CMOS Broadband Tuner
4 * Copyright (c) 2009 Jochen Friedrich <jochen@scram.de>
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
15 * GNU General Public License for more details.
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.=
22 #include <linux/module.h>
23 #include <linux/delay.h>
24 #include <linux/dvb/frontend.h>
25 #include <linux/i2c.h>
26 #include <linux/slab.h>
28 #include "dvb_frontend.h"
31 #include "mc44s803_priv.h"
33 #define mc_printk(level, format, arg...) \
34 printk(level "mc44s803: " format , ## arg)
36 /* Writes a single register */
37 static int mc44s803_writereg(struct mc44s803_priv
*priv
, u32 val
)
40 struct i2c_msg msg
= {
41 .addr
= priv
->cfg
->i2c_address
, .flags
= 0, .buf
= buf
, .len
= 3
44 buf
[0] = (val
& 0xff0000) >> 16;
45 buf
[1] = (val
& 0xff00) >> 8;
46 buf
[2] = (val
& 0xff);
48 if (i2c_transfer(priv
->i2c
, &msg
, 1) != 1) {
49 mc_printk(KERN_WARNING
, "I2C write failed\n");
55 /* Reads a single register */
56 static int mc44s803_readreg(struct mc44s803_priv
*priv
, u8 reg
, u32
*val
)
61 struct i2c_msg msg
[] = {
62 { .addr
= priv
->cfg
->i2c_address
, .flags
= I2C_M_RD
,
63 .buf
= buf
, .len
= 3 },
66 wval
= MC44S803_REG_SM(MC44S803_REG_DATAREG
, MC44S803_ADDR
) |
67 MC44S803_REG_SM(reg
, MC44S803_D
);
69 ret
= mc44s803_writereg(priv
, wval
);
73 if (i2c_transfer(priv
->i2c
, msg
, 1) != 1) {
74 mc_printk(KERN_WARNING
, "I2C read failed\n");
78 *val
= (buf
[0] << 16) | (buf
[1] << 8) | buf
[2];
83 static int mc44s803_release(struct dvb_frontend
*fe
)
85 struct mc44s803_priv
*priv
= fe
->tuner_priv
;
87 fe
->tuner_priv
= NULL
;
93 static int mc44s803_init(struct dvb_frontend
*fe
)
95 struct mc44s803_priv
*priv
= fe
->tuner_priv
;
99 if (fe
->ops
.i2c_gate_ctrl
)
100 fe
->ops
.i2c_gate_ctrl(fe
, 1);
103 val
= MC44S803_REG_SM(MC44S803_REG_RESET
, MC44S803_ADDR
) |
104 MC44S803_REG_SM(1, MC44S803_RS
);
106 err
= mc44s803_writereg(priv
, val
);
110 val
= MC44S803_REG_SM(MC44S803_REG_RESET
, MC44S803_ADDR
);
112 err
= mc44s803_writereg(priv
, val
);
116 /* Power Up and Start Osc */
118 val
= MC44S803_REG_SM(MC44S803_REG_REFOSC
, MC44S803_ADDR
) |
119 MC44S803_REG_SM(0xC0, MC44S803_REFOSC
) |
120 MC44S803_REG_SM(1, MC44S803_OSCSEL
);
122 err
= mc44s803_writereg(priv
, val
);
126 val
= MC44S803_REG_SM(MC44S803_REG_POWER
, MC44S803_ADDR
) |
127 MC44S803_REG_SM(0x200, MC44S803_POWER
);
129 err
= mc44s803_writereg(priv
, val
);
135 val
= MC44S803_REG_SM(MC44S803_REG_REFOSC
, MC44S803_ADDR
) |
136 MC44S803_REG_SM(0x40, MC44S803_REFOSC
) |
137 MC44S803_REG_SM(1, MC44S803_OSCSEL
);
139 err
= mc44s803_writereg(priv
, val
);
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
);
151 err
= mc44s803_writereg(priv
, val
);
155 /* Setup Cirquit Adjust */
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
);
167 err
= mc44s803_writereg(priv
, val
);
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
);
179 err
= mc44s803_writereg(priv
, val
);
185 val
= MC44S803_REG_SM(MC44S803_REG_DIGTUNE
, MC44S803_ADDR
) |
186 MC44S803_REG_SM(3, MC44S803_XOD
);
188 err
= mc44s803_writereg(priv
, val
);
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
);
201 err
= mc44s803_writereg(priv
, val
);
205 if (fe
->ops
.i2c_gate_ctrl
)
206 fe
->ops
.i2c_gate_ctrl(fe
, 0);
210 if (fe
->ops
.i2c_gate_ctrl
)
211 fe
->ops
.i2c_gate_ctrl(fe
, 0);
213 mc_printk(KERN_WARNING
, "I/O Error\n");
217 static int mc44s803_set_params(struct dvb_frontend
*fe
)
219 struct mc44s803_priv
*priv
= fe
->tuner_priv
;
220 struct dtv_frontend_properties
*c
= &fe
->dtv_property_cache
;
221 u32 r1
, r2
, n1
, n2
, lo1
, lo2
, freq
, val
;
224 priv
->frequency
= c
->frequency
;
226 r1
= MC44S803_OSC
/ 1000000;
227 r2
= MC44S803_OSC
/ 100000;
229 n1
= (c
->frequency
+ MC44S803_IF1
+ 500000) / 1000000;
230 freq
= MC44S803_OSC
/ r1
* n1
;
231 lo1
= ((60 * n1
) + (r1
/ 2)) / r1
;
232 freq
= freq
- c
->frequency
;
234 n2
= (freq
- MC44S803_IF2
+ 50000) / 100000;
235 lo2
= ((60 * n2
) + (r2
/ 2)) / r2
;
237 if (fe
->ops
.i2c_gate_ctrl
)
238 fe
->ops
.i2c_gate_ctrl(fe
, 1);
240 val
= MC44S803_REG_SM(MC44S803_REG_REFDIV
, MC44S803_ADDR
) |
241 MC44S803_REG_SM(r1
-1, MC44S803_R1
) |
242 MC44S803_REG_SM(r2
-1, MC44S803_R2
) |
243 MC44S803_REG_SM(1, MC44S803_REFBUF_EN
);
245 err
= mc44s803_writereg(priv
, val
);
249 val
= MC44S803_REG_SM(MC44S803_REG_LO1
, MC44S803_ADDR
) |
250 MC44S803_REG_SM(n1
-2, MC44S803_LO1
);
252 err
= mc44s803_writereg(priv
, val
);
256 val
= MC44S803_REG_SM(MC44S803_REG_LO2
, MC44S803_ADDR
) |
257 MC44S803_REG_SM(n2
-2, MC44S803_LO2
);
259 err
= mc44s803_writereg(priv
, val
);
263 val
= MC44S803_REG_SM(MC44S803_REG_DIGTUNE
, MC44S803_ADDR
) |
264 MC44S803_REG_SM(1, MC44S803_DA
) |
265 MC44S803_REG_SM(lo1
, MC44S803_LO_REF
) |
266 MC44S803_REG_SM(1, MC44S803_AT
);
268 err
= mc44s803_writereg(priv
, val
);
272 val
= MC44S803_REG_SM(MC44S803_REG_DIGTUNE
, MC44S803_ADDR
) |
273 MC44S803_REG_SM(2, MC44S803_DA
) |
274 MC44S803_REG_SM(lo2
, MC44S803_LO_REF
) |
275 MC44S803_REG_SM(1, MC44S803_AT
);
277 err
= mc44s803_writereg(priv
, val
);
281 if (fe
->ops
.i2c_gate_ctrl
)
282 fe
->ops
.i2c_gate_ctrl(fe
, 0);
287 if (fe
->ops
.i2c_gate_ctrl
)
288 fe
->ops
.i2c_gate_ctrl(fe
, 0);
290 mc_printk(KERN_WARNING
, "I/O Error\n");
294 static int mc44s803_get_frequency(struct dvb_frontend
*fe
, u32
*frequency
)
296 struct mc44s803_priv
*priv
= fe
->tuner_priv
;
297 *frequency
= priv
->frequency
;
301 static const struct dvb_tuner_ops mc44s803_tuner_ops
= {
303 .name
= "Freescale MC44S803",
304 .frequency_min
= 48000000,
305 .frequency_max
= 1000000000,
306 .frequency_step
= 100000,
309 .release
= mc44s803_release
,
310 .init
= mc44s803_init
,
311 .set_params
= mc44s803_set_params
,
312 .get_frequency
= mc44s803_get_frequency
315 /* This functions tries to identify a MC44S803 tuner by reading the ID
316 register. This is hasty. */
317 struct dvb_frontend
*mc44s803_attach(struct dvb_frontend
*fe
,
318 struct i2c_adapter
*i2c
, struct mc44s803_config
*cfg
)
320 struct mc44s803_priv
*priv
;
327 priv
= kzalloc(sizeof(struct mc44s803_priv
), GFP_KERNEL
);
335 if (fe
->ops
.i2c_gate_ctrl
)
336 fe
->ops
.i2c_gate_ctrl(fe
, 1); /* open i2c_gate */
338 ret
= mc44s803_readreg(priv
, MC44S803_REG_ID
, ®
);
342 id
= MC44S803_REG_MS(reg
, MC44S803_ID
);
345 mc_printk(KERN_ERR
, "unsupported ID "
346 "(%x should be 0x14)\n", id
);
350 mc_printk(KERN_INFO
, "successfully identified (ID = %x)\n", id
);
351 memcpy(&fe
->ops
.tuner_ops
, &mc44s803_tuner_ops
,
352 sizeof(struct dvb_tuner_ops
));
354 fe
->tuner_priv
= priv
;
356 if (fe
->ops
.i2c_gate_ctrl
)
357 fe
->ops
.i2c_gate_ctrl(fe
, 0); /* close i2c_gate */
362 if (fe
->ops
.i2c_gate_ctrl
)
363 fe
->ops
.i2c_gate_ctrl(fe
, 0); /* close i2c_gate */
368 EXPORT_SYMBOL(mc44s803_attach
);
370 MODULE_AUTHOR("Jochen Friedrich");
371 MODULE_DESCRIPTION("Freescale MC44S803 silicon tuner driver");
372 MODULE_LICENSE("GPL");