1 // SPDX-License-Identifier: GPL-2.0-or-later
3 STV6110(A) Silicon tuner driver
7 Copyright (C) ST Microelectronics
11 #include <linux/init.h>
12 #include <linux/kernel.h>
13 #include <linux/module.h>
14 #include <linux/slab.h>
15 #include <linux/string.h>
17 #include <media/dvb_frontend.h>
19 #include "stv6110x_reg.h"
21 #include "stv6110x_priv.h"
23 /* Max transfer size done by I2C transfer functions */
24 #define MAX_XFER_SIZE 64
26 static unsigned int verbose;
27 module_param(verbose, int, 0644);
28 MODULE_PARM_DESC(verbose, "Set Verbosity level");
30 static int stv6110x_read_reg(struct stv6110x_state *stv6110x, u8 reg, u8 *data)
33 const struct stv6110x_config *config = stv6110x->config;
36 struct i2c_msg msg[] = {
37 { .addr = config->addr, .flags = 0, .buf = b0, .len = 1 },
38 { .addr = config->addr, .flags = I2C_M_RD, .buf = b1, .len = 1 }
41 ret = i2c_transfer(stv6110x->i2c, msg, 2);
43 dprintk(FE_ERROR, 1, "I/O Error");
51 static int stv6110x_write_regs(struct stv6110x_state *stv6110x, int start, u8 data[], int len)
54 const struct stv6110x_config *config = stv6110x->config;
55 u8 buf[MAX_XFER_SIZE];
57 struct i2c_msg msg = {
64 if (1 + len > sizeof(buf)) {
66 "%s: i2c wr: len=%d is too big!\n",
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 u8 tmp = data; /* see gcc.gnu.org/bugzilla/show_bug.cgi?id=81715 */
90 return stv6110x_write_regs(stv6110x, reg, &tmp, 1);
93 static int stv6110x_init(struct dvb_frontend *fe)
95 struct stv6110x_state *stv6110x = fe->tuner_priv;
98 ret = stv6110x_write_regs(stv6110x, 0, stv6110x->regs,
99 ARRAY_SIZE(stv6110x->regs));
101 dprintk(FE_ERROR, 1, "Initialization failed");
108 static int stv6110x_set_frequency(struct dvb_frontend *fe, u32 frequency)
110 struct stv6110x_state *stv6110x = fe->tuner_priv;
112 s32 pVal, pCalc, rDivOpt = 0, pCalcOpt = 1000;
115 STV6110x_SETFIELD(stv6110x->regs[STV6110x_CTRL1], CTRL1_K, (REFCLOCK_MHz - 16));
117 if (frequency <= 1023000) {
118 STV6110x_SETFIELD(stv6110x->regs[STV6110x_TNG1], TNG1_DIV4SEL, 1);
119 STV6110x_SETFIELD(stv6110x->regs[STV6110x_TNG1], TNG1_PRESC32_ON, 0);
121 } else if (frequency <= 1300000) {
122 STV6110x_SETFIELD(stv6110x->regs[STV6110x_TNG1], TNG1_DIV4SEL, 1);
123 STV6110x_SETFIELD(stv6110x->regs[STV6110x_TNG1], TNG1_PRESC32_ON, 1);
125 } else if (frequency <= 2046000) {
126 STV6110x_SETFIELD(stv6110x->regs[STV6110x_TNG1], TNG1_DIV4SEL, 0);
127 STV6110x_SETFIELD(stv6110x->regs[STV6110x_TNG1], TNG1_PRESC32_ON, 0);
130 STV6110x_SETFIELD(stv6110x->regs[STV6110x_TNG1], TNG1_DIV4SEL, 0);
131 STV6110x_SETFIELD(stv6110x->regs[STV6110x_TNG1], TNG1_PRESC32_ON, 1);
135 for (rDiv = 0; rDiv <= 3; rDiv++) {
136 pCalc = (REFCLOCK_kHz / 100) / R_DIV(rDiv);
138 if ((abs((s32)(pCalc - pVal))) < (abs((s32)(pCalcOpt - pVal))))
141 pCalcOpt = (REFCLOCK_kHz / 100) / R_DIV(rDivOpt);
144 divider = (frequency * R_DIV(rDivOpt) * pVal) / REFCLOCK_kHz;
145 divider = (divider + 5) / 10;
147 STV6110x_SETFIELD(stv6110x->regs[STV6110x_TNG1], TNG1_R_DIV, rDivOpt);
148 STV6110x_SETFIELD(stv6110x->regs[STV6110x_TNG1], TNG1_N_DIV_11_8, MSB(divider));
149 STV6110x_SETFIELD(stv6110x->regs[STV6110x_TNG0], TNG0_N_DIV_7_0, LSB(divider));
151 /* VCO Auto calibration */
152 STV6110x_SETFIELD(stv6110x->regs[STV6110x_STAT1], STAT1_CALVCO_STRT, 1);
154 stv6110x_write_reg(stv6110x, STV6110x_CTRL1, stv6110x->regs[STV6110x_CTRL1]);
155 stv6110x_write_reg(stv6110x, STV6110x_TNG1, stv6110x->regs[STV6110x_TNG1]);
156 stv6110x_write_reg(stv6110x, STV6110x_TNG0, stv6110x->regs[STV6110x_TNG0]);
157 stv6110x_write_reg(stv6110x, STV6110x_STAT1, stv6110x->regs[STV6110x_STAT1]);
159 for (i = 0; i < TRIALS; i++) {
160 stv6110x_read_reg(stv6110x, STV6110x_STAT1, &stv6110x->regs[STV6110x_STAT1]);
161 if (!STV6110x_GETFIELD(STAT1_CALVCO_STRT, stv6110x->regs[STV6110x_STAT1]))
169 static int stv6110x_get_frequency(struct dvb_frontend *fe, u32 *frequency)
171 struct stv6110x_state *stv6110x = fe->tuner_priv;
173 stv6110x_read_reg(stv6110x, STV6110x_TNG1, &stv6110x->regs[STV6110x_TNG1]);
174 stv6110x_read_reg(stv6110x, STV6110x_TNG0, &stv6110x->regs[STV6110x_TNG0]);
176 *frequency = (MAKEWORD16(STV6110x_GETFIELD(TNG1_N_DIV_11_8, stv6110x->regs[STV6110x_TNG1]),
177 STV6110x_GETFIELD(TNG0_N_DIV_7_0, stv6110x->regs[STV6110x_TNG0]))) * REFCLOCK_kHz;
179 *frequency /= (1 << (STV6110x_GETFIELD(TNG1_R_DIV, stv6110x->regs[STV6110x_TNG1]) +
180 STV6110x_GETFIELD(TNG1_DIV4SEL, stv6110x->regs[STV6110x_TNG1])));
187 static int stv6110x_set_bandwidth(struct dvb_frontend *fe, u32 bandwidth)
189 struct stv6110x_state *stv6110x = fe->tuner_priv;
193 halfbw = bandwidth >> 1;
195 if (halfbw > 36000000)
196 STV6110x_SETFIELD(stv6110x->regs[STV6110x_CTRL3], CTRL3_CF, 31); /* LPF */
197 else if (halfbw < 5000000)
198 STV6110x_SETFIELD(stv6110x->regs[STV6110x_CTRL3], CTRL3_CF, 0); /* LPF */
200 STV6110x_SETFIELD(stv6110x->regs[STV6110x_CTRL3], CTRL3_CF, ((halfbw / 1000000) - 5)); /* LPF */
203 STV6110x_SETFIELD(stv6110x->regs[STV6110x_CTRL3], CTRL3_RCCLK_OFF, 0x0); /* cal. clk activated */
204 STV6110x_SETFIELD(stv6110x->regs[STV6110x_STAT1], STAT1_CALRC_STRT, 0x1); /* LPF auto cal */
206 stv6110x_write_reg(stv6110x, STV6110x_CTRL3, stv6110x->regs[STV6110x_CTRL3]);
207 stv6110x_write_reg(stv6110x, STV6110x_STAT1, stv6110x->regs[STV6110x_STAT1]);
209 for (i = 0; i < TRIALS; i++) {
210 stv6110x_read_reg(stv6110x, STV6110x_STAT1, &stv6110x->regs[STV6110x_STAT1]);
211 if (!STV6110x_GETFIELD(STAT1_CALRC_STRT, stv6110x->regs[STV6110x_STAT1]))
215 STV6110x_SETFIELD(stv6110x->regs[STV6110x_CTRL3], CTRL3_RCCLK_OFF, 0x1); /* cal. done */
216 stv6110x_write_reg(stv6110x, STV6110x_CTRL3, stv6110x->regs[STV6110x_CTRL3]);
221 static int stv6110x_get_bandwidth(struct dvb_frontend *fe, u32 *bandwidth)
223 struct stv6110x_state *stv6110x = fe->tuner_priv;
225 stv6110x_read_reg(stv6110x, STV6110x_CTRL3, &stv6110x->regs[STV6110x_CTRL3]);
226 *bandwidth = (STV6110x_GETFIELD(CTRL3_CF, stv6110x->regs[STV6110x_CTRL3]) + 5) * 2000000;
231 static int stv6110x_set_refclock(struct dvb_frontend *fe, u32 refclock)
233 struct stv6110x_state *stv6110x = fe->tuner_priv;
239 STV6110x_SETFIELD(stv6110x->regs[STV6110x_CTRL2], CTRL2_CO_DIV, 0);
242 STV6110x_SETFIELD(stv6110x->regs[STV6110x_CTRL2], CTRL2_CO_DIV, 1);
245 STV6110x_SETFIELD(stv6110x->regs[STV6110x_CTRL2], CTRL2_CO_DIV, 2);
249 STV6110x_SETFIELD(stv6110x->regs[STV6110x_CTRL2], CTRL2_CO_DIV, 3);
252 stv6110x_write_reg(stv6110x, STV6110x_CTRL2, stv6110x->regs[STV6110x_CTRL2]);
257 static int stv6110x_get_bbgain(struct dvb_frontend *fe, u32 *gain)
259 struct stv6110x_state *stv6110x = fe->tuner_priv;
261 stv6110x_read_reg(stv6110x, STV6110x_CTRL2, &stv6110x->regs[STV6110x_CTRL2]);
262 *gain = 2 * STV6110x_GETFIELD(CTRL2_BBGAIN, stv6110x->regs[STV6110x_CTRL2]);
267 static int stv6110x_set_bbgain(struct dvb_frontend *fe, u32 gain)
269 struct stv6110x_state *stv6110x = fe->tuner_priv;
271 STV6110x_SETFIELD(stv6110x->regs[STV6110x_CTRL2], CTRL2_BBGAIN, gain / 2);
272 stv6110x_write_reg(stv6110x, STV6110x_CTRL2, stv6110x->regs[STV6110x_CTRL2]);
277 static int stv6110x_set_mode(struct dvb_frontend *fe, enum tuner_mode mode)
279 struct stv6110x_state *stv6110x = fe->tuner_priv;
284 STV6110x_SETFIELD(stv6110x->regs[STV6110x_CTRL1], CTRL1_SYN, 0);
285 STV6110x_SETFIELD(stv6110x->regs[STV6110x_CTRL1], CTRL1_RX, 0);
286 STV6110x_SETFIELD(stv6110x->regs[STV6110x_CTRL1], CTRL1_LPT, 0);
290 STV6110x_SETFIELD(stv6110x->regs[STV6110x_CTRL1], CTRL1_SYN, 1);
291 STV6110x_SETFIELD(stv6110x->regs[STV6110x_CTRL1], CTRL1_RX, 1);
292 STV6110x_SETFIELD(stv6110x->regs[STV6110x_CTRL1], CTRL1_LPT, 1);
296 ret = stv6110x_write_reg(stv6110x, STV6110x_CTRL1, stv6110x->regs[STV6110x_CTRL1]);
298 dprintk(FE_ERROR, 1, "I/O Error");
305 static int stv6110x_sleep(struct dvb_frontend *fe)
308 return stv6110x_set_mode(fe, TUNER_SLEEP);
313 static int stv6110x_get_status(struct dvb_frontend *fe, u32 *status)
315 struct stv6110x_state *stv6110x = fe->tuner_priv;
317 stv6110x_read_reg(stv6110x, STV6110x_STAT1, &stv6110x->regs[STV6110x_STAT1]);
319 if (STV6110x_GETFIELD(STAT1_LOCK, stv6110x->regs[STV6110x_STAT1]))
320 *status = TUNER_PHASELOCKED;
328 static void stv6110x_release(struct dvb_frontend *fe)
330 struct stv6110x_state *stv6110x = fe->tuner_priv;
332 fe->tuner_priv = NULL;
336 static void st6110x_init_regs(struct stv6110x_state *stv6110x)
338 u8 default_regs[] = {0x07, 0x11, 0xdc, 0x85, 0x17, 0x01, 0xe6, 0x1e};
340 memcpy(stv6110x->regs, default_regs, 8);
343 static void stv6110x_setup_divider(struct stv6110x_state *stv6110x)
345 switch (stv6110x->config->clk_div) {
348 STV6110x_SETFIELD(stv6110x->regs[STV6110x_CTRL2],
353 STV6110x_SETFIELD(stv6110x->regs[STV6110x_CTRL2],
358 STV6110x_SETFIELD(stv6110x->regs[STV6110x_CTRL2],
364 STV6110x_SETFIELD(stv6110x->regs[STV6110x_CTRL2],
371 static const struct dvb_tuner_ops stv6110x_ops = {
373 .name = "STV6110(A) Silicon Tuner",
374 .frequency_min_hz = 950 * MHz,
375 .frequency_max_hz = 2150 * MHz,
377 .release = stv6110x_release
380 static struct stv6110x_devctl stv6110x_ctl = {
381 .tuner_init = stv6110x_init,
382 .tuner_sleep = stv6110x_sleep,
383 .tuner_set_mode = stv6110x_set_mode,
384 .tuner_set_frequency = stv6110x_set_frequency,
385 .tuner_get_frequency = stv6110x_get_frequency,
386 .tuner_set_bandwidth = stv6110x_set_bandwidth,
387 .tuner_get_bandwidth = stv6110x_get_bandwidth,
388 .tuner_set_bbgain = stv6110x_set_bbgain,
389 .tuner_get_bbgain = stv6110x_get_bbgain,
390 .tuner_set_refclk = stv6110x_set_refclock,
391 .tuner_get_status = stv6110x_get_status,
394 static void stv6110x_set_frontend_opts(struct stv6110x_state *stv6110x)
396 stv6110x->frontend->tuner_priv = stv6110x;
397 stv6110x->frontend->ops.tuner_ops = stv6110x_ops;
400 static struct stv6110x_devctl *stv6110x_get_devctl(struct i2c_client *client)
402 struct stv6110x_state *stv6110x = i2c_get_clientdata(client);
404 dev_dbg(&client->dev, "\n");
406 return stv6110x->devctl;
409 static int stv6110x_probe(struct i2c_client *client)
411 struct stv6110x_config *config = client->dev.platform_data;
413 struct stv6110x_state *stv6110x;
415 stv6110x = kzalloc(sizeof(*stv6110x), GFP_KERNEL);
419 stv6110x->frontend = config->frontend;
420 stv6110x->i2c = client->adapter;
421 stv6110x->config = config;
422 stv6110x->devctl = &stv6110x_ctl;
424 st6110x_init_regs(stv6110x);
425 stv6110x_setup_divider(stv6110x);
426 stv6110x_set_frontend_opts(stv6110x);
428 dev_info(&stv6110x->i2c->dev, "Probed STV6110x\n");
430 i2c_set_clientdata(client, stv6110x);
432 /* setup callbacks */
433 config->get_devctl = stv6110x_get_devctl;
438 static void stv6110x_remove(struct i2c_client *client)
440 struct stv6110x_state *stv6110x = i2c_get_clientdata(client);
442 stv6110x_release(stv6110x->frontend);
445 const struct stv6110x_devctl *stv6110x_attach(struct dvb_frontend *fe,
446 const struct stv6110x_config *config,
447 struct i2c_adapter *i2c)
449 struct stv6110x_state *stv6110x;
451 stv6110x = kzalloc(sizeof(*stv6110x), GFP_KERNEL);
455 stv6110x->frontend = fe;
457 stv6110x->config = config;
458 stv6110x->devctl = &stv6110x_ctl;
460 st6110x_init_regs(stv6110x);
461 stv6110x_setup_divider(stv6110x);
462 stv6110x_set_frontend_opts(stv6110x);
464 fe->tuner_priv = stv6110x;
465 fe->ops.tuner_ops = stv6110x_ops;
467 dev_info(&stv6110x->i2c->dev, "Attaching STV6110x\n");
468 return stv6110x->devctl;
470 EXPORT_SYMBOL_GPL(stv6110x_attach);
472 static const struct i2c_device_id stv6110x_id_table[] = {
476 MODULE_DEVICE_TABLE(i2c, stv6110x_id_table);
478 static struct i2c_driver stv6110x_driver = {
481 .suppress_bind_attrs = true,
483 .probe = stv6110x_probe,
484 .remove = stv6110x_remove,
485 .id_table = stv6110x_id_table,
488 module_i2c_driver(stv6110x_driver);
490 MODULE_AUTHOR("Manu Abraham");
491 MODULE_DESCRIPTION("STV6110x Silicon tuner");
492 MODULE_LICENSE("GPL");