Merge tag 'io_uring-5.11-2021-01-16' of git://git.kernel.dk/linux-block
[linux/fpc-iii.git] / drivers / media / dvb-frontends / cx24113.c
blob60a9f70275f75d1e7bfd9f10de98d0484b5d2181
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * Driver for Conexant CX24113/CX24128 Tuner (Satellite)
5 * Copyright (C) 2007-8 Patrick Boettcher <pb@linuxtv.org>
7 * Developed for BBTI / Technisat
8 */
10 #include <linux/slab.h>
11 #include <linux/kernel.h>
12 #include <linux/module.h>
13 #include <linux/init.h>
15 #include <media/dvb_frontend.h>
16 #include "cx24113.h"
18 static int debug;
20 #define cx_info(args...) do { printk(KERN_INFO "CX24113: " args); } while (0)
21 #define cx_err(args...) do { printk(KERN_ERR "CX24113: " args); } while (0)
23 #define dprintk(args...) \
24 do { \
25 if (debug) { \
26 printk(KERN_DEBUG "CX24113: %s: ", __func__); \
27 printk(args); \
28 } \
29 } while (0)
31 struct cx24113_state {
32 struct i2c_adapter *i2c;
33 const struct cx24113_config *config;
35 #define REV_CX24113 0x23
36 u8 rev;
37 u8 ver;
39 u8 icp_mode:1;
41 #define ICP_LEVEL1 0
42 #define ICP_LEVEL2 1
43 #define ICP_LEVEL3 2
44 #define ICP_LEVEL4 3
45 u8 icp_man:2;
46 u8 icp_auto_low:2;
47 u8 icp_auto_mlow:2;
48 u8 icp_auto_mhi:2;
49 u8 icp_auto_hi:2;
50 u8 icp_dig;
52 #define LNA_MIN_GAIN 0
53 #define LNA_MID_GAIN 1
54 #define LNA_MAX_GAIN 2
55 u8 lna_gain:2;
57 u8 acp_on:1;
59 u8 vco_mode:2;
60 u8 vco_shift:1;
61 #define VCOBANDSEL_6 0x80
62 #define VCOBANDSEL_5 0x01
63 #define VCOBANDSEL_4 0x02
64 #define VCOBANDSEL_3 0x04
65 #define VCOBANDSEL_2 0x08
66 #define VCOBANDSEL_1 0x10
67 u8 vco_band;
69 #define VCODIV4 4
70 #define VCODIV2 2
71 u8 vcodiv;
73 u8 bs_delay:4;
74 u16 bs_freqcnt:13;
75 u16 bs_rdiv;
76 u8 prescaler_mode:1;
78 u8 rfvga_bias_ctrl;
80 s16 tuner_gain_thres;
81 u8 gain_level;
83 u32 frequency;
85 u8 refdiv;
87 u8 Fwindow_enabled;
90 static int cx24113_writereg(struct cx24113_state *state, int reg, int data)
92 u8 buf[] = { reg, data };
93 struct i2c_msg msg = { .addr = state->config->i2c_addr,
94 .flags = 0, .buf = buf, .len = 2 };
95 int err = i2c_transfer(state->i2c, &msg, 1);
96 if (err != 1) {
97 printk(KERN_DEBUG "%s: writereg error(err == %i, reg == 0x%02x, data == 0x%02x)\n",
98 __func__, err, reg, data);
99 return err;
102 return 0;
105 static int cx24113_readreg(struct cx24113_state *state, u8 reg)
107 int ret;
108 u8 b;
109 struct i2c_msg msg[] = {
110 { .addr = state->config->i2c_addr,
111 .flags = 0, .buf = &reg, .len = 1 },
112 { .addr = state->config->i2c_addr,
113 .flags = I2C_M_RD, .buf = &b, .len = 1 }
116 ret = i2c_transfer(state->i2c, msg, 2);
118 if (ret != 2) {
119 printk(KERN_DEBUG "%s: reg=0x%x (error=%d)\n",
120 __func__, reg, ret);
121 return ret;
124 return b;
127 static void cx24113_set_parameters(struct cx24113_state *state)
129 u8 r;
131 r = cx24113_readreg(state, 0x10) & 0x82;
132 r |= state->icp_mode;
133 r |= state->icp_man << 4;
134 r |= state->icp_dig << 2;
135 r |= state->prescaler_mode << 5;
136 cx24113_writereg(state, 0x10, r);
138 r = (state->icp_auto_low << 0) | (state->icp_auto_mlow << 2)
139 | (state->icp_auto_mhi << 4) | (state->icp_auto_hi << 6);
140 cx24113_writereg(state, 0x11, r);
142 if (state->rev == REV_CX24113) {
143 r = cx24113_readreg(state, 0x20) & 0xec;
144 r |= state->lna_gain;
145 r |= state->rfvga_bias_ctrl << 4;
146 cx24113_writereg(state, 0x20, r);
149 r = cx24113_readreg(state, 0x12) & 0x03;
150 r |= state->acp_on << 2;
151 r |= state->bs_delay << 4;
152 cx24113_writereg(state, 0x12, r);
154 r = cx24113_readreg(state, 0x18) & 0x40;
155 r |= state->vco_shift;
156 if (state->vco_band == VCOBANDSEL_6)
157 r |= (1 << 7);
158 else
159 r |= (state->vco_band << 1);
160 cx24113_writereg(state, 0x18, r);
162 r = cx24113_readreg(state, 0x14) & 0x20;
163 r |= (state->vco_mode << 6) | ((state->bs_freqcnt >> 8) & 0x1f);
164 cx24113_writereg(state, 0x14, r);
165 cx24113_writereg(state, 0x15, (state->bs_freqcnt & 0xff));
167 cx24113_writereg(state, 0x16, (state->bs_rdiv >> 4) & 0xff);
168 r = (cx24113_readreg(state, 0x17) & 0x0f) |
169 ((state->bs_rdiv & 0x0f) << 4);
170 cx24113_writereg(state, 0x17, r);
173 #define VGA_0 0x00
174 #define VGA_1 0x04
175 #define VGA_2 0x02
176 #define VGA_3 0x06
177 #define VGA_4 0x01
178 #define VGA_5 0x05
179 #define VGA_6 0x03
180 #define VGA_7 0x07
182 #define RFVGA_0 0x00
183 #define RFVGA_1 0x01
184 #define RFVGA_2 0x02
185 #define RFVGA_3 0x03
187 static int cx24113_set_gain_settings(struct cx24113_state *state,
188 s16 power_estimation)
190 u8 ampout = cx24113_readreg(state, 0x1d) & 0xf0,
191 vga = cx24113_readreg(state, 0x1f) & 0x3f,
192 rfvga = cx24113_readreg(state, 0x20) & 0xf3;
193 u8 gain_level = power_estimation >= state->tuner_gain_thres;
195 dprintk("power estimation: %d, thres: %d, gain_level: %d/%d\n",
196 power_estimation, state->tuner_gain_thres,
197 state->gain_level, gain_level);
199 if (gain_level == state->gain_level)
200 return 0; /* nothing to be done */
202 ampout |= 0xf;
204 if (gain_level) {
205 rfvga |= RFVGA_0 << 2;
206 vga |= (VGA_7 << 3) | VGA_7;
207 } else {
208 rfvga |= RFVGA_2 << 2;
209 vga |= (VGA_6 << 3) | VGA_2;
211 state->gain_level = gain_level;
213 cx24113_writereg(state, 0x1d, ampout);
214 cx24113_writereg(state, 0x1f, vga);
215 cx24113_writereg(state, 0x20, rfvga);
217 return 1; /* did something */
220 static int cx24113_set_Fref(struct cx24113_state *state, u8 high)
222 u8 xtal = cx24113_readreg(state, 0x02);
223 if (state->rev == 0x43 && state->vcodiv == VCODIV4)
224 high = 1;
226 xtal &= ~0x2;
227 if (high)
228 xtal |= high << 1;
229 return cx24113_writereg(state, 0x02, xtal);
232 static int cx24113_enable(struct cx24113_state *state, u8 enable)
234 u8 r21 = (cx24113_readreg(state, 0x21) & 0xc0) | enable;
235 if (state->rev == REV_CX24113)
236 r21 |= (1 << 1);
237 return cx24113_writereg(state, 0x21, r21);
240 static int cx24113_set_bandwidth(struct cx24113_state *state, u32 bandwidth_khz)
242 u8 r;
244 if (bandwidth_khz <= 19000)
245 r = 0x03 << 6;
246 else if (bandwidth_khz <= 25000)
247 r = 0x02 << 6;
248 else
249 r = 0x01 << 6;
251 dprintk("bandwidth to be set: %d\n", bandwidth_khz);
252 bandwidth_khz *= 10;
253 bandwidth_khz -= 10000;
254 bandwidth_khz /= 1000;
255 bandwidth_khz += 5;
256 bandwidth_khz /= 10;
258 dprintk("bandwidth: %d %d\n", r >> 6, bandwidth_khz);
260 r |= bandwidth_khz & 0x3f;
262 return cx24113_writereg(state, 0x1e, r);
265 static int cx24113_set_clk_inversion(struct cx24113_state *state, u8 on)
267 u8 r = (cx24113_readreg(state, 0x10) & 0x7f) | ((on & 0x1) << 7);
268 return cx24113_writereg(state, 0x10, r);
271 static int cx24113_get_status(struct dvb_frontend *fe, u32 *status)
273 struct cx24113_state *state = fe->tuner_priv;
274 u8 r = (cx24113_readreg(state, 0x10) & 0x02) >> 1;
275 if (r)
276 *status |= TUNER_STATUS_LOCKED;
277 dprintk("PLL locked: %d\n", r);
278 return 0;
281 static u8 cx24113_set_ref_div(struct cx24113_state *state, u8 refdiv)
283 if (state->rev == 0x43 && state->vcodiv == VCODIV4)
284 refdiv = 2;
285 return state->refdiv = refdiv;
288 static void cx24113_calc_pll_nf(struct cx24113_state *state, u16 *n, s32 *f)
290 s32 N;
291 s64 F;
292 u64 dividend;
293 u8 R, r;
294 u8 vcodiv;
295 u8 factor;
296 s32 freq_hz = state->frequency * 1000;
298 if (state->config->xtal_khz < 20000)
299 factor = 1;
300 else
301 factor = 2;
303 if (state->rev == REV_CX24113) {
304 if (state->frequency >= 1100000)
305 vcodiv = VCODIV2;
306 else
307 vcodiv = VCODIV4;
308 } else {
309 if (state->frequency >= 1165000)
310 vcodiv = VCODIV2;
311 else
312 vcodiv = VCODIV4;
314 state->vcodiv = vcodiv;
316 dprintk("calculating N/F for %dHz with vcodiv %d\n", freq_hz, vcodiv);
317 R = 0;
318 do {
319 R = cx24113_set_ref_div(state, R + 1);
321 /* calculate tuner PLL settings: */
322 N = (freq_hz / 100 * vcodiv) * R;
323 N /= (state->config->xtal_khz) * factor * 2;
324 N += 5; /* For round up. */
325 N /= 10;
326 N -= 32;
327 } while (N < 6 && R < 3);
329 if (N < 6) {
330 cx_err("strange frequency: N < 6\n");
331 return;
333 F = freq_hz;
334 F *= (u64) (R * vcodiv * 262144);
335 dprintk("1 N: %d, F: %lld, R: %d\n", N, (long long)F, R);
336 /* do_div needs an u64 as first argument */
337 dividend = F;
338 do_div(dividend, state->config->xtal_khz * 1000 * factor * 2);
339 F = dividend;
340 dprintk("2 N: %d, F: %lld, R: %d\n", N, (long long)F, R);
341 F -= (N + 32) * 262144;
343 dprintk("3 N: %d, F: %lld, R: %d\n", N, (long long)F, R);
345 if (state->Fwindow_enabled) {
346 if (F > (262144 / 2 - 1638))
347 F = 262144 / 2 - 1638;
348 if (F < (-262144 / 2 + 1638))
349 F = -262144 / 2 + 1638;
350 if ((F < 3277 && F > 0) || (F > -3277 && F < 0)) {
351 F = 0;
352 r = cx24113_readreg(state, 0x10);
353 cx24113_writereg(state, 0x10, r | (1 << 6));
356 dprintk("4 N: %d, F: %lld, R: %d\n", N, (long long)F, R);
358 *n = (u16) N;
359 *f = (s32) F;
363 static void cx24113_set_nfr(struct cx24113_state *state, u16 n, s32 f, u8 r)
365 u8 reg;
366 cx24113_writereg(state, 0x19, (n >> 1) & 0xff);
368 reg = ((n & 0x1) << 7) | ((f >> 11) & 0x7f);
369 cx24113_writereg(state, 0x1a, reg);
371 cx24113_writereg(state, 0x1b, (f >> 3) & 0xff);
373 reg = cx24113_readreg(state, 0x1c) & 0x1f;
374 cx24113_writereg(state, 0x1c, reg | ((f & 0x7) << 5));
376 cx24113_set_Fref(state, r - 1);
379 static int cx24113_set_frequency(struct cx24113_state *state, u32 frequency)
381 u8 r = 1; /* or 2 */
382 u16 n = 6;
383 s32 f = 0;
385 r = cx24113_readreg(state, 0x14);
386 cx24113_writereg(state, 0x14, r & 0x3f);
388 r = cx24113_readreg(state, 0x10);
389 cx24113_writereg(state, 0x10, r & 0xbf);
391 state->frequency = frequency;
393 dprintk("tuning to frequency: %d\n", frequency);
395 cx24113_calc_pll_nf(state, &n, &f);
396 cx24113_set_nfr(state, n, f, state->refdiv);
398 r = cx24113_readreg(state, 0x18) & 0xbf;
399 if (state->vcodiv != VCODIV2)
400 r |= 1 << 6;
401 cx24113_writereg(state, 0x18, r);
403 /* The need for this sleep is not clear. But helps in some cases */
404 msleep(5);
406 r = cx24113_readreg(state, 0x1c) & 0xef;
407 cx24113_writereg(state, 0x1c, r | (1 << 4));
408 return 0;
411 static int cx24113_init(struct dvb_frontend *fe)
413 struct cx24113_state *state = fe->tuner_priv;
414 int ret;
416 state->tuner_gain_thres = -50;
417 state->gain_level = 255; /* to force a gain-setting initialization */
418 state->icp_mode = 0;
420 if (state->config->xtal_khz < 11000) {
421 state->icp_auto_hi = ICP_LEVEL4;
422 state->icp_auto_mhi = ICP_LEVEL4;
423 state->icp_auto_mlow = ICP_LEVEL3;
424 state->icp_auto_low = ICP_LEVEL3;
425 } else {
426 state->icp_auto_hi = ICP_LEVEL4;
427 state->icp_auto_mhi = ICP_LEVEL4;
428 state->icp_auto_mlow = ICP_LEVEL3;
429 state->icp_auto_low = ICP_LEVEL2;
432 state->icp_dig = ICP_LEVEL3;
433 state->icp_man = ICP_LEVEL1;
434 state->acp_on = 1;
435 state->vco_mode = 0;
436 state->vco_shift = 0;
437 state->vco_band = VCOBANDSEL_1;
438 state->bs_delay = 8;
439 state->bs_freqcnt = 0x0fff;
440 state->bs_rdiv = 0x0fff;
441 state->prescaler_mode = 0;
442 state->lna_gain = LNA_MAX_GAIN;
443 state->rfvga_bias_ctrl = 1;
444 state->Fwindow_enabled = 1;
446 cx24113_set_Fref(state, 0);
447 cx24113_enable(state, 0x3d);
448 cx24113_set_parameters(state);
450 cx24113_set_gain_settings(state, -30);
452 cx24113_set_bandwidth(state, 18025);
453 cx24113_set_clk_inversion(state, 1);
455 if (state->config->xtal_khz >= 40000)
456 ret = cx24113_writereg(state, 0x02,
457 (cx24113_readreg(state, 0x02) & 0xfb) | (1 << 2));
458 else
459 ret = cx24113_writereg(state, 0x02,
460 (cx24113_readreg(state, 0x02) & 0xfb) | (0 << 2));
462 return ret;
465 static int cx24113_set_params(struct dvb_frontend *fe)
467 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
468 struct cx24113_state *state = fe->tuner_priv;
469 /* for a ROLL-OFF factor of 0.35, 0.2: 600, 0.25: 625 */
470 u32 roll_off = 675;
471 u32 bw;
473 bw = ((c->symbol_rate/100) * roll_off) / 1000;
474 bw += (10000000/100) + 5;
475 bw /= 10;
476 bw += 1000;
477 cx24113_set_bandwidth(state, bw);
479 cx24113_set_frequency(state, c->frequency);
480 msleep(5);
481 return cx24113_get_status(fe, &bw);
484 static s8 cx24113_agc_table[2][10] = {
485 {-54, -41, -35, -30, -25, -21, -16, -10, -6, -2},
486 {-39, -35, -30, -25, -19, -15, -11, -5, 1, 9},
489 void cx24113_agc_callback(struct dvb_frontend *fe)
491 struct cx24113_state *state = fe->tuner_priv;
492 s16 s, i;
493 if (!fe->ops.read_signal_strength)
494 return;
496 do {
497 /* this only works with the current CX24123 implementation */
498 fe->ops.read_signal_strength(fe, (u16 *) &s);
499 s >>= 8;
500 dprintk("signal strength: %d\n", s);
501 for (i = 0; i < sizeof(cx24113_agc_table[0]); i++)
502 if (cx24113_agc_table[state->gain_level][i] > s)
503 break;
504 s = -25 - i*5;
505 } while (cx24113_set_gain_settings(state, s));
507 EXPORT_SYMBOL(cx24113_agc_callback);
509 static int cx24113_get_frequency(struct dvb_frontend *fe, u32 *frequency)
511 struct cx24113_state *state = fe->tuner_priv;
512 *frequency = state->frequency;
513 return 0;
516 static void cx24113_release(struct dvb_frontend *fe)
518 struct cx24113_state *state = fe->tuner_priv;
519 dprintk("\n");
520 fe->tuner_priv = NULL;
521 kfree(state);
524 static const struct dvb_tuner_ops cx24113_tuner_ops = {
525 .info = {
526 .name = "Conexant CX24113",
527 .frequency_min_hz = 950 * MHz,
528 .frequency_max_hz = 2150 * MHz,
529 .frequency_step_hz = 125 * kHz,
532 .release = cx24113_release,
534 .init = cx24113_init,
536 .set_params = cx24113_set_params,
537 .get_frequency = cx24113_get_frequency,
538 .get_status = cx24113_get_status,
541 struct dvb_frontend *cx24113_attach(struct dvb_frontend *fe,
542 const struct cx24113_config *config, struct i2c_adapter *i2c)
544 /* allocate memory for the internal state */
545 struct cx24113_state *state = kzalloc(sizeof(*state), GFP_KERNEL);
546 int rc;
548 if (!state)
549 return NULL;
551 /* setup the state */
552 state->config = config;
553 state->i2c = i2c;
555 cx_info("trying to detect myself\n");
557 /* making a dummy read, because of some expected troubles
558 * after power on */
559 cx24113_readreg(state, 0x00);
561 rc = cx24113_readreg(state, 0x00);
562 if (rc < 0) {
563 cx_info("CX24113 not found.\n");
564 goto error;
566 state->rev = rc;
568 switch (rc) {
569 case 0x43:
570 cx_info("detected CX24113 variant\n");
571 break;
572 case REV_CX24113:
573 cx_info("successfully detected\n");
574 break;
575 default:
576 cx_err("unsupported device id: %x\n", state->rev);
577 goto error;
579 state->ver = cx24113_readreg(state, 0x01);
580 cx_info("version: %x\n", state->ver);
582 /* create dvb_frontend */
583 memcpy(&fe->ops.tuner_ops, &cx24113_tuner_ops,
584 sizeof(struct dvb_tuner_ops));
585 fe->tuner_priv = state;
586 return fe;
588 error:
589 kfree(state);
591 return NULL;
593 EXPORT_SYMBOL(cx24113_attach);
595 module_param(debug, int, 0644);
596 MODULE_PARM_DESC(debug, "Activates frontend debugging (default:0)");
598 MODULE_AUTHOR("Patrick Boettcher <pb@linuxtv.org>");
599 MODULE_DESCRIPTION("DVB Frontend module for Conexant CX24113/CX24128hardware");
600 MODULE_LICENSE("GPL");