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/string.h>
28 #include "dvb_frontend.h"
30 #include "stv6110x_reg.h"
32 #include "stv6110x_priv.h"
34 static unsigned int verbose;
35 module_param(verbose, int, 0644);
36 MODULE_PARM_DESC(verbose, "Set Verbosity level");
38 static int stv6110x_read_reg(struct stv6110x_state *stv6110x, u8 reg, u8 *data)
41 const struct stv6110x_config *config = stv6110x->config;
44 struct i2c_msg msg[] = {
45 { .addr = config->addr, .flags = 0, .buf = b0, .len = 1 },
46 { .addr = config->addr, .flags = I2C_M_RD, .buf = b1, .len = 1 }
49 ret = i2c_transfer(stv6110x->i2c, msg, 2);
51 dprintk(FE_ERROR, 1, "I/O Error");
59 static int stv6110x_write_regs(struct stv6110x_state *stv6110x, int start, u8 data[], int len)
62 const struct stv6110x_config *config = stv6110x->config;
64 struct i2c_msg msg = {
75 memcpy(&buf[1], data, len);
77 ret = i2c_transfer(stv6110x->i2c, &msg, 1);
79 dprintk(FE_ERROR, 1, "I/O Error");
86 static int stv6110x_write_reg(struct stv6110x_state *stv6110x, u8 reg, u8 data)
88 return stv6110x_write_regs(stv6110x, reg, &data, 1);
91 static int stv6110x_init(struct dvb_frontend *fe)
93 struct stv6110x_state *stv6110x = fe->tuner_priv;
96 ret = stv6110x_write_regs(stv6110x, 0, stv6110x->regs,
97 ARRAY_SIZE(stv6110x->regs));
99 dprintk(FE_ERROR, 1, "Initialization failed");
106 static int stv6110x_set_frequency(struct dvb_frontend *fe, u32 frequency)
108 struct stv6110x_state *stv6110x = fe->tuner_priv;
110 s32 pVal, pCalc, rDivOpt = 0, pCalcOpt = 1000;
113 STV6110x_SETFIELD(stv6110x->regs[STV6110x_CTRL1], CTRL1_K, (REFCLOCK_MHz - 16));
115 if (frequency <= 1023000) {
116 STV6110x_SETFIELD(stv6110x->regs[STV6110x_TNG1], TNG1_DIV4SEL, 1);
117 STV6110x_SETFIELD(stv6110x->regs[STV6110x_TNG1], TNG1_PRESC32_ON, 0);
119 } else if (frequency <= 1300000) {
120 STV6110x_SETFIELD(stv6110x->regs[STV6110x_TNG1], TNG1_DIV4SEL, 1);
121 STV6110x_SETFIELD(stv6110x->regs[STV6110x_TNG1], TNG1_PRESC32_ON, 1);
123 } else if (frequency <= 2046000) {
124 STV6110x_SETFIELD(stv6110x->regs[STV6110x_TNG1], TNG1_DIV4SEL, 0);
125 STV6110x_SETFIELD(stv6110x->regs[STV6110x_TNG1], TNG1_PRESC32_ON, 0);
128 STV6110x_SETFIELD(stv6110x->regs[STV6110x_TNG1], TNG1_DIV4SEL, 0);
129 STV6110x_SETFIELD(stv6110x->regs[STV6110x_TNG1], TNG1_PRESC32_ON, 1);
133 for (rDiv = 0; rDiv <= 3; rDiv++) {
134 pCalc = (REFCLOCK_kHz / 100) / R_DIV(rDiv);
136 if ((abs((s32)(pCalc - pVal))) < (abs((s32)(pCalcOpt - pVal))))
139 pCalcOpt = (REFCLOCK_kHz / 100) / R_DIV(rDivOpt);
142 divider = (frequency * R_DIV(rDivOpt) * pVal) / REFCLOCK_kHz;
143 divider = (divider + 5) / 10;
145 STV6110x_SETFIELD(stv6110x->regs[STV6110x_TNG1], TNG1_R_DIV, rDivOpt);
146 STV6110x_SETFIELD(stv6110x->regs[STV6110x_TNG1], TNG1_N_DIV_11_8, MSB(divider));
147 STV6110x_SETFIELD(stv6110x->regs[STV6110x_TNG0], TNG0_N_DIV_7_0, LSB(divider));
149 /* VCO Auto calibration */
150 STV6110x_SETFIELD(stv6110x->regs[STV6110x_STAT1], STAT1_CALVCO_STRT, 1);
152 stv6110x_write_reg(stv6110x, STV6110x_CTRL1, stv6110x->regs[STV6110x_CTRL1]);
153 stv6110x_write_reg(stv6110x, STV6110x_TNG1, stv6110x->regs[STV6110x_TNG1]);
154 stv6110x_write_reg(stv6110x, STV6110x_TNG0, stv6110x->regs[STV6110x_TNG0]);
155 stv6110x_write_reg(stv6110x, STV6110x_STAT1, stv6110x->regs[STV6110x_STAT1]);
157 for (i = 0; i < TRIALS; i++) {
158 stv6110x_read_reg(stv6110x, STV6110x_STAT1, &stv6110x->regs[STV6110x_STAT1]);
159 if (!STV6110x_GETFIELD(STAT1_CALVCO_STRT, stv6110x->regs[STV6110x_STAT1]))
167 static int stv6110x_get_frequency(struct dvb_frontend *fe, u32 *frequency)
169 struct stv6110x_state *stv6110x = fe->tuner_priv;
171 stv6110x_read_reg(stv6110x, STV6110x_TNG1, &stv6110x->regs[STV6110x_TNG1]);
172 stv6110x_read_reg(stv6110x, STV6110x_TNG0, &stv6110x->regs[STV6110x_TNG0]);
174 *frequency = (MAKEWORD16(STV6110x_GETFIELD(TNG1_N_DIV_11_8, stv6110x->regs[STV6110x_TNG1]),
175 STV6110x_GETFIELD(TNG0_N_DIV_7_0, stv6110x->regs[STV6110x_TNG0]))) * REFCLOCK_kHz;
177 *frequency /= (1 << (STV6110x_GETFIELD(TNG1_R_DIV, stv6110x->regs[STV6110x_TNG1]) +
178 STV6110x_GETFIELD(TNG1_DIV4SEL, stv6110x->regs[STV6110x_TNG1])));
185 static int stv6110x_set_bandwidth(struct dvb_frontend *fe, u32 bandwidth)
187 struct stv6110x_state *stv6110x = fe->tuner_priv;
191 halfbw = bandwidth >> 1;
193 if (halfbw > 36000000)
194 STV6110x_SETFIELD(stv6110x->regs[STV6110x_CTRL3], CTRL3_CF, 31); /* LPF */
195 else if (halfbw < 5000000)
196 STV6110x_SETFIELD(stv6110x->regs[STV6110x_CTRL3], CTRL3_CF, 0); /* LPF */
198 STV6110x_SETFIELD(stv6110x->regs[STV6110x_CTRL3], CTRL3_CF, ((halfbw / 1000000) - 5)); /* LPF */
201 STV6110x_SETFIELD(stv6110x->regs[STV6110x_CTRL3], CTRL3_RCCLK_OFF, 0x0); /* cal. clk activated */
202 STV6110x_SETFIELD(stv6110x->regs[STV6110x_STAT1], STAT1_CALRC_STRT, 0x1); /* LPF auto cal */
204 stv6110x_write_reg(stv6110x, STV6110x_CTRL3, stv6110x->regs[STV6110x_CTRL3]);
205 stv6110x_write_reg(stv6110x, STV6110x_STAT1, stv6110x->regs[STV6110x_STAT1]);
207 for (i = 0; i < TRIALS; i++) {
208 stv6110x_read_reg(stv6110x, STV6110x_STAT1, &stv6110x->regs[STV6110x_STAT1]);
209 if (!STV6110x_GETFIELD(STAT1_CALRC_STRT, stv6110x->regs[STV6110x_STAT1]))
213 STV6110x_SETFIELD(stv6110x->regs[STV6110x_CTRL3], CTRL3_RCCLK_OFF, 0x1); /* cal. done */
214 stv6110x_write_reg(stv6110x, STV6110x_CTRL3, stv6110x->regs[STV6110x_CTRL3]);
219 static int stv6110x_get_bandwidth(struct dvb_frontend *fe, u32 *bandwidth)
221 struct stv6110x_state *stv6110x = fe->tuner_priv;
223 stv6110x_read_reg(stv6110x, STV6110x_CTRL3, &stv6110x->regs[STV6110x_CTRL3]);
224 *bandwidth = (STV6110x_GETFIELD(CTRL3_CF, stv6110x->regs[STV6110x_CTRL3]) + 5) * 2000000;
229 static int stv6110x_set_refclock(struct dvb_frontend *fe, u32 refclock)
231 struct stv6110x_state *stv6110x = fe->tuner_priv;
237 STV6110x_SETFIELD(stv6110x->regs[STV6110x_CTRL2], CTRL2_CO_DIV, 0);
240 STV6110x_SETFIELD(stv6110x->regs[STV6110x_CTRL2], CTRL2_CO_DIV, 1);
243 STV6110x_SETFIELD(stv6110x->regs[STV6110x_CTRL2], CTRL2_CO_DIV, 2);
247 STV6110x_SETFIELD(stv6110x->regs[STV6110x_CTRL2], CTRL2_CO_DIV, 3);
250 stv6110x_write_reg(stv6110x, STV6110x_CTRL2, stv6110x->regs[STV6110x_CTRL2]);
255 static int stv6110x_get_bbgain(struct dvb_frontend *fe, u32 *gain)
257 struct stv6110x_state *stv6110x = fe->tuner_priv;
259 stv6110x_read_reg(stv6110x, STV6110x_CTRL2, &stv6110x->regs[STV6110x_CTRL2]);
260 *gain = 2 * STV6110x_GETFIELD(CTRL2_BBGAIN, stv6110x->regs[STV6110x_CTRL2]);
265 static int stv6110x_set_bbgain(struct dvb_frontend *fe, u32 gain)
267 struct stv6110x_state *stv6110x = fe->tuner_priv;
269 STV6110x_SETFIELD(stv6110x->regs[STV6110x_CTRL2], CTRL2_BBGAIN, gain / 2);
270 stv6110x_write_reg(stv6110x, STV6110x_CTRL2, stv6110x->regs[STV6110x_CTRL2]);
275 static int stv6110x_set_mode(struct dvb_frontend *fe, enum tuner_mode mode)
277 struct stv6110x_state *stv6110x = fe->tuner_priv;
282 STV6110x_SETFIELD(stv6110x->regs[STV6110x_CTRL1], CTRL1_SYN, 0);
283 STV6110x_SETFIELD(stv6110x->regs[STV6110x_CTRL1], CTRL1_RX, 0);
284 STV6110x_SETFIELD(stv6110x->regs[STV6110x_CTRL1], CTRL1_LPT, 0);
288 STV6110x_SETFIELD(stv6110x->regs[STV6110x_CTRL1], CTRL1_SYN, 1);
289 STV6110x_SETFIELD(stv6110x->regs[STV6110x_CTRL1], CTRL1_RX, 1);
290 STV6110x_SETFIELD(stv6110x->regs[STV6110x_CTRL1], CTRL1_LPT, 1);
294 ret = stv6110x_write_reg(stv6110x, STV6110x_CTRL1, stv6110x->regs[STV6110x_CTRL1]);
296 dprintk(FE_ERROR, 1, "I/O Error");
303 static int stv6110x_sleep(struct dvb_frontend *fe)
305 return stv6110x_set_mode(fe, TUNER_SLEEP);
308 static int stv6110x_get_status(struct dvb_frontend *fe, u32 *status)
310 struct stv6110x_state *stv6110x = fe->tuner_priv;
312 stv6110x_read_reg(stv6110x, STV6110x_STAT1, &stv6110x->regs[STV6110x_STAT1]);
314 if (STV6110x_GETFIELD(STAT1_LOCK, stv6110x->regs[STV6110x_STAT1]))
315 *status = TUNER_PHASELOCKED;
323 static int stv6110x_release(struct dvb_frontend *fe)
325 struct stv6110x_state *stv6110x = fe->tuner_priv;
327 fe->tuner_priv = NULL;
333 static struct dvb_tuner_ops stv6110x_ops = {
335 .name = "STV6110(A) Silicon Tuner",
336 .frequency_min = 950000,
337 .frequency_max = 2150000,
341 .init = stv6110x_init,
342 .sleep = stv6110x_sleep,
343 .release = stv6110x_release
346 static struct stv6110x_devctl stv6110x_ctl = {
347 .tuner_init = stv6110x_init,
348 .tuner_set_mode = stv6110x_set_mode,
349 .tuner_set_frequency = stv6110x_set_frequency,
350 .tuner_get_frequency = stv6110x_get_frequency,
351 .tuner_set_bandwidth = stv6110x_set_bandwidth,
352 .tuner_get_bandwidth = stv6110x_get_bandwidth,
353 .tuner_set_bbgain = stv6110x_set_bbgain,
354 .tuner_get_bbgain = stv6110x_get_bbgain,
355 .tuner_set_refclk = stv6110x_set_refclock,
356 .tuner_get_status = stv6110x_get_status,
359 struct stv6110x_devctl *stv6110x_attach(struct dvb_frontend *fe,
360 const struct stv6110x_config *config,
361 struct i2c_adapter *i2c)
363 struct stv6110x_state *stv6110x;
364 u8 default_regs[] = {0x07, 0x11, 0xdc, 0x85, 0x17, 0x01, 0xe6, 0x1e};
367 stv6110x = kzalloc(sizeof (struct stv6110x_state), GFP_KERNEL);
368 if (stv6110x == NULL)
372 stv6110x->config = config;
373 stv6110x->devctl = &stv6110x_ctl;
374 memcpy(stv6110x->regs, default_regs, 8);
377 switch (stv6110x->config->clk_div) {
380 STV6110x_SETFIELD(stv6110x->regs[STV6110x_CTRL2], CTRL2_CO_DIV, 0);
383 STV6110x_SETFIELD(stv6110x->regs[STV6110x_CTRL2], CTRL2_CO_DIV, 1);
386 STV6110x_SETFIELD(stv6110x->regs[STV6110x_CTRL2], CTRL2_CO_DIV, 2);
390 STV6110x_SETFIELD(stv6110x->regs[STV6110x_CTRL2], CTRL2_CO_DIV, 3);
394 if (fe->ops.i2c_gate_ctrl) {
395 ret = fe->ops.i2c_gate_ctrl(fe, 1);
400 ret = stv6110x_write_regs(stv6110x, 0, stv6110x->regs,
401 ARRAY_SIZE(stv6110x->regs));
403 dprintk(FE_ERROR, 1, "Initialization failed");
407 if (fe->ops.i2c_gate_ctrl) {
408 ret = fe->ops.i2c_gate_ctrl(fe, 0);
413 fe->tuner_priv = stv6110x;
414 fe->ops.tuner_ops = stv6110x_ops;
416 printk("%s: Attaching STV6110x \n", __func__);
417 return stv6110x->devctl;
423 EXPORT_SYMBOL(stv6110x_attach);
425 MODULE_AUTHOR("Manu Abraham");
426 MODULE_DESCRIPTION("STV6110x Silicon tuner");
427 MODULE_LICENSE("GPL");