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.
18 #include <linux/module.h>
19 #include <linux/delay.h>
20 #include <linux/dvb/frontend.h>
21 #include <linux/i2c.h>
22 #include <linux/slab.h>
24 #include <media/dvb_frontend.h>
27 #include "mc44s803_priv.h"
29 #define mc_printk(level, format, arg...) \
30 printk(level "mc44s803: " format , ## arg)
32 /* Writes a single register */
33 static int mc44s803_writereg(struct mc44s803_priv
*priv
, u32 val
)
36 struct i2c_msg msg
= {
37 .addr
= priv
->cfg
->i2c_address
, .flags
= 0, .buf
= buf
, .len
= 3
40 buf
[0] = (val
& 0xff0000) >> 16;
41 buf
[1] = (val
& 0xff00) >> 8;
42 buf
[2] = (val
& 0xff);
44 if (i2c_transfer(priv
->i2c
, &msg
, 1) != 1) {
45 mc_printk(KERN_WARNING
, "I2C write failed\n");
51 /* Reads a single register */
52 static int mc44s803_readreg(struct mc44s803_priv
*priv
, u8 reg
, u32
*val
)
57 struct i2c_msg msg
[] = {
58 { .addr
= priv
->cfg
->i2c_address
, .flags
= I2C_M_RD
,
59 .buf
= buf
, .len
= 3 },
62 wval
= MC44S803_REG_SM(MC44S803_REG_DATAREG
, MC44S803_ADDR
) |
63 MC44S803_REG_SM(reg
, MC44S803_D
);
65 ret
= mc44s803_writereg(priv
, wval
);
69 if (i2c_transfer(priv
->i2c
, msg
, 1) != 1) {
70 mc_printk(KERN_WARNING
, "I2C read failed\n");
74 *val
= (buf
[0] << 16) | (buf
[1] << 8) | buf
[2];
79 static void mc44s803_release(struct dvb_frontend
*fe
)
81 struct mc44s803_priv
*priv
= fe
->tuner_priv
;
83 fe
->tuner_priv
= NULL
;
87 static int mc44s803_init(struct dvb_frontend
*fe
)
89 struct mc44s803_priv
*priv
= fe
->tuner_priv
;
93 if (fe
->ops
.i2c_gate_ctrl
)
94 fe
->ops
.i2c_gate_ctrl(fe
, 1);
97 val
= MC44S803_REG_SM(MC44S803_REG_RESET
, MC44S803_ADDR
) |
98 MC44S803_REG_SM(1, MC44S803_RS
);
100 err
= mc44s803_writereg(priv
, val
);
104 val
= MC44S803_REG_SM(MC44S803_REG_RESET
, MC44S803_ADDR
);
106 err
= mc44s803_writereg(priv
, val
);
110 /* Power Up and Start Osc */
112 val
= MC44S803_REG_SM(MC44S803_REG_REFOSC
, MC44S803_ADDR
) |
113 MC44S803_REG_SM(0xC0, MC44S803_REFOSC
) |
114 MC44S803_REG_SM(1, MC44S803_OSCSEL
);
116 err
= mc44s803_writereg(priv
, val
);
120 val
= MC44S803_REG_SM(MC44S803_REG_POWER
, MC44S803_ADDR
) |
121 MC44S803_REG_SM(0x200, MC44S803_POWER
);
123 err
= mc44s803_writereg(priv
, val
);
129 val
= MC44S803_REG_SM(MC44S803_REG_REFOSC
, MC44S803_ADDR
) |
130 MC44S803_REG_SM(0x40, MC44S803_REFOSC
) |
131 MC44S803_REG_SM(1, MC44S803_OSCSEL
);
133 err
= mc44s803_writereg(priv
, val
);
141 val
= MC44S803_REG_SM(MC44S803_REG_MIXER
, MC44S803_ADDR
) |
142 MC44S803_REG_SM(1, MC44S803_TRI_STATE
) |
143 MC44S803_REG_SM(0x7F, MC44S803_MIXER_RES
);
145 err
= mc44s803_writereg(priv
, val
);
149 /* Setup Cirquit Adjust */
151 val
= MC44S803_REG_SM(MC44S803_REG_CIRCADJ
, MC44S803_ADDR
) |
152 MC44S803_REG_SM(1, MC44S803_G1
) |
153 MC44S803_REG_SM(1, MC44S803_G3
) |
154 MC44S803_REG_SM(0x3, MC44S803_CIRCADJ_RES
) |
155 MC44S803_REG_SM(1, MC44S803_G6
) |
156 MC44S803_REG_SM(priv
->cfg
->dig_out
, MC44S803_S1
) |
157 MC44S803_REG_SM(0x3, MC44S803_LP
) |
158 MC44S803_REG_SM(1, MC44S803_CLRF
) |
159 MC44S803_REG_SM(1, MC44S803_CLIF
);
161 err
= mc44s803_writereg(priv
, val
);
165 val
= MC44S803_REG_SM(MC44S803_REG_CIRCADJ
, MC44S803_ADDR
) |
166 MC44S803_REG_SM(1, MC44S803_G1
) |
167 MC44S803_REG_SM(1, MC44S803_G3
) |
168 MC44S803_REG_SM(0x3, MC44S803_CIRCADJ_RES
) |
169 MC44S803_REG_SM(1, MC44S803_G6
) |
170 MC44S803_REG_SM(priv
->cfg
->dig_out
, MC44S803_S1
) |
171 MC44S803_REG_SM(0x3, MC44S803_LP
);
173 err
= mc44s803_writereg(priv
, val
);
179 val
= MC44S803_REG_SM(MC44S803_REG_DIGTUNE
, MC44S803_ADDR
) |
180 MC44S803_REG_SM(3, MC44S803_XOD
);
182 err
= mc44s803_writereg(priv
, val
);
188 val
= MC44S803_REG_SM(MC44S803_REG_LNAAGC
, MC44S803_ADDR
) |
189 MC44S803_REG_SM(1, MC44S803_AT1
) |
190 MC44S803_REG_SM(1, MC44S803_AT2
) |
191 MC44S803_REG_SM(1, MC44S803_AGC_AN_DIG
) |
192 MC44S803_REG_SM(1, MC44S803_AGC_READ_EN
) |
193 MC44S803_REG_SM(1, MC44S803_LNA0
);
195 err
= mc44s803_writereg(priv
, val
);
199 if (fe
->ops
.i2c_gate_ctrl
)
200 fe
->ops
.i2c_gate_ctrl(fe
, 0);
204 if (fe
->ops
.i2c_gate_ctrl
)
205 fe
->ops
.i2c_gate_ctrl(fe
, 0);
207 mc_printk(KERN_WARNING
, "I/O Error\n");
211 static int mc44s803_set_params(struct dvb_frontend
*fe
)
213 struct mc44s803_priv
*priv
= fe
->tuner_priv
;
214 struct dtv_frontend_properties
*c
= &fe
->dtv_property_cache
;
215 u32 r1
, r2
, n1
, n2
, lo1
, lo2
, freq
, val
;
218 priv
->frequency
= c
->frequency
;
220 r1
= MC44S803_OSC
/ 1000000;
221 r2
= MC44S803_OSC
/ 100000;
223 n1
= (c
->frequency
+ MC44S803_IF1
+ 500000) / 1000000;
224 freq
= MC44S803_OSC
/ r1
* n1
;
225 lo1
= ((60 * n1
) + (r1
/ 2)) / r1
;
226 freq
= freq
- c
->frequency
;
228 n2
= (freq
- MC44S803_IF2
+ 50000) / 100000;
229 lo2
= ((60 * n2
) + (r2
/ 2)) / r2
;
231 if (fe
->ops
.i2c_gate_ctrl
)
232 fe
->ops
.i2c_gate_ctrl(fe
, 1);
234 val
= MC44S803_REG_SM(MC44S803_REG_REFDIV
, MC44S803_ADDR
) |
235 MC44S803_REG_SM(r1
-1, MC44S803_R1
) |
236 MC44S803_REG_SM(r2
-1, MC44S803_R2
) |
237 MC44S803_REG_SM(1, MC44S803_REFBUF_EN
);
239 err
= mc44s803_writereg(priv
, val
);
243 val
= MC44S803_REG_SM(MC44S803_REG_LO1
, MC44S803_ADDR
) |
244 MC44S803_REG_SM(n1
-2, MC44S803_LO1
);
246 err
= mc44s803_writereg(priv
, val
);
250 val
= MC44S803_REG_SM(MC44S803_REG_LO2
, MC44S803_ADDR
) |
251 MC44S803_REG_SM(n2
-2, MC44S803_LO2
);
253 err
= mc44s803_writereg(priv
, val
);
257 val
= MC44S803_REG_SM(MC44S803_REG_DIGTUNE
, MC44S803_ADDR
) |
258 MC44S803_REG_SM(1, MC44S803_DA
) |
259 MC44S803_REG_SM(lo1
, MC44S803_LO_REF
) |
260 MC44S803_REG_SM(1, MC44S803_AT
);
262 err
= mc44s803_writereg(priv
, val
);
266 val
= MC44S803_REG_SM(MC44S803_REG_DIGTUNE
, MC44S803_ADDR
) |
267 MC44S803_REG_SM(2, MC44S803_DA
) |
268 MC44S803_REG_SM(lo2
, MC44S803_LO_REF
) |
269 MC44S803_REG_SM(1, MC44S803_AT
);
271 err
= mc44s803_writereg(priv
, val
);
275 if (fe
->ops
.i2c_gate_ctrl
)
276 fe
->ops
.i2c_gate_ctrl(fe
, 0);
281 if (fe
->ops
.i2c_gate_ctrl
)
282 fe
->ops
.i2c_gate_ctrl(fe
, 0);
284 mc_printk(KERN_WARNING
, "I/O Error\n");
288 static int mc44s803_get_frequency(struct dvb_frontend
*fe
, u32
*frequency
)
290 struct mc44s803_priv
*priv
= fe
->tuner_priv
;
291 *frequency
= priv
->frequency
;
295 static int mc44s803_get_if_frequency(struct dvb_frontend
*fe
, u32
*frequency
)
297 *frequency
= MC44S803_IF2
; /* 36.125 MHz */
301 static const struct dvb_tuner_ops mc44s803_tuner_ops
= {
303 .name
= "Freescale MC44S803",
304 .frequency_min_hz
= 48 * MHz
,
305 .frequency_max_hz
= 1000 * MHz
,
306 .frequency_step_hz
= 100 * kHz
,
309 .release
= mc44s803_release
,
310 .init
= mc44s803_init
,
311 .set_params
= mc44s803_set_params
,
312 .get_frequency
= mc44s803_get_frequency
,
313 .get_if_frequency
= mc44s803_get_if_frequency
,
316 /* This functions tries to identify a MC44S803 tuner by reading the ID
317 register. This is hasty. */
318 struct dvb_frontend
*mc44s803_attach(struct dvb_frontend
*fe
,
319 struct i2c_adapter
*i2c
, struct mc44s803_config
*cfg
)
321 struct mc44s803_priv
*priv
;
328 priv
= kzalloc(sizeof(struct mc44s803_priv
), GFP_KERNEL
);
336 if (fe
->ops
.i2c_gate_ctrl
)
337 fe
->ops
.i2c_gate_ctrl(fe
, 1); /* open i2c_gate */
339 ret
= mc44s803_readreg(priv
, MC44S803_REG_ID
, ®
);
343 id
= MC44S803_REG_MS(reg
, MC44S803_ID
);
346 mc_printk(KERN_ERR
, "unsupported ID (%x should be 0x14)\n",
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
));
355 fe
->tuner_priv
= priv
;
357 if (fe
->ops
.i2c_gate_ctrl
)
358 fe
->ops
.i2c_gate_ctrl(fe
, 0); /* close i2c_gate */
363 if (fe
->ops
.i2c_gate_ctrl
)
364 fe
->ops
.i2c_gate_ctrl(fe
, 0); /* close i2c_gate */
369 EXPORT_SYMBOL(mc44s803_attach
);
371 MODULE_AUTHOR("Jochen Friedrich");
372 MODULE_DESCRIPTION("Freescale MC44S803 silicon tuner driver");
373 MODULE_LICENSE("GPL");