2 STV6110(A) Silicon tuner driver
4 Copyright (C) Manu Abraham <abraham.manu@gmail.com>
6 Copyright (C) ST Microelectronics
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 #include <linux/init.h>
24 #include <linux/kernel.h>
25 #include <linux/module.h>
26 #include <linux/slab.h>
27 #include <linux/string.h>
29 #include <media/dvb_frontend.h>
31 #include "stv6110x_reg.h"
33 #include "stv6110x_priv.h"
35 /* Max transfer size done by I2C transfer functions */
36 #define MAX_XFER_SIZE 64
38 static unsigned int verbose;
39 module_param(verbose, int, 0644);
40 MODULE_PARM_DESC(verbose, "Set Verbosity level");
42 static int stv6110x_read_reg(struct stv6110x_state *stv6110x, u8 reg, u8 *data)
45 const struct stv6110x_config *config = stv6110x->config;
48 struct i2c_msg msg[] = {
49 { .addr = config->addr, .flags = 0, .buf = b0, .len = 1 },
50 { .addr = config->addr, .flags = I2C_M_RD, .buf = b1, .len = 1 }
53 ret = i2c_transfer(stv6110x->i2c, msg, 2);
55 dprintk(FE_ERROR, 1, "I/O Error");
63 static int stv6110x_write_regs(struct stv6110x_state *stv6110x, int start, u8 data[], int len)
66 const struct stv6110x_config *config = stv6110x->config;
67 u8 buf[MAX_XFER_SIZE];
69 struct i2c_msg msg = {
76 if (1 + len > sizeof(buf)) {
78 "%s: i2c wr: len=%d is too big!\n",
87 memcpy(&buf[1], data, len);
89 ret = i2c_transfer(stv6110x->i2c, &msg, 1);
91 dprintk(FE_ERROR, 1, "I/O Error");
98 static int stv6110x_write_reg(struct stv6110x_state *stv6110x, u8 reg, u8 data)
100 u8 tmp = data; /* see gcc.gnu.org/bugzilla/show_bug.cgi?id=81715 */
102 return stv6110x_write_regs(stv6110x, reg, &tmp, 1);
105 static int stv6110x_init(struct dvb_frontend *fe)
107 struct stv6110x_state *stv6110x = fe->tuner_priv;
110 ret = stv6110x_write_regs(stv6110x, 0, stv6110x->regs,
111 ARRAY_SIZE(stv6110x->regs));
113 dprintk(FE_ERROR, 1, "Initialization failed");
120 static int stv6110x_set_frequency(struct dvb_frontend *fe, u32 frequency)
122 struct stv6110x_state *stv6110x = fe->tuner_priv;
124 s32 pVal, pCalc, rDivOpt = 0, pCalcOpt = 1000;
127 STV6110x_SETFIELD(stv6110x->regs[STV6110x_CTRL1], CTRL1_K, (REFCLOCK_MHz - 16));
129 if (frequency <= 1023000) {
130 STV6110x_SETFIELD(stv6110x->regs[STV6110x_TNG1], TNG1_DIV4SEL, 1);
131 STV6110x_SETFIELD(stv6110x->regs[STV6110x_TNG1], TNG1_PRESC32_ON, 0);
133 } else if (frequency <= 1300000) {
134 STV6110x_SETFIELD(stv6110x->regs[STV6110x_TNG1], TNG1_DIV4SEL, 1);
135 STV6110x_SETFIELD(stv6110x->regs[STV6110x_TNG1], TNG1_PRESC32_ON, 1);
137 } else if (frequency <= 2046000) {
138 STV6110x_SETFIELD(stv6110x->regs[STV6110x_TNG1], TNG1_DIV4SEL, 0);
139 STV6110x_SETFIELD(stv6110x->regs[STV6110x_TNG1], TNG1_PRESC32_ON, 0);
142 STV6110x_SETFIELD(stv6110x->regs[STV6110x_TNG1], TNG1_DIV4SEL, 0);
143 STV6110x_SETFIELD(stv6110x->regs[STV6110x_TNG1], TNG1_PRESC32_ON, 1);
147 for (rDiv = 0; rDiv <= 3; rDiv++) {
148 pCalc = (REFCLOCK_kHz / 100) / R_DIV(rDiv);
150 if ((abs((s32)(pCalc - pVal))) < (abs((s32)(pCalcOpt - pVal))))
153 pCalcOpt = (REFCLOCK_kHz / 100) / R_DIV(rDivOpt);
156 divider = (frequency * R_DIV(rDivOpt) * pVal) / REFCLOCK_kHz;
157 divider = (divider + 5) / 10;
159 STV6110x_SETFIELD(stv6110x->regs[STV6110x_TNG1], TNG1_R_DIV, rDivOpt);
160 STV6110x_SETFIELD(stv6110x->regs[STV6110x_TNG1], TNG1_N_DIV_11_8, MSB(divider));
161 STV6110x_SETFIELD(stv6110x->regs[STV6110x_TNG0], TNG0_N_DIV_7_0, LSB(divider));
163 /* VCO Auto calibration */
164 STV6110x_SETFIELD(stv6110x->regs[STV6110x_STAT1], STAT1_CALVCO_STRT, 1);
166 stv6110x_write_reg(stv6110x, STV6110x_CTRL1, stv6110x->regs[STV6110x_CTRL1]);
167 stv6110x_write_reg(stv6110x, STV6110x_TNG1, stv6110x->regs[STV6110x_TNG1]);
168 stv6110x_write_reg(stv6110x, STV6110x_TNG0, stv6110x->regs[STV6110x_TNG0]);
169 stv6110x_write_reg(stv6110x, STV6110x_STAT1, stv6110x->regs[STV6110x_STAT1]);
171 for (i = 0; i < TRIALS; i++) {
172 stv6110x_read_reg(stv6110x, STV6110x_STAT1, &stv6110x->regs[STV6110x_STAT1]);
173 if (!STV6110x_GETFIELD(STAT1_CALVCO_STRT, stv6110x->regs[STV6110x_STAT1]))
181 static int stv6110x_get_frequency(struct dvb_frontend *fe, u32 *frequency)
183 struct stv6110x_state *stv6110x = fe->tuner_priv;
185 stv6110x_read_reg(stv6110x, STV6110x_TNG1, &stv6110x->regs[STV6110x_TNG1]);
186 stv6110x_read_reg(stv6110x, STV6110x_TNG0, &stv6110x->regs[STV6110x_TNG0]);
188 *frequency = (MAKEWORD16(STV6110x_GETFIELD(TNG1_N_DIV_11_8, stv6110x->regs[STV6110x_TNG1]),
189 STV6110x_GETFIELD(TNG0_N_DIV_7_0, stv6110x->regs[STV6110x_TNG0]))) * REFCLOCK_kHz;
191 *frequency /= (1 << (STV6110x_GETFIELD(TNG1_R_DIV, stv6110x->regs[STV6110x_TNG1]) +
192 STV6110x_GETFIELD(TNG1_DIV4SEL, stv6110x->regs[STV6110x_TNG1])));
199 static int stv6110x_set_bandwidth(struct dvb_frontend *fe, u32 bandwidth)
201 struct stv6110x_state *stv6110x = fe->tuner_priv;
205 halfbw = bandwidth >> 1;
207 if (halfbw > 36000000)
208 STV6110x_SETFIELD(stv6110x->regs[STV6110x_CTRL3], CTRL3_CF, 31); /* LPF */
209 else if (halfbw < 5000000)
210 STV6110x_SETFIELD(stv6110x->regs[STV6110x_CTRL3], CTRL3_CF, 0); /* LPF */
212 STV6110x_SETFIELD(stv6110x->regs[STV6110x_CTRL3], CTRL3_CF, ((halfbw / 1000000) - 5)); /* LPF */
215 STV6110x_SETFIELD(stv6110x->regs[STV6110x_CTRL3], CTRL3_RCCLK_OFF, 0x0); /* cal. clk activated */
216 STV6110x_SETFIELD(stv6110x->regs[STV6110x_STAT1], STAT1_CALRC_STRT, 0x1); /* LPF auto cal */
218 stv6110x_write_reg(stv6110x, STV6110x_CTRL3, stv6110x->regs[STV6110x_CTRL3]);
219 stv6110x_write_reg(stv6110x, STV6110x_STAT1, stv6110x->regs[STV6110x_STAT1]);
221 for (i = 0; i < TRIALS; i++) {
222 stv6110x_read_reg(stv6110x, STV6110x_STAT1, &stv6110x->regs[STV6110x_STAT1]);
223 if (!STV6110x_GETFIELD(STAT1_CALRC_STRT, stv6110x->regs[STV6110x_STAT1]))
227 STV6110x_SETFIELD(stv6110x->regs[STV6110x_CTRL3], CTRL3_RCCLK_OFF, 0x1); /* cal. done */
228 stv6110x_write_reg(stv6110x, STV6110x_CTRL3, stv6110x->regs[STV6110x_CTRL3]);
233 static int stv6110x_get_bandwidth(struct dvb_frontend *fe, u32 *bandwidth)
235 struct stv6110x_state *stv6110x = fe->tuner_priv;
237 stv6110x_read_reg(stv6110x, STV6110x_CTRL3, &stv6110x->regs[STV6110x_CTRL3]);
238 *bandwidth = (STV6110x_GETFIELD(CTRL3_CF, stv6110x->regs[STV6110x_CTRL3]) + 5) * 2000000;
243 static int stv6110x_set_refclock(struct dvb_frontend *fe, u32 refclock)
245 struct stv6110x_state *stv6110x = fe->tuner_priv;
251 STV6110x_SETFIELD(stv6110x->regs[STV6110x_CTRL2], CTRL2_CO_DIV, 0);
254 STV6110x_SETFIELD(stv6110x->regs[STV6110x_CTRL2], CTRL2_CO_DIV, 1);
257 STV6110x_SETFIELD(stv6110x->regs[STV6110x_CTRL2], CTRL2_CO_DIV, 2);
261 STV6110x_SETFIELD(stv6110x->regs[STV6110x_CTRL2], CTRL2_CO_DIV, 3);
264 stv6110x_write_reg(stv6110x, STV6110x_CTRL2, stv6110x->regs[STV6110x_CTRL2]);
269 static int stv6110x_get_bbgain(struct dvb_frontend *fe, u32 *gain)
271 struct stv6110x_state *stv6110x = fe->tuner_priv;
273 stv6110x_read_reg(stv6110x, STV6110x_CTRL2, &stv6110x->regs[STV6110x_CTRL2]);
274 *gain = 2 * STV6110x_GETFIELD(CTRL2_BBGAIN, stv6110x->regs[STV6110x_CTRL2]);
279 static int stv6110x_set_bbgain(struct dvb_frontend *fe, u32 gain)
281 struct stv6110x_state *stv6110x = fe->tuner_priv;
283 STV6110x_SETFIELD(stv6110x->regs[STV6110x_CTRL2], CTRL2_BBGAIN, gain / 2);
284 stv6110x_write_reg(stv6110x, STV6110x_CTRL2, stv6110x->regs[STV6110x_CTRL2]);
289 static int stv6110x_set_mode(struct dvb_frontend *fe, enum tuner_mode mode)
291 struct stv6110x_state *stv6110x = fe->tuner_priv;
296 STV6110x_SETFIELD(stv6110x->regs[STV6110x_CTRL1], CTRL1_SYN, 0);
297 STV6110x_SETFIELD(stv6110x->regs[STV6110x_CTRL1], CTRL1_RX, 0);
298 STV6110x_SETFIELD(stv6110x->regs[STV6110x_CTRL1], CTRL1_LPT, 0);
302 STV6110x_SETFIELD(stv6110x->regs[STV6110x_CTRL1], CTRL1_SYN, 1);
303 STV6110x_SETFIELD(stv6110x->regs[STV6110x_CTRL1], CTRL1_RX, 1);
304 STV6110x_SETFIELD(stv6110x->regs[STV6110x_CTRL1], CTRL1_LPT, 1);
308 ret = stv6110x_write_reg(stv6110x, STV6110x_CTRL1, stv6110x->regs[STV6110x_CTRL1]);
310 dprintk(FE_ERROR, 1, "I/O Error");
317 static int stv6110x_sleep(struct dvb_frontend *fe)
320 return stv6110x_set_mode(fe, TUNER_SLEEP);
325 static int stv6110x_get_status(struct dvb_frontend *fe, u32 *status)
327 struct stv6110x_state *stv6110x = fe->tuner_priv;
329 stv6110x_read_reg(stv6110x, STV6110x_STAT1, &stv6110x->regs[STV6110x_STAT1]);
331 if (STV6110x_GETFIELD(STAT1_LOCK, stv6110x->regs[STV6110x_STAT1]))
332 *status = TUNER_PHASELOCKED;
340 static void stv6110x_release(struct dvb_frontend *fe)
342 struct stv6110x_state *stv6110x = fe->tuner_priv;
344 fe->tuner_priv = NULL;
348 static const struct dvb_tuner_ops stv6110x_ops = {
350 .name = "STV6110(A) Silicon Tuner",
351 .frequency_min_hz = 950 * MHz,
352 .frequency_max_hz = 2150 * MHz,
354 .release = stv6110x_release
357 static const struct stv6110x_devctl stv6110x_ctl = {
358 .tuner_init = stv6110x_init,
359 .tuner_sleep = stv6110x_sleep,
360 .tuner_set_mode = stv6110x_set_mode,
361 .tuner_set_frequency = stv6110x_set_frequency,
362 .tuner_get_frequency = stv6110x_get_frequency,
363 .tuner_set_bandwidth = stv6110x_set_bandwidth,
364 .tuner_get_bandwidth = stv6110x_get_bandwidth,
365 .tuner_set_bbgain = stv6110x_set_bbgain,
366 .tuner_get_bbgain = stv6110x_get_bbgain,
367 .tuner_set_refclk = stv6110x_set_refclock,
368 .tuner_get_status = stv6110x_get_status,
371 const struct stv6110x_devctl *stv6110x_attach(struct dvb_frontend *fe,
372 const struct stv6110x_config *config,
373 struct i2c_adapter *i2c)
375 struct stv6110x_state *stv6110x;
376 u8 default_regs[] = {0x07, 0x11, 0xdc, 0x85, 0x17, 0x01, 0xe6, 0x1e};
378 stv6110x = kzalloc(sizeof (struct stv6110x_state), GFP_KERNEL);
383 stv6110x->config = config;
384 stv6110x->devctl = &stv6110x_ctl;
385 memcpy(stv6110x->regs, default_regs, 8);
388 switch (stv6110x->config->clk_div) {
391 STV6110x_SETFIELD(stv6110x->regs[STV6110x_CTRL2], CTRL2_CO_DIV, 0);
394 STV6110x_SETFIELD(stv6110x->regs[STV6110x_CTRL2], CTRL2_CO_DIV, 1);
397 STV6110x_SETFIELD(stv6110x->regs[STV6110x_CTRL2], CTRL2_CO_DIV, 2);
401 STV6110x_SETFIELD(stv6110x->regs[STV6110x_CTRL2], CTRL2_CO_DIV, 3);
405 fe->tuner_priv = stv6110x;
406 fe->ops.tuner_ops = stv6110x_ops;
408 printk(KERN_INFO "%s: Attaching STV6110x\n", __func__);
409 return stv6110x->devctl;
411 EXPORT_SYMBOL(stv6110x_attach);
413 MODULE_AUTHOR("Manu Abraham");
414 MODULE_DESCRIPTION("STV6110x Silicon tuner");
415 MODULE_LICENSE("GPL");