sh_eth: fix EESIPR values for SH77{34|63}
[linux/fpc-iii.git] / drivers / media / tuners / mc44s803.c
blobaba580b4ac2cd2779e076c61d64cfef98f87042b
1 /*
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"
30 #include "mc44s803.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)
39 u8 buf[3];
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");
50 return -EREMOTEIO;
52 return 0;
55 /* Reads a single register */
56 static int mc44s803_readreg(struct mc44s803_priv *priv, u8 reg, u32 *val)
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 },
66 wval = MC44S803_REG_SM(MC44S803_REG_DATAREG, MC44S803_ADDR) |
67 MC44S803_REG_SM(reg, MC44S803_D);
69 ret = mc44s803_writereg(priv, wval);
70 if (ret)
71 return ret;
73 if (i2c_transfer(priv->i2c, msg, 1) != 1) {
74 mc_printk(KERN_WARNING, "I2C read failed\n");
75 return -EREMOTEIO;
78 *val = (buf[0] << 16) | (buf[1] << 8) | buf[2];
80 return 0;
83 static void mc44s803_release(struct dvb_frontend *fe)
85 struct mc44s803_priv *priv = fe->tuner_priv;
87 fe->tuner_priv = NULL;
88 kfree(priv);
91 static int mc44s803_init(struct dvb_frontend *fe)
93 struct mc44s803_priv *priv = fe->tuner_priv;
94 u32 val;
95 int err;
97 if (fe->ops.i2c_gate_ctrl)
98 fe->ops.i2c_gate_ctrl(fe, 1);
100 /* Reset chip */
101 val = MC44S803_REG_SM(MC44S803_REG_RESET, MC44S803_ADDR) |
102 MC44S803_REG_SM(1, MC44S803_RS);
104 err = mc44s803_writereg(priv, val);
105 if (err)
106 goto exit;
108 val = MC44S803_REG_SM(MC44S803_REG_RESET, MC44S803_ADDR);
110 err = mc44s803_writereg(priv, val);
111 if (err)
112 goto exit;
114 /* Power Up and Start Osc */
116 val = MC44S803_REG_SM(MC44S803_REG_REFOSC, MC44S803_ADDR) |
117 MC44S803_REG_SM(0xC0, MC44S803_REFOSC) |
118 MC44S803_REG_SM(1, MC44S803_OSCSEL);
120 err = mc44s803_writereg(priv, val);
121 if (err)
122 goto exit;
124 val = MC44S803_REG_SM(MC44S803_REG_POWER, MC44S803_ADDR) |
125 MC44S803_REG_SM(0x200, MC44S803_POWER);
127 err = mc44s803_writereg(priv, val);
128 if (err)
129 goto exit;
131 msleep(10);
133 val = MC44S803_REG_SM(MC44S803_REG_REFOSC, MC44S803_ADDR) |
134 MC44S803_REG_SM(0x40, MC44S803_REFOSC) |
135 MC44S803_REG_SM(1, MC44S803_OSCSEL);
137 err = mc44s803_writereg(priv, val);
138 if (err)
139 goto exit;
141 msleep(20);
143 /* Setup Mixer */
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);
149 err = mc44s803_writereg(priv, val);
150 if (err)
151 goto exit;
153 /* Setup Cirquit Adjust */
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);
165 err = mc44s803_writereg(priv, val);
166 if (err)
167 goto exit;
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);
177 err = mc44s803_writereg(priv, val);
178 if (err)
179 goto exit;
181 /* Setup Digtune */
183 val = MC44S803_REG_SM(MC44S803_REG_DIGTUNE, MC44S803_ADDR) |
184 MC44S803_REG_SM(3, MC44S803_XOD);
186 err = mc44s803_writereg(priv, val);
187 if (err)
188 goto exit;
190 /* Setup AGC */
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);
199 err = mc44s803_writereg(priv, val);
200 if (err)
201 goto exit;
203 if (fe->ops.i2c_gate_ctrl)
204 fe->ops.i2c_gate_ctrl(fe, 0);
205 return 0;
207 exit:
208 if (fe->ops.i2c_gate_ctrl)
209 fe->ops.i2c_gate_ctrl(fe, 0);
211 mc_printk(KERN_WARNING, "I/O Error\n");
212 return err;
215 static int mc44s803_set_params(struct dvb_frontend *fe)
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;
222 priv->frequency = c->frequency;
224 r1 = MC44S803_OSC / 1000000;
225 r2 = MC44S803_OSC / 100000;
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;
232 n2 = (freq - MC44S803_IF2 + 50000) / 100000;
233 lo2 = ((60 * n2) + (r2 / 2)) / r2;
235 if (fe->ops.i2c_gate_ctrl)
236 fe->ops.i2c_gate_ctrl(fe, 1);
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);
243 err = mc44s803_writereg(priv, val);
244 if (err)
245 goto exit;
247 val = MC44S803_REG_SM(MC44S803_REG_LO1, MC44S803_ADDR) |
248 MC44S803_REG_SM(n1-2, MC44S803_LO1);
250 err = mc44s803_writereg(priv, val);
251 if (err)
252 goto exit;
254 val = MC44S803_REG_SM(MC44S803_REG_LO2, MC44S803_ADDR) |
255 MC44S803_REG_SM(n2-2, MC44S803_LO2);
257 err = mc44s803_writereg(priv, val);
258 if (err)
259 goto exit;
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);
266 err = mc44s803_writereg(priv, val);
267 if (err)
268 goto exit;
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);
275 err = mc44s803_writereg(priv, val);
276 if (err)
277 goto exit;
279 if (fe->ops.i2c_gate_ctrl)
280 fe->ops.i2c_gate_ctrl(fe, 0);
282 return 0;
284 exit:
285 if (fe->ops.i2c_gate_ctrl)
286 fe->ops.i2c_gate_ctrl(fe, 0);
288 mc_printk(KERN_WARNING, "I/O Error\n");
289 return err;
292 static int mc44s803_get_frequency(struct dvb_frontend *fe, u32 *frequency)
294 struct mc44s803_priv *priv = fe->tuner_priv;
295 *frequency = priv->frequency;
296 return 0;
299 static int mc44s803_get_if_frequency(struct dvb_frontend *fe, u32 *frequency)
301 *frequency = MC44S803_IF2; /* 36.125 MHz */
302 return 0;
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,
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,
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)
325 struct mc44s803_priv *priv;
326 u32 reg;
327 u8 id;
328 int ret;
330 reg = 0;
332 priv = kzalloc(sizeof(struct mc44s803_priv), GFP_KERNEL);
333 if (priv == NULL)
334 return NULL;
336 priv->cfg = cfg;
337 priv->i2c = i2c;
338 priv->fe = fe;
340 if (fe->ops.i2c_gate_ctrl)
341 fe->ops.i2c_gate_ctrl(fe, 1); /* open i2c_gate */
343 ret = mc44s803_readreg(priv, MC44S803_REG_ID, &reg);
344 if (ret)
345 goto error;
347 id = MC44S803_REG_MS(reg, MC44S803_ID);
349 if (id != 0x14) {
350 mc_printk(KERN_ERR, "unsupported ID (%x should be 0x14)\n",
351 id);
352 goto error;
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));
359 fe->tuner_priv = priv;
361 if (fe->ops.i2c_gate_ctrl)
362 fe->ops.i2c_gate_ctrl(fe, 0); /* close i2c_gate */
364 return fe;
366 error:
367 if (fe->ops.i2c_gate_ctrl)
368 fe->ops.i2c_gate_ctrl(fe, 0); /* close i2c_gate */
370 kfree(priv);
371 return NULL;
373 EXPORT_SYMBOL(mc44s803_attach);
375 MODULE_AUTHOR("Jochen Friedrich");
376 MODULE_DESCRIPTION("Freescale MC44S803 silicon tuner driver");
377 MODULE_LICENSE("GPL");