WIP FPC-III support
[linux/fpc-iii.git] / drivers / media / dvb-frontends / stv6110x.c
blob5012d0231652237ac1de26938e1a32794e142b8c
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 STV6110(A) Silicon tuner driver
5 Copyright (C) Manu Abraham <abraham.manu@gmail.com>
7 Copyright (C) ST Microelectronics
9 */
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"
20 #include "stv6110x.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)
32 int ret;
33 const struct stv6110x_config *config = stv6110x->config;
34 u8 b0[] = { reg };
35 u8 b1[] = { 0 };
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);
42 if (ret != 2) {
43 dprintk(FE_ERROR, 1, "I/O Error");
44 return -EREMOTEIO;
46 *data = b1[0];
48 return 0;
51 static int stv6110x_write_regs(struct stv6110x_state *stv6110x, int start, u8 data[], int len)
53 int ret;
54 const struct stv6110x_config *config = stv6110x->config;
55 u8 buf[MAX_XFER_SIZE];
57 struct i2c_msg msg = {
58 .addr = config->addr,
59 .flags = 0,
60 .buf = buf,
61 .len = len + 1
64 if (1 + len > sizeof(buf)) {
65 printk(KERN_WARNING
66 "%s: i2c wr: len=%d is too big!\n",
67 KBUILD_MODNAME, len);
68 return -EINVAL;
71 if (start + len > 8)
72 return -EINVAL;
74 buf[0] = start;
75 memcpy(&buf[1], data, len);
77 ret = i2c_transfer(stv6110x->i2c, &msg, 1);
78 if (ret != 1) {
79 dprintk(FE_ERROR, 1, "I/O Error");
80 return -EREMOTEIO;
83 return 0;
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;
96 int ret;
98 ret = stv6110x_write_regs(stv6110x, 0, stv6110x->regs,
99 ARRAY_SIZE(stv6110x->regs));
100 if (ret < 0) {
101 dprintk(FE_ERROR, 1, "Initialization failed");
102 return -1;
105 return 0;
108 static int stv6110x_set_frequency(struct dvb_frontend *fe, u32 frequency)
110 struct stv6110x_state *stv6110x = fe->tuner_priv;
111 u32 rDiv, divider;
112 s32 pVal, pCalc, rDivOpt = 0, pCalcOpt = 1000;
113 u8 i;
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);
120 pVal = 40;
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);
124 pVal = 40;
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);
128 pVal = 20;
129 } else {
130 STV6110x_SETFIELD(stv6110x->regs[STV6110x_TNG1], TNG1_DIV4SEL, 0);
131 STV6110x_SETFIELD(stv6110x->regs[STV6110x_TNG1], TNG1_PRESC32_ON, 1);
132 pVal = 20;
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))))
139 rDivOpt = rDiv;
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]))
162 break;
163 msleep(1);
166 return 0;
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])));
182 *frequency >>= 2;
184 return 0;
187 static int stv6110x_set_bandwidth(struct dvb_frontend *fe, u32 bandwidth)
189 struct stv6110x_state *stv6110x = fe->tuner_priv;
190 u32 halfbw;
191 u8 i;
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 */
199 else
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]))
212 break;
213 msleep(1);
215 STV6110x_SETFIELD(stv6110x->regs[STV6110x_CTRL3], CTRL3_RCCLK_OFF, 0x1); /* cal. done */
216 stv6110x_write_reg(stv6110x, STV6110x_CTRL3, stv6110x->regs[STV6110x_CTRL3]);
218 return 0;
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;
228 return 0;
231 static int stv6110x_set_refclock(struct dvb_frontend *fe, u32 refclock)
233 struct stv6110x_state *stv6110x = fe->tuner_priv;
235 /* setup divider */
236 switch (refclock) {
237 default:
238 case 1:
239 STV6110x_SETFIELD(stv6110x->regs[STV6110x_CTRL2], CTRL2_CO_DIV, 0);
240 break;
241 case 2:
242 STV6110x_SETFIELD(stv6110x->regs[STV6110x_CTRL2], CTRL2_CO_DIV, 1);
243 break;
244 case 4:
245 STV6110x_SETFIELD(stv6110x->regs[STV6110x_CTRL2], CTRL2_CO_DIV, 2);
246 break;
247 case 8:
248 case 0:
249 STV6110x_SETFIELD(stv6110x->regs[STV6110x_CTRL2], CTRL2_CO_DIV, 3);
250 break;
252 stv6110x_write_reg(stv6110x, STV6110x_CTRL2, stv6110x->regs[STV6110x_CTRL2]);
254 return 0;
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]);
264 return 0;
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]);
274 return 0;
277 static int stv6110x_set_mode(struct dvb_frontend *fe, enum tuner_mode mode)
279 struct stv6110x_state *stv6110x = fe->tuner_priv;
280 int ret;
282 switch (mode) {
283 case TUNER_SLEEP:
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);
287 break;
289 case TUNER_WAKE:
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);
293 break;
296 ret = stv6110x_write_reg(stv6110x, STV6110x_CTRL1, stv6110x->regs[STV6110x_CTRL1]);
297 if (ret < 0) {
298 dprintk(FE_ERROR, 1, "I/O Error");
299 return -EIO;
302 return 0;
305 static int stv6110x_sleep(struct dvb_frontend *fe)
307 if (fe->tuner_priv)
308 return stv6110x_set_mode(fe, TUNER_SLEEP);
310 return 0;
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;
321 else
322 *status = 0;
324 return 0;
328 static void stv6110x_release(struct dvb_frontend *fe)
330 struct stv6110x_state *stv6110x = fe->tuner_priv;
332 fe->tuner_priv = NULL;
333 kfree(stv6110x);
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) {
346 default:
347 case 1:
348 STV6110x_SETFIELD(stv6110x->regs[STV6110x_CTRL2],
349 CTRL2_CO_DIV,
351 break;
352 case 2:
353 STV6110x_SETFIELD(stv6110x->regs[STV6110x_CTRL2],
354 CTRL2_CO_DIV,
356 break;
357 case 4:
358 STV6110x_SETFIELD(stv6110x->regs[STV6110x_CTRL2],
359 CTRL2_CO_DIV,
361 break;
362 case 8:
363 case 0:
364 STV6110x_SETFIELD(stv6110x->regs[STV6110x_CTRL2],
365 CTRL2_CO_DIV,
367 break;
371 static const struct dvb_tuner_ops stv6110x_ops = {
372 .info = {
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,
410 const struct i2c_device_id *id)
412 struct stv6110x_config *config = client->dev.platform_data;
414 struct stv6110x_state *stv6110x;
416 stv6110x = kzalloc(sizeof(*stv6110x), GFP_KERNEL);
417 if (!stv6110x)
418 return -ENOMEM;
420 stv6110x->frontend = config->frontend;
421 stv6110x->i2c = client->adapter;
422 stv6110x->config = config;
423 stv6110x->devctl = &stv6110x_ctl;
425 st6110x_init_regs(stv6110x);
426 stv6110x_setup_divider(stv6110x);
427 stv6110x_set_frontend_opts(stv6110x);
429 dev_info(&stv6110x->i2c->dev, "Probed STV6110x\n");
431 i2c_set_clientdata(client, stv6110x);
433 /* setup callbacks */
434 config->get_devctl = stv6110x_get_devctl;
436 return 0;
439 static int stv6110x_remove(struct i2c_client *client)
441 struct stv6110x_state *stv6110x = i2c_get_clientdata(client);
443 stv6110x_release(stv6110x->frontend);
444 return 0;
447 const struct stv6110x_devctl *stv6110x_attach(struct dvb_frontend *fe,
448 const struct stv6110x_config *config,
449 struct i2c_adapter *i2c)
451 struct stv6110x_state *stv6110x;
453 stv6110x = kzalloc(sizeof(*stv6110x), GFP_KERNEL);
454 if (!stv6110x)
455 return NULL;
457 stv6110x->frontend = fe;
458 stv6110x->i2c = i2c;
459 stv6110x->config = config;
460 stv6110x->devctl = &stv6110x_ctl;
462 st6110x_init_regs(stv6110x);
463 stv6110x_setup_divider(stv6110x);
464 stv6110x_set_frontend_opts(stv6110x);
466 fe->tuner_priv = stv6110x;
467 fe->ops.tuner_ops = stv6110x_ops;
469 dev_info(&stv6110x->i2c->dev, "Attaching STV6110x\n");
470 return stv6110x->devctl;
472 EXPORT_SYMBOL(stv6110x_attach);
474 static const struct i2c_device_id stv6110x_id_table[] = {
475 {"stv6110x", 0},
478 MODULE_DEVICE_TABLE(i2c, stv6110x_id_table);
480 static struct i2c_driver stv6110x_driver = {
481 .driver = {
482 .name = "stv6110x",
483 .suppress_bind_attrs = true,
485 .probe = stv6110x_probe,
486 .remove = stv6110x_remove,
487 .id_table = stv6110x_id_table,
490 module_i2c_driver(stv6110x_driver);
492 MODULE_AUTHOR("Manu Abraham");
493 MODULE_DESCRIPTION("STV6110x Silicon tuner");
494 MODULE_LICENSE("GPL");