xref: /linux/drivers/media/tuners/mc44s803.c (revision 32786fdc9506aeba98278c1844d4bfb766863832)
1 /*
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>
26 #include <linux/slab.h>
27 
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 */
37 static 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 */
56 static 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 
83 static void 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 
91 static int mc44s803_init(struct dvb_frontend *fe)
92 {
93 	struct mc44s803_priv *priv = fe->tuner_priv;
94 	u32 val;
95 	int err;
96 
97 	if (fe->ops.i2c_gate_ctrl)
98 		fe->ops.i2c_gate_ctrl(fe, 1);
99 
100 /* Reset chip */
101 	val = MC44S803_REG_SM(MC44S803_REG_RESET, MC44S803_ADDR) |
102 	      MC44S803_REG_SM(1, MC44S803_RS);
103 
104 	err = mc44s803_writereg(priv, val);
105 	if (err)
106 		goto exit;
107 
108 	val = MC44S803_REG_SM(MC44S803_REG_RESET, MC44S803_ADDR);
109 
110 	err = mc44s803_writereg(priv, val);
111 	if (err)
112 		goto exit;
113 
114 /* Power Up and Start Osc */
115 
116 	val = MC44S803_REG_SM(MC44S803_REG_REFOSC, MC44S803_ADDR) |
117 	      MC44S803_REG_SM(0xC0, MC44S803_REFOSC) |
118 	      MC44S803_REG_SM(1, MC44S803_OSCSEL);
119 
120 	err = mc44s803_writereg(priv, val);
121 	if (err)
122 		goto exit;
123 
124 	val = MC44S803_REG_SM(MC44S803_REG_POWER, MC44S803_ADDR) |
125 	      MC44S803_REG_SM(0x200, MC44S803_POWER);
126 
127 	err = mc44s803_writereg(priv, val);
128 	if (err)
129 		goto exit;
130 
131 	msleep(10);
132 
133 	val = MC44S803_REG_SM(MC44S803_REG_REFOSC, MC44S803_ADDR) |
134 	      MC44S803_REG_SM(0x40, MC44S803_REFOSC) |
135 	      MC44S803_REG_SM(1, MC44S803_OSCSEL);
136 
137 	err = mc44s803_writereg(priv, val);
138 	if (err)
139 		goto exit;
140 
141 	msleep(20);
142 
143 /* Setup Mixer */
144 
145 	val = MC44S803_REG_SM(MC44S803_REG_MIXER, MC44S803_ADDR) |
146 	      MC44S803_REG_SM(1, MC44S803_TRI_STATE) |
147 	      MC44S803_REG_SM(0x7F, MC44S803_MIXER_RES);
148 
149 	err = mc44s803_writereg(priv, val);
150 	if (err)
151 		goto exit;
152 
153 /* Setup Cirquit Adjust */
154 
155 	val = MC44S803_REG_SM(MC44S803_REG_CIRCADJ, MC44S803_ADDR) |
156 	      MC44S803_REG_SM(1, MC44S803_G1) |
157 	      MC44S803_REG_SM(1, MC44S803_G3) |
158 	      MC44S803_REG_SM(0x3, MC44S803_CIRCADJ_RES) |
159 	      MC44S803_REG_SM(1, MC44S803_G6) |
160 	      MC44S803_REG_SM(priv->cfg->dig_out, MC44S803_S1) |
161 	      MC44S803_REG_SM(0x3, MC44S803_LP) |
162 	      MC44S803_REG_SM(1, MC44S803_CLRF) |
163 	      MC44S803_REG_SM(1, MC44S803_CLIF);
164 
165 	err = mc44s803_writereg(priv, val);
166 	if (err)
167 		goto exit;
168 
169 	val = MC44S803_REG_SM(MC44S803_REG_CIRCADJ, MC44S803_ADDR) |
170 	      MC44S803_REG_SM(1, MC44S803_G1) |
171 	      MC44S803_REG_SM(1, MC44S803_G3) |
172 	      MC44S803_REG_SM(0x3, MC44S803_CIRCADJ_RES) |
173 	      MC44S803_REG_SM(1, MC44S803_G6) |
174 	      MC44S803_REG_SM(priv->cfg->dig_out, MC44S803_S1) |
175 	      MC44S803_REG_SM(0x3, MC44S803_LP);
176 
177 	err = mc44s803_writereg(priv, val);
178 	if (err)
179 		goto exit;
180 
181 /* Setup Digtune */
182 
183 	val = MC44S803_REG_SM(MC44S803_REG_DIGTUNE, MC44S803_ADDR) |
184 	      MC44S803_REG_SM(3, MC44S803_XOD);
185 
186 	err = mc44s803_writereg(priv, val);
187 	if (err)
188 		goto exit;
189 
190 /* Setup AGC */
191 
192 	val = MC44S803_REG_SM(MC44S803_REG_LNAAGC, MC44S803_ADDR) |
193 	      MC44S803_REG_SM(1, MC44S803_AT1) |
194 	      MC44S803_REG_SM(1, MC44S803_AT2) |
195 	      MC44S803_REG_SM(1, MC44S803_AGC_AN_DIG) |
196 	      MC44S803_REG_SM(1, MC44S803_AGC_READ_EN) |
197 	      MC44S803_REG_SM(1, MC44S803_LNA0);
198 
199 	err = mc44s803_writereg(priv, val);
200 	if (err)
201 		goto exit;
202 
203 	if (fe->ops.i2c_gate_ctrl)
204 		fe->ops.i2c_gate_ctrl(fe, 0);
205 	return 0;
206 
207 exit:
208 	if (fe->ops.i2c_gate_ctrl)
209 		fe->ops.i2c_gate_ctrl(fe, 0);
210 
211 	mc_printk(KERN_WARNING, "I/O Error\n");
212 	return err;
213 }
214 
215 static int mc44s803_set_params(struct dvb_frontend *fe)
216 {
217 	struct mc44s803_priv *priv = fe->tuner_priv;
218 	struct dtv_frontend_properties *c = &fe->dtv_property_cache;
219 	u32 r1, r2, n1, n2, lo1, lo2, freq, val;
220 	int err;
221 
222 	priv->frequency = c->frequency;
223 
224 	r1 = MC44S803_OSC / 1000000;
225 	r2 = MC44S803_OSC /  100000;
226 
227 	n1 = (c->frequency + MC44S803_IF1 + 500000) / 1000000;
228 	freq = MC44S803_OSC / r1 * n1;
229 	lo1 = ((60 * n1) + (r1 / 2)) / r1;
230 	freq = freq - c->frequency;
231 
232 	n2 = (freq - MC44S803_IF2 + 50000) / 100000;
233 	lo2 = ((60 * n2) + (r2 / 2)) / r2;
234 
235 	if (fe->ops.i2c_gate_ctrl)
236 		fe->ops.i2c_gate_ctrl(fe, 1);
237 
238 	val = MC44S803_REG_SM(MC44S803_REG_REFDIV, MC44S803_ADDR) |
239 	      MC44S803_REG_SM(r1-1, MC44S803_R1) |
240 	      MC44S803_REG_SM(r2-1, MC44S803_R2) |
241 	      MC44S803_REG_SM(1, MC44S803_REFBUF_EN);
242 
243 	err = mc44s803_writereg(priv, val);
244 	if (err)
245 		goto exit;
246 
247 	val = MC44S803_REG_SM(MC44S803_REG_LO1, MC44S803_ADDR) |
248 	      MC44S803_REG_SM(n1-2, MC44S803_LO1);
249 
250 	err = mc44s803_writereg(priv, val);
251 	if (err)
252 		goto exit;
253 
254 	val = MC44S803_REG_SM(MC44S803_REG_LO2, MC44S803_ADDR) |
255 	      MC44S803_REG_SM(n2-2, MC44S803_LO2);
256 
257 	err = mc44s803_writereg(priv, val);
258 	if (err)
259 		goto exit;
260 
261 	val = MC44S803_REG_SM(MC44S803_REG_DIGTUNE, MC44S803_ADDR) |
262 	      MC44S803_REG_SM(1, MC44S803_DA) |
263 	      MC44S803_REG_SM(lo1, MC44S803_LO_REF) |
264 	      MC44S803_REG_SM(1, MC44S803_AT);
265 
266 	err = mc44s803_writereg(priv, val);
267 	if (err)
268 		goto exit;
269 
270 	val = MC44S803_REG_SM(MC44S803_REG_DIGTUNE, MC44S803_ADDR) |
271 	      MC44S803_REG_SM(2, MC44S803_DA) |
272 	      MC44S803_REG_SM(lo2, MC44S803_LO_REF) |
273 	      MC44S803_REG_SM(1, MC44S803_AT);
274 
275 	err = mc44s803_writereg(priv, val);
276 	if (err)
277 		goto exit;
278 
279 	if (fe->ops.i2c_gate_ctrl)
280 		fe->ops.i2c_gate_ctrl(fe, 0);
281 
282 	return 0;
283 
284 exit:
285 	if (fe->ops.i2c_gate_ctrl)
286 		fe->ops.i2c_gate_ctrl(fe, 0);
287 
288 	mc_printk(KERN_WARNING, "I/O Error\n");
289 	return err;
290 }
291 
292 static int mc44s803_get_frequency(struct dvb_frontend *fe, u32 *frequency)
293 {
294 	struct mc44s803_priv *priv = fe->tuner_priv;
295 	*frequency = priv->frequency;
296 	return 0;
297 }
298 
299 static int mc44s803_get_if_frequency(struct dvb_frontend *fe, u32 *frequency)
300 {
301 	*frequency = MC44S803_IF2; /* 36.125 MHz */
302 	return 0;
303 }
304 
305 static const struct dvb_tuner_ops mc44s803_tuner_ops = {
306 	.info = {
307 		.name           = "Freescale MC44S803",
308 		.frequency_min  =   48000000,
309 		.frequency_max  = 1000000000,
310 		.frequency_step =     100000,
311 	},
312 
313 	.release       = mc44s803_release,
314 	.init          = mc44s803_init,
315 	.set_params    = mc44s803_set_params,
316 	.get_frequency = mc44s803_get_frequency,
317 	.get_if_frequency = mc44s803_get_if_frequency,
318 };
319 
320 /* This functions tries to identify a MC44S803 tuner by reading the ID
321    register. This is hasty. */
322 struct dvb_frontend *mc44s803_attach(struct dvb_frontend *fe,
323 	 struct i2c_adapter *i2c, struct mc44s803_config *cfg)
324 {
325 	struct mc44s803_priv *priv;
326 	u32 reg;
327 	u8 id;
328 	int ret;
329 
330 	reg = 0;
331 
332 	priv = kzalloc(sizeof(struct mc44s803_priv), GFP_KERNEL);
333 	if (priv == NULL)
334 		return NULL;
335 
336 	priv->cfg = cfg;
337 	priv->i2c = i2c;
338 	priv->fe  = fe;
339 
340 	if (fe->ops.i2c_gate_ctrl)
341 		fe->ops.i2c_gate_ctrl(fe, 1); /* open i2c_gate */
342 
343 	ret = mc44s803_readreg(priv, MC44S803_REG_ID, &reg);
344 	if (ret)
345 		goto error;
346 
347 	id = MC44S803_REG_MS(reg, MC44S803_ID);
348 
349 	if (id != 0x14) {
350 		mc_printk(KERN_ERR, "unsupported ID (%x should be 0x14)\n",
351 			  id);
352 		goto error;
353 	}
354 
355 	mc_printk(KERN_INFO, "successfully identified (ID = %x)\n", id);
356 	memcpy(&fe->ops.tuner_ops, &mc44s803_tuner_ops,
357 	       sizeof(struct dvb_tuner_ops));
358 
359 	fe->tuner_priv = priv;
360 
361 	if (fe->ops.i2c_gate_ctrl)
362 		fe->ops.i2c_gate_ctrl(fe, 0); /* close i2c_gate */
363 
364 	return fe;
365 
366 error:
367 	if (fe->ops.i2c_gate_ctrl)
368 		fe->ops.i2c_gate_ctrl(fe, 0); /* close i2c_gate */
369 
370 	kfree(priv);
371 	return NULL;
372 }
373 EXPORT_SYMBOL(mc44s803_attach);
374 
375 MODULE_AUTHOR("Jochen Friedrich");
376 MODULE_DESCRIPTION("Freescale MC44S803 silicon tuner driver");
377 MODULE_LICENSE("GPL");
378